blob: 153f71a5bba91975fc17e528769b8992016819d9 [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"
Chris Masonfec577f2007-02-26 10:40:21 -050035
Arne Jansen709c0482011-09-12 12:22:57 +020036#undef SCRAMBLE_DELAYED_REFS
37
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080038
Yan Zheng5d4f98a2009-06-10 10:45:14 -040039static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030040 struct btrfs_delayed_ref_node *node, u64 parent,
41 u64 root_objectid, u64 owner_objectid,
42 u64 owner_offset, int refs_to_drop,
43 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040044static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
45 struct extent_buffer *leaf,
46 struct btrfs_extent_item *ei);
47static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040048 u64 parent, u64 root_objectid,
49 u64 flags, u64 owner, u64 offset,
50 struct btrfs_key *ins, int ref_mod);
51static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030052 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030053 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040054static int find_next_key(struct btrfs_path *path, int level,
55 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050056
David Sterba32da53862019-10-29 19:20:18 +010057static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
Josef Bacik0f9dd462008-09-23 13:14:11 -040058{
59 return (cache->flags & bits) == bits;
60}
61
Josef Bacik6f410d12019-06-20 15:37:49 -040062int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
63 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -040064{
Yan Zheng11833d62009-09-11 16:11:19 -040065 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040066 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +020067 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040068 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +020069 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040070 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -040071}
72
David Sterba32da53862019-10-29 19:20:18 +010073void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -040074{
Nikolay Borisov9e715da2018-06-20 15:49:08 +030075 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -040076 u64 start, end;
77
David Sterbab3470b52019-10-23 18:48:22 +020078 start = cache->start;
79 end = start + cache->length - 1;
Yan Zheng11833d62009-09-11 16:11:19 -040080
Jeff Mahoney0b246af2016-06-22 18:54:23 -040081 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +020082 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040083 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +020084 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040085}
86
Qu Wenruo78192442019-05-15 07:33:48 +080087static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
Omar Sandoval0d9f8242017-06-06 16:45:26 -070088{
Qu Wenruoddf30cf2019-04-04 14:45:34 +080089 if (ref->type == BTRFS_REF_METADATA) {
90 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
Qu Wenruo78192442019-05-15 07:33:48 +080091 return BTRFS_BLOCK_GROUP_SYSTEM;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070092 else
Qu Wenruo78192442019-05-15 07:33:48 +080093 return BTRFS_BLOCK_GROUP_METADATA;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070094 }
Qu Wenruo78192442019-05-15 07:33:48 +080095 return BTRFS_BLOCK_GROUP_DATA;
96}
97
98static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
99 struct btrfs_ref *ref)
100{
101 struct btrfs_space_info *space_info;
102 u64 flags = generic_ref_to_space_flags(ref);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700103
Josef Bacik280c29082019-06-18 16:09:19 -0400104 space_info = btrfs_find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700105 ASSERT(space_info);
Qu Wenruo78192442019-05-15 07:33:48 +0800106 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
107 BTRFS_TOTAL_BYTES_PINNED_BATCH);
108}
109
110static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
111 struct btrfs_ref *ref)
112{
113 struct btrfs_space_info *space_info;
114 u64 flags = generic_ref_to_space_flags(ref);
115
Josef Bacik280c29082019-06-18 16:09:19 -0400116 space_info = btrfs_find_space_info(fs_info, flags);
Qu Wenruo78192442019-05-15 07:33:48 +0800117 ASSERT(space_info);
118 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
Ethan Liendec59fa2018-07-13 16:50:42 +0800119 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700120}
121
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000122/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400123int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400124{
125 int ret;
126 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400127 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400128
Zheng Yan31840ae2008-09-23 13:14:14 -0400129 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700130 if (!path)
131 return -ENOMEM;
132
Chris Masone02119d2008-09-05 16:13:11 -0400133 key.objectid = start;
134 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500135 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400136 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400137 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500138 return ret;
139}
140
Chris Masond8d5f3e2007-12-11 12:42:00 -0500141/*
Josef Bacik3173a182013-03-07 14:22:04 -0500142 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400143 *
144 * the head node for delayed ref is used to store the sum of all the
145 * reference count modifications queued up in the rbtree. the head
146 * node may also store the extent flags to set. This way you can check
147 * to see what the reference count and extent flags would be if all of
148 * the delayed refs are not processed.
149 */
150int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400151 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500152 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400153{
154 struct btrfs_delayed_ref_head *head;
155 struct btrfs_delayed_ref_root *delayed_refs;
156 struct btrfs_path *path;
157 struct btrfs_extent_item *ei;
158 struct extent_buffer *leaf;
159 struct btrfs_key key;
160 u32 item_size;
161 u64 num_refs;
162 u64 extent_flags;
163 int ret;
164
Josef Bacik3173a182013-03-07 14:22:04 -0500165 /*
166 * If we don't have skinny metadata, don't bother doing anything
167 * different
168 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400169 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
170 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500171 metadata = 0;
172 }
173
Yan, Zhenga22285a2010-05-16 10:48:46 -0400174 path = btrfs_alloc_path();
175 if (!path)
176 return -ENOMEM;
177
Yan, Zhenga22285a2010-05-16 10:48:46 -0400178 if (!trans) {
179 path->skip_locking = 1;
180 path->search_commit_root = 1;
181 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000182
183search_again:
184 key.objectid = bytenr;
185 key.offset = offset;
186 if (metadata)
187 key.type = BTRFS_METADATA_ITEM_KEY;
188 else
189 key.type = BTRFS_EXTENT_ITEM_KEY;
190
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400192 if (ret < 0)
193 goto out_free;
194
Josef Bacik3173a182013-03-07 14:22:04 -0500195 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100196 if (path->slots[0]) {
197 path->slots[0]--;
198 btrfs_item_key_to_cpu(path->nodes[0], &key,
199 path->slots[0]);
200 if (key.objectid == bytenr &&
201 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400202 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100203 ret = 0;
204 }
Josef Bacik3173a182013-03-07 14:22:04 -0500205 }
206
Yan, Zhenga22285a2010-05-16 10:48:46 -0400207 if (ret == 0) {
208 leaf = path->nodes[0];
209 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
210 if (item_size >= sizeof(*ei)) {
211 ei = btrfs_item_ptr(leaf, path->slots[0],
212 struct btrfs_extent_item);
213 num_refs = btrfs_extent_refs(leaf, ei);
214 extent_flags = btrfs_extent_flags(leaf, ei);
215 } else {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300216 ret = -EINVAL;
217 btrfs_print_v0_err(fs_info);
218 if (trans)
219 btrfs_abort_transaction(trans, ret);
220 else
221 btrfs_handle_fs_error(fs_info, ret, NULL);
222
223 goto out_free;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400224 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300225
Yan, Zhenga22285a2010-05-16 10:48:46 -0400226 BUG_ON(num_refs == 0);
227 } else {
228 num_refs = 0;
229 extent_flags = 0;
230 ret = 0;
231 }
232
233 if (!trans)
234 goto out;
235
236 delayed_refs = &trans->transaction->delayed_refs;
237 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800238 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400239 if (head) {
240 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400241 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400242 spin_unlock(&delayed_refs->lock);
243
David Sterbab3b4aa72011-04-21 01:20:15 +0200244 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400245
David Sterba8cc33e52011-05-02 15:29:25 +0200246 /*
247 * Mutex was contended, block until it's released and try
248 * again
249 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400250 mutex_lock(&head->mutex);
251 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400252 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000253 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400254 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500255 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400256 if (head->extent_op && head->extent_op->update_flags)
257 extent_flags |= head->extent_op->flags_to_set;
258 else
259 BUG_ON(num_refs == 0);
260
Josef Bacikd2788502017-09-29 15:43:57 -0400261 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500262 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400263 mutex_unlock(&head->mutex);
264 }
265 spin_unlock(&delayed_refs->lock);
266out:
267 WARN_ON(num_refs == 0);
268 if (refs)
269 *refs = num_refs;
270 if (flags)
271 *flags = extent_flags;
272out_free:
273 btrfs_free_path(path);
274 return ret;
275}
276
277/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500278 * Back reference rules. Back refs have three main goals:
279 *
280 * 1) differentiate between all holders of references to an extent so that
281 * when a reference is dropped we can make sure it was a valid reference
282 * before freeing the extent.
283 *
284 * 2) Provide enough information to quickly find the holders of an extent
285 * if we notice a given block is corrupted or bad.
286 *
287 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
288 * maintenance. This is actually the same as #2, but with a slightly
289 * different use case.
290 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400291 * There are two kinds of back refs. The implicit back refs is optimized
292 * for pointers in non-shared tree blocks. For a given pointer in a block,
293 * back refs of this kind provide information about the block's owner tree
294 * and the pointer's key. These information allow us to find the block by
295 * b-tree searching. The full back refs is for pointers in tree blocks not
296 * referenced by their owner trees. The location of tree block is recorded
297 * in the back refs. Actually the full back refs is generic, and can be
298 * used in all cases the implicit back refs is used. The major shortcoming
299 * of the full back refs is its overhead. Every time a tree block gets
300 * COWed, we have to update back refs entry for all pointers in it.
301 *
302 * For a newly allocated tree block, we use implicit back refs for
303 * pointers in it. This means most tree related operations only involve
304 * implicit back refs. For a tree block created in old transaction, the
305 * only way to drop a reference to it is COW it. So we can detect the
306 * event that tree block loses its owner tree's reference and do the
307 * back refs conversion.
308 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400309 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400310 *
311 * The reference count of the block is one and the tree is the block's
312 * owner tree. Nothing to do in this case.
313 *
314 * The reference count of the block is one and the tree is not the
315 * block's owner tree. In this case, full back refs is used for pointers
316 * in the block. Remove these full back refs, add implicit back refs for
317 * every pointers in the new block.
318 *
319 * The reference count of the block is greater than one and the tree is
320 * the block's owner tree. In this case, implicit back refs is used for
321 * pointers in the block. Add full back refs for every pointers in the
322 * block, increase lower level extents' reference counts. The original
323 * implicit back refs are entailed to the new block.
324 *
325 * The reference count of the block is greater than one and the tree is
326 * not the block's owner tree. Add implicit back refs for every pointer in
327 * the new block, increase lower level extents' reference count.
328 *
329 * Back Reference Key composing:
330 *
331 * The key objectid corresponds to the first byte in the extent,
332 * The key type is used to differentiate between types of back refs.
333 * There are different meanings of the key offset for different types
334 * of back refs.
335 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500336 * File extents can be referenced by:
337 *
338 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400339 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500340 * - different offsets inside a file (bookend extents in file.c)
341 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400342 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500343 *
344 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500345 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400346 * - original offset in the file
347 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400348 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400349 * The key offset for the implicit back refs is hash of the first
350 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500351 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400352 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500353 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500355 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400356 * The key offset for the implicit back refs is the first byte of
357 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500358 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 * When a file extent is allocated, The implicit back refs is used.
360 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500361 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400362 * (root_key.objectid, inode objectid, offset in file, 1)
363 *
364 * When a file extent is removed file truncation, we find the
365 * corresponding implicit back refs and check the following fields:
366 *
367 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500368 *
369 * Btree extents can be referenced by:
370 *
371 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500372 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400373 * Both the implicit back refs and the full back refs for tree blocks
374 * only consist of key. The key offset for the implicit back refs is
375 * objectid of block's owner tree. The key offset for the full back refs
376 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500377 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400378 * When implicit back refs is used, information about the lowest key and
379 * level of the tree block are required. These information are stored in
380 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500381 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400382
Liu Bo167ce952017-08-18 15:15:18 -0600383/*
384 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100385 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600386 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
387 */
388int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
389 struct btrfs_extent_inline_ref *iref,
390 enum btrfs_inline_ref_type is_data)
391{
392 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -0600393 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600394
395 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
396 type == BTRFS_SHARED_BLOCK_REF_KEY ||
397 type == BTRFS_SHARED_DATA_REF_KEY ||
398 type == BTRFS_EXTENT_DATA_REF_KEY) {
399 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600400 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600401 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600402 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
403 ASSERT(eb->fs_info);
404 /*
405 * Every shared one has parent tree
406 * block, which must be aligned to
407 * nodesize.
408 */
409 if (offset &&
410 IS_ALIGNED(offset, eb->fs_info->nodesize))
411 return type;
412 }
Liu Bo167ce952017-08-18 15:15:18 -0600413 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600414 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600415 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600416 if (type == BTRFS_SHARED_DATA_REF_KEY) {
417 ASSERT(eb->fs_info);
418 /*
419 * Every shared one has parent tree
420 * block, which must be aligned to
421 * nodesize.
422 */
423 if (offset &&
424 IS_ALIGNED(offset, eb->fs_info->nodesize))
425 return type;
426 }
Liu Bo167ce952017-08-18 15:15:18 -0600427 } else {
428 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
429 return type;
430 }
431 }
432
433 btrfs_print_leaf((struct extent_buffer *)eb);
434 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
435 eb->start, type);
436 WARN_ON(1);
437
438 return BTRFS_REF_TYPE_INVALID;
439}
440
Qu Wenruo0785a9a2019-08-09 09:24:24 +0800441u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400442{
443 u32 high_crc = ~(u32)0;
444 u32 low_crc = ~(u32)0;
445 __le64 lenum;
446
447 lenum = cpu_to_le64(root_objectid);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200448 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400449 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200450 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400451 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200452 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453
454 return ((u64)high_crc << 31) ^ (u64)low_crc;
455}
456
457static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
458 struct btrfs_extent_data_ref *ref)
459{
460 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
461 btrfs_extent_data_ref_objectid(leaf, ref),
462 btrfs_extent_data_ref_offset(leaf, ref));
463}
464
465static int match_extent_data_ref(struct extent_buffer *leaf,
466 struct btrfs_extent_data_ref *ref,
467 u64 root_objectid, u64 owner, u64 offset)
468{
469 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
470 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
471 btrfs_extent_data_ref_offset(leaf, ref) != offset)
472 return 0;
473 return 1;
474}
475
476static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 struct btrfs_path *path,
478 u64 bytenr, u64 parent,
479 u64 root_objectid,
480 u64 owner, u64 offset)
481{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300482 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483 struct btrfs_key key;
484 struct btrfs_extent_data_ref *ref;
485 struct extent_buffer *leaf;
486 u32 nritems;
487 int ret;
488 int recow;
489 int err = -ENOENT;
490
491 key.objectid = bytenr;
492 if (parent) {
493 key.type = BTRFS_SHARED_DATA_REF_KEY;
494 key.offset = parent;
495 } else {
496 key.type = BTRFS_EXTENT_DATA_REF_KEY;
497 key.offset = hash_extent_data_ref(root_objectid,
498 owner, offset);
499 }
500again:
501 recow = 0;
502 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
503 if (ret < 0) {
504 err = ret;
505 goto fail;
506 }
507
508 if (parent) {
509 if (!ret)
510 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400511 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400512 }
513
514 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400515 nritems = btrfs_header_nritems(leaf);
516 while (1) {
517 if (path->slots[0] >= nritems) {
518 ret = btrfs_next_leaf(root, path);
519 if (ret < 0)
520 err = ret;
521 if (ret)
522 goto fail;
523
524 leaf = path->nodes[0];
525 nritems = btrfs_header_nritems(leaf);
526 recow = 1;
527 }
528
529 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
530 if (key.objectid != bytenr ||
531 key.type != BTRFS_EXTENT_DATA_REF_KEY)
532 goto fail;
533
534 ref = btrfs_item_ptr(leaf, path->slots[0],
535 struct btrfs_extent_data_ref);
536
537 if (match_extent_data_ref(leaf, ref, root_objectid,
538 owner, offset)) {
539 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200540 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400541 goto again;
542 }
543 err = 0;
544 break;
545 }
546 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400547 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548fail:
549 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400550}
551
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400552static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553 struct btrfs_path *path,
554 u64 bytenr, u64 parent,
555 u64 root_objectid, u64 owner,
556 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400557{
Nikolay Borisov62b895a2018-06-20 15:48:44 +0300558 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -0400559 struct btrfs_key key;
560 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400562 u32 num_refs;
563 int ret;
564
565 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400566 if (parent) {
567 key.type = BTRFS_SHARED_DATA_REF_KEY;
568 key.offset = parent;
569 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400570 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400571 key.type = BTRFS_EXTENT_DATA_REF_KEY;
572 key.offset = hash_extent_data_ref(root_objectid,
573 owner, offset);
574 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400575 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576
577 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
578 if (ret && ret != -EEXIST)
579 goto fail;
580
581 leaf = path->nodes[0];
582 if (parent) {
583 struct btrfs_shared_data_ref *ref;
584 ref = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_shared_data_ref);
586 if (ret == 0) {
587 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
588 } else {
589 num_refs = btrfs_shared_data_ref_count(leaf, ref);
590 num_refs += refs_to_add;
591 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
592 }
593 } else {
594 struct btrfs_extent_data_ref *ref;
595 while (ret == -EEXIST) {
596 ref = btrfs_item_ptr(leaf, path->slots[0],
597 struct btrfs_extent_data_ref);
598 if (match_extent_data_ref(leaf, ref, root_objectid,
599 owner, offset))
600 break;
David Sterbab3b4aa72011-04-21 01:20:15 +0200601 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400602 key.offset++;
603 ret = btrfs_insert_empty_item(trans, root, path, &key,
604 size);
605 if (ret && ret != -EEXIST)
606 goto fail;
607
608 leaf = path->nodes[0];
609 }
610 ref = btrfs_item_ptr(leaf, path->slots[0],
611 struct btrfs_extent_data_ref);
612 if (ret == 0) {
613 btrfs_set_extent_data_ref_root(leaf, ref,
614 root_objectid);
615 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
616 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
617 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
618 } else {
619 num_refs = btrfs_extent_data_ref_count(leaf, ref);
620 num_refs += refs_to_add;
621 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
622 }
623 }
624 btrfs_mark_buffer_dirty(leaf);
625 ret = 0;
626fail:
David Sterbab3b4aa72011-04-21 01:20:15 +0200627 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500628 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500629}
630
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400631static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -0700633 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -0400634{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400635 struct btrfs_key key;
636 struct btrfs_extent_data_ref *ref1 = NULL;
637 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400638 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400639 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400640 int ret = 0;
641
642 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644
645 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
646 ref1 = btrfs_item_ptr(leaf, path->slots[0],
647 struct btrfs_extent_data_ref);
648 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
649 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
650 ref2 = btrfs_item_ptr(leaf, path->slots[0],
651 struct btrfs_shared_data_ref);
652 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200653 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300654 btrfs_print_v0_err(trans->fs_info);
655 btrfs_abort_transaction(trans, -EINVAL);
656 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400657 } else {
658 BUG();
659 }
660
Chris Mason56bec292009-03-13 10:10:06 -0400661 BUG_ON(num_refs < refs_to_drop);
662 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400663
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +0300665 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -0700666 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400667 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400668 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
669 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
670 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
671 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan31840ae2008-09-23 13:14:14 -0400672 btrfs_mark_buffer_dirty(leaf);
673 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 return ret;
675}
676
Zhaolei9ed0dea2015-08-06 22:16:24 +0800677static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678 struct btrfs_extent_inline_ref *iref)
679{
680 struct btrfs_key key;
681 struct extent_buffer *leaf;
682 struct btrfs_extent_data_ref *ref1;
683 struct btrfs_shared_data_ref *ref2;
684 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -0600685 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686
687 leaf = path->nodes[0];
688 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300689
690 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -0600692 /*
693 * If type is invalid, we should have bailed out earlier than
694 * this call.
695 */
696 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
697 ASSERT(type != BTRFS_REF_TYPE_INVALID);
698 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400699 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
700 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
701 } else {
702 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
703 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
704 }
705 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
706 ref1 = btrfs_item_ptr(leaf, path->slots[0],
707 struct btrfs_extent_data_ref);
708 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
709 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
710 ref2 = btrfs_item_ptr(leaf, path->slots[0],
711 struct btrfs_shared_data_ref);
712 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 } else {
714 WARN_ON(1);
715 }
716 return num_refs;
717}
718
719static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400720 struct btrfs_path *path,
721 u64 bytenr, u64 parent,
722 u64 root_objectid)
723{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +0300724 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400725 struct btrfs_key key;
726 int ret;
727
728 key.objectid = bytenr;
729 if (parent) {
730 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
731 key.offset = parent;
732 } else {
733 key.type = BTRFS_TREE_BLOCK_REF_KEY;
734 key.offset = root_objectid;
735 }
736
737 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
738 if (ret > 0)
739 ret = -ENOENT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400740 return ret;
741}
742
743static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400744 struct btrfs_path *path,
745 u64 bytenr, u64 parent,
746 u64 root_objectid)
747{
748 struct btrfs_key key;
749 int ret;
750
751 key.objectid = bytenr;
752 if (parent) {
753 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
754 key.offset = parent;
755 } else {
756 key.type = BTRFS_TREE_BLOCK_REF_KEY;
757 key.offset = root_objectid;
758 }
759
Nikolay Borisov10728402018-06-20 15:48:43 +0300760 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500761 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +0200762 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -0400763 return ret;
764}
765
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400766static inline int extent_ref_type(u64 parent, u64 owner)
767{
768 int type;
769 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
770 if (parent > 0)
771 type = BTRFS_SHARED_BLOCK_REF_KEY;
772 else
773 type = BTRFS_TREE_BLOCK_REF_KEY;
774 } else {
775 if (parent > 0)
776 type = BTRFS_SHARED_DATA_REF_KEY;
777 else
778 type = BTRFS_EXTENT_DATA_REF_KEY;
779 }
780 return type;
781}
782
Yan Zheng2c47e6052009-06-27 21:07:35 -0400783static int find_next_key(struct btrfs_path *path, int level,
784 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400785
786{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400787 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400788 if (!path->nodes[level])
789 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400790 if (path->slots[level] + 1 >=
791 btrfs_header_nritems(path->nodes[level]))
792 continue;
793 if (level == 0)
794 btrfs_item_key_to_cpu(path->nodes[level], key,
795 path->slots[level] + 1);
796 else
797 btrfs_node_key_to_cpu(path->nodes[level], key,
798 path->slots[level] + 1);
799 return 0;
800 }
801 return 1;
802}
803
804/*
805 * look for inline back ref. if back ref is found, *ref_ret is set
806 * to the address of inline back ref, and 0 is returned.
807 *
808 * if back ref isn't found, *ref_ret is set to the address where it
809 * should be inserted, and -ENOENT is returned.
810 *
811 * if insert is true and there are too many inline back refs, the path
812 * points to the extent item, and -EAGAIN is returned.
813 *
814 * NOTE: inline back refs are ordered in the same way that back ref
815 * items in the tree are ordered.
816 */
817static noinline_for_stack
818int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400819 struct btrfs_path *path,
820 struct btrfs_extent_inline_ref **ref_ret,
821 u64 bytenr, u64 num_bytes,
822 u64 parent, u64 root_objectid,
823 u64 owner, u64 offset, int insert)
824{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +0300825 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500826 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 struct btrfs_key key;
828 struct extent_buffer *leaf;
829 struct btrfs_extent_item *ei;
830 struct btrfs_extent_inline_ref *iref;
831 u64 flags;
832 u64 item_size;
833 unsigned long ptr;
834 unsigned long end;
835 int extra_size;
836 int type;
837 int want;
838 int ret;
839 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400840 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -0600841 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842
843 key.objectid = bytenr;
844 key.type = BTRFS_EXTENT_ITEM_KEY;
845 key.offset = num_bytes;
846
847 want = extent_ref_type(parent, owner);
848 if (insert) {
849 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -0400850 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851 } else
852 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -0500853
854 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +0300855 * Owner is our level, so we can just add one to get the level for the
856 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -0500857 */
858 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
859 key.type = BTRFS_METADATA_ITEM_KEY;
860 key.offset = owner;
861 }
862
863again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
865 if (ret < 0) {
866 err = ret;
867 goto out;
868 }
Josef Bacik3173a182013-03-07 14:22:04 -0500869
870 /*
871 * We may be a newly converted file system which still has the old fat
872 * extent entries for metadata, so try and see if we have one of those.
873 */
874 if (ret > 0 && skinny_metadata) {
875 skinny_metadata = false;
876 if (path->slots[0]) {
877 path->slots[0]--;
878 btrfs_item_key_to_cpu(path->nodes[0], &key,
879 path->slots[0]);
880 if (key.objectid == bytenr &&
881 key.type == BTRFS_EXTENT_ITEM_KEY &&
882 key.offset == num_bytes)
883 ret = 0;
884 }
885 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +0100886 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500887 key.type = BTRFS_EXTENT_ITEM_KEY;
888 key.offset = num_bytes;
889 btrfs_release_path(path);
890 goto again;
891 }
892 }
893
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 if (ret && !insert) {
895 err = -ENOENT;
896 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530897 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -0500898 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -0500899 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901
902 leaf = path->nodes[0];
903 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200904 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300905 err = -EINVAL;
906 btrfs_print_v0_err(fs_info);
907 btrfs_abort_transaction(trans, err);
908 goto out;
909 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
912 flags = btrfs_extent_flags(leaf, ei);
913
914 ptr = (unsigned long)(ei + 1);
915 end = (unsigned long)ei + item_size;
916
Josef Bacik3173a182013-03-07 14:22:04 -0500917 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 ptr += sizeof(struct btrfs_tree_block_info);
919 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 }
921
Liu Bo3de28d52017-08-18 15:15:19 -0600922 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
923 needed = BTRFS_REF_TYPE_DATA;
924 else
925 needed = BTRFS_REF_TYPE_BLOCK;
926
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 err = -ENOENT;
928 while (1) {
929 if (ptr >= end) {
930 WARN_ON(ptr > end);
931 break;
932 }
933 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600934 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
935 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800936 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600937 goto out;
938 }
939
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400940 if (want < type)
941 break;
942 if (want > type) {
943 ptr += btrfs_extent_inline_ref_size(type);
944 continue;
945 }
946
947 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
948 struct btrfs_extent_data_ref *dref;
949 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
950 if (match_extent_data_ref(leaf, dref, root_objectid,
951 owner, offset)) {
952 err = 0;
953 break;
954 }
955 if (hash_extent_data_ref_item(leaf, dref) <
956 hash_extent_data_ref(root_objectid, owner, offset))
957 break;
958 } else {
959 u64 ref_offset;
960 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
961 if (parent > 0) {
962 if (parent == ref_offset) {
963 err = 0;
964 break;
965 }
966 if (ref_offset < parent)
967 break;
968 } else {
969 if (root_objectid == ref_offset) {
970 err = 0;
971 break;
972 }
973 if (ref_offset < root_objectid)
974 break;
975 }
976 }
977 ptr += btrfs_extent_inline_ref_size(type);
978 }
979 if (err == -ENOENT && insert) {
980 if (item_size + extra_size >=
981 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
982 err = -EAGAIN;
983 goto out;
984 }
985 /*
986 * To add new inline back ref, we have to make sure
987 * there is no corresponding back ref item.
988 * For simplicity, we just do not add new inline back
989 * ref if there is any kind of item for this block
990 */
Yan Zheng2c47e6052009-06-27 21:07:35 -0400991 if (find_next_key(path, 0, &key) == 0 &&
992 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -0400993 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400994 err = -EAGAIN;
995 goto out;
996 }
997 }
998 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
999out:
Yan Zheng85d41982009-06-11 08:51:10 -04001000 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 path->keep_locks = 0;
1002 btrfs_unlock_up_safe(path, 1);
1003 }
1004 return err;
1005}
1006
1007/*
1008 * helper to add new inline back ref
1009 */
1010static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001011void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001012 struct btrfs_path *path,
1013 struct btrfs_extent_inline_ref *iref,
1014 u64 parent, u64 root_objectid,
1015 u64 owner, u64 offset, int refs_to_add,
1016 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017{
1018 struct extent_buffer *leaf;
1019 struct btrfs_extent_item *ei;
1020 unsigned long ptr;
1021 unsigned long end;
1022 unsigned long item_offset;
1023 u64 refs;
1024 int size;
1025 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026
1027 leaf = path->nodes[0];
1028 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1029 item_offset = (unsigned long)iref - (unsigned long)ei;
1030
1031 type = extent_ref_type(parent, owner);
1032 size = btrfs_extent_inline_ref_size(type);
1033
David Sterbac71dd882019-03-20 14:51:10 +01001034 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035
1036 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1037 refs = btrfs_extent_refs(leaf, ei);
1038 refs += refs_to_add;
1039 btrfs_set_extent_refs(leaf, ei, refs);
1040 if (extent_op)
1041 __run_delayed_extent_op(extent_op, leaf, ei);
1042
1043 ptr = (unsigned long)ei + item_offset;
1044 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1045 if (ptr < end - size)
1046 memmove_extent_buffer(leaf, ptr + size, ptr,
1047 end - size - ptr);
1048
1049 iref = (struct btrfs_extent_inline_ref *)ptr;
1050 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1051 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1052 struct btrfs_extent_data_ref *dref;
1053 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1054 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1055 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1056 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1057 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1058 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1059 struct btrfs_shared_data_ref *sref;
1060 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1061 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1062 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1063 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1064 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1065 } else {
1066 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1067 }
1068 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069}
1070
1071static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001072 struct btrfs_path *path,
1073 struct btrfs_extent_inline_ref **ref_ret,
1074 u64 bytenr, u64 num_bytes, u64 parent,
1075 u64 root_objectid, u64 owner, u64 offset)
1076{
1077 int ret;
1078
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001079 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1080 num_bytes, parent, root_objectid,
1081 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 if (ret != -ENOENT)
1083 return ret;
1084
David Sterbab3b4aa72011-04-21 01:20:15 +02001085 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086 *ref_ret = NULL;
1087
1088 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001089 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1090 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001092 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1093 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 }
1095 return ret;
1096}
1097
1098/*
1099 * helper to update/remove inline back ref
1100 */
1101static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001102void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001103 struct btrfs_extent_inline_ref *iref,
1104 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001105 struct btrfs_delayed_extent_op *extent_op,
1106 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001107{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001108 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 struct btrfs_extent_item *ei;
1110 struct btrfs_extent_data_ref *dref = NULL;
1111 struct btrfs_shared_data_ref *sref = NULL;
1112 unsigned long ptr;
1113 unsigned long end;
1114 u32 item_size;
1115 int size;
1116 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001117 u64 refs;
1118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001119 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1120 refs = btrfs_extent_refs(leaf, ei);
1121 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1122 refs += refs_to_mod;
1123 btrfs_set_extent_refs(leaf, ei, refs);
1124 if (extent_op)
1125 __run_delayed_extent_op(extent_op, leaf, ei);
1126
Liu Bo3de28d52017-08-18 15:15:19 -06001127 /*
1128 * If type is invalid, we should have bailed out after
1129 * lookup_inline_extent_backref().
1130 */
1131 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1132 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001133
1134 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1135 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1136 refs = btrfs_extent_data_ref_count(leaf, dref);
1137 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1138 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1139 refs = btrfs_shared_data_ref_count(leaf, sref);
1140 } else {
1141 refs = 1;
1142 BUG_ON(refs_to_mod != -1);
1143 }
1144
1145 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1146 refs += refs_to_mod;
1147
1148 if (refs > 0) {
1149 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1150 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1151 else
1152 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1153 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001154 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155 size = btrfs_extent_inline_ref_size(type);
1156 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1157 ptr = (unsigned long)iref;
1158 end = (unsigned long)ei + item_size;
1159 if (ptr + size < end)
1160 memmove_extent_buffer(leaf, ptr, ptr + size,
1161 end - ptr - size);
1162 item_size -= size;
David Sterba78ac4f92019-03-20 14:49:12 +01001163 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001164 }
1165 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001166}
1167
1168static noinline_for_stack
1169int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001170 struct btrfs_path *path,
1171 u64 bytenr, u64 num_bytes, u64 parent,
1172 u64 root_objectid, u64 owner,
1173 u64 offset, int refs_to_add,
1174 struct btrfs_delayed_extent_op *extent_op)
1175{
1176 struct btrfs_extent_inline_ref *iref;
1177 int ret;
1178
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001179 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1180 num_bytes, parent, root_objectid,
1181 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182 if (ret == 0) {
1183 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001184 update_inline_extent_backref(path, iref, refs_to_add,
1185 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001187 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001188 root_objectid, owner, offset,
1189 refs_to_add, extent_op);
1190 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 }
1192 return ret;
1193}
1194
1195static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196 struct btrfs_path *path,
1197 u64 bytenr, u64 parent, u64 root_objectid,
1198 u64 owner, u64 offset, int refs_to_add)
1199{
1200 int ret;
1201 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1202 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001203 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1204 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001205 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001206 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1207 root_objectid, owner, offset,
1208 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 }
1210 return ret;
1211}
1212
1213static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214 struct btrfs_path *path,
1215 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001216 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001217{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001218 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001219
1220 BUG_ON(!is_data && refs_to_drop != 1);
1221 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001222 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1223 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001224 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001225 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001226 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001228 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001229 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001230 }
1231 return ret;
1232}
1233
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001234static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1235 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001236{
Jeff Mahoney86557862015-06-15 09:41:16 -04001237 int j, ret = 0;
1238 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001239 u64 aligned_start = ALIGN(start, 1 << 9);
1240
1241 if (WARN_ON(start != aligned_start)) {
1242 len -= aligned_start - start;
1243 len = round_down(len, 1 << 9);
1244 start = aligned_start;
1245 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001246
1247 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001248
1249 if (!len)
1250 return 0;
1251
1252 end = start + len;
1253 bytes_left = len;
1254
1255 /* Skip any superblocks on this device. */
1256 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1257 u64 sb_start = btrfs_sb_offset(j);
1258 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1259 u64 size = sb_start - start;
1260
1261 if (!in_range(sb_start, start, bytes_left) &&
1262 !in_range(sb_end, start, bytes_left) &&
1263 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1264 continue;
1265
1266 /*
1267 * Superblock spans beginning of range. Adjust start and
1268 * try again.
1269 */
1270 if (sb_start <= start) {
1271 start += sb_end - start;
1272 if (start > end) {
1273 bytes_left = 0;
1274 break;
1275 }
1276 bytes_left = end - start;
1277 continue;
1278 }
1279
1280 if (size) {
1281 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1282 GFP_NOFS, 0);
1283 if (!ret)
1284 *discarded_bytes += size;
1285 else if (ret != -EOPNOTSUPP)
1286 return ret;
1287 }
1288
1289 start = sb_end;
1290 if (start > end) {
1291 bytes_left = 0;
1292 break;
1293 }
1294 bytes_left = end - start;
1295 }
1296
1297 if (bytes_left) {
1298 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001299 GFP_NOFS, 0);
1300 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001301 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001302 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001303 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001304}
Chris Mason15916de2008-11-19 21:17:22 -05001305
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001306int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001307 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001308{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001309 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001310 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001311 u64 end = bytenr + num_bytes;
1312 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001313 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001314
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001315
Filipe Manana29992412016-05-27 17:42:05 +01001316 /*
1317 * Avoid races with device replace and make sure our bbio has devices
1318 * associated to its stripes that don't go away while we are discarding.
1319 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001320 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001321 while (cur < end) {
1322 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001323 int i;
1324
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001325 num_bytes = end - cur;
1326 /* Tell the block device(s) that the sectors can be discarded */
1327 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1328 &num_bytes, &bbio, 0);
1329 /*
1330 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1331 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1332 * thus we can't continue anyway.
1333 */
1334 if (ret < 0)
1335 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001336
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001337 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001338 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001339 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001340 struct request_queue *req_q;
1341
Filipe Manana627e0872018-01-30 18:40:22 +00001342 if (!stripe->dev->bdev) {
1343 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1344 continue;
1345 }
Anand Jain38b5f682017-11-29 18:53:43 +08001346 req_q = bdev_get_queue(stripe->dev->bdev);
1347 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001348 continue;
1349
Li Dongyang5378e602011-03-24 10:24:27 +00001350 ret = btrfs_issue_discard(stripe->dev->bdev,
1351 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001352 stripe->length,
1353 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001354 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001355 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001356 } else if (ret != -EOPNOTSUPP) {
1357 /*
1358 * Logic errors or -ENOMEM, or -EIO, but
1359 * unlikely to happen.
1360 *
1361 * And since there are two loops, explicitly
1362 * go to out to avoid confusion.
1363 */
1364 btrfs_put_bbio(bbio);
1365 goto out;
1366 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001367
1368 /*
1369 * Just in case we get back EOPNOTSUPP for some reason,
1370 * just ignore the return value so we don't screw up
1371 * people calling discard_extent.
1372 */
1373 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001374 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001375 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001376 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001377 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001378out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001379 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001380
1381 if (actual_bytes)
1382 *actual_bytes = discarded_bytes;
1383
Liu Hui1f3c79a2009-01-05 15:57:51 -05001384
David Woodhouse53b381b2013-01-29 18:40:14 -05001385 if (ret == -EOPNOTSUPP)
1386 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001387 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001388}
1389
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001390/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001392 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001393{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001394 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001395 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001396 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001397
Qu Wenruo82fa1132019-04-04 14:45:35 +08001398 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1399 generic_ref->action);
1400 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1401 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001402
Qu Wenruo82fa1132019-04-04 14:45:35 +08001403 if (generic_ref->type == BTRFS_REF_METADATA)
1404 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001405 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001406 else
1407 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001408 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001409
Qu Wenruo82fa1132019-04-04 14:45:35 +08001410 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001411
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001412 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001413 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001414
Zheng Yan31840ae2008-09-23 13:14:14 -04001415 return ret;
1416}
1417
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001418/*
1419 * __btrfs_inc_extent_ref - insert backreference for a given extent
1420 *
1421 * @trans: Handle of transaction
1422 *
1423 * @node: The delayed ref node used to get the bytenr/length for
1424 * extent whose references are incremented.
1425 *
1426 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1427 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1428 * bytenr of the parent block. Since new extents are always
1429 * created with indirect references, this will only be the case
1430 * when relocating a shared extent. In that case, root_objectid
1431 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1432 * be 0
1433 *
1434 * @root_objectid: The id of the root where this modification has originated,
1435 * this can be either one of the well-known metadata trees or
1436 * the subvolume id which references this extent.
1437 *
1438 * @owner: For data extents it is the inode number of the owning file.
1439 * For metadata extents this parameter holds the level in the
1440 * tree of the extent.
1441 *
1442 * @offset: For metadata extents the offset is ignored and is currently
1443 * always passed as 0. For data extents it is the fileoffset
1444 * this extent belongs to.
1445 *
1446 * @refs_to_add Number of references to add
1447 *
1448 * @extent_op Pointer to a structure, holding information necessary when
1449 * updating a tree block's flags
1450 *
1451 */
Chris Mason925baed2008-06-25 16:01:30 -04001452static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001453 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001454 u64 parent, u64 root_objectid,
1455 u64 owner, u64 offset, int refs_to_add,
1456 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001457{
Chris Mason5caf2a02007-04-02 11:20:42 -04001458 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001460 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001461 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001462 u64 bytenr = node->bytenr;
1463 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001464 u64 refs;
1465 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001466
Chris Mason5caf2a02007-04-02 11:20:42 -04001467 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001468 if (!path)
1469 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001470
David Sterbae4058b52015-11-27 16:31:35 +01001471 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001472 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001473 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001474 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1475 parent, root_objectid, owner,
1476 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001477 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001479
1480 /*
1481 * Ok we had -EAGAIN which means we didn't have space to insert and
1482 * inline extent ref, so just update the reference count and add a
1483 * normal backref.
1484 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001485 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001486 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001487 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1488 refs = btrfs_extent_refs(leaf, item);
1489 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1490 if (extent_op)
1491 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001494 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001495
David Sterbae4058b52015-11-27 16:31:35 +01001496 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001497 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001498 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001499 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1500 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001501 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001502 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503out:
Chris Mason74493f72007-12-11 09:25:06 -05001504 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001505 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001506}
1507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509 struct btrfs_delayed_ref_node *node,
1510 struct btrfs_delayed_extent_op *extent_op,
1511 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001512{
Chris Mason56bec292009-03-13 10:10:06 -04001513 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 struct btrfs_delayed_data_ref *ref;
1515 struct btrfs_key ins;
1516 u64 parent = 0;
1517 u64 ref_root = 0;
1518 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001520 ins.objectid = node->bytenr;
1521 ins.offset = node->num_bytes;
1522 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001525 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001526
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1528 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001529 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530
1531 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001532 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001533 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001534 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1535 flags, ref->objectid,
1536 ref->offset, &ins,
1537 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001539 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1540 ref->objectid, ref->offset,
1541 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001542 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001543 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001544 ref_root, ref->objectid,
1545 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001546 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001547 } else {
1548 BUG();
1549 }
Chris Mason56bec292009-03-13 10:10:06 -04001550 return ret;
1551}
1552
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001553static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1554 struct extent_buffer *leaf,
1555 struct btrfs_extent_item *ei)
1556{
1557 u64 flags = btrfs_extent_flags(leaf, ei);
1558 if (extent_op->update_flags) {
1559 flags |= extent_op->flags_to_set;
1560 btrfs_set_extent_flags(leaf, ei, flags);
1561 }
1562
1563 if (extent_op->update_key) {
1564 struct btrfs_tree_block_info *bi;
1565 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1566 bi = (struct btrfs_tree_block_info *)(ei + 1);
1567 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1568 }
1569}
1570
1571static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001572 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 struct btrfs_delayed_extent_op *extent_op)
1574{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001575 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001576 struct btrfs_key key;
1577 struct btrfs_path *path;
1578 struct btrfs_extent_item *ei;
1579 struct extent_buffer *leaf;
1580 u32 item_size;
1581 int ret;
1582 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001583 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001584
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001585 if (trans->aborted)
1586 return 0;
1587
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001588 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001589 metadata = 0;
1590
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 path = btrfs_alloc_path();
1592 if (!path)
1593 return -ENOMEM;
1594
Josef Bacikd2788502017-09-29 15:43:57 -04001595 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001596
Josef Bacik3173a182013-03-07 14:22:04 -05001597 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001598 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001599 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001600 } else {
1601 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001602 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001603 }
1604
1605again:
David Sterbae4058b52015-11-27 16:31:35 +01001606 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001607 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609 if (ret < 0) {
1610 err = ret;
1611 goto out;
1612 }
1613 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001614 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001615 if (path->slots[0] > 0) {
1616 path->slots[0]--;
1617 btrfs_item_key_to_cpu(path->nodes[0], &key,
1618 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001619 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001620 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001621 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001622 ret = 0;
1623 }
1624 if (ret > 0) {
1625 btrfs_release_path(path);
1626 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001627
Josef Bacikd2788502017-09-29 15:43:57 -04001628 key.objectid = head->bytenr;
1629 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001630 key.type = BTRFS_EXTENT_ITEM_KEY;
1631 goto again;
1632 }
1633 } else {
1634 err = -EIO;
1635 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001636 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001637 }
1638
1639 leaf = path->nodes[0];
1640 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001641
David Sterba6d8ff4e2018-06-26 16:20:59 +02001642 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001643 err = -EINVAL;
1644 btrfs_print_v0_err(fs_info);
1645 btrfs_abort_transaction(trans, err);
1646 goto out;
1647 }
1648
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001649 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1650 __run_delayed_extent_op(extent_op, leaf, ei);
1651
1652 btrfs_mark_buffer_dirty(leaf);
1653out:
1654 btrfs_free_path(path);
1655 return err;
1656}
1657
1658static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659 struct btrfs_delayed_ref_node *node,
1660 struct btrfs_delayed_extent_op *extent_op,
1661 int insert_reserved)
1662{
1663 int ret = 0;
1664 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001665 u64 parent = 0;
1666 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667
1668 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001669 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001670
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001671 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1672 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001673 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001674
Liu Bo02794222016-09-14 19:19:05 -07001675 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001676 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001677 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1678 node->bytenr, node->ref_mod, node->action, ref_root,
1679 parent);
1680 return -EIO;
1681 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001683 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001684 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001686 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1687 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001689 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001690 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 } else {
1692 BUG();
1693 }
1694 return ret;
1695}
1696
Chris Mason56bec292009-03-13 10:10:06 -04001697/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 struct btrfs_delayed_ref_node *node,
1700 struct btrfs_delayed_extent_op *extent_op,
1701 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001702{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001703 int ret = 0;
1704
Josef Bacik857cc2f2013-10-07 15:21:08 -04001705 if (trans->aborted) {
1706 if (insert_reserved)
Nikolay Borisov5fac7f92018-06-20 15:49:11 +03001707 btrfs_pin_extent(trans->fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04001708 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001709 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001710 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001711
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001712 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1713 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001714 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 insert_reserved);
1716 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1717 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001718 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 insert_reserved);
1720 else
1721 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001722 if (ret && insert_reserved)
1723 btrfs_pin_extent(trans->fs_info, node->bytenr,
1724 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001725 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001726}
1727
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001728static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001729select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001730{
Filipe Mananacffc3372015-07-09 13:13:44 +01001731 struct btrfs_delayed_ref_node *ref;
1732
Liu Boe3d03962018-08-23 03:51:50 +08001733 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001734 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001735
Filipe Mananacffc3372015-07-09 13:13:44 +01001736 /*
1737 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1738 * This is to prevent a ref count from going down to zero, which deletes
1739 * the extent item from the extent tree, when there still are references
1740 * to add, which would fail because they would not find the extent item.
1741 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001742 if (!list_empty(&head->ref_add_list))
1743 return list_first_entry(&head->ref_add_list,
1744 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001745
Liu Boe3d03962018-08-23 03:51:50 +08001746 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001747 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001748 ASSERT(list_empty(&ref->add_list));
1749 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001750}
1751
Josef Bacik2eadaa22017-09-29 15:43:52 -04001752static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1753 struct btrfs_delayed_ref_head *head)
1754{
1755 spin_lock(&delayed_refs->lock);
1756 head->processing = 0;
1757 delayed_refs->num_heads_ready++;
1758 spin_unlock(&delayed_refs->lock);
1759 btrfs_delayed_ref_unlock(head);
1760}
1761
Josef Bacikbedc66172018-12-03 10:20:31 -05001762static struct btrfs_delayed_extent_op *cleanup_extent_op(
1763 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001764{
1765 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001766
1767 if (!extent_op)
1768 return NULL;
1769
1770 if (head->must_insert_reserved) {
1771 head->extent_op = NULL;
1772 btrfs_free_delayed_extent_op(extent_op);
1773 return NULL;
1774 }
1775 return extent_op;
1776}
1777
1778static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1779 struct btrfs_delayed_ref_head *head)
1780{
1781 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001782 int ret;
1783
Josef Bacikbedc66172018-12-03 10:20:31 -05001784 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001785 if (!extent_op)
1786 return 0;
1787 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001788 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001789 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001790 btrfs_free_delayed_extent_op(extent_op);
1791 return ret ? ret : 1;
1792}
1793
Josef Bacik31890da2018-11-21 14:05:41 -05001794void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1795 struct btrfs_delayed_ref_root *delayed_refs,
1796 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001797{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001798 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001799
1800 if (head->total_ref_mod < 0) {
1801 struct btrfs_space_info *space_info;
1802 u64 flags;
1803
1804 if (head->is_data)
1805 flags = BTRFS_BLOCK_GROUP_DATA;
1806 else if (head->is_system)
1807 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1808 else
1809 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001810 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001811 ASSERT(space_info);
1812 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1813 -head->num_bytes,
1814 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1815
Josef Bacikba2c4d42018-12-03 10:20:33 -05001816 /*
1817 * We had csum deletions accounted for in our delayed refs rsv,
1818 * we need to drop the csum leaves for this update from our
1819 * delayed_refs_rsv.
1820 */
Josef Bacik07c47772018-12-03 10:20:30 -05001821 if (head->is_data) {
1822 spin_lock(&delayed_refs->lock);
1823 delayed_refs->pending_csums -= head->num_bytes;
1824 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001825 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1826 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001827 }
1828 }
1829
Josef Bacikba2c4d42018-12-03 10:20:33 -05001830 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001831}
1832
Josef Bacik194ab0b2017-09-29 15:43:54 -04001833static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001834 struct btrfs_delayed_ref_head *head)
1835{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001836
1837 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001838 struct btrfs_delayed_ref_root *delayed_refs;
1839 int ret;
1840
1841 delayed_refs = &trans->transaction->delayed_refs;
1842
Josef Bacikbedc66172018-12-03 10:20:31 -05001843 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001844 if (ret < 0) {
1845 unselect_delayed_ref_head(delayed_refs, head);
1846 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1847 return ret;
1848 } else if (ret) {
1849 return ret;
1850 }
1851
1852 /*
1853 * Need to drop our head ref lock and re-acquire the delayed ref lock
1854 * and then re-check to make sure nobody got added.
1855 */
1856 spin_unlock(&head->lock);
1857 spin_lock(&delayed_refs->lock);
1858 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001859 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001860 spin_unlock(&head->lock);
1861 spin_unlock(&delayed_refs->lock);
1862 return 1;
1863 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001864 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001865 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001866 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001867
Josef Bacikc1103f72017-09-29 15:43:56 -04001868 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04001869 btrfs_pin_extent(fs_info, head->bytenr,
1870 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001871 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04001872 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
1873 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001874 }
1875 }
1876
Josef Bacik31890da2018-11-21 14:05:41 -05001877 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001878
1879 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001880 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001881 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001882 return 0;
1883}
1884
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001885static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1886 struct btrfs_trans_handle *trans)
1887{
1888 struct btrfs_delayed_ref_root *delayed_refs =
1889 &trans->transaction->delayed_refs;
1890 struct btrfs_delayed_ref_head *head = NULL;
1891 int ret;
1892
1893 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001894 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001895 if (!head) {
1896 spin_unlock(&delayed_refs->lock);
1897 return head;
1898 }
1899
1900 /*
1901 * Grab the lock that says we are going to process all the refs for
1902 * this head
1903 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001904 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001905 spin_unlock(&delayed_refs->lock);
1906
1907 /*
1908 * We may have dropped the spin lock to get the head mutex lock, and
1909 * that might have given someone else time to free the head. If that's
1910 * true, it has been removed from our list and we can move on.
1911 */
1912 if (ret == -EAGAIN)
1913 head = ERR_PTR(-EAGAIN);
1914
1915 return head;
1916}
1917
Nikolay Borisove7261382018-08-15 10:39:55 +03001918static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1919 struct btrfs_delayed_ref_head *locked_ref,
1920 unsigned long *run_refs)
1921{
1922 struct btrfs_fs_info *fs_info = trans->fs_info;
1923 struct btrfs_delayed_ref_root *delayed_refs;
1924 struct btrfs_delayed_extent_op *extent_op;
1925 struct btrfs_delayed_ref_node *ref;
1926 int must_insert_reserved = 0;
1927 int ret;
1928
1929 delayed_refs = &trans->transaction->delayed_refs;
1930
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001931 lockdep_assert_held(&locked_ref->mutex);
1932 lockdep_assert_held(&locked_ref->lock);
1933
Nikolay Borisove7261382018-08-15 10:39:55 +03001934 while ((ref = select_delayed_ref(locked_ref))) {
1935 if (ref->seq &&
1936 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1937 spin_unlock(&locked_ref->lock);
1938 unselect_delayed_ref_head(delayed_refs, locked_ref);
1939 return -EAGAIN;
1940 }
1941
1942 (*run_refs)++;
1943 ref->in_tree = 0;
1944 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1945 RB_CLEAR_NODE(&ref->ref_node);
1946 if (!list_empty(&ref->add_list))
1947 list_del(&ref->add_list);
1948 /*
1949 * When we play the delayed ref, also correct the ref_mod on
1950 * head
1951 */
1952 switch (ref->action) {
1953 case BTRFS_ADD_DELAYED_REF:
1954 case BTRFS_ADD_DELAYED_EXTENT:
1955 locked_ref->ref_mod -= ref->ref_mod;
1956 break;
1957 case BTRFS_DROP_DELAYED_REF:
1958 locked_ref->ref_mod += ref->ref_mod;
1959 break;
1960 default:
1961 WARN_ON(1);
1962 }
1963 atomic_dec(&delayed_refs->num_entries);
1964
1965 /*
1966 * Record the must_insert_reserved flag before we drop the
1967 * spin lock.
1968 */
1969 must_insert_reserved = locked_ref->must_insert_reserved;
1970 locked_ref->must_insert_reserved = 0;
1971
1972 extent_op = locked_ref->extent_op;
1973 locked_ref->extent_op = NULL;
1974 spin_unlock(&locked_ref->lock);
1975
1976 ret = run_one_delayed_ref(trans, ref, extent_op,
1977 must_insert_reserved);
1978
1979 btrfs_free_delayed_extent_op(extent_op);
1980 if (ret) {
1981 unselect_delayed_ref_head(delayed_refs, locked_ref);
1982 btrfs_put_delayed_ref(ref);
1983 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1984 ret);
1985 return ret;
1986 }
1987
1988 btrfs_put_delayed_ref(ref);
1989 cond_resched();
1990
1991 spin_lock(&locked_ref->lock);
1992 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1993 }
1994
1995 return 0;
1996}
1997
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001998/*
1999 * Returns 0 on success or if called with an already aborted transaction.
2000 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2001 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002002static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002003 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002004{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002005 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002006 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002007 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002008 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002009 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002010 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002011 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002012
2013 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002014 do {
Chris Mason56bec292009-03-13 10:10:06 -04002015 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002016 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002017 if (IS_ERR_OR_NULL(locked_ref)) {
2018 if (PTR_ERR(locked_ref) == -EAGAIN) {
2019 continue;
2020 } else {
2021 break;
2022 }
Chris Mason56bec292009-03-13 10:10:06 -04002023 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002024 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002025 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002026 /*
2027 * We need to try and merge add/drops of the same ref since we
2028 * can run into issues with relocate dropping the implicit ref
2029 * and then it being added back again before the drop can
2030 * finish. If we merged anything we need to re-loop so we can
2031 * get a good ref.
2032 * Or we can get node references of the same type that weren't
2033 * merged when created due to bumps in the tree mod seq, and
2034 * we need to merge them to prevent adding an inline extent
2035 * backref before dropping it (triggering a BUG_ON at
2036 * insert_inline_extent_backref()).
2037 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002038 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002039 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002040
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002041 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2042 &actual_count);
2043 if (ret < 0 && ret != -EAGAIN) {
2044 /*
2045 * Error, btrfs_run_delayed_refs_for_head already
2046 * unlocked everything so just bail out
2047 */
2048 return ret;
2049 } else if (!ret) {
2050 /*
2051 * Success, perform the usual cleanup of a processed
2052 * head
2053 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002054 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002055 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002056 /* We dropped our lock, we need to loop. */
2057 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002058 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002059 } else if (ret) {
2060 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002062 }
Chris Mason56bec292009-03-13 10:10:06 -04002063
Josef Bacikb00e6252017-09-29 15:43:53 -04002064 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002065 * Either success case or btrfs_run_delayed_refs_for_head
2066 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002067 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002068
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002069 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002070 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002071 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002072
2073 /*
2074 * We don't want to include ref heads since we can have empty ref heads
2075 * and those will drastically skew our runtime down since we just do
2076 * accounting, no actual extent tree updates.
2077 */
2078 if (actual_count > 0) {
2079 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2080 u64 avg;
2081
2082 /*
2083 * We weigh the current average higher than our current runtime
2084 * to avoid large swings in the average.
2085 */
2086 spin_lock(&delayed_refs->lock);
2087 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002088 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002089 spin_unlock(&delayed_refs->lock);
2090 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002091 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002092}
2093
Arne Jansen709c0482011-09-12 12:22:57 +02002094#ifdef SCRAMBLE_DELAYED_REFS
2095/*
2096 * Normally delayed refs get processed in ascending bytenr order. This
2097 * correlates in most cases to the order added. To expose dependencies on this
2098 * order, we start to process the tree in the middle instead of the beginning
2099 */
2100static u64 find_middle(struct rb_root *root)
2101{
2102 struct rb_node *n = root->rb_node;
2103 struct btrfs_delayed_ref_node *entry;
2104 int alt = 1;
2105 u64 middle;
2106 u64 first = 0, last = 0;
2107
2108 n = rb_first(root);
2109 if (n) {
2110 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2111 first = entry->bytenr;
2112 }
2113 n = rb_last(root);
2114 if (n) {
2115 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2116 last = entry->bytenr;
2117 }
2118 n = root->rb_node;
2119
2120 while (n) {
2121 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2122 WARN_ON(!entry->in_tree);
2123
2124 middle = entry->bytenr;
2125
2126 if (alt)
2127 n = n->rb_left;
2128 else
2129 n = n->rb_right;
2130
2131 alt = 1 - alt;
2132 }
2133 return middle;
2134}
2135#endif
2136
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002137static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002138{
2139 u64 num_bytes;
2140
2141 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2142 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002143 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002144 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2145
2146 /*
2147 * 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 -04002148 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002149 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002150 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002151}
2152
Josef Bacik12621332015-02-03 07:50:16 -08002153/*
2154 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2155 * would require to store the csums for that many bytes.
2156 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002157u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002158{
2159 u64 csum_size;
2160 u64 num_csums_per_leaf;
2161 u64 num_csums;
2162
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002163 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002164 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002165 (u64)btrfs_super_csum_size(fs_info->super_copy));
2166 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002167 num_csums += num_csums_per_leaf - 1;
2168 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2169 return num_csums;
2170}
2171
Chris Masonc3e69d52009-03-13 10:17:05 -04002172/*
2173 * this starts processing the delayed reference count updates and
2174 * extent insertions we have queued up so far. count can be
2175 * 0, which means to process everything in the tree at the start
2176 * of the run (but not newly added entries), or it can be some target
2177 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002178 *
2179 * Returns 0 on success or if called with an aborted transaction
2180 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002181 */
2182int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002183 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002184{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002185 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002186 struct rb_node *node;
2187 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002188 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002189 int ret;
2190 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002191
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002192 /* We'll clean this up in btrfs_cleanup_transaction */
2193 if (trans->aborted)
2194 return 0;
2195
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002196 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002197 return 0;
2198
Chris Masonc3e69d52009-03-13 10:17:05 -04002199 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002200 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002201 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002202
Chris Masonc3e69d52009-03-13 10:17:05 -04002203again:
Arne Jansen709c0482011-09-12 12:22:57 +02002204#ifdef SCRAMBLE_DELAYED_REFS
2205 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2206#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002207 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002208 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002209 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002210 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002211 }
2212
Chris Mason56bec292009-03-13 10:10:06 -04002213 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002214 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002215
Josef Bacikd7df2c72014-01-23 09:21:38 -05002216 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002217 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002218 if (!node) {
2219 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002220 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002221 }
Josef Bacikd2788502017-09-29 15:43:57 -04002222 head = rb_entry(node, struct btrfs_delayed_ref_head,
2223 href_node);
2224 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002225 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002226
2227 /* Mutex was contended, block until it's released and retry. */
2228 mutex_lock(&head->mutex);
2229 mutex_unlock(&head->mutex);
2230
2231 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002232 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002233 goto again;
2234 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002235out:
Chris Masona28ec192007-03-06 20:08:01 -05002236 return 0;
2237}
2238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002239int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002241 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242{
2243 struct btrfs_delayed_extent_op *extent_op;
2244 int ret;
2245
Miao Xie78a61842012-11-21 02:21:28 +00002246 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 if (!extent_op)
2248 return -ENOMEM;
2249
2250 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002251 extent_op->update_flags = true;
2252 extent_op->update_key = false;
2253 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002254 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255
David Sterbac6e340b2019-03-20 11:42:34 +01002256 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002257 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002258 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 return ret;
2260}
2261
Liu Boe4c3b2d2017-01-30 12:25:28 -08002262static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 struct btrfs_path *path,
2264 u64 objectid, u64 offset, u64 bytenr)
2265{
2266 struct btrfs_delayed_ref_head *head;
2267 struct btrfs_delayed_ref_node *ref;
2268 struct btrfs_delayed_data_ref *data_ref;
2269 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002270 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002271 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002272 int ret = 0;
2273
ethanwu998ac6d2018-04-29 15:59:42 +08002274 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002275 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002276 if (cur_trans)
2277 refcount_inc(&cur_trans->use_count);
2278 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002279 if (!cur_trans)
2280 return 0;
2281
2282 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002284 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002285 if (!head) {
2286 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002287 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002288 return 0;
2289 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290
2291 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002292 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293 spin_unlock(&delayed_refs->lock);
2294
David Sterbab3b4aa72011-04-21 01:20:15 +02002295 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002296
David Sterba8cc33e52011-05-02 15:29:25 +02002297 /*
2298 * Mutex was contended, block until it's released and let
2299 * caller try again
2300 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301 mutex_lock(&head->mutex);
2302 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002303 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002304 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 return -EAGAIN;
2306 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002307 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002308
2309 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002310 /*
2311 * XXX: We should replace this with a proper search function in the
2312 * future.
2313 */
Liu Boe3d03962018-08-23 03:51:50 +08002314 for (node = rb_first_cached(&head->ref_tree); node;
2315 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002316 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002317 /* If it's a shared ref we know a cross reference exists */
2318 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2319 ret = 1;
2320 break;
2321 }
2322
2323 data_ref = btrfs_delayed_node_to_data_ref(ref);
2324
2325 /*
2326 * If our ref doesn't match the one we're currently looking at
2327 * then we have a cross reference.
2328 */
2329 if (data_ref->root != root->root_key.objectid ||
2330 data_ref->objectid != objectid ||
2331 data_ref->offset != offset) {
2332 ret = 1;
2333 break;
2334 }
2335 }
2336 spin_unlock(&head->lock);
2337 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002338 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002339 return ret;
2340}
2341
Liu Boe4c3b2d2017-01-30 12:25:28 -08002342static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343 struct btrfs_path *path,
2344 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002345{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002346 struct btrfs_fs_info *fs_info = root->fs_info;
2347 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002348 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002349 struct btrfs_extent_data_ref *ref;
2350 struct btrfs_extent_inline_ref *iref;
2351 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002352 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002354 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002355 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002356
Chris Masonbe20aa92007-12-17 20:14:01 -05002357 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002358 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002359 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002360
Chris Masonbe20aa92007-12-17 20:14:01 -05002361 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2362 if (ret < 0)
2363 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002364 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002365
2366 ret = -ENOENT;
2367 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002368 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002369
Zheng Yan31840ae2008-09-23 13:14:14 -04002370 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002371 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002373
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002375 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377 ret = 1;
2378 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2380
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002381 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382 if (item_size != sizeof(*ei) +
2383 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2384 goto out;
2385
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002386 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002387 if (btrfs_extent_generation(leaf, ei) <=
2388 btrfs_root_last_snapshot(&root->root_item))
2389 goto out;
2390
2391 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002392
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002393 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002394 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2395 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396 goto out;
2397
2398 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2399 if (btrfs_extent_refs(leaf, ei) !=
2400 btrfs_extent_data_ref_count(leaf, ref) ||
2401 btrfs_extent_data_ref_root(leaf, ref) !=
2402 root->root_key.objectid ||
2403 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2404 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2405 goto out;
2406
Yan Zhengf321e492008-07-30 09:26:11 -04002407 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002408out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409 return ret;
2410}
2411
Liu Boe4c3b2d2017-01-30 12:25:28 -08002412int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2413 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002414{
2415 struct btrfs_path *path;
2416 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417
2418 path = btrfs_alloc_path();
2419 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002420 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421
2422 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002423 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002424 offset, bytenr);
2425 if (ret && ret != -ENOENT)
2426 goto out;
2427
Misono Tomohiro380fd062018-08-30 10:59:16 +09002428 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2429 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002431out:
Yan Zhengf321e492008-07-30 09:26:11 -04002432 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002433 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2434 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002435 return ret;
2436}
2437
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002438static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002439 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002441 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002444 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002445 u64 num_bytes;
2446 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002447 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002448 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002449 struct btrfs_key key;
2450 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002451 struct btrfs_ref generic_ref = { 0 };
2452 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002454 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002455 int level;
2456 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002458 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002459 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002460
Zheng Yan31840ae2008-09-23 13:14:14 -04002461 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 nritems = btrfs_header_nritems(buf);
2463 level = btrfs_header_level(buf);
2464
Miao Xie27cdeb72014-04-02 19:51:05 +08002465 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002466 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 if (full_backref)
2469 parent = buf->start;
2470 else
2471 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002472 if (inc)
2473 action = BTRFS_ADD_DELAYED_REF;
2474 else
2475 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002476
Zheng Yan31840ae2008-09-23 13:14:14 -04002477 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002478 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002479 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002480 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002481 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002482 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002483 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002484 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002485 BTRFS_FILE_EXTENT_INLINE)
2486 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002487 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2488 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002489 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002490
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002491 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2492 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002493 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2494 num_bytes, parent);
2495 generic_ref.real_root = root->root_key.objectid;
2496 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2497 key.offset);
2498 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002499 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002500 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002501 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002502 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002503 if (ret)
2504 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002505 } else {
2506 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002508 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2509 num_bytes, parent);
2510 generic_ref.real_root = root->root_key.objectid;
2511 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2512 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002513 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002514 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002515 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002516 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002517 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002518 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002519 }
2520 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002521 return 0;
2522fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002523 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002524}
2525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002526int btrfs_inc_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)
Zheng Yan31840ae2008-09-23 13:14:14 -04002528{
Josef Bacike339a6b2014-07-02 10:54:25 -07002529 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530}
Zheng Yan31840ae2008-09-23 13:14:14 -04002531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002533 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002534{
Josef Bacike339a6b2014-07-02 10:54:25 -07002535 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002536}
2537
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002538int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002539{
David Sterba32da53862019-10-29 19:20:18 +01002540 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002541 int readonly = 0;
2542
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002543 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002544 if (!block_group || block_group->ro)
2545 readonly = 1;
2546 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002547 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002548 return readonly;
2549}
2550
Jeff Mahoney1b868262017-05-17 11:38:35 -04002551static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002552{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002553 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002554 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002555 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002556
Yan, Zhengb742bb822010-05-16 10:46:24 -04002557 if (data)
2558 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002559 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002560 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2561 else
2562 flags = BTRFS_BLOCK_GROUP_METADATA;
2563
Josef Bacik878d7b62019-06-20 15:38:05 -04002564 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002565 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002566}
2567
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002568static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002569{
David Sterba32da53862019-10-29 19:20:18 +01002570 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002571 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002572
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002573 spin_lock(&fs_info->block_group_cache_lock);
2574 bytenr = fs_info->first_logical_byte;
2575 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002576
2577 if (bytenr < (u64)-1)
2578 return bytenr;
2579
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002580 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002581 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002582 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002583
David Sterbab3470b52019-10-23 18:48:22 +02002584 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002585 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002586
2587 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002588}
2589
David Sterba32da53862019-10-29 19:20:18 +01002590static int pin_down_extent(struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002591 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002592{
David Sterbafdf08602019-03-20 12:12:32 +01002593 struct btrfs_fs_info *fs_info = cache->fs_info;
2594
Yan Zheng11833d62009-09-11 16:11:19 -04002595 spin_lock(&cache->space_info->lock);
2596 spin_lock(&cache->lock);
2597 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002598 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2599 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002600 if (reserved) {
2601 cache->reserved -= num_bytes;
2602 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002603 }
Yan Zheng11833d62009-09-11 16:11:19 -04002604 spin_unlock(&cache->lock);
2605 spin_unlock(&cache->space_info->lock);
2606
Ethan Liendec59fa2018-07-13 16:50:42 +08002607 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2608 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002609 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002610 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002611 return 0;
2612}
Chris Mason9078a3e2007-04-26 16:46:15 -04002613
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002614int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002615 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002616{
David Sterba32da53862019-10-29 19:20:18 +01002617 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002618
Nikolay Borisovce6d3eb2019-10-08 16:26:16 +03002619 ASSERT(fs_info->running_transaction);
2620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002621 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002623
David Sterbafdf08602019-03-20 12:12:32 +01002624 pin_down_extent(cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002625
2626 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002627 return 0;
2628}
Zheng Yane8569812008-09-26 10:05:48 -04002629
Yan, Zhengf0486c62010-05-16 10:46:25 -04002630/*
Chris Masone688b7252011-10-31 20:52:39 -04002631 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002632 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002633int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04002634 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002635{
David Sterba32da53862019-10-29 19:20:18 +01002636 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002637 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002638
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002639 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002640 if (!cache)
2641 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002642
2643 /*
2644 * pull in the free space cache (if any) so that our pin
2645 * removes the free space from the cache. We have load_only set
2646 * to one because the slow code to read in the free extents does check
2647 * the pinned extents.
2648 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002649 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002650
David Sterbafdf08602019-03-20 12:12:32 +01002651 pin_down_extent(cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002652
2653 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002654 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002655 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002656 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002657}
2658
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002659static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2660 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002661{
2662 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002663 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002664 struct btrfs_caching_control *caching_ctl;
2665
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002666 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002667 if (!block_group)
2668 return -EINVAL;
2669
Josef Bacik676f1f72019-06-20 15:37:48 -04002670 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002671 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002672
2673 if (!caching_ctl) {
2674 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002675 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002676 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2677 } else {
2678 mutex_lock(&caching_ctl->mutex);
2679
2680 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002681 ret = btrfs_add_excluded_extent(fs_info, start,
2682 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002683 } else if (start + num_bytes <= caching_ctl->progress) {
2684 ret = btrfs_remove_free_space(block_group,
2685 start, num_bytes);
2686 } else {
2687 num_bytes = caching_ctl->progress - start;
2688 ret = btrfs_remove_free_space(block_group,
2689 start, num_bytes);
2690 if (ret)
2691 goto out_lock;
2692
2693 num_bytes = (start + num_bytes) -
2694 caching_ctl->progress;
2695 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002696 ret = btrfs_add_excluded_extent(fs_info, start,
2697 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002698 }
2699out_lock:
2700 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002701 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002702 }
2703 btrfs_put_block_group(block_group);
2704 return ret;
2705}
2706
David Sterbabcdc4282019-03-20 12:14:33 +01002707int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002708{
David Sterbabcdc4282019-03-20 12:14:33 +01002709 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002710 struct btrfs_file_extent_item *item;
2711 struct btrfs_key key;
2712 int found_type;
2713 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002714 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002716 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002717 return 0;
2718
2719 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2720 btrfs_item_key_to_cpu(eb, &key, i);
2721 if (key.type != BTRFS_EXTENT_DATA_KEY)
2722 continue;
2723 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2724 found_type = btrfs_file_extent_type(eb, item);
2725 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2726 continue;
2727 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2728 continue;
2729 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2730 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002731 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2732 if (ret)
2733 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002734 }
2735
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002736 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002737}
2738
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002739static void
David Sterba32da53862019-10-29 19:20:18 +01002740btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002741{
2742 atomic_inc(&bg->reservations);
2743}
2744
David Sterba8b74c032017-02-10 19:20:56 +01002745void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002746{
Yan Zheng11833d62009-09-11 16:11:19 -04002747 struct btrfs_caching_control *next;
2748 struct btrfs_caching_control *caching_ctl;
David Sterba32da53862019-10-29 19:20:18 +01002749 struct btrfs_block_group *cache;
Yan Zheng11833d62009-09-11 16:11:19 -04002750
Josef Bacik9e351cc2014-03-13 15:42:13 -04002751 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002752
2753 list_for_each_entry_safe(caching_ctl, next,
2754 &fs_info->caching_block_groups, list) {
2755 cache = caching_ctl->block_group;
David Sterba32da53862019-10-29 19:20:18 +01002756 if (btrfs_block_group_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002757 cache->last_byte_to_unpin = (u64)-1;
2758 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002759 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002760 } else {
2761 cache->last_byte_to_unpin = caching_ctl->progress;
2762 }
2763 }
2764
2765 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2766 fs_info->pinned_extents = &fs_info->freed_extents[1];
2767 else
2768 fs_info->pinned_extents = &fs_info->freed_extents[0];
2769
Josef Bacik9e351cc2014-03-13 15:42:13 -04002770 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002771
Josef Bacik67f9c222019-06-19 13:47:23 -04002772 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002773}
2774
Josef Bacikc759c4e2015-10-02 15:25:10 -04002775/*
2776 * Returns the free cluster for the given space info and sets empty_cluster to
2777 * what it should be based on the mount options.
2778 */
2779static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002780fetch_cluster_info(struct btrfs_fs_info *fs_info,
2781 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002782{
2783 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002784
2785 *empty_cluster = 0;
2786 if (btrfs_mixed_space_info(space_info))
2787 return ret;
2788
Josef Bacikc759c4e2015-10-02 15:25:10 -04002789 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002791 if (btrfs_test_opt(fs_info, SSD))
2792 *empty_cluster = SZ_2M;
2793 else
Byongho Leeee221842015-12-15 01:42:10 +09002794 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002795 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2796 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2797 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002798 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002799 }
2800
2801 return ret;
2802}
2803
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002804static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2805 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002806 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002807{
David Sterba32da53862019-10-29 19:20:18 +01002808 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002809 struct btrfs_space_info *space_info;
2810 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002811 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002812 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002813 u64 total_unpinned = 0;
2814 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002815 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002816
2817 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002818 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002819 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002820 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002821 if (cache)
2822 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002823 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002824 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002825 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002826
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002827 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002828 cache->space_info,
2829 &empty_cluster);
2830 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002831 }
2832
David Sterbab3470b52019-10-23 18:48:22 +02002833 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002834 len = min(len, end + 1 - start);
2835
2836 if (start < cache->last_byte_to_unpin) {
2837 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002838 if (return_free_space)
2839 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002840 }
Josef Bacik25179202008-10-29 14:49:05 -04002841
Yan, Zhengf0486c62010-05-16 10:46:25 -04002842 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002843 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002844 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002845
Josef Bacikc759c4e2015-10-02 15:25:10 -04002846 /*
2847 * If this space cluster has been marked as fragmented and we've
2848 * unpinned enough in this block group to potentially allow a
2849 * cluster to be created inside of it go ahead and clear the
2850 * fragmented check.
2851 */
2852 if (cluster && cluster->fragmented &&
2853 total_unpinned > empty_cluster) {
2854 spin_lock(&cluster->lock);
2855 cluster->fragmented = 0;
2856 spin_unlock(&cluster->lock);
2857 }
2858
Josef Bacik7b398f82012-10-22 15:52:28 -04002859 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002860 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002861 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002862 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002863 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002864 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2865 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002866 if (cache->ro) {
2867 space_info->bytes_readonly += len;
2868 readonly = true;
2869 }
Josef Bacik25179202008-10-29 14:49:05 -04002870 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002871 if (!readonly && return_free_space &&
2872 global_rsv->space_info == space_info) {
2873 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002874
Josef Bacik7b398f82012-10-22 15:52:28 -04002875 spin_lock(&global_rsv->lock);
2876 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002877 to_add = min(len, global_rsv->size -
2878 global_rsv->reserved);
2879 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002880 btrfs_space_info_update_bytes_may_use(fs_info,
2881 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002882 if (global_rsv->reserved >= global_rsv->size)
2883 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002884 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002885 }
2886 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002887 /* Add to any tickets we may have */
2888 if (len)
Josef Bacik18fa2282019-08-22 15:10:58 -04002889 btrfs_try_granting_tickets(fs_info,
2890 space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002891 }
2892 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002893 }
2894
2895 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002896 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002897 return 0;
2898}
2899
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002900int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002901{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002902 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002903 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002904 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002905 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002906 u64 start;
2907 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002908 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002909
Yan Zheng11833d62009-09-11 16:11:19 -04002910 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2911 unpin = &fs_info->freed_extents[1];
2912 else
2913 unpin = &fs_info->freed_extents[0];
2914
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002915 while (!trans->aborted) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002916 struct extent_state *cached_state = NULL;
2917
Filipe Mananad4b450c2015-01-29 19:18:25 +00002918 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002919 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002920 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002921 if (ret) {
2922 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002923 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002924 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002925
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002926 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002927 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002928 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002929
Filipe Manana0e6ec382018-11-16 13:04:44 +00002930 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002931 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002932 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002933 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002934 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002935 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002936
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002937 /*
2938 * Transaction is finished. We don't need the lock anymore. We
2939 * do need to clean up the block groups in case of a transaction
2940 * abort.
2941 */
2942 deleted_bgs = &trans->transaction->deleted_bgs;
2943 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2944 u64 trimmed = 0;
2945
2946 ret = -EROFS;
2947 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002948 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002949 block_group->start,
2950 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002951 &trimmed);
2952
2953 list_del_init(&block_group->bg_list);
2954 btrfs_put_block_group_trimming(block_group);
2955 btrfs_put_block_group(block_group);
2956
2957 if (ret) {
2958 const char *errstr = btrfs_decode_error(ret);
2959 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002960 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002961 ret, errstr);
2962 }
2963 }
2964
Chris Masone20d96d2007-03-22 12:13:20 -04002965 return 0;
2966}
2967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002968static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002969 struct btrfs_delayed_ref_node *node, u64 parent,
2970 u64 root_objectid, u64 owner_objectid,
2971 u64 owner_offset, int refs_to_drop,
2972 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002973{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002974 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002975 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002976 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002977 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002978 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 struct btrfs_extent_item *ei;
2980 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002981 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002982 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002983 int extent_slot = 0;
2984 int found_extent = 0;
2985 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 u32 item_size;
2987 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002988 u64 bytenr = node->bytenr;
2989 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002990 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002991 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002992
Chris Mason5caf2a02007-04-02 11:20:42 -04002993 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002994 if (!path)
2995 return -ENOMEM;
2996
David Sterbae4058b52015-11-27 16:31:35 +01002997 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002998 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999
3000 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3001 BUG_ON(!is_data && refs_to_drop != 1);
3002
Josef Bacik3173a182013-03-07 14:22:04 -05003003 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02003004 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05003005
Nikolay Borisovfbe48012018-06-20 15:48:52 +03003006 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
3007 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003008 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003009 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003010 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003011 while (extent_slot >= 0) {
3012 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003013 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003015 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3017 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003018 found_extent = 1;
3019 break;
3020 }
Josef Bacik3173a182013-03-07 14:22:04 -05003021 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3022 key.offset == owner_objectid) {
3023 found_extent = 1;
3024 break;
3025 }
Chris Mason952fcca2008-02-18 16:33:44 -05003026 if (path->slots[0] - extent_slot > 5)
3027 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003028 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003029 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003030
Zheng Yan31840ae2008-09-23 13:14:14 -04003031 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003032 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003033 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003034 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003035 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003036 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003037 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003038 goto out;
3039 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003040 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003041 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003042
3043 key.objectid = bytenr;
3044 key.type = BTRFS_EXTENT_ITEM_KEY;
3045 key.offset = num_bytes;
3046
Josef Bacik3173a182013-03-07 14:22:04 -05003047 if (!is_data && skinny_metadata) {
3048 key.type = BTRFS_METADATA_ITEM_KEY;
3049 key.offset = owner_objectid;
3050 }
3051
Zheng Yan31840ae2008-09-23 13:14:14 -04003052 ret = btrfs_search_slot(trans, extent_root,
3053 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003054 if (ret > 0 && skinny_metadata && path->slots[0]) {
3055 /*
3056 * Couldn't find our skinny metadata item,
3057 * see if we have ye olde extent item.
3058 */
3059 path->slots[0]--;
3060 btrfs_item_key_to_cpu(path->nodes[0], &key,
3061 path->slots[0]);
3062 if (key.objectid == bytenr &&
3063 key.type == BTRFS_EXTENT_ITEM_KEY &&
3064 key.offset == num_bytes)
3065 ret = 0;
3066 }
3067
3068 if (ret > 0 && skinny_metadata) {
3069 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003070 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003071 key.type = BTRFS_EXTENT_ITEM_KEY;
3072 key.offset = num_bytes;
3073 btrfs_release_path(path);
3074 ret = btrfs_search_slot(trans, extent_root,
3075 &key, path, -1, 1);
3076 }
3077
Josef Bacikf3465ca2008-11-12 14:19:50 -05003078 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003079 btrfs_err(info,
3080 "umm, got %d back from search, was looking for %llu",
3081 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003082 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003083 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003084 }
David Sterba005d6422012-09-18 07:52:32 -06003085 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003087 goto out;
3088 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003089 extent_slot = path->slots[0];
3090 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303091 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003092 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003093 btrfs_err(info,
3094 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003095 bytenr, parent, root_objectid, owner_objectid,
3096 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003097 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003098 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003099 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003100 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003101 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003102 }
Chris Mason5f39d392007-10-15 16:14:19 -04003103
3104 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003106 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003107 ret = -EINVAL;
3108 btrfs_print_v0_err(info);
3109 btrfs_abort_transaction(trans, ret);
3110 goto out;
3111 }
Chris Mason952fcca2008-02-18 16:33:44 -05003112 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003113 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003114 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3115 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116 struct btrfs_tree_block_info *bi;
3117 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3118 bi = (struct btrfs_tree_block_info *)(ei + 1);
3119 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003120 }
3121
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003122 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003123 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003124 btrfs_err(info,
3125 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3126 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003127 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003128 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003129 goto out;
3130 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131 refs -= refs_to_drop;
3132
3133 if (refs > 0) {
3134 if (extent_op)
3135 __run_delayed_extent_op(extent_op, leaf, ei);
3136 /*
3137 * In the case of inline back ref, reference count will
3138 * be updated by remove_extent_backref
3139 */
3140 if (iref) {
3141 BUG_ON(!found_extent);
3142 } else {
3143 btrfs_set_extent_refs(leaf, ei, refs);
3144 btrfs_mark_buffer_dirty(leaf);
3145 }
3146 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003147 ret = remove_extent_backref(trans, path, iref,
3148 refs_to_drop, is_data,
3149 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003150 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003151 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003152 goto out;
3153 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003154 }
3155 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003156 if (found_extent) {
3157 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003158 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003159 if (iref) {
3160 BUG_ON(path->slots[0] != extent_slot);
3161 } else {
3162 BUG_ON(path->slots[0] != extent_slot + 1);
3163 path->slots[0] = extent_slot;
3164 num_to_del = 2;
3165 }
Chris Mason78fae272007-03-25 11:35:08 -04003166 }
Chris Masonb9473432009-03-13 11:00:37 -04003167
Josef Bacikfcebe452014-05-13 17:30:47 -07003168 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003169 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3170 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003171 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003172 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003173 goto out;
3174 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003175 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003177 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003178 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003179 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003180 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003181 goto out;
3182 }
Chris Mason459931e2008-12-10 09:10:46 -05003183 }
3184
Nikolay Borisove7355e52018-05-10 15:44:55 +03003185 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003186 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003187 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003188 goto out;
3189 }
3190
Josef Bacikade4b512019-06-20 15:38:01 -04003191 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003192 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003193 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003194 goto out;
3195 }
Chris Masona28ec192007-03-06 20:08:01 -05003196 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003197 btrfs_release_path(path);
3198
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003199out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003200 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003201 return ret;
3202}
3203
3204/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003205 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003206 * delayed ref for that extent as well. This searches the delayed ref tree for
3207 * a given extent, and if there are no other delayed refs to be processed, it
3208 * removes it from the tree.
3209 */
3210static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003211 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003212{
3213 struct btrfs_delayed_ref_head *head;
3214 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003215 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003216
3217 delayed_refs = &trans->transaction->delayed_refs;
3218 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003219 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003220 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003221 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003222
Josef Bacikd7df2c72014-01-23 09:21:38 -05003223 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003224 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003225 goto out;
3226
Josef Bacikbedc66172018-12-03 10:20:31 -05003227 if (cleanup_extent_op(head) != NULL)
3228 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003229
Chris Mason1887be62009-03-13 10:11:24 -04003230 /*
3231 * waiting for the lock here would deadlock. If someone else has it
3232 * locked they are already in the process of dropping it anyway
3233 */
3234 if (!mutex_trylock(&head->mutex))
3235 goto out;
3236
Josef Bacikd7baffd2018-12-03 10:20:29 -05003237 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003238 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003239
Josef Bacikd7df2c72014-01-23 09:21:38 -05003240 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003241 spin_unlock(&delayed_refs->lock);
3242
Yan, Zhengf0486c62010-05-16 10:46:25 -04003243 BUG_ON(head->extent_op);
3244 if (head->must_insert_reserved)
3245 ret = 1;
3246
Josef Bacik31890da2018-11-21 14:05:41 -05003247 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003248 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003249 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003250 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003251out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003252 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003253
3254out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003255 spin_unlock(&delayed_refs->lock);
3256 return 0;
3257}
3258
Yan, Zhengf0486c62010-05-16 10:46:25 -04003259void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3260 struct btrfs_root *root,
3261 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003262 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003264 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003265 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003266 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003267 int ret;
3268
Qu Wenruoed4f2552019-04-04 14:45:31 +08003269 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3270 buf->start, buf->len, parent);
3271 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3272 root->root_key.objectid);
3273
Yan, Zhengf0486c62010-05-16 10:46:25 -04003274 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003275 int old_ref_mod, new_ref_mod;
3276
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003277 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003278 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003279 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003280 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003281 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003282 }
3283
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003284 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003285 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003286
Yan, Zhengf0486c62010-05-16 10:46:25 -04003287 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003288 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003289 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003290 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003291 }
3292
Omar Sandoval4da8b762017-06-06 16:45:28 -07003293 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003295
Yan, Zhengf0486c62010-05-16 10:46:25 -04003296 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
David Sterbafdf08602019-03-20 12:12:32 +01003297 pin_down_extent(cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003298 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003299 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003300 }
3301
3302 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3303
3304 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003305 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003306 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003307 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003308 }
3309out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003310 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003311 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003312
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003313 if (last_ref) {
3314 /*
3315 * Deleting the buffer, clear the corrupt flag since it doesn't
3316 * matter anymore.
3317 */
3318 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3319 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003320}
3321
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003322/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003323int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003324{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003325 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003326 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003327 int ret;
3328
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003329 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003330 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003331
Chris Mason56bec292009-03-13 10:10:06 -04003332 /*
3333 * tree log blocks never actually go into the extent allocation
3334 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003335 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003336 if ((ref->type == BTRFS_REF_METADATA &&
3337 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3338 (ref->type == BTRFS_REF_DATA &&
3339 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003340 /* unlocks the pinned mutex */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003341 btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003342 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003343 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003344 } else if (ref->type == BTRFS_REF_METADATA) {
3345 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003346 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003347 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003348 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003349 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003350 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003351
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003352 if (!((ref->type == BTRFS_REF_METADATA &&
3353 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3354 (ref->type == BTRFS_REF_DATA &&
3355 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3356 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003357
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003358 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003359 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003360
Chris Mason925baed2008-06-25 16:01:30 -04003361 return ret;
3362}
3363
Josef Bacik817d52f2009-07-13 21:29:25 -04003364enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003365 LOOP_CACHING_NOWAIT,
3366 LOOP_CACHING_WAIT,
3367 LOOP_ALLOC_CHUNK,
3368 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003369};
3370
Miao Xiee570fd22014-06-19 10:42:50 +08003371static inline void
David Sterba32da53862019-10-29 19:20:18 +01003372btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003373 int delalloc)
3374{
3375 if (delalloc)
3376 down_read(&cache->data_rwsem);
3377}
3378
David Sterba32da53862019-10-29 19:20:18 +01003379static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003380 int delalloc)
3381{
3382 btrfs_get_block_group(cache);
3383 if (delalloc)
3384 down_read(&cache->data_rwsem);
3385}
3386
David Sterba32da53862019-10-29 19:20:18 +01003387static struct btrfs_block_group *btrfs_lock_cluster(
3388 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003389 struct btrfs_free_cluster *cluster,
3390 int delalloc)
3391{
David Sterba32da53862019-10-29 19:20:18 +01003392 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003393
Miao Xiee570fd22014-06-19 10:42:50 +08003394 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003395 while (1) {
3396 used_bg = cluster->block_group;
3397 if (!used_bg)
3398 return NULL;
3399
3400 if (used_bg == block_group)
3401 return used_bg;
3402
3403 btrfs_get_block_group(used_bg);
3404
3405 if (!delalloc)
3406 return used_bg;
3407
3408 if (down_read_trylock(&used_bg->data_rwsem))
3409 return used_bg;
3410
3411 spin_unlock(&cluster->refill_lock);
3412
Liu Boe321f8a2016-11-30 16:11:04 -08003413 /* We should only have one-level nested. */
3414 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003415
3416 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003417 if (used_bg == cluster->block_group)
3418 return used_bg;
3419
3420 up_read(&used_bg->data_rwsem);
3421 btrfs_put_block_group(used_bg);
3422 }
Miao Xiee570fd22014-06-19 10:42:50 +08003423}
3424
3425static inline void
David Sterba32da53862019-10-29 19:20:18 +01003426btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003427 int delalloc)
3428{
3429 if (delalloc)
3430 up_read(&cache->data_rwsem);
3431 btrfs_put_block_group(cache);
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 */
3440 u64 ram_bytes;
3441 u64 num_bytes;
3442 u64 empty_size;
3443 u64 flags;
3444 int delalloc;
3445
3446 /* Where to start the search inside the bg */
3447 u64 search_start;
3448
3449 /* For clustered allocation */
3450 u64 empty_cluster;
3451
3452 bool have_caching_bg;
3453 bool orig_have_caching_bg;
3454
3455 /* RAID index, converted from flags */
3456 int index;
3457
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003458 /*
3459 * Current loop number, check find_free_extent_update_loop() for details
3460 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003461 int loop;
3462
3463 /*
3464 * Whether we're refilling a cluster, if true we need to re-search
3465 * current block group but don't try to refill the cluster again.
3466 */
3467 bool retry_clustered;
3468
3469 /*
3470 * Whether we're updating free space cache, if true we need to re-search
3471 * current block group but don't try updating free space cache again.
3472 */
3473 bool retry_unclustered;
3474
3475 /* If current block group is cached */
3476 int cached;
3477
3478 /* Max contiguous hole found */
3479 u64 max_extent_size;
3480
3481 /* Total free space from free space cache, not always contiguous */
3482 u64 total_free_space;
3483
3484 /* Found result */
3485 u64 found_offset;
3486};
3487
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003488
3489/*
3490 * Helper function for find_free_extent().
3491 *
3492 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3493 * Return -EAGAIN to inform caller that we need to re-search this block group
3494 * Return >0 to inform caller that we find nothing
3495 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3496 */
David Sterba32da53862019-10-29 19:20:18 +01003497static int find_free_extent_clustered(struct btrfs_block_group *bg,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003498 struct btrfs_free_cluster *last_ptr,
3499 struct find_free_extent_ctl *ffe_ctl,
David Sterba32da53862019-10-29 19:20:18 +01003500 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003501{
David Sterba32da53862019-10-29 19:20:18 +01003502 struct btrfs_block_group *cluster_bg;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003503 u64 aligned_cluster;
3504 u64 offset;
3505 int ret;
3506
3507 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3508 if (!cluster_bg)
3509 goto refill_cluster;
3510 if (cluster_bg != bg && (cluster_bg->ro ||
3511 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3512 goto release_cluster;
3513
3514 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003515 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003516 &ffe_ctl->max_extent_size);
3517 if (offset) {
3518 /* We have a block, we're done */
3519 spin_unlock(&last_ptr->refill_lock);
3520 trace_btrfs_reserve_extent_cluster(cluster_bg,
3521 ffe_ctl->search_start, ffe_ctl->num_bytes);
3522 *cluster_bg_ret = cluster_bg;
3523 ffe_ctl->found_offset = offset;
3524 return 0;
3525 }
3526 WARN_ON(last_ptr->block_group != cluster_bg);
3527
3528release_cluster:
3529 /*
3530 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3531 * lets just skip it and let the allocator find whatever block it can
3532 * find. If we reach this point, we will have tried the cluster
3533 * allocator plenty of times and not have found anything, so we are
3534 * likely way too fragmented for the clustering stuff to find anything.
3535 *
3536 * However, if the cluster is taken from the current block group,
3537 * release the cluster first, so that we stand a better chance of
3538 * succeeding in the unclustered allocation.
3539 */
3540 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3541 spin_unlock(&last_ptr->refill_lock);
3542 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3543 return -ENOENT;
3544 }
3545
3546 /* This cluster didn't work out, free it and start over */
3547 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3548
3549 if (cluster_bg != bg)
3550 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3551
3552refill_cluster:
3553 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3554 spin_unlock(&last_ptr->refill_lock);
3555 return -ENOENT;
3556 }
3557
3558 aligned_cluster = max_t(u64,
3559 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3560 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003561 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3562 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003563 if (ret == 0) {
3564 /* Now pull our allocation out of this cluster */
3565 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3566 ffe_ctl->num_bytes, ffe_ctl->search_start,
3567 &ffe_ctl->max_extent_size);
3568 if (offset) {
3569 /* We found one, proceed */
3570 spin_unlock(&last_ptr->refill_lock);
3571 trace_btrfs_reserve_extent_cluster(bg,
3572 ffe_ctl->search_start,
3573 ffe_ctl->num_bytes);
3574 ffe_ctl->found_offset = offset;
3575 return 0;
3576 }
3577 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3578 !ffe_ctl->retry_clustered) {
3579 spin_unlock(&last_ptr->refill_lock);
3580
3581 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003582 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003583 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3584 return -EAGAIN;
3585 }
3586 /*
3587 * At this point we either didn't find a cluster or we weren't able to
3588 * allocate a block from our cluster. Free the cluster we've been
3589 * trying to use, and go to the next block group.
3590 */
3591 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3592 spin_unlock(&last_ptr->refill_lock);
3593 return 1;
3594}
3595
Qu Wenruob4bd7452018-11-02 09:39:47 +08003596/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003597 * Return >0 to inform caller that we find nothing
3598 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3599 * Return -EAGAIN to inform caller that we need to re-search this block group
3600 */
David Sterba32da53862019-10-29 19:20:18 +01003601static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Qu Wenruoe1a41842018-11-02 09:39:49 +08003602 struct btrfs_free_cluster *last_ptr,
3603 struct find_free_extent_ctl *ffe_ctl)
3604{
3605 u64 offset;
3606
3607 /*
3608 * We are doing an unclustered allocation, set the fragmented flag so
3609 * we don't bother trying to setup a cluster again until we get more
3610 * space.
3611 */
3612 if (unlikely(last_ptr)) {
3613 spin_lock(&last_ptr->lock);
3614 last_ptr->fragmented = 1;
3615 spin_unlock(&last_ptr->lock);
3616 }
3617 if (ffe_ctl->cached) {
3618 struct btrfs_free_space_ctl *free_space_ctl;
3619
3620 free_space_ctl = bg->free_space_ctl;
3621 spin_lock(&free_space_ctl->tree_lock);
3622 if (free_space_ctl->free_space <
3623 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3624 ffe_ctl->empty_size) {
3625 ffe_ctl->total_free_space = max_t(u64,
3626 ffe_ctl->total_free_space,
3627 free_space_ctl->free_space);
3628 spin_unlock(&free_space_ctl->tree_lock);
3629 return 1;
3630 }
3631 spin_unlock(&free_space_ctl->tree_lock);
3632 }
3633
3634 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3635 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3636 &ffe_ctl->max_extent_size);
3637
3638 /*
3639 * If we didn't find a chunk, and we haven't failed on this block group
3640 * before, and this block group is in the middle of caching and we are
3641 * ok with waiting, then go ahead and wait for progress to be made, and
3642 * set @retry_unclustered to true.
3643 *
3644 * If @retry_unclustered is true then we've already waited on this
3645 * block group once and should move on to the next block group.
3646 */
3647 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3648 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003649 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3650 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003651 ffe_ctl->retry_unclustered = true;
3652 return -EAGAIN;
3653 } else if (!offset) {
3654 return 1;
3655 }
3656 ffe_ctl->found_offset = offset;
3657 return 0;
3658}
3659
3660/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003661 * Return >0 means caller needs to re-search for free extent
3662 * Return 0 means we have the needed free extent.
3663 * Return <0 means we failed to locate any free extent.
3664 */
3665static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3666 struct btrfs_free_cluster *last_ptr,
3667 struct btrfs_key *ins,
3668 struct find_free_extent_ctl *ffe_ctl,
3669 int full_search, bool use_cluster)
3670{
3671 struct btrfs_root *root = fs_info->extent_root;
3672 int ret;
3673
3674 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3675 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3676 ffe_ctl->orig_have_caching_bg = true;
3677
3678 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3679 ffe_ctl->have_caching_bg)
3680 return 1;
3681
3682 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3683 return 1;
3684
3685 if (ins->objectid) {
3686 if (!use_cluster && last_ptr) {
3687 spin_lock(&last_ptr->lock);
3688 last_ptr->window_start = ins->objectid;
3689 spin_unlock(&last_ptr->lock);
3690 }
3691 return 0;
3692 }
3693
3694 /*
3695 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3696 * caching kthreads as we move along
3697 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3698 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3699 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3700 * again
3701 */
3702 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3703 ffe_ctl->index = 0;
3704 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3705 /*
3706 * We want to skip the LOOP_CACHING_WAIT step if we
3707 * don't have any uncached bgs and we've already done a
3708 * full search through.
3709 */
3710 if (ffe_ctl->orig_have_caching_bg || !full_search)
3711 ffe_ctl->loop = LOOP_CACHING_WAIT;
3712 else
3713 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3714 } else {
3715 ffe_ctl->loop++;
3716 }
3717
3718 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3719 struct btrfs_trans_handle *trans;
3720 int exist = 0;
3721
3722 trans = current->journal_info;
3723 if (trans)
3724 exist = 1;
3725 else
3726 trans = btrfs_join_transaction(root);
3727
3728 if (IS_ERR(trans)) {
3729 ret = PTR_ERR(trans);
3730 return ret;
3731 }
3732
Josef Bacikfc471cb2019-06-18 16:09:17 -04003733 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3734 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003735
3736 /*
3737 * If we can't allocate a new chunk we've already looped
3738 * through at least once, move on to the NO_EMPTY_SIZE
3739 * case.
3740 */
3741 if (ret == -ENOSPC)
3742 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3743
3744 /* Do not bail out on ENOSPC since we can do more. */
3745 if (ret < 0 && ret != -ENOSPC)
3746 btrfs_abort_transaction(trans, ret);
3747 else
3748 ret = 0;
3749 if (!exist)
3750 btrfs_end_transaction(trans);
3751 if (ret)
3752 return ret;
3753 }
3754
3755 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3756 /*
3757 * Don't loop again if we already have no empty_size and
3758 * no empty_cluster.
3759 */
3760 if (ffe_ctl->empty_size == 0 &&
3761 ffe_ctl->empty_cluster == 0)
3762 return -ENOSPC;
3763 ffe_ctl->empty_size = 0;
3764 ffe_ctl->empty_cluster = 0;
3765 }
3766 return 1;
3767 }
3768 return -ENOSPC;
3769}
3770
3771/*
Chris Masonfec577f2007-02-26 10:40:21 -05003772 * walks the btree of allocated extents and find a hole of a given size.
3773 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003774 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003775 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003776 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003777 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003778 *
3779 * If there is no suitable free space, we will record the max size of
3780 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003781 *
3782 * The overall logic and call chain:
3783 *
3784 * find_free_extent()
3785 * |- Iterate through all block groups
3786 * | |- Get a valid block group
3787 * | |- Try to do clustered allocation in that block group
3788 * | |- Try to do unclustered allocation in that block group
3789 * | |- Check if the result is valid
3790 * | | |- If valid, then exit
3791 * | |- Jump to next block group
3792 * |
3793 * |- Push harder to find free extents
3794 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003795 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003796static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003797 u64 ram_bytes, u64 num_bytes, u64 empty_size,
3798 u64 hint_byte, struct btrfs_key *ins,
3799 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003800{
Josef Bacik80eb2342008-10-29 14:49:05 -04003801 int ret = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003802 struct btrfs_free_cluster *last_ptr = NULL;
David Sterba32da53862019-10-29 19:20:18 +01003803 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003804 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003805 struct btrfs_space_info *space_info;
Josef Bacik67377732010-09-16 16:19:09 -04003806 bool use_cluster = true;
Josef Bacika5e681d2015-10-01 14:54:10 -04003807 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003808
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003809 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003810
3811 ffe_ctl.ram_bytes = ram_bytes;
3812 ffe_ctl.num_bytes = num_bytes;
3813 ffe_ctl.empty_size = empty_size;
3814 ffe_ctl.flags = flags;
3815 ffe_ctl.search_start = 0;
3816 ffe_ctl.retry_clustered = false;
3817 ffe_ctl.retry_unclustered = false;
3818 ffe_ctl.delalloc = delalloc;
3819 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3820 ffe_ctl.have_caching_bg = false;
3821 ffe_ctl.orig_have_caching_bg = false;
3822 ffe_ctl.found_offset = 0;
3823
David Sterba962a2982014-06-04 18:41:45 +02003824 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003825 ins->objectid = 0;
3826 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003827
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003828 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003829
Josef Bacik280c29082019-06-18 16:09:19 -04003830 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003831 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003832 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003833 return -ENOSPC;
3834 }
Josef Bacik2552d172009-04-03 10:14:19 -04003835
Josef Bacik67377732010-09-16 16:19:09 -04003836 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04003837 * If our free space is heavily fragmented we may not be able to make
3838 * big contiguous allocations, so instead of doing the expensive search
3839 * for free space, simply return ENOSPC with our max_extent_size so we
3840 * can go ahead and search for a more manageable chunk.
3841 *
3842 * If our max_extent_size is large enough for our allocation simply
3843 * disable clustering since we will likely not be able to find enough
3844 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04003845 */
Josef Bacik4f4db212015-09-29 11:40:47 -04003846 if (unlikely(space_info->max_extent_size)) {
3847 spin_lock(&space_info->lock);
3848 if (space_info->max_extent_size &&
3849 num_bytes > space_info->max_extent_size) {
3850 ins->offset = space_info->max_extent_size;
3851 spin_unlock(&space_info->lock);
3852 return -ENOSPC;
3853 } else if (space_info->max_extent_size) {
3854 use_cluster = false;
3855 }
3856 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003857 }
3858
Qu Wenruob4bd7452018-11-02 09:39:47 +08003859 last_ptr = fetch_cluster_info(fs_info, space_info,
3860 &ffe_ctl.empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04003861 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003862 spin_lock(&last_ptr->lock);
3863 if (last_ptr->block_group)
3864 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003865 if (last_ptr->fragmented) {
3866 /*
3867 * We still set window_start so we can keep track of the
3868 * last place we found an allocation to try and save
3869 * some time.
3870 */
3871 hint_byte = last_ptr->window_start;
3872 use_cluster = false;
3873 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003874 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003875 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003876
Qu Wenruob4bd7452018-11-02 09:39:47 +08003877 ffe_ctl.search_start = max(ffe_ctl.search_start,
3878 first_logical_byte(fs_info, 0));
3879 ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte);
3880 if (ffe_ctl.search_start == hint_byte) {
3881 block_group = btrfs_lookup_block_group(fs_info,
3882 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003883 /*
3884 * we don't want to use the block group if it doesn't match our
3885 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003886 *
3887 * However if we are re-searching with an ideal block group
3888 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003889 */
David Sterbab6919a52013-04-29 13:39:40 +00003890 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003891 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003892 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003893 if (list_empty(&block_group->list) ||
3894 block_group->ro) {
3895 /*
3896 * someone is removing this block group,
3897 * we can't jump into the have_block_group
3898 * target because our list pointers are not
3899 * valid
3900 */
3901 btrfs_put_block_group(block_group);
3902 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05003903 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003904 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08003905 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08003906 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04003907 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05003908 }
Josef Bacik2552d172009-04-03 10:14:19 -04003909 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003910 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003911 }
Chris Mason42e70e72008-11-07 18:17:11 -05003912 }
Josef Bacik2552d172009-04-03 10:14:19 -04003913search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003914 ffe_ctl.have_caching_bg = false;
3915 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
3916 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04003917 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04003918 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003919 list_for_each_entry(block_group,
3920 &space_info->block_groups[ffe_ctl.index], list) {
Jeff Mahoney14443932017-07-19 23:25:51 -04003921 /* If the block group is read-only, we can skip it entirely. */
3922 if (unlikely(block_group->ro))
3923 continue;
3924
Miao Xiee570fd22014-06-19 10:42:50 +08003925 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02003926 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05003927
Chris Mason83a50de2010-12-13 15:06:46 -05003928 /*
3929 * this can happen if we end up cycling through all the
3930 * raid types, but we want to make sure we only allocate
3931 * for the proper type.
3932 */
David Sterbab6919a52013-04-29 13:39:40 +00003933 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07003934 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02003935 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02003936 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05003937 BTRFS_BLOCK_GROUP_RAID10;
3938
3939 /*
3940 * if they asked for extra copies and this block group
3941 * doesn't provide them, bail. This does allow us to
3942 * fill raid0 from raid1.
3943 */
David Sterbab6919a52013-04-29 13:39:40 +00003944 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05003945 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08003946
3947 /*
3948 * This block group has different flags than we want.
3949 * It's possible that we have MIXED_GROUP flag but no
3950 * block group is mixed. Just skip such block group.
3951 */
3952 btrfs_release_block_group(block_group, delalloc);
3953 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05003954 }
3955
Josef Bacik2552d172009-04-03 10:14:19 -04003956have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01003957 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003958 if (unlikely(!ffe_ctl.cached)) {
3959 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003960 ret = btrfs_cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04003961 BUG_ON(ret < 0);
3962 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05003963 }
3964
Josef Bacik36cce922013-08-05 11:15:21 -04003965 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
3966 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003967
Josef Bacik0a243252009-09-11 16:11:20 -04003968 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05003969 * Ok we want to try and use the cluster allocator, so
3970 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04003971 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04003972 if (last_ptr && use_cluster) {
David Sterba32da53862019-10-29 19:20:18 +01003973 struct btrfs_block_group *cluster_bg = NULL;
Chris Mason44fb5512009-06-04 15:34:51 -04003974
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003975 ret = find_free_extent_clustered(block_group, last_ptr,
3976 &ffe_ctl, &cluster_bg);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05003977
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003978 if (ret == 0) {
3979 if (cluster_bg && cluster_bg != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08003980 btrfs_release_block_group(block_group,
3981 delalloc);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003982 block_group = cluster_bg;
Miao Xie215a63d2014-01-15 20:00:56 +08003983 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003984 goto checks;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003985 } else if (ret == -EAGAIN) {
Josef Bacik817d52f2009-07-13 21:29:25 -04003986 goto have_block_group;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003987 } else if (ret > 0) {
3988 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003989 }
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003990 /* ret == -ENOENT case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003991 }
3992
Qu Wenruoe1a41842018-11-02 09:39:49 +08003993 ret = find_free_extent_unclustered(block_group, last_ptr,
3994 &ffe_ctl);
3995 if (ret == -EAGAIN)
Josef Bacik817d52f2009-07-13 21:29:25 -04003996 goto have_block_group;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003997 else if (ret > 0)
Josef Bacik1cdda9b2009-10-06 10:04:28 -04003998 goto loop;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003999 /* ret == 0 case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004000checks:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004001 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4002 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004003
Josef Bacik2552d172009-04-03 10:14:19 -04004004 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004005 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004006 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004007 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4008 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004009 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004010 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004011
Qu Wenruob4bd7452018-11-02 09:39:47 +08004012 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4013 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4014 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004015
Wang Xiaoguang18513092016-07-25 15:51:40 +08004016 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4017 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004018 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004019 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4020 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004021 goto loop;
4022 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004023 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004024
Josef Bacik2552d172009-04-03 10:14:19 -04004025 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004026 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004027 ins->offset = num_bytes;
4028
Qu Wenruob4bd7452018-11-02 09:39:47 +08004029 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4030 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004031 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004032 break;
4033loop:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004034 ffe_ctl.retry_clustered = false;
4035 ffe_ctl.retry_unclustered = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004036 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
Qu Wenruob4bd7452018-11-02 09:39:47 +08004037 ffe_ctl.index);
Miao Xiee570fd22014-06-19 10:42:50 +08004038 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004039 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004040 }
4041 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004042
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004043 ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl,
4044 full_search, use_cluster);
4045 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004046 goto search;
4047
Josef Bacik4f4db212015-09-29 11:40:47 -04004048 if (ret == -ENOSPC) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004049 /*
4050 * Use ffe_ctl->total_free_space as fallback if we can't find
4051 * any contiguous hole.
4052 */
4053 if (!ffe_ctl.max_extent_size)
4054 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004055 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004056 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004057 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004058 ins->offset = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004059 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004060 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004061}
Chris Masonec44a352008-04-28 15:29:52 -04004062
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004063/*
4064 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4065 * hole that is at least as big as @num_bytes.
4066 *
4067 * @root - The root that will contain this extent
4068 *
4069 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4070 * is used for accounting purposes. This value differs
4071 * from @num_bytes only in the case of compressed extents.
4072 *
4073 * @num_bytes - Number of bytes to allocate on-disk.
4074 *
4075 * @min_alloc_size - Indicates the minimum amount of space that the
4076 * allocator should try to satisfy. In some cases
4077 * @num_bytes may be larger than what is required and if
4078 * the filesystem is fragmented then allocation fails.
4079 * However, the presence of @min_alloc_size gives a
4080 * chance to try and satisfy the smaller allocation.
4081 *
4082 * @empty_size - A hint that you plan on doing more COW. This is the
4083 * size in bytes the allocator should try to find free
4084 * next to the block it returns. This is just a hint and
4085 * may be ignored by the allocator.
4086 *
4087 * @hint_byte - Hint to the allocator to start searching above the byte
4088 * address passed. It might be ignored.
4089 *
4090 * @ins - This key is modified to record the found hole. It will
4091 * have the following values:
4092 * ins->objectid == start position
4093 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4094 * ins->offset == the size of the hole.
4095 *
4096 * @is_data - Boolean flag indicating whether an extent is
4097 * allocated for data (true) or metadata (false)
4098 *
4099 * @delalloc - Boolean flag indicating whether this allocation is for
4100 * delalloc or not. If 'true' data_rwsem of block groups
4101 * is going to be acquired.
4102 *
4103 *
4104 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4105 * case -ENOSPC is returned then @ins->offset will contain the size of the
4106 * largest available hole the allocator managed to find.
4107 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004108int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004109 u64 num_bytes, u64 min_alloc_size,
4110 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004111 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004112{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004113 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004114 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004115 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004116 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004117
Jeff Mahoney1b868262017-05-17 11:38:35 -04004118 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004119again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004120 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004121 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004122 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004123 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004124 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004125 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004126 if (!final_tried && ins->offset) {
4127 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004128 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004129 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004130 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004131 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004132 if (num_bytes == min_alloc_size)
4133 final_tried = true;
4134 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004135 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004136 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004137
Josef Bacik280c29082019-06-18 16:09:19 -04004138 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004139 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004140 "allocation failed flags %llu, wanted %llu",
4141 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004142 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004143 btrfs_dump_space_info(fs_info, sinfo,
4144 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004145 }
Chris Mason925baed2008-06-25 16:01:30 -04004146 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004147
4148 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004149}
4150
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004151static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004152 u64 start, u64 len,
4153 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04004154{
David Sterba32da53862019-10-29 19:20:18 +01004155 struct btrfs_block_group *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004156 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004157
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004158 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004159 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 btrfs_err(fs_info, "Unable to find block group for %llu",
4161 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004162 return -ENOSPC;
4163 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004164
Chris Masone688b7252011-10-31 20:52:39 -04004165 if (pin)
David Sterbafdf08602019-03-20 12:12:32 +01004166 pin_down_extent(cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04004167 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004168 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004169 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04004170 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004171 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004172 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04004173 }
Dongsheng Yang31193212014-12-12 16:44:35 +08004174
Chris Masonfa9c0d792009-04-03 09:47:43 -04004175 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004176 return ret;
4177}
4178
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004179int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004180 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004181{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004182 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04004183}
4184
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004185int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04004186 u64 start, u64 len)
4187{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004188 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04004189}
4190
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004191static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004192 u64 parent, u64 root_objectid,
4193 u64 flags, u64 owner, u64 offset,
4194 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004195{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004196 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004197 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004198 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004200 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 struct extent_buffer *leaf;
4202 int type;
4203 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004204
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004205 if (parent > 0)
4206 type = BTRFS_SHARED_DATA_REF_KEY;
4207 else
4208 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004211
4212 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004213 if (!path)
4214 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004215
Chris Masonb9473432009-03-13 11:00:37 -04004216 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4218 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (ret) {
4220 btrfs_free_path(path);
4221 return ret;
4222 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004224 leaf = path->nodes[0];
4225 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004226 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4228 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4229 btrfs_set_extent_flags(leaf, extent_item,
4230 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004232 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4233 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4234 if (parent > 0) {
4235 struct btrfs_shared_data_ref *ref;
4236 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4237 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4238 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4239 } else {
4240 struct btrfs_extent_data_ref *ref;
4241 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4242 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4243 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4244 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4245 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4246 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004247
4248 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004249 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004250
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004251 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004252 if (ret)
4253 return ret;
4254
Josef Bacikade4b512019-06-20 15:38:01 -04004255 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004257 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004258 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004259 BUG();
4260 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004261 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004262 return ret;
4263}
4264
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004265static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004266 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004267 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004268{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004269 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004270 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004271 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004272 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004273 struct btrfs_tree_block_info *block_info;
4274 struct btrfs_extent_inline_ref *iref;
4275 struct btrfs_path *path;
4276 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004277 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004278 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004279 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004280 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004281 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004282
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004283 ref = btrfs_delayed_node_to_tree_ref(node);
4284
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004285 extent_key.objectid = node->bytenr;
4286 if (skinny_metadata) {
4287 extent_key.offset = ref->level;
4288 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4289 num_bytes = fs_info->nodesize;
4290 } else {
4291 extent_key.offset = node->num_bytes;
4292 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004293 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004294 num_bytes = node->num_bytes;
4295 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296
4297 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004298 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004299 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300
4301 path->leave_spinning = 1;
4302 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004303 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004305 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004306 return ret;
4307 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004308
4309 leaf = path->nodes[0];
4310 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4311 struct btrfs_extent_item);
4312 btrfs_set_extent_refs(leaf, extent_item, 1);
4313 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4314 btrfs_set_extent_flags(leaf, extent_item,
4315 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004316
Josef Bacik3173a182013-03-07 14:22:04 -05004317 if (skinny_metadata) {
4318 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4319 } else {
4320 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004321 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004322 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004323 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4324 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004326 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004327 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4328 btrfs_set_extent_inline_ref_type(leaf, iref,
4329 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004330 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004331 } else {
4332 btrfs_set_extent_inline_ref_type(leaf, iref,
4333 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004334 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004335 }
4336
4337 btrfs_mark_buffer_dirty(leaf);
4338 btrfs_free_path(path);
4339
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004340 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4341 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004342 if (ret)
4343 return ret;
4344
Josef Bacikade4b512019-06-20 15:38:01 -04004345 ret = btrfs_update_block_group(trans, extent_key.objectid,
4346 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004347 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004348 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004349 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004350 BUG();
4351 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004352
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004353 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004355 return ret;
4356}
4357
4358int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004359 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004360 u64 offset, u64 ram_bytes,
4361 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004362{
Qu Wenruo76675592019-04-04 14:45:32 +08004363 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004365
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004366 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004367
Qu Wenruo76675592019-04-04 14:45:32 +08004368 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4369 ins->objectid, ins->offset, 0);
4370 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004371 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004372 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4373 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004374 return ret;
4375}
Chris Masone02119d2008-09-05 16:13:11 -04004376
4377/*
4378 * this is used by the tree logging recovery code. It records that
4379 * an extent has been allocated and makes sure to clear the free
4380 * space cache bits as well
4381 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004382int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383 u64 root_objectid, u64 owner, u64 offset,
4384 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004385{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004386 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004387 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004388 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004389 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004390
4391 /*
4392 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004393 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004394 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004395 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004396 ret = __exclude_logged_extent(fs_info, ins->objectid,
4397 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004398 if (ret)
4399 return ret;
4400 }
Chris Masone02119d2008-09-05 16:13:11 -04004401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004402 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004403 if (!block_group)
4404 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004405
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004406 space_info = block_group->space_info;
4407 spin_lock(&space_info->lock);
4408 spin_lock(&block_group->lock);
4409 space_info->bytes_reserved += ins->offset;
4410 block_group->reserved += ins->offset;
4411 spin_unlock(&block_group->lock);
4412 spin_unlock(&space_info->lock);
4413
Nikolay Borisovef89b822018-06-20 15:48:58 +03004414 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4415 offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004416 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004417 return ret;
4418}
4419
Eric Sandeen48a3b632013-04-25 20:41:01 +00004420static struct extent_buffer *
4421btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004422 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004425 struct extent_buffer *buf;
4426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004427 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004428 if (IS_ERR(buf))
4429 return buf;
4430
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004431 /*
4432 * Extra safety check in case the extent tree is corrupted and extent
4433 * allocator chooses to use a tree block which is already used and
4434 * locked.
4435 */
4436 if (buf->lock_owner == current->pid) {
4437 btrfs_err_rl(fs_info,
4438"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4439 buf->start, btrfs_header_owner(buf), current->pid);
4440 free_extent_buffer(buf);
4441 return ERR_PTR(-EUCLEAN);
4442 }
4443
Chris Mason85d4e462011-07-26 16:11:19 -04004444 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004445 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004446 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004447 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004448
David Sterba8bead252018-04-04 02:03:48 +02004449 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004450 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004451
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004452 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4453 btrfs_set_header_level(buf, level);
4454 btrfs_set_header_bytenr(buf, buf->start);
4455 btrfs_set_header_generation(buf, trans->transid);
4456 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4457 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004458 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004459 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004460 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004461 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004462 /*
4463 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004464 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004465 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004466 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004467 set_extent_dirty(&root->dirty_log_pages, buf->start,
4468 buf->start + buf->len - 1, GFP_NOFS);
4469 else
4470 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004471 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004472 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004473 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004474 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4475 buf->start + buf->len - 1, GFP_NOFS);
4476 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004477 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004478 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004479 return buf;
4480}
4481
Chris Masonfec577f2007-02-26 10:40:21 -05004482/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004483 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004484 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004485 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004486struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004487 struct btrfs_root *root,
4488 u64 parent, u64 root_objectid,
4489 const struct btrfs_disk_key *key,
4490 int level, u64 hint,
4491 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004492{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004493 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004494 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004495 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004496 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004497 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004498 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004499 u64 flags = 0;
4500 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004501 u32 blocksize = fs_info->nodesize;
4502 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004503
David Sterba05653ef2016-07-15 15:23:37 +02004504#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004505 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004506 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004507 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004508 if (!IS_ERR(buf))
4509 root->alloc_bytenr += blocksize;
4510 return buf;
4511 }
David Sterba05653ef2016-07-15 15:23:37 +02004512#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004513
Josef Bacik67f9c222019-06-19 13:47:23 -04004514 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004515 if (IS_ERR(block_rsv))
4516 return ERR_CAST(block_rsv);
4517
Wang Xiaoguang18513092016-07-25 15:51:40 +08004518 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004519 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004520 if (ret)
4521 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004522
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004523 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4524 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004525 if (IS_ERR(buf)) {
4526 ret = PTR_ERR(buf);
4527 goto out_free_reserved;
4528 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004529
4530 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4531 if (parent == 0)
4532 parent = ins.objectid;
4533 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4534 } else
4535 BUG_ON(parent > 0);
4536
4537 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004538 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004539 if (!extent_op) {
4540 ret = -ENOMEM;
4541 goto out_free_buf;
4542 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004543 if (key)
4544 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4545 else
4546 memset(&extent_op->key, 0, sizeof(extent_op->key));
4547 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004548 extent_op->update_key = skinny_metadata ? false : true;
4549 extent_op->update_flags = true;
4550 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004551 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004552
Qu Wenruoed4f2552019-04-04 14:45:31 +08004553 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4554 ins.objectid, ins.offset, parent);
4555 generic_ref.real_root = root->root_key.objectid;
4556 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004557 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004558 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004559 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004560 if (ret)
4561 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004562 }
Chris Masonfec577f2007-02-26 10:40:21 -05004563 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004564
4565out_free_delayed:
4566 btrfs_free_delayed_extent_op(extent_op);
4567out_free_buf:
4568 free_extent_buffer(buf);
4569out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004570 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004571out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004572 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004573 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004574}
Chris Masona28ec192007-03-06 20:08:01 -05004575
Yan Zheng2c47e6052009-06-27 21:07:35 -04004576struct walk_control {
4577 u64 refs[BTRFS_MAX_LEVEL];
4578 u64 flags[BTRFS_MAX_LEVEL];
4579 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004580 struct btrfs_key drop_progress;
4581 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004582 int stage;
4583 int level;
4584 int shared_level;
4585 int update_ref;
4586 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004587 int reada_slot;
4588 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004589 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004590};
4591
4592#define DROP_REFERENCE 1
4593#define UPDATE_BACKREF 2
4594
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004595static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4596 struct btrfs_root *root,
4597 struct walk_control *wc,
4598 struct btrfs_path *path)
4599{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004600 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004601 u64 bytenr;
4602 u64 generation;
4603 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004604 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004605 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004606 struct btrfs_key key;
4607 struct extent_buffer *eb;
4608 int ret;
4609 int slot;
4610 int nread = 0;
4611
4612 if (path->slots[wc->level] < wc->reada_slot) {
4613 wc->reada_count = wc->reada_count * 2 / 3;
4614 wc->reada_count = max(wc->reada_count, 2);
4615 } else {
4616 wc->reada_count = wc->reada_count * 3 / 2;
4617 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004618 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004619 }
4620
4621 eb = path->nodes[wc->level];
4622 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004623
4624 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4625 if (nread >= wc->reada_count)
4626 break;
4627
4628 cond_resched();
4629 bytenr = btrfs_node_blockptr(eb, slot);
4630 generation = btrfs_node_ptr_generation(eb, slot);
4631
4632 if (slot == path->slots[wc->level])
4633 goto reada;
4634
4635 if (wc->stage == UPDATE_BACKREF &&
4636 generation <= root->root_key.offset)
4637 continue;
4638
Yan, Zheng94fcca92009-10-09 09:25:16 -04004639 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004640 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004641 wc->level - 1, 1, &refs,
4642 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004643 /* We don't care about errors in readahead. */
4644 if (ret < 0)
4645 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004646 BUG_ON(refs == 0);
4647
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004648 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004649 if (refs == 1)
4650 goto reada;
4651
Yan, Zheng94fcca92009-10-09 09:25:16 -04004652 if (wc->level == 1 &&
4653 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4654 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004655 if (!wc->update_ref ||
4656 generation <= root->root_key.offset)
4657 continue;
4658 btrfs_node_key_to_cpu(eb, &key, slot);
4659 ret = btrfs_comp_cpu_keys(&key,
4660 &wc->update_progress);
4661 if (ret < 0)
4662 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004663 } else {
4664 if (wc->level == 1 &&
4665 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4666 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004667 }
4668reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004670 nread++;
4671 }
4672 wc->reada_slot = slot;
4673}
4674
Chris Mason9aca1d52007-03-13 11:09:37 -04004675/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004676 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004677 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004678 * when wc->stage == UPDATE_BACKREF, this function updates
4679 * back refs for pointers in the block.
4680 *
4681 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004682 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004683static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4684 struct btrfs_root *root,
4685 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004686 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004687{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004688 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004689 int level = wc->level;
4690 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004691 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4692 int ret;
4693
4694 if (wc->stage == UPDATE_BACKREF &&
4695 btrfs_header_owner(eb) != root->root_key.objectid)
4696 return 1;
4697
4698 /*
4699 * when reference count of tree block is 1, it won't increase
4700 * again. once full backref flag is set, we never clear it.
4701 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004702 if (lookup_info &&
4703 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4704 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004705 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004706 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004707 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004708 &wc->refs[level],
4709 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004710 BUG_ON(ret == -ENOMEM);
4711 if (ret)
4712 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004713 BUG_ON(wc->refs[level] == 0);
4714 }
4715
Yan Zheng2c47e6052009-06-27 21:07:35 -04004716 if (wc->stage == DROP_REFERENCE) {
4717 if (wc->refs[level] > 1)
4718 return 1;
4719
4720 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004721 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004722 path->locks[level] = 0;
4723 }
4724 return 0;
4725 }
4726
4727 /* wc->stage == UPDATE_BACKREF */
4728 if (!(wc->flags[level] & flag)) {
4729 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004730 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004731 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004732 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004733 BUG_ON(ret); /* -ENOMEM */
David Sterbaf5c8daa2019-03-20 11:43:36 +01004734 ret = btrfs_set_disk_extent_flags(trans, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004735 eb->len, flag,
4736 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004737 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004738 wc->flags[level] |= flag;
4739 }
4740
4741 /*
4742 * the block is shared by multiple trees, so it's not good to
4743 * keep the tree lock
4744 */
4745 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004746 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004747 path->locks[level] = 0;
4748 }
4749 return 0;
4750}
4751
4752/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004753 * This is used to verify a ref exists for this root to deal with a bug where we
4754 * would have a drop_progress key that hadn't been updated properly.
4755 */
4756static int check_ref_exists(struct btrfs_trans_handle *trans,
4757 struct btrfs_root *root, u64 bytenr, u64 parent,
4758 int level)
4759{
4760 struct btrfs_path *path;
4761 struct btrfs_extent_inline_ref *iref;
4762 int ret;
4763
4764 path = btrfs_alloc_path();
4765 if (!path)
4766 return -ENOMEM;
4767
4768 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4769 root->fs_info->nodesize, parent,
4770 root->root_key.objectid, level, 0);
4771 btrfs_free_path(path);
4772 if (ret == -ENOENT)
4773 return 0;
4774 if (ret < 0)
4775 return ret;
4776 return 1;
4777}
4778
4779/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004780 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004781 *
4782 * when wc->stage == DROP_REFERENCE, this function checks
4783 * reference count of the block pointed to. if the block
4784 * is shared and we need update back refs for the subtree
4785 * rooted at the block, this function changes wc->stage to
4786 * UPDATE_BACKREF. if the block is shared and there is no
4787 * need to update back, this function drops the reference
4788 * to the block.
4789 *
4790 * NOTE: return value 1 means we should stop walking down.
4791 */
4792static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4793 struct btrfs_root *root,
4794 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004795 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004796{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004797 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004798 u64 bytenr;
4799 u64 generation;
4800 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004801 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004802 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004803 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004804 struct extent_buffer *next;
4805 int level = wc->level;
4806 int reada = 0;
4807 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004808 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004809
4810 generation = btrfs_node_ptr_generation(path->nodes[level],
4811 path->slots[level]);
4812 /*
4813 * if the lower level block was created before the snapshot
4814 * was created, we know there is no need to update back refs
4815 * for the subtree
4816 */
4817 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004818 generation <= root->root_key.offset) {
4819 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004820 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004821 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004822
4823 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004824 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4825 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004826
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004827 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004828 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004829 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004830 if (IS_ERR(next))
4831 return PTR_ERR(next);
4832
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004833 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4834 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004835 reada = 1;
4836 }
4837 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004838 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004839
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004840 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004841 &wc->refs[level - 1],
4842 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004843 if (ret < 0)
4844 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004845
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004846 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004848 ret = -EIO;
4849 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004850 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004851 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004852
Yan, Zheng94fcca92009-10-09 09:25:16 -04004853 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004854 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004855 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004856 if (level == 1 &&
4857 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4858 goto skip;
4859
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004860 if (!wc->update_ref ||
4861 generation <= root->root_key.offset)
4862 goto skip;
4863
4864 btrfs_node_key_to_cpu(path->nodes[level], &key,
4865 path->slots[level]);
4866 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4867 if (ret < 0)
4868 goto skip;
4869
4870 wc->stage = UPDATE_BACKREF;
4871 wc->shared_level = level - 1;
4872 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004873 } else {
4874 if (level == 1 &&
4875 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4876 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004877 }
4878
Chris Masonb9fab912012-05-06 07:23:47 -04004879 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004880 btrfs_tree_unlock(next);
4881 free_extent_buffer(next);
4882 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004883 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004884 }
4885
4886 if (!next) {
4887 if (reada && level == 1)
4888 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004889 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4890 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004891 if (IS_ERR(next)) {
4892 return PTR_ERR(next);
4893 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004894 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004895 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004896 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004897 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004898 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004899 }
4900
4901 level--;
Josef Bacik48672682016-09-23 13:23:28 +02004902 ASSERT(level == btrfs_header_level(next));
4903 if (level != btrfs_header_level(next)) {
4904 btrfs_err(root->fs_info, "mismatched level");
4905 ret = -EIO;
4906 goto out_unlock;
4907 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004908 path->nodes[level] = next;
4909 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004910 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004911 wc->level = level;
4912 if (wc->level == 1)
4913 wc->reada_slot = 0;
4914 return 0;
4915skip:
4916 wc->refs[level - 1] = 0;
4917 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004918 if (wc->stage == DROP_REFERENCE) {
4919 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4920 parent = path->nodes[level]->start;
4921 } else {
Josef Bacik48672682016-09-23 13:23:28 +02004922 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04004923 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02004924 if (root->root_key.objectid !=
4925 btrfs_header_owner(path->nodes[level])) {
4926 btrfs_err(root->fs_info,
4927 "mismatched block owner");
4928 ret = -EIO;
4929 goto out_unlock;
4930 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004931 parent = 0;
4932 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004933
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004934 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05004935 * If we had a drop_progress we need to verify the refs are set
4936 * as expected. If we find our ref then we know that from here
4937 * on out everything should be correct, and we can clear the
4938 * ->restarted flag.
4939 */
4940 if (wc->restarted) {
4941 ret = check_ref_exists(trans, root, bytenr, parent,
4942 level - 1);
4943 if (ret < 0)
4944 goto out_unlock;
4945 if (ret == 0)
4946 goto no_delete;
4947 ret = 0;
4948 wc->restarted = 0;
4949 }
4950
4951 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004952 * Reloc tree doesn't contribute to qgroup numbers, and we have
4953 * already accounted them at merge time (replace_path),
4954 * thus we could skip expensive subtree trace here.
4955 */
4956 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
4957 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08004958 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08004959 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07004960 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004961 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004962 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
4963 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07004964 }
4965 }
Josef Bacikaea6f022019-02-06 15:46:15 -05004966
4967 /*
4968 * We need to update the next key in our walk control so we can
4969 * update the drop_progress key accordingly. We don't care if
4970 * find_next_key doesn't find a key because that means we're at
4971 * the end and are going to clean up now.
4972 */
4973 wc->drop_level = level;
4974 find_next_key(path, level, &wc->drop_progress);
4975
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004976 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
4977 fs_info->nodesize, parent);
4978 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
4979 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02004980 if (ret)
4981 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004982 }
Josef Bacik78c52d92019-02-06 15:46:14 -05004983no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02004984 *lookup_info = 1;
4985 ret = 1;
4986
4987out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004988 btrfs_tree_unlock(next);
4989 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02004990
4991 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004992}
4993
4994/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004995 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004996 *
4997 * when wc->stage == DROP_REFERENCE, this function drops
4998 * reference count on the block.
4999 *
5000 * when wc->stage == UPDATE_BACKREF, this function changes
5001 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5002 * to UPDATE_BACKREF previously while processing the block.
5003 *
5004 * NOTE: return value 1 means we should stop walking up.
5005 */
5006static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5007 struct btrfs_root *root,
5008 struct btrfs_path *path,
5009 struct walk_control *wc)
5010{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005012 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005013 int level = wc->level;
5014 struct extent_buffer *eb = path->nodes[level];
5015 u64 parent = 0;
5016
5017 if (wc->stage == UPDATE_BACKREF) {
5018 BUG_ON(wc->shared_level < level);
5019 if (level < wc->shared_level)
5020 goto out;
5021
Yan Zheng2c47e6052009-06-27 21:07:35 -04005022 ret = find_next_key(path, level + 1, &wc->update_progress);
5023 if (ret > 0)
5024 wc->update_ref = 0;
5025
5026 wc->stage = DROP_REFERENCE;
5027 wc->shared_level = -1;
5028 path->slots[level] = 0;
5029
5030 /*
5031 * check reference count again if the block isn't locked.
5032 * we should start walking down the tree again if reference
5033 * count is one.
5034 */
5035 if (!path->locks[level]) {
5036 BUG_ON(level == 0);
5037 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005038 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005039 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005040
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005041 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005042 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005043 &wc->refs[level],
5044 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005045 if (ret < 0) {
5046 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005047 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005048 return ret;
5049 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005050 BUG_ON(wc->refs[level] == 0);
5051 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005052 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005053 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005054 return 1;
5055 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005056 }
5057 }
5058
5059 /* wc->stage == DROP_REFERENCE */
5060 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5061
5062 if (wc->refs[level] == 1) {
5063 if (level == 0) {
5064 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005065 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005066 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005067 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005068 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005069 if (is_fstree(root->root_key.objectid)) {
5070 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5071 if (ret) {
5072 btrfs_err_rl(fs_info,
5073 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005074 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005075 }
Mark Fasheh11526512014-07-17 12:39:01 -07005076 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005077 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005078 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005079 if (!path->locks[level] &&
5080 btrfs_header_generation(eb) == trans->transid) {
5081 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005082 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005083 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005084 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005085 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005086 }
5087
5088 if (eb == root->node) {
5089 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5090 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005091 else if (root->root_key.objectid != btrfs_header_owner(eb))
5092 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005093 } else {
5094 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5095 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005096 else if (root->root_key.objectid !=
5097 btrfs_header_owner(path->nodes[level + 1]))
5098 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005099 }
5100
Jan Schmidt5581a512012-05-16 17:04:52 +02005101 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005102out:
5103 wc->refs[level] = 0;
5104 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005105 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005106
5107owner_mismatch:
5108 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5109 btrfs_header_owner(eb), root->root_key.objectid);
5110 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005111}
5112
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005113static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5114 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005115 struct btrfs_path *path,
5116 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005117{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005118 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005119 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005120 int ret;
5121
Yan Zheng2c47e6052009-06-27 21:07:35 -04005122 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005123 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005124 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005125 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005126
Yan Zheng2c47e6052009-06-27 21:07:35 -04005127 if (level == 0)
5128 break;
5129
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005130 if (path->slots[level] >=
5131 btrfs_header_nritems(path->nodes[level]))
5132 break;
5133
Yan, Zheng94fcca92009-10-09 09:25:16 -04005134 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005135 if (ret > 0) {
5136 path->slots[level]++;
5137 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005138 } else if (ret < 0)
5139 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005140 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005141 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005142 return 0;
5143}
5144
Chris Masond3977122009-01-05 21:25:51 -05005145static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005146 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005147 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005148 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005149{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005150 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005151 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005152
Yan Zheng2c47e6052009-06-27 21:07:35 -04005153 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5154 while (level < max_level && path->nodes[level]) {
5155 wc->level = level;
5156 if (path->slots[level] + 1 <
5157 btrfs_header_nritems(path->nodes[level])) {
5158 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005159 return 0;
5160 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005161 ret = walk_up_proc(trans, root, path, wc);
5162 if (ret > 0)
5163 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005164 if (ret < 0)
5165 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005166
Yan Zheng2c47e6052009-06-27 21:07:35 -04005167 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005168 btrfs_tree_unlock_rw(path->nodes[level],
5169 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005170 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005171 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005172 free_extent_buffer(path->nodes[level]);
5173 path->nodes[level] = NULL;
5174 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005175 }
5176 }
5177 return 1;
5178}
5179
Chris Mason9aca1d52007-03-13 11:09:37 -04005180/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005181 * drop a subvolume tree.
5182 *
5183 * this function traverses the tree freeing any blocks that only
5184 * referenced by the tree.
5185 *
5186 * when a shared tree block is found. this function decreases its
5187 * reference count by one. if update_ref is true, this function
5188 * also make sure backrefs for the shared block and all lower level
5189 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005190 *
5191 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005192 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005193int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005194 struct btrfs_block_rsv *block_rsv, int update_ref,
5195 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005196{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005197 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005198 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005199 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005200 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005201 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005202 struct walk_control *wc;
5203 struct btrfs_key key;
5204 int err = 0;
5205 int ret;
5206 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005207 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005208
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005209 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005210
Chris Mason5caf2a02007-04-02 11:20:42 -04005211 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005212 if (!path) {
5213 err = -ENOMEM;
5214 goto out;
5215 }
Chris Mason20524f02007-03-10 06:35:47 -05005216
Yan Zheng2c47e6052009-06-27 21:07:35 -04005217 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005218 if (!wc) {
5219 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005220 err = -ENOMEM;
5221 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005222 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005223
Yan, Zhenga22285a2010-05-16 10:48:46 -04005224 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005225 if (IS_ERR(trans)) {
5226 err = PTR_ERR(trans);
5227 goto out_free;
5228 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005229
Josef Bacik0568e822018-11-30 11:52:14 -05005230 err = btrfs_run_delayed_items(trans);
5231 if (err)
5232 goto out_end_trans;
5233
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005234 if (block_rsv)
5235 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005236
Josef Bacik83354f02018-11-30 11:52:13 -05005237 /*
5238 * This will help us catch people modifying the fs tree while we're
5239 * dropping it. It is unsafe to mess with the fs tree while it's being
5240 * dropped as we unlock the root node and parent nodes as we walk down
5241 * the tree, assuming nothing will change. If something does change
5242 * then we'll have stale information and drop references to blocks we've
5243 * already dropped.
5244 */
5245 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005246 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005247 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005248 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005249 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005250 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005251 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005252 memset(&wc->update_progress, 0,
5253 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005254 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005255 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005256 memcpy(&wc->update_progress, &key,
5257 sizeof(wc->update_progress));
5258
Chris Mason6702ed42007-08-07 16:15:09 -04005259 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005260 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005261 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5263 path->lowest_level = 0;
5264 if (ret < 0) {
5265 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005266 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005267 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005268 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005269
Chris Mason7d9eb122008-07-08 14:19:17 -04005270 /*
5271 * unlock our path, this is safe because only this
5272 * function is allowed to delete this snapshot
5273 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005274 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005275
Yan Zheng2c47e6052009-06-27 21:07:35 -04005276 level = btrfs_header_level(root->node);
5277 while (1) {
5278 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005279 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005280 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005281
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005282 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005283 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005284 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005285 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005286 if (ret < 0) {
5287 err = ret;
5288 goto out_end_trans;
5289 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005290 BUG_ON(wc->refs[level] == 0);
5291
5292 if (level == root_item->drop_level)
5293 break;
5294
5295 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005296 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005297 WARN_ON(wc->refs[level] != 1);
5298 level--;
5299 }
5300 }
5301
Josef Bacik78c52d92019-02-06 15:46:14 -05005302 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005303 wc->level = level;
5304 wc->shared_level = -1;
5305 wc->stage = DROP_REFERENCE;
5306 wc->update_ref = update_ref;
5307 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005308 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005309
5310 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005311
Yan Zheng2c47e6052009-06-27 21:07:35 -04005312 ret = walk_down_tree(trans, root, path, wc);
5313 if (ret < 0) {
5314 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005315 break;
5316 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005317
5318 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5319 if (ret < 0) {
5320 err = ret;
5321 break;
5322 }
5323
5324 if (ret > 0) {
5325 BUG_ON(wc->stage != DROP_REFERENCE);
5326 break;
5327 }
5328
5329 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005330 wc->drop_level = wc->level;
5331 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5332 &wc->drop_progress,
5333 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005334 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005335 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5336 &wc->drop_progress);
5337 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005338
5339 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005340 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005341 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005342 ret = btrfs_update_root(trans, tree_root,
5343 &root->root_key,
5344 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005345 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005346 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005347 err = ret;
5348 goto out_end_trans;
5349 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005350
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005351 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005352 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005353 btrfs_debug(fs_info,
5354 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005355 err = -EAGAIN;
5356 goto out_free;
5357 }
5358
Yan, Zhenga22285a2010-05-16 10:48:46 -04005359 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005360 if (IS_ERR(trans)) {
5361 err = PTR_ERR(trans);
5362 goto out_free;
5363 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005364 if (block_rsv)
5365 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005366 }
Chris Mason20524f02007-03-10 06:35:47 -05005367 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005368 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005369 if (err)
5370 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005371
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005372 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005373 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005374 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005375 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005376 goto out_end_trans;
5377 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005378
Yan, Zheng76dda932009-09-21 16:00:26 -04005379 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005380 ret = btrfs_find_root(tree_root, &root->root_key, path,
5381 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005382 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005383 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005384 err = ret;
5385 goto out_end_trans;
5386 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005387 /* if we fail to delete the orphan item this time
5388 * around, it'll get picked up the next time.
5389 *
5390 * The most common failure here is just -ENOENT.
5391 */
5392 btrfs_del_orphan_item(trans, tree_root,
5393 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005394 }
5395 }
5396
Miao Xie27cdeb72014-04-02 19:51:05 +08005397 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005398 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005399 } else {
5400 free_extent_buffer(root->node);
5401 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00005402 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005403 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005404 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005405out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005406 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005407out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005408 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005409 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005410out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005411 /*
5412 * So if we need to stop dropping the snapshot for whatever reason we
5413 * need to make sure to add it back to the dead root list so that we
5414 * keep trying to do the work later. This also cleans up roots if we
5415 * don't have it in the radix (like when we recover after a power fail
5416 * or unmount) so we don't leak memory.
5417 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005418 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005419 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005420 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005421 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005422 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005423}
Chris Mason9078a3e2007-04-26 16:46:15 -04005424
Yan Zheng2c47e6052009-06-27 21:07:35 -04005425/*
5426 * drop subtree rooted at tree block 'node'.
5427 *
5428 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005429 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005430 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005431int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5432 struct btrfs_root *root,
5433 struct extent_buffer *node,
5434 struct extent_buffer *parent)
5435{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005436 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005437 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005438 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005439 int level;
5440 int parent_level;
5441 int ret = 0;
5442 int wret;
5443
Yan Zheng2c47e6052009-06-27 21:07:35 -04005444 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5445
Yan Zhengf82d02d2008-10-29 14:49:05 -04005446 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005447 if (!path)
5448 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005449
Yan Zheng2c47e6052009-06-27 21:07:35 -04005450 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005451 if (!wc) {
5452 btrfs_free_path(path);
5453 return -ENOMEM;
5454 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005455
Chris Masonb9447ef82009-03-09 11:45:38 -04005456 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005457 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005458 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005459 path->nodes[parent_level] = parent;
5460 path->slots[parent_level] = btrfs_header_nritems(parent);
5461
Chris Masonb9447ef82009-03-09 11:45:38 -04005462 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005463 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005464 path->nodes[level] = node;
5465 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005466 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005467
5468 wc->refs[parent_level] = 1;
5469 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5470 wc->level = level;
5471 wc->shared_level = -1;
5472 wc->stage = DROP_REFERENCE;
5473 wc->update_ref = 0;
5474 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005475 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005476
5477 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005478 wret = walk_down_tree(trans, root, path, wc);
5479 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005480 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005481 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005482 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005483
Yan Zheng2c47e6052009-06-27 21:07:35 -04005484 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005485 if (wret < 0)
5486 ret = wret;
5487 if (wret != 0)
5488 break;
5489 }
5490
Yan Zheng2c47e6052009-06-27 21:07:35 -04005491 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005492 btrfs_free_path(path);
5493 return ret;
5494}
5495
Miao Xie6d07bce2011-01-05 10:07:31 +00005496/*
5497 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005498 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005499 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005500u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005501{
David Sterba32da53862019-10-29 19:20:18 +01005502 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005503 u64 free_bytes = 0;
5504 int factor;
5505
Nicholas D Steeves01327612016-05-19 21:18:45 -04005506 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005507 if (list_empty(&sinfo->ro_bgs))
5508 return 0;
5509
5510 spin_lock(&sinfo->lock);
5511 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005512 spin_lock(&block_group->lock);
5513
5514 if (!block_group->ro) {
5515 spin_unlock(&block_group->lock);
5516 continue;
5517 }
5518
David Sterba46df06b2018-07-13 20:46:30 +02005519 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005520 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005521 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005522
5523 spin_unlock(&block_group->lock);
5524 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005525 spin_unlock(&sinfo->lock);
5526
5527 return free_bytes;
5528}
5529
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005530int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5531 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005532{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005533 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005534}
5535
Jeff Mahoney499f3772015-06-15 09:41:17 -04005536/*
5537 * It used to be that old block groups would be left around forever.
5538 * Iterating over them would be enough to trim unused space. Since we
5539 * now automatically remove them, we also need to iterate over unallocated
5540 * space.
5541 *
5542 * We don't want a transaction for this since the discard may take a
5543 * substantial amount of time. We don't require that a transaction be
5544 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005545 * to ensure that we're not discarding chunks that were released or
5546 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005547 *
5548 * Holding the chunks lock will prevent other threads from allocating
5549 * or releasing chunks, but it won't prevent a running transaction
5550 * from committing and releasing the memory that the pending chunks
5551 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005552 * transaction and hold the commit root sem. We only need to hold
5553 * it while performing the free space search since we have already
5554 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005555 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005556static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005557{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005558 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005559 int ret;
5560
5561 *trimmed = 0;
5562
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005563 /* Discard not supported = nothing to do. */
5564 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5565 return 0;
5566
Andrea Gelmini52042d82018-11-28 12:05:13 +01005567 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005568 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005569 return 0;
5570
5571 /* No free space = nothing to do. */
5572 if (device->total_bytes <= device->bytes_used)
5573 return 0;
5574
5575 ret = 0;
5576
5577 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005578 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005579 u64 bytes;
5580
5581 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5582 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005583 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005584
Nikolay Borisov929be172019-03-27 14:24:18 +02005585 find_first_clear_extent_bit(&device->alloc_state, start,
5586 &start, &end,
5587 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005588
5589 /* Ensure we skip the reserved area in the first 1M */
5590 start = max_t(u64, start, SZ_1M);
5591
Nikolay Borisov929be172019-03-27 14:24:18 +02005592 /*
5593 * If find_first_clear_extent_bit find a range that spans the
5594 * end of the device it will set end to -1, in this case it's up
5595 * to the caller to trim the value to the size of the device.
5596 */
5597 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005598
Nikolay Borisov929be172019-03-27 14:24:18 +02005599 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005600
Nikolay Borisov929be172019-03-27 14:24:18 +02005601 /* We didn't find any extents */
5602 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005603 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005604 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005605 break;
5606 }
5607
Nikolay Borisov929be172019-03-27 14:24:18 +02005608 ret = btrfs_issue_discard(device->bdev, start, len,
5609 &bytes);
5610 if (!ret)
5611 set_extent_bits(&device->alloc_state, start,
5612 start + bytes - 1,
5613 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005614 mutex_unlock(&fs_info->chunk_mutex);
5615
5616 if (ret)
5617 break;
5618
5619 start += len;
5620 *trimmed += bytes;
5621
5622 if (fatal_signal_pending(current)) {
5623 ret = -ERESTARTSYS;
5624 break;
5625 }
5626
5627 cond_resched();
5628 }
5629
5630 return ret;
5631}
5632
Qu Wenruo93bba242018-09-07 14:16:23 +08005633/*
5634 * Trim the whole filesystem by:
5635 * 1) trimming the free space in each block group
5636 * 2) trimming the unallocated space on each device
5637 *
5638 * This will also continue trimming even if a block group or device encounters
5639 * an error. The return value will be the last error, or 0 if nothing bad
5640 * happens.
5641 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005642int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005643{
David Sterba32da53862019-10-29 19:20:18 +01005644 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005645 struct btrfs_device *device;
5646 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005647 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005648 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005649 u64 start;
5650 u64 end;
5651 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005652 u64 bg_failed = 0;
5653 u64 dev_failed = 0;
5654 int bg_ret = 0;
5655 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005656 int ret = 0;
5657
Qu Wenruo07301df2019-05-28 16:21:54 +08005658 /*
5659 * Check range overflow if range->len is set.
5660 * The default range->len is U64_MAX.
5661 */
5662 if (range->len != U64_MAX &&
5663 check_add_overflow(range->start, range->len, &range_end))
5664 return -EINVAL;
5665
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005666 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005667 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005668 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005669 btrfs_put_block_group(cache);
5670 break;
5671 }
5672
David Sterbab3470b52019-10-23 18:48:22 +02005673 start = max(range->start, cache->start);
5674 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005675
5676 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005677 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005678 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005679 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005680 bg_failed++;
5681 bg_ret = ret;
5682 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005683 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005684 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005685 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005686 bg_failed++;
5687 bg_ret = ret;
5688 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005689 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005690 }
5691 ret = btrfs_trim_block_group(cache,
5692 &group_trimmed,
5693 start,
5694 end,
5695 range->minlen);
5696
5697 trimmed += group_trimmed;
5698 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005699 bg_failed++;
5700 bg_ret = ret;
5701 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005702 }
5703 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005704 }
5705
Qu Wenruo93bba242018-09-07 14:16:23 +08005706 if (bg_failed)
5707 btrfs_warn(fs_info,
5708 "failed to trim %llu block group(s), last error %d",
5709 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005710 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005711 devices = &fs_info->fs_devices->devices;
5712 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005713 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005714 if (ret) {
5715 dev_failed++;
5716 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005717 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005718 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005719
5720 trimmed += group_trimmed;
5721 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005722 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005723
Qu Wenruo93bba242018-09-07 14:16:23 +08005724 if (dev_failed)
5725 btrfs_warn(fs_info,
5726 "failed to trim %llu device(s), last error %d",
5727 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005728 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005729 if (bg_ret)
5730 return bg_ret;
5731 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005732}
Miao Xie8257b2d2014-03-06 13:38:19 +08005733
5734/*
David Sterbaea14b57f2017-06-22 02:19:11 +02005735 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005736 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
5737 * data into the page cache through nocow before the subvolume is snapshoted,
5738 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +02005739 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005740 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +08005741 */
David Sterbaea14b57f2017-06-22 02:19:11 +02005742void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005743{
5744 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +01005745 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +08005746}
5747
David Sterbaea14b57f2017-06-22 02:19:11 +02005748int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005749{
David Sterbaea14b57f2017-06-22 02:19:11 +02005750 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +08005751 return 0;
5752
5753 percpu_counter_inc(&root->subv_writers->counter);
5754 /*
5755 * Make sure counter is updated before we check for snapshot creation.
5756 */
5757 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +02005758 if (atomic_read(&root->will_be_snapshotted)) {
5759 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08005760 return 0;
5761 }
5762 return 1;
5763}
Zhao Lei0bc19f902016-01-06 18:56:36 +08005764
Zhao Lei0bc19f902016-01-06 18:56:36 +08005765void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
5766{
5767 while (true) {
5768 int ret;
5769
David Sterbaea14b57f2017-06-22 02:19:11 +02005770 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +08005771 if (ret)
5772 break;
Peter Zijlstra46259562018-03-15 11:43:08 +01005773 wait_var_event(&root->will_be_snapshotted,
5774 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +08005775 }
5776}