blob: af76314720739159a0329806d9c38b7c4932bac0 [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>
Miao Xie995946d2014-04-02 19:51:06 +080019#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "disk-io.h"
21#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040022#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050023#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070026#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060027#include "math.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
Josef Bacik0f9dd462008-09-23 13:14:11 -040057static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
58{
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
Josef Bacik6f410d12019-06-20 15:37:49 -040073void btrfs_free_excluded_extents(struct btrfs_block_group_cache *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
78 start = cache->key.objectid;
79 end = start + cache->key.offset - 1;
80
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{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001309 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001310 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001311 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001312
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001313
Filipe Manana29992412016-05-27 17:42:05 +01001314 /*
1315 * Avoid races with device replace and make sure our bbio has devices
1316 * associated to its stripes that don't go away while we are discarding.
1317 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001318 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001319 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001320 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
1321 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001323 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001324 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001325 int i;
1326
Liu Hui1f3c79a2009-01-05 15:57:51 -05001327
Jan Schmidta1d3c472011-08-04 17:15:33 +02001328 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001329 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001330 struct request_queue *req_q;
1331
Filipe Manana627e0872018-01-30 18:40:22 +00001332 if (!stripe->dev->bdev) {
1333 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1334 continue;
1335 }
Anand Jain38b5f682017-11-29 18:53:43 +08001336 req_q = bdev_get_queue(stripe->dev->bdev);
1337 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001338 continue;
1339
Li Dongyang5378e602011-03-24 10:24:27 +00001340 ret = btrfs_issue_discard(stripe->dev->bdev,
1341 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001342 stripe->length,
1343 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00001344 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001345 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00001346 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001347 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001348
1349 /*
1350 * Just in case we get back EOPNOTSUPP for some reason,
1351 * just ignore the return value so we don't screw up
1352 * people calling discard_extent.
1353 */
1354 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001355 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001356 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001357 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001359
1360 if (actual_bytes)
1361 *actual_bytes = discarded_bytes;
1362
Liu Hui1f3c79a2009-01-05 15:57:51 -05001363
David Woodhouse53b381b2013-01-29 18:40:14 -05001364 if (ret == -EOPNOTSUPP)
1365 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001366 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001367}
1368
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001371 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001372{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001373 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001374 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001375 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001376
Qu Wenruo82fa1132019-04-04 14:45:35 +08001377 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1378 generic_ref->action);
1379 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1380 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001381
Qu Wenruo82fa1132019-04-04 14:45:35 +08001382 if (generic_ref->type == BTRFS_REF_METADATA)
1383 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001384 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001385 else
1386 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001387 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001388
Qu Wenruo82fa1132019-04-04 14:45:35 +08001389 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001390
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001391 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001392 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001393
Zheng Yan31840ae2008-09-23 13:14:14 -04001394 return ret;
1395}
1396
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001397/*
1398 * __btrfs_inc_extent_ref - insert backreference for a given extent
1399 *
1400 * @trans: Handle of transaction
1401 *
1402 * @node: The delayed ref node used to get the bytenr/length for
1403 * extent whose references are incremented.
1404 *
1405 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1406 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1407 * bytenr of the parent block. Since new extents are always
1408 * created with indirect references, this will only be the case
1409 * when relocating a shared extent. In that case, root_objectid
1410 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1411 * be 0
1412 *
1413 * @root_objectid: The id of the root where this modification has originated,
1414 * this can be either one of the well-known metadata trees or
1415 * the subvolume id which references this extent.
1416 *
1417 * @owner: For data extents it is the inode number of the owning file.
1418 * For metadata extents this parameter holds the level in the
1419 * tree of the extent.
1420 *
1421 * @offset: For metadata extents the offset is ignored and is currently
1422 * always passed as 0. For data extents it is the fileoffset
1423 * this extent belongs to.
1424 *
1425 * @refs_to_add Number of references to add
1426 *
1427 * @extent_op Pointer to a structure, holding information necessary when
1428 * updating a tree block's flags
1429 *
1430 */
Chris Mason925baed2008-06-25 16:01:30 -04001431static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001432 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001433 u64 parent, u64 root_objectid,
1434 u64 owner, u64 offset, int refs_to_add,
1435 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001436{
Chris Mason5caf2a02007-04-02 11:20:42 -04001437 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001439 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001440 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001441 u64 bytenr = node->bytenr;
1442 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001443 u64 refs;
1444 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001445
Chris Mason5caf2a02007-04-02 11:20:42 -04001446 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001447 if (!path)
1448 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001449
David Sterbae4058b52015-11-27 16:31:35 +01001450 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001451 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001452 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001453 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1454 parent, root_objectid, owner,
1455 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001456 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001458
1459 /*
1460 * Ok we had -EAGAIN which means we didn't have space to insert and
1461 * inline extent ref, so just update the reference count and add a
1462 * normal backref.
1463 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001464 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001465 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001466 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1467 refs = btrfs_extent_refs(leaf, item);
1468 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1469 if (extent_op)
1470 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001472 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001473 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001474
David Sterbae4058b52015-11-27 16:31:35 +01001475 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001476 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001477 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001478 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1479 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001480 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001481 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001482out:
Chris Mason74493f72007-12-11 09:25:06 -05001483 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001484 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001485}
1486
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001487static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 struct btrfs_delayed_ref_node *node,
1489 struct btrfs_delayed_extent_op *extent_op,
1490 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001491{
Chris Mason56bec292009-03-13 10:10:06 -04001492 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 struct btrfs_delayed_data_ref *ref;
1494 struct btrfs_key ins;
1495 u64 parent = 0;
1496 u64 ref_root = 0;
1497 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001498
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001499 ins.objectid = node->bytenr;
1500 ins.offset = node->num_bytes;
1501 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001504 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1507 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001508 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509
1510 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001511 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001512 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001513 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1514 flags, ref->objectid,
1515 ref->offset, &ins,
1516 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001517 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001518 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1519 ref->objectid, ref->offset,
1520 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001522 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001523 ref_root, ref->objectid,
1524 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001525 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 } else {
1527 BUG();
1528 }
Chris Mason56bec292009-03-13 10:10:06 -04001529 return ret;
1530}
1531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001532static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1533 struct extent_buffer *leaf,
1534 struct btrfs_extent_item *ei)
1535{
1536 u64 flags = btrfs_extent_flags(leaf, ei);
1537 if (extent_op->update_flags) {
1538 flags |= extent_op->flags_to_set;
1539 btrfs_set_extent_flags(leaf, ei, flags);
1540 }
1541
1542 if (extent_op->update_key) {
1543 struct btrfs_tree_block_info *bi;
1544 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1545 bi = (struct btrfs_tree_block_info *)(ei + 1);
1546 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1547 }
1548}
1549
1550static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001551 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001552 struct btrfs_delayed_extent_op *extent_op)
1553{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001554 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001555 struct btrfs_key key;
1556 struct btrfs_path *path;
1557 struct btrfs_extent_item *ei;
1558 struct extent_buffer *leaf;
1559 u32 item_size;
1560 int ret;
1561 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001562 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 if (trans->aborted)
1565 return 0;
1566
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001567 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001568 metadata = 0;
1569
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001570 path = btrfs_alloc_path();
1571 if (!path)
1572 return -ENOMEM;
1573
Josef Bacikd2788502017-09-29 15:43:57 -04001574 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575
Josef Bacik3173a182013-03-07 14:22:04 -05001576 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001577 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001578 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001579 } else {
1580 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001581 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001582 }
1583
1584again:
David Sterbae4058b52015-11-27 16:31:35 +01001585 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001586 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001587 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001588 if (ret < 0) {
1589 err = ret;
1590 goto out;
1591 }
1592 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001593 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001594 if (path->slots[0] > 0) {
1595 path->slots[0]--;
1596 btrfs_item_key_to_cpu(path->nodes[0], &key,
1597 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001598 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001599 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001600 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001601 ret = 0;
1602 }
1603 if (ret > 0) {
1604 btrfs_release_path(path);
1605 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001606
Josef Bacikd2788502017-09-29 15:43:57 -04001607 key.objectid = head->bytenr;
1608 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001609 key.type = BTRFS_EXTENT_ITEM_KEY;
1610 goto again;
1611 }
1612 } else {
1613 err = -EIO;
1614 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001615 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001616 }
1617
1618 leaf = path->nodes[0];
1619 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001620
David Sterba6d8ff4e2018-06-26 16:20:59 +02001621 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001622 err = -EINVAL;
1623 btrfs_print_v0_err(fs_info);
1624 btrfs_abort_transaction(trans, err);
1625 goto out;
1626 }
1627
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1629 __run_delayed_extent_op(extent_op, leaf, ei);
1630
1631 btrfs_mark_buffer_dirty(leaf);
1632out:
1633 btrfs_free_path(path);
1634 return err;
1635}
1636
1637static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 struct btrfs_delayed_ref_node *node,
1639 struct btrfs_delayed_extent_op *extent_op,
1640 int insert_reserved)
1641{
1642 int ret = 0;
1643 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 u64 parent = 0;
1645 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646
1647 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001648 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001650 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1651 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001652 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653
Liu Bo02794222016-09-14 19:19:05 -07001654 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001655 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001656 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1657 node->bytenr, node->ref_mod, node->action, ref_root,
1658 parent);
1659 return -EIO;
1660 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001662 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001663 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001664 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001665 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1666 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001668 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001669 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001670 } else {
1671 BUG();
1672 }
1673 return ret;
1674}
1675
Chris Mason56bec292009-03-13 10:10:06 -04001676/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678 struct btrfs_delayed_ref_node *node,
1679 struct btrfs_delayed_extent_op *extent_op,
1680 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001681{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001682 int ret = 0;
1683
Josef Bacik857cc2f2013-10-07 15:21:08 -04001684 if (trans->aborted) {
1685 if (insert_reserved)
Nikolay Borisov5fac7f92018-06-20 15:49:11 +03001686 btrfs_pin_extent(trans->fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04001687 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001688 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001689 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1692 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001693 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 insert_reserved);
1695 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1696 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001697 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 insert_reserved);
1699 else
1700 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001701 if (ret && insert_reserved)
1702 btrfs_pin_extent(trans->fs_info, node->bytenr,
1703 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001705}
1706
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001707static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001708select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001709{
Filipe Mananacffc3372015-07-09 13:13:44 +01001710 struct btrfs_delayed_ref_node *ref;
1711
Liu Boe3d03962018-08-23 03:51:50 +08001712 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001713 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001714
Filipe Mananacffc3372015-07-09 13:13:44 +01001715 /*
1716 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1717 * This is to prevent a ref count from going down to zero, which deletes
1718 * the extent item from the extent tree, when there still are references
1719 * to add, which would fail because they would not find the extent item.
1720 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001721 if (!list_empty(&head->ref_add_list))
1722 return list_first_entry(&head->ref_add_list,
1723 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001724
Liu Boe3d03962018-08-23 03:51:50 +08001725 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001726 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001727 ASSERT(list_empty(&ref->add_list));
1728 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001729}
1730
Josef Bacik2eadaa22017-09-29 15:43:52 -04001731static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1732 struct btrfs_delayed_ref_head *head)
1733{
1734 spin_lock(&delayed_refs->lock);
1735 head->processing = 0;
1736 delayed_refs->num_heads_ready++;
1737 spin_unlock(&delayed_refs->lock);
1738 btrfs_delayed_ref_unlock(head);
1739}
1740
Josef Bacikbedc66172018-12-03 10:20:31 -05001741static struct btrfs_delayed_extent_op *cleanup_extent_op(
1742 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001743{
1744 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001745
1746 if (!extent_op)
1747 return NULL;
1748
1749 if (head->must_insert_reserved) {
1750 head->extent_op = NULL;
1751 btrfs_free_delayed_extent_op(extent_op);
1752 return NULL;
1753 }
1754 return extent_op;
1755}
1756
1757static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1758 struct btrfs_delayed_ref_head *head)
1759{
1760 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001761 int ret;
1762
Josef Bacikbedc66172018-12-03 10:20:31 -05001763 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001764 if (!extent_op)
1765 return 0;
1766 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001767 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001768 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001769 btrfs_free_delayed_extent_op(extent_op);
1770 return ret ? ret : 1;
1771}
1772
Josef Bacik31890da2018-11-21 14:05:41 -05001773void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1774 struct btrfs_delayed_ref_root *delayed_refs,
1775 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001776{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001777 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001778
1779 if (head->total_ref_mod < 0) {
1780 struct btrfs_space_info *space_info;
1781 u64 flags;
1782
1783 if (head->is_data)
1784 flags = BTRFS_BLOCK_GROUP_DATA;
1785 else if (head->is_system)
1786 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1787 else
1788 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001789 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001790 ASSERT(space_info);
1791 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1792 -head->num_bytes,
1793 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1794
Josef Bacikba2c4d42018-12-03 10:20:33 -05001795 /*
1796 * We had csum deletions accounted for in our delayed refs rsv,
1797 * we need to drop the csum leaves for this update from our
1798 * delayed_refs_rsv.
1799 */
Josef Bacik07c47772018-12-03 10:20:30 -05001800 if (head->is_data) {
1801 spin_lock(&delayed_refs->lock);
1802 delayed_refs->pending_csums -= head->num_bytes;
1803 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001804 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1805 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001806 }
1807 }
1808
Josef Bacikba2c4d42018-12-03 10:20:33 -05001809 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001810}
1811
Josef Bacik194ab0b2017-09-29 15:43:54 -04001812static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001813 struct btrfs_delayed_ref_head *head)
1814{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001815
1816 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001817 struct btrfs_delayed_ref_root *delayed_refs;
1818 int ret;
1819
1820 delayed_refs = &trans->transaction->delayed_refs;
1821
Josef Bacikbedc66172018-12-03 10:20:31 -05001822 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001823 if (ret < 0) {
1824 unselect_delayed_ref_head(delayed_refs, head);
1825 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1826 return ret;
1827 } else if (ret) {
1828 return ret;
1829 }
1830
1831 /*
1832 * Need to drop our head ref lock and re-acquire the delayed ref lock
1833 * and then re-check to make sure nobody got added.
1834 */
1835 spin_unlock(&head->lock);
1836 spin_lock(&delayed_refs->lock);
1837 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001838 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001839 spin_unlock(&head->lock);
1840 spin_unlock(&delayed_refs->lock);
1841 return 1;
1842 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001843 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001844 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001845 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001846
Josef Bacikc1103f72017-09-29 15:43:56 -04001847 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04001848 btrfs_pin_extent(fs_info, head->bytenr,
1849 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001850 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04001851 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
1852 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001853 }
1854 }
1855
Josef Bacik31890da2018-11-21 14:05:41 -05001856 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001857
1858 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001859 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001860 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001861 return 0;
1862}
1863
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001864static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1865 struct btrfs_trans_handle *trans)
1866{
1867 struct btrfs_delayed_ref_root *delayed_refs =
1868 &trans->transaction->delayed_refs;
1869 struct btrfs_delayed_ref_head *head = NULL;
1870 int ret;
1871
1872 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001873 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001874 if (!head) {
1875 spin_unlock(&delayed_refs->lock);
1876 return head;
1877 }
1878
1879 /*
1880 * Grab the lock that says we are going to process all the refs for
1881 * this head
1882 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001883 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001884 spin_unlock(&delayed_refs->lock);
1885
1886 /*
1887 * We may have dropped the spin lock to get the head mutex lock, and
1888 * that might have given someone else time to free the head. If that's
1889 * true, it has been removed from our list and we can move on.
1890 */
1891 if (ret == -EAGAIN)
1892 head = ERR_PTR(-EAGAIN);
1893
1894 return head;
1895}
1896
Nikolay Borisove7261382018-08-15 10:39:55 +03001897static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1898 struct btrfs_delayed_ref_head *locked_ref,
1899 unsigned long *run_refs)
1900{
1901 struct btrfs_fs_info *fs_info = trans->fs_info;
1902 struct btrfs_delayed_ref_root *delayed_refs;
1903 struct btrfs_delayed_extent_op *extent_op;
1904 struct btrfs_delayed_ref_node *ref;
1905 int must_insert_reserved = 0;
1906 int ret;
1907
1908 delayed_refs = &trans->transaction->delayed_refs;
1909
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001910 lockdep_assert_held(&locked_ref->mutex);
1911 lockdep_assert_held(&locked_ref->lock);
1912
Nikolay Borisove7261382018-08-15 10:39:55 +03001913 while ((ref = select_delayed_ref(locked_ref))) {
1914 if (ref->seq &&
1915 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1916 spin_unlock(&locked_ref->lock);
1917 unselect_delayed_ref_head(delayed_refs, locked_ref);
1918 return -EAGAIN;
1919 }
1920
1921 (*run_refs)++;
1922 ref->in_tree = 0;
1923 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1924 RB_CLEAR_NODE(&ref->ref_node);
1925 if (!list_empty(&ref->add_list))
1926 list_del(&ref->add_list);
1927 /*
1928 * When we play the delayed ref, also correct the ref_mod on
1929 * head
1930 */
1931 switch (ref->action) {
1932 case BTRFS_ADD_DELAYED_REF:
1933 case BTRFS_ADD_DELAYED_EXTENT:
1934 locked_ref->ref_mod -= ref->ref_mod;
1935 break;
1936 case BTRFS_DROP_DELAYED_REF:
1937 locked_ref->ref_mod += ref->ref_mod;
1938 break;
1939 default:
1940 WARN_ON(1);
1941 }
1942 atomic_dec(&delayed_refs->num_entries);
1943
1944 /*
1945 * Record the must_insert_reserved flag before we drop the
1946 * spin lock.
1947 */
1948 must_insert_reserved = locked_ref->must_insert_reserved;
1949 locked_ref->must_insert_reserved = 0;
1950
1951 extent_op = locked_ref->extent_op;
1952 locked_ref->extent_op = NULL;
1953 spin_unlock(&locked_ref->lock);
1954
1955 ret = run_one_delayed_ref(trans, ref, extent_op,
1956 must_insert_reserved);
1957
1958 btrfs_free_delayed_extent_op(extent_op);
1959 if (ret) {
1960 unselect_delayed_ref_head(delayed_refs, locked_ref);
1961 btrfs_put_delayed_ref(ref);
1962 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1963 ret);
1964 return ret;
1965 }
1966
1967 btrfs_put_delayed_ref(ref);
1968 cond_resched();
1969
1970 spin_lock(&locked_ref->lock);
1971 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1972 }
1973
1974 return 0;
1975}
1976
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001977/*
1978 * Returns 0 on success or if called with an already aborted transaction.
1979 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
1980 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05001981static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05001982 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04001983{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02001984 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04001985 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04001986 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001987 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04001988 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001989 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05001990 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001991
1992 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001993 do {
Chris Mason56bec292009-03-13 10:10:06 -04001994 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001995 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001996 if (IS_ERR_OR_NULL(locked_ref)) {
1997 if (PTR_ERR(locked_ref) == -EAGAIN) {
1998 continue;
1999 } else {
2000 break;
2001 }
Chris Mason56bec292009-03-13 10:10:06 -04002002 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002003 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002004 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002005 /*
2006 * We need to try and merge add/drops of the same ref since we
2007 * can run into issues with relocate dropping the implicit ref
2008 * and then it being added back again before the drop can
2009 * finish. If we merged anything we need to re-loop so we can
2010 * get a good ref.
2011 * Or we can get node references of the same type that weren't
2012 * merged when created due to bumps in the tree mod seq, and
2013 * we need to merge them to prevent adding an inline extent
2014 * backref before dropping it (triggering a BUG_ON at
2015 * insert_inline_extent_backref()).
2016 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002017 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002018 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002019
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002020 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2021 &actual_count);
2022 if (ret < 0 && ret != -EAGAIN) {
2023 /*
2024 * Error, btrfs_run_delayed_refs_for_head already
2025 * unlocked everything so just bail out
2026 */
2027 return ret;
2028 } else if (!ret) {
2029 /*
2030 * Success, perform the usual cleanup of a processed
2031 * head
2032 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002033 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002034 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002035 /* We dropped our lock, we need to loop. */
2036 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002037 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002038 } else if (ret) {
2039 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002041 }
Chris Mason56bec292009-03-13 10:10:06 -04002042
Josef Bacikb00e6252017-09-29 15:43:53 -04002043 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002044 * Either success case or btrfs_run_delayed_refs_for_head
2045 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002046 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002047
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002048 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002049 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002050 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002051
2052 /*
2053 * We don't want to include ref heads since we can have empty ref heads
2054 * and those will drastically skew our runtime down since we just do
2055 * accounting, no actual extent tree updates.
2056 */
2057 if (actual_count > 0) {
2058 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2059 u64 avg;
2060
2061 /*
2062 * We weigh the current average higher than our current runtime
2063 * to avoid large swings in the average.
2064 */
2065 spin_lock(&delayed_refs->lock);
2066 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002067 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002068 spin_unlock(&delayed_refs->lock);
2069 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002070 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002071}
2072
Arne Jansen709c0482011-09-12 12:22:57 +02002073#ifdef SCRAMBLE_DELAYED_REFS
2074/*
2075 * Normally delayed refs get processed in ascending bytenr order. This
2076 * correlates in most cases to the order added. To expose dependencies on this
2077 * order, we start to process the tree in the middle instead of the beginning
2078 */
2079static u64 find_middle(struct rb_root *root)
2080{
2081 struct rb_node *n = root->rb_node;
2082 struct btrfs_delayed_ref_node *entry;
2083 int alt = 1;
2084 u64 middle;
2085 u64 first = 0, last = 0;
2086
2087 n = rb_first(root);
2088 if (n) {
2089 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2090 first = entry->bytenr;
2091 }
2092 n = rb_last(root);
2093 if (n) {
2094 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2095 last = entry->bytenr;
2096 }
2097 n = root->rb_node;
2098
2099 while (n) {
2100 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2101 WARN_ON(!entry->in_tree);
2102
2103 middle = entry->bytenr;
2104
2105 if (alt)
2106 n = n->rb_left;
2107 else
2108 n = n->rb_right;
2109
2110 alt = 1 - alt;
2111 }
2112 return middle;
2113}
2114#endif
2115
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002116static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002117{
2118 u64 num_bytes;
2119
2120 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2121 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002122 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002123 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2124
2125 /*
2126 * 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 -04002127 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002128 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002130}
2131
Josef Bacik12621332015-02-03 07:50:16 -08002132/*
2133 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2134 * would require to store the csums for that many bytes.
2135 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002136u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002137{
2138 u64 csum_size;
2139 u64 num_csums_per_leaf;
2140 u64 num_csums;
2141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002142 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002143 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 (u64)btrfs_super_csum_size(fs_info->super_copy));
2145 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002146 num_csums += num_csums_per_leaf - 1;
2147 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2148 return num_csums;
2149}
2150
Chris Masonc3e69d52009-03-13 10:17:05 -04002151/*
2152 * this starts processing the delayed reference count updates and
2153 * extent insertions we have queued up so far. count can be
2154 * 0, which means to process everything in the tree at the start
2155 * of the run (but not newly added entries), or it can be some target
2156 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002157 *
2158 * Returns 0 on success or if called with an aborted transaction
2159 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002160 */
2161int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002162 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002163{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002164 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002165 struct rb_node *node;
2166 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002167 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002168 int ret;
2169 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002170
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002171 /* We'll clean this up in btrfs_cleanup_transaction */
2172 if (trans->aborted)
2173 return 0;
2174
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002175 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002176 return 0;
2177
Chris Masonc3e69d52009-03-13 10:17:05 -04002178 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002179 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002180 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002181
Chris Masonc3e69d52009-03-13 10:17:05 -04002182again:
Arne Jansen709c0482011-09-12 12:22:57 +02002183#ifdef SCRAMBLE_DELAYED_REFS
2184 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2185#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002186 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002187 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002188 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002189 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002190 }
2191
Chris Mason56bec292009-03-13 10:10:06 -04002192 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002193 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002194
Josef Bacikd7df2c72014-01-23 09:21:38 -05002195 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002196 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002197 if (!node) {
2198 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002199 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002200 }
Josef Bacikd2788502017-09-29 15:43:57 -04002201 head = rb_entry(node, struct btrfs_delayed_ref_head,
2202 href_node);
2203 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002204 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002205
2206 /* Mutex was contended, block until it's released and retry. */
2207 mutex_lock(&head->mutex);
2208 mutex_unlock(&head->mutex);
2209
2210 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002211 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002212 goto again;
2213 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002214out:
Chris Masona28ec192007-03-06 20:08:01 -05002215 return 0;
2216}
2217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002220 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221{
2222 struct btrfs_delayed_extent_op *extent_op;
2223 int ret;
2224
Miao Xie78a61842012-11-21 02:21:28 +00002225 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002226 if (!extent_op)
2227 return -ENOMEM;
2228
2229 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002230 extent_op->update_flags = true;
2231 extent_op->update_key = false;
2232 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002233 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234
David Sterbac6e340b2019-03-20 11:42:34 +01002235 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002236 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002237 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238 return ret;
2239}
2240
Liu Boe4c3b2d2017-01-30 12:25:28 -08002241static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 struct btrfs_path *path,
2243 u64 objectid, u64 offset, u64 bytenr)
2244{
2245 struct btrfs_delayed_ref_head *head;
2246 struct btrfs_delayed_ref_node *ref;
2247 struct btrfs_delayed_data_ref *data_ref;
2248 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002249 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002250 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002251 int ret = 0;
2252
ethanwu998ac6d2018-04-29 15:59:42 +08002253 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002254 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002255 if (cur_trans)
2256 refcount_inc(&cur_trans->use_count);
2257 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002258 if (!cur_trans)
2259 return 0;
2260
2261 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002263 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002264 if (!head) {
2265 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002266 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002267 return 0;
2268 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002269
2270 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002271 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002272 spin_unlock(&delayed_refs->lock);
2273
David Sterbab3b4aa72011-04-21 01:20:15 +02002274 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002275
David Sterba8cc33e52011-05-02 15:29:25 +02002276 /*
2277 * Mutex was contended, block until it's released and let
2278 * caller try again
2279 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280 mutex_lock(&head->mutex);
2281 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002282 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002283 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002284 return -EAGAIN;
2285 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002287
2288 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002289 /*
2290 * XXX: We should replace this with a proper search function in the
2291 * future.
2292 */
Liu Boe3d03962018-08-23 03:51:50 +08002293 for (node = rb_first_cached(&head->ref_tree); node;
2294 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002295 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002296 /* If it's a shared ref we know a cross reference exists */
2297 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2298 ret = 1;
2299 break;
2300 }
2301
2302 data_ref = btrfs_delayed_node_to_data_ref(ref);
2303
2304 /*
2305 * If our ref doesn't match the one we're currently looking at
2306 * then we have a cross reference.
2307 */
2308 if (data_ref->root != root->root_key.objectid ||
2309 data_ref->objectid != objectid ||
2310 data_ref->offset != offset) {
2311 ret = 1;
2312 break;
2313 }
2314 }
2315 spin_unlock(&head->lock);
2316 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002317 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318 return ret;
2319}
2320
Liu Boe4c3b2d2017-01-30 12:25:28 -08002321static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002322 struct btrfs_path *path,
2323 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002325 struct btrfs_fs_info *fs_info = root->fs_info;
2326 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002327 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002328 struct btrfs_extent_data_ref *ref;
2329 struct btrfs_extent_inline_ref *iref;
2330 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002331 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002333 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002334 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002335
Chris Masonbe20aa92007-12-17 20:14:01 -05002336 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002337 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002338 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002339
Chris Masonbe20aa92007-12-17 20:14:01 -05002340 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2341 if (ret < 0)
2342 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002344
2345 ret = -ENOENT;
2346 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002347 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002348
Zheng Yan31840ae2008-09-23 13:14:14 -04002349 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002350 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002352
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002354 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002355
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002356 ret = 1;
2357 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002358 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2359
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002360 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002361 if (item_size != sizeof(*ei) +
2362 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2363 goto out;
2364
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002365 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 if (btrfs_extent_generation(leaf, ei) <=
2367 btrfs_root_last_snapshot(&root->root_item))
2368 goto out;
2369
2370 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002371
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002372 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002373 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2374 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 goto out;
2376
2377 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2378 if (btrfs_extent_refs(leaf, ei) !=
2379 btrfs_extent_data_ref_count(leaf, ref) ||
2380 btrfs_extent_data_ref_root(leaf, ref) !=
2381 root->root_key.objectid ||
2382 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2383 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2384 goto out;
2385
Yan Zhengf321e492008-07-30 09:26:11 -04002386 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002387out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002388 return ret;
2389}
2390
Liu Boe4c3b2d2017-01-30 12:25:28 -08002391int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2392 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393{
2394 struct btrfs_path *path;
2395 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396
2397 path = btrfs_alloc_path();
2398 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002399 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400
2401 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002402 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403 offset, bytenr);
2404 if (ret && ret != -ENOENT)
2405 goto out;
2406
Misono Tomohiro380fd062018-08-30 10:59:16 +09002407 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2408 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410out:
Yan Zhengf321e492008-07-30 09:26:11 -04002411 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002412 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2413 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002414 return ret;
2415}
2416
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002418 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002419 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002420 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002421{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002422 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002423 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002424 u64 num_bytes;
2425 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002426 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002427 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002428 struct btrfs_key key;
2429 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002430 struct btrfs_ref generic_ref = { 0 };
2431 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002432 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002433 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002434 int level;
2435 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002436
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002438 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002439
Zheng Yan31840ae2008-09-23 13:14:14 -04002440 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002441 nritems = btrfs_header_nritems(buf);
2442 level = btrfs_header_level(buf);
2443
Miao Xie27cdeb72014-04-02 19:51:05 +08002444 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002445 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 if (full_backref)
2448 parent = buf->start;
2449 else
2450 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002451 if (inc)
2452 action = BTRFS_ADD_DELAYED_REF;
2453 else
2454 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002455
Zheng Yan31840ae2008-09-23 13:14:14 -04002456 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002457 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002458 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002459 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002460 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002461 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002462 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002463 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002464 BTRFS_FILE_EXTENT_INLINE)
2465 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002466 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2467 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002468 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002469
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2471 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002472 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2473 num_bytes, parent);
2474 generic_ref.real_root = root->root_key.objectid;
2475 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2476 key.offset);
2477 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002478 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002479 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002480 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002481 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482 if (ret)
2483 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002484 } else {
2485 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002487 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2488 num_bytes, parent);
2489 generic_ref.real_root = root->root_key.objectid;
2490 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2491 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002492 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002493 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002494 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002495 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002497 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002498 }
2499 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002500 return 0;
2501fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002503}
2504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002505int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002506 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002507{
Josef Bacike339a6b2014-07-02 10:54:25 -07002508 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509}
Zheng Yan31840ae2008-09-23 13:14:14 -04002510
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002511int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002512 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513{
Josef Bacike339a6b2014-07-02 10:54:25 -07002514 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002515}
2516
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002517int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002518{
2519 struct btrfs_block_group_cache *block_group;
2520 int readonly = 0;
2521
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002522 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002523 if (!block_group || block_group->ro)
2524 readonly = 1;
2525 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002526 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002527 return readonly;
2528}
2529
Jeff Mahoney1b868262017-05-17 11:38:35 -04002530static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002531{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002532 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002533 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002534 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002535
Yan, Zhengb742bb822010-05-16 10:46:24 -04002536 if (data)
2537 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002538 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002539 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2540 else
2541 flags = BTRFS_BLOCK_GROUP_METADATA;
2542
Josef Bacik878d7b62019-06-20 15:38:05 -04002543 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002544 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002545}
2546
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002547static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002548{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002549 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002550 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002551
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002552 spin_lock(&fs_info->block_group_cache_lock);
2553 bytenr = fs_info->first_logical_byte;
2554 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002555
2556 if (bytenr < (u64)-1)
2557 return bytenr;
2558
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002559 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002560 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002561 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002562
Yan Zhengd2fb3432008-12-11 16:30:39 -05002563 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002564 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002565
2566 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002567}
2568
David Sterbafdf08602019-03-20 12:12:32 +01002569static int pin_down_extent(struct btrfs_block_group_cache *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002570 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002571{
David Sterbafdf08602019-03-20 12:12:32 +01002572 struct btrfs_fs_info *fs_info = cache->fs_info;
2573
Yan Zheng11833d62009-09-11 16:11:19 -04002574 spin_lock(&cache->space_info->lock);
2575 spin_lock(&cache->lock);
2576 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002577 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2578 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002579 if (reserved) {
2580 cache->reserved -= num_bytes;
2581 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002582 }
Yan Zheng11833d62009-09-11 16:11:19 -04002583 spin_unlock(&cache->lock);
2584 spin_unlock(&cache->space_info->lock);
2585
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002586 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04002587 cache->space_info->flags, num_bytes, 1);
Ethan Liendec59fa2018-07-13 16:50:42 +08002588 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2589 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002590 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002591 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002592 return 0;
2593}
Chris Mason9078a3e2007-04-26 16:46:15 -04002594
Yan, Zhengf0486c62010-05-16 10:46:25 -04002595/*
2596 * this function must be called within transaction
2597 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002598int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002599 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002600{
Yan, Zhengf0486c62010-05-16 10:46:25 -04002601 struct btrfs_block_group_cache *cache;
2602
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002603 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002604 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002605
David Sterbafdf08602019-03-20 12:12:32 +01002606 pin_down_extent(cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002607
2608 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002609 return 0;
2610}
Zheng Yane8569812008-09-26 10:05:48 -04002611
Yan, Zhengf0486c62010-05-16 10:46:25 -04002612/*
Chris Masone688b7252011-10-31 20:52:39 -04002613 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002614 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002615int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04002616 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002617{
Chris Masone688b7252011-10-31 20:52:39 -04002618 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002619 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002621 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002622 if (!cache)
2623 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002624
2625 /*
2626 * pull in the free space cache (if any) so that our pin
2627 * removes the free space from the cache. We have load_only set
2628 * to one because the slow code to read in the free extents does check
2629 * the pinned extents.
2630 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002631 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002632
David Sterbafdf08602019-03-20 12:12:32 +01002633 pin_down_extent(cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002634
2635 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002636 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002637 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002638 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002639}
2640
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002641static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2642 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002643{
2644 int ret;
2645 struct btrfs_block_group_cache *block_group;
2646 struct btrfs_caching_control *caching_ctl;
2647
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002648 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002649 if (!block_group)
2650 return -EINVAL;
2651
Josef Bacik676f1f72019-06-20 15:37:48 -04002652 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002653 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002654
2655 if (!caching_ctl) {
2656 /* Logic error */
Josef Bacik676f1f72019-06-20 15:37:48 -04002657 BUG_ON(!btrfs_block_group_cache_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002658 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2659 } else {
2660 mutex_lock(&caching_ctl->mutex);
2661
2662 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002663 ret = btrfs_add_excluded_extent(fs_info, start,
2664 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002665 } else if (start + num_bytes <= caching_ctl->progress) {
2666 ret = btrfs_remove_free_space(block_group,
2667 start, num_bytes);
2668 } else {
2669 num_bytes = caching_ctl->progress - start;
2670 ret = btrfs_remove_free_space(block_group,
2671 start, num_bytes);
2672 if (ret)
2673 goto out_lock;
2674
2675 num_bytes = (start + num_bytes) -
2676 caching_ctl->progress;
2677 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002678 ret = btrfs_add_excluded_extent(fs_info, start,
2679 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002680 }
2681out_lock:
2682 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002683 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002684 }
2685 btrfs_put_block_group(block_group);
2686 return ret;
2687}
2688
David Sterbabcdc4282019-03-20 12:14:33 +01002689int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002690{
David Sterbabcdc4282019-03-20 12:14:33 +01002691 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002692 struct btrfs_file_extent_item *item;
2693 struct btrfs_key key;
2694 int found_type;
2695 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002696 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002697
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002698 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002699 return 0;
2700
2701 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2702 btrfs_item_key_to_cpu(eb, &key, i);
2703 if (key.type != BTRFS_EXTENT_DATA_KEY)
2704 continue;
2705 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2706 found_type = btrfs_file_extent_type(eb, item);
2707 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2708 continue;
2709 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2710 continue;
2711 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2712 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002713 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2714 if (ret)
2715 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002716 }
2717
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002718 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002719}
2720
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002721static void
2722btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
2723{
2724 atomic_inc(&bg->reservations);
2725}
2726
David Sterba8b74c032017-02-10 19:20:56 +01002727void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002728{
Yan Zheng11833d62009-09-11 16:11:19 -04002729 struct btrfs_caching_control *next;
2730 struct btrfs_caching_control *caching_ctl;
2731 struct btrfs_block_group_cache *cache;
2732
Josef Bacik9e351cc2014-03-13 15:42:13 -04002733 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002734
2735 list_for_each_entry_safe(caching_ctl, next,
2736 &fs_info->caching_block_groups, list) {
2737 cache = caching_ctl->block_group;
Josef Bacik676f1f72019-06-20 15:37:48 -04002738 if (btrfs_block_group_cache_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002739 cache->last_byte_to_unpin = (u64)-1;
2740 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002741 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002742 } else {
2743 cache->last_byte_to_unpin = caching_ctl->progress;
2744 }
2745 }
2746
2747 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2748 fs_info->pinned_extents = &fs_info->freed_extents[1];
2749 else
2750 fs_info->pinned_extents = &fs_info->freed_extents[0];
2751
Josef Bacik9e351cc2014-03-13 15:42:13 -04002752 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002753
Josef Bacik67f9c222019-06-19 13:47:23 -04002754 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002755}
2756
Josef Bacikc759c4e2015-10-02 15:25:10 -04002757/*
2758 * Returns the free cluster for the given space info and sets empty_cluster to
2759 * what it should be based on the mount options.
2760 */
2761static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002762fetch_cluster_info(struct btrfs_fs_info *fs_info,
2763 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002764{
2765 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002766
2767 *empty_cluster = 0;
2768 if (btrfs_mixed_space_info(space_info))
2769 return ret;
2770
Josef Bacikc759c4e2015-10-02 15:25:10 -04002771 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002772 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002773 if (btrfs_test_opt(fs_info, SSD))
2774 *empty_cluster = SZ_2M;
2775 else
Byongho Leeee221842015-12-15 01:42:10 +09002776 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002777 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2778 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2779 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002780 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002781 }
2782
2783 return ret;
2784}
2785
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002786static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2787 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002788 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002789{
Yan Zheng11833d62009-09-11 16:11:19 -04002790 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002791 struct btrfs_space_info *space_info;
2792 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002793 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002794 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002795 u64 total_unpinned = 0;
2796 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002797 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002798
2799 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002800 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002801 if (!cache ||
2802 start >= cache->key.objectid + cache->key.offset) {
2803 if (cache)
2804 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002805 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002806 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002808
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002809 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002810 cache->space_info,
2811 &empty_cluster);
2812 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002813 }
2814
2815 len = cache->key.objectid + cache->key.offset - start;
2816 len = min(len, end + 1 - start);
2817
2818 if (start < cache->last_byte_to_unpin) {
2819 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002820 if (return_free_space)
2821 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002822 }
Josef Bacik25179202008-10-29 14:49:05 -04002823
Yan, Zhengf0486c62010-05-16 10:46:25 -04002824 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002825 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002826 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002827
Josef Bacikc759c4e2015-10-02 15:25:10 -04002828 /*
2829 * If this space cluster has been marked as fragmented and we've
2830 * unpinned enough in this block group to potentially allow a
2831 * cluster to be created inside of it go ahead and clear the
2832 * fragmented check.
2833 */
2834 if (cluster && cluster->fragmented &&
2835 total_unpinned > empty_cluster) {
2836 spin_lock(&cluster->lock);
2837 cluster->fragmented = 0;
2838 spin_unlock(&cluster->lock);
2839 }
2840
Josef Bacik7b398f82012-10-22 15:52:28 -04002841 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002842 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002843 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002844 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacikc51e7bb2016-03-25 13:25:54 -04002845
2846 trace_btrfs_space_reservation(fs_info, "pinned",
2847 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04002848 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002849 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2850 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002851 if (cache->ro) {
2852 space_info->bytes_readonly += len;
2853 readonly = true;
2854 }
Josef Bacik25179202008-10-29 14:49:05 -04002855 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002856 if (!readonly && return_free_space &&
2857 global_rsv->space_info == space_info) {
2858 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002859
Josef Bacik7b398f82012-10-22 15:52:28 -04002860 spin_lock(&global_rsv->lock);
2861 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002862 to_add = min(len, global_rsv->size -
2863 global_rsv->reserved);
2864 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002865 btrfs_space_info_update_bytes_may_use(fs_info,
2866 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002867 if (global_rsv->reserved >= global_rsv->size)
2868 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002869 trace_btrfs_space_reservation(fs_info,
2870 "space_info",
2871 space_info->flags,
2872 to_add, 1);
2873 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002874 }
2875 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002876 /* Add to any tickets we may have */
2877 if (len)
Josef Bacikd44b72a2019-06-18 16:09:18 -04002878 btrfs_space_info_add_new_bytes(fs_info,
2879 space_info, len);
Josef Bacik7b398f82012-10-22 15:52:28 -04002880 }
2881 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002882 }
2883
2884 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002885 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002886 return 0;
2887}
2888
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002889int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002890{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002891 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002892 struct btrfs_block_group_cache *block_group, *tmp;
2893 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002894 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002895 u64 start;
2896 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002897 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002898
Yan Zheng11833d62009-09-11 16:11:19 -04002899 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2900 unpin = &fs_info->freed_extents[1];
2901 else
2902 unpin = &fs_info->freed_extents[0];
2903
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002904 while (!trans->aborted) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002905 struct extent_state *cached_state = NULL;
2906
Filipe Mananad4b450c2015-01-29 19:18:25 +00002907 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002908 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002909 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002910 if (ret) {
2911 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002912 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002913 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002914
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002916 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002917 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002918
Filipe Manana0e6ec382018-11-16 13:04:44 +00002919 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002920 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002921 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002922 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002923 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002924 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002925
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002926 /*
2927 * Transaction is finished. We don't need the lock anymore. We
2928 * do need to clean up the block groups in case of a transaction
2929 * abort.
2930 */
2931 deleted_bgs = &trans->transaction->deleted_bgs;
2932 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2933 u64 trimmed = 0;
2934
2935 ret = -EROFS;
2936 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002937 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002938 block_group->key.objectid,
2939 block_group->key.offset,
2940 &trimmed);
2941
2942 list_del_init(&block_group->bg_list);
2943 btrfs_put_block_group_trimming(block_group);
2944 btrfs_put_block_group(block_group);
2945
2946 if (ret) {
2947 const char *errstr = btrfs_decode_error(ret);
2948 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002949 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002950 ret, errstr);
2951 }
2952 }
2953
Chris Masone20d96d2007-03-22 12:13:20 -04002954 return 0;
2955}
2956
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002957static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002958 struct btrfs_delayed_ref_node *node, u64 parent,
2959 u64 root_objectid, u64 owner_objectid,
2960 u64 owner_offset, int refs_to_drop,
2961 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002962{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002963 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002964 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002966 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002967 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002968 struct btrfs_extent_item *ei;
2969 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002970 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002971 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002972 int extent_slot = 0;
2973 int found_extent = 0;
2974 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 u32 item_size;
2976 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002977 u64 bytenr = node->bytenr;
2978 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002979 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002980 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002981
Chris Mason5caf2a02007-04-02 11:20:42 -04002982 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002983 if (!path)
2984 return -ENOMEM;
2985
David Sterbae4058b52015-11-27 16:31:35 +01002986 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002987 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002988
2989 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2990 BUG_ON(!is_data && refs_to_drop != 1);
2991
Josef Bacik3173a182013-03-07 14:22:04 -05002992 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02002993 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05002994
Nikolay Borisovfbe48012018-06-20 15:48:52 +03002995 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
2996 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05002998 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002999 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 while (extent_slot >= 0) {
3001 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003002 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003003 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003004 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3006 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003007 found_extent = 1;
3008 break;
3009 }
Josef Bacik3173a182013-03-07 14:22:04 -05003010 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3011 key.offset == owner_objectid) {
3012 found_extent = 1;
3013 break;
3014 }
Chris Mason952fcca2008-02-18 16:33:44 -05003015 if (path->slots[0] - extent_slot > 5)
3016 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003018 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003019
Zheng Yan31840ae2008-09-23 13:14:14 -04003020 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003021 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003022 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003023 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003024 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003025 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003026 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003027 goto out;
3028 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003029 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003030 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003031
3032 key.objectid = bytenr;
3033 key.type = BTRFS_EXTENT_ITEM_KEY;
3034 key.offset = num_bytes;
3035
Josef Bacik3173a182013-03-07 14:22:04 -05003036 if (!is_data && skinny_metadata) {
3037 key.type = BTRFS_METADATA_ITEM_KEY;
3038 key.offset = owner_objectid;
3039 }
3040
Zheng Yan31840ae2008-09-23 13:14:14 -04003041 ret = btrfs_search_slot(trans, extent_root,
3042 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003043 if (ret > 0 && skinny_metadata && path->slots[0]) {
3044 /*
3045 * Couldn't find our skinny metadata item,
3046 * see if we have ye olde extent item.
3047 */
3048 path->slots[0]--;
3049 btrfs_item_key_to_cpu(path->nodes[0], &key,
3050 path->slots[0]);
3051 if (key.objectid == bytenr &&
3052 key.type == BTRFS_EXTENT_ITEM_KEY &&
3053 key.offset == num_bytes)
3054 ret = 0;
3055 }
3056
3057 if (ret > 0 && skinny_metadata) {
3058 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003059 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003060 key.type = BTRFS_EXTENT_ITEM_KEY;
3061 key.offset = num_bytes;
3062 btrfs_release_path(path);
3063 ret = btrfs_search_slot(trans, extent_root,
3064 &key, path, -1, 1);
3065 }
3066
Josef Bacikf3465ca2008-11-12 14:19:50 -05003067 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003068 btrfs_err(info,
3069 "umm, got %d back from search, was looking for %llu",
3070 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003071 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003072 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003073 }
David Sterba005d6422012-09-18 07:52:32 -06003074 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003075 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003076 goto out;
3077 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003078 extent_slot = path->slots[0];
3079 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303080 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003081 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003082 btrfs_err(info,
3083 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003084 bytenr, parent, root_objectid, owner_objectid,
3085 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003087 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003088 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003089 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003090 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003091 }
Chris Mason5f39d392007-10-15 16:14:19 -04003092
3093 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003094 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003095 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003096 ret = -EINVAL;
3097 btrfs_print_v0_err(info);
3098 btrfs_abort_transaction(trans, ret);
3099 goto out;
3100 }
Chris Mason952fcca2008-02-18 16:33:44 -05003101 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003102 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003103 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3104 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 struct btrfs_tree_block_info *bi;
3106 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3107 bi = (struct btrfs_tree_block_info *)(ei + 1);
3108 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003109 }
3110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003112 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003113 btrfs_err(info,
3114 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3115 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003116 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003117 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003118 goto out;
3119 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003120 refs -= refs_to_drop;
3121
3122 if (refs > 0) {
3123 if (extent_op)
3124 __run_delayed_extent_op(extent_op, leaf, ei);
3125 /*
3126 * In the case of inline back ref, reference count will
3127 * be updated by remove_extent_backref
3128 */
3129 if (iref) {
3130 BUG_ON(!found_extent);
3131 } else {
3132 btrfs_set_extent_refs(leaf, ei, refs);
3133 btrfs_mark_buffer_dirty(leaf);
3134 }
3135 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003136 ret = remove_extent_backref(trans, path, iref,
3137 refs_to_drop, is_data,
3138 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003139 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003140 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003141 goto out;
3142 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003143 }
3144 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003145 if (found_extent) {
3146 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003147 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003148 if (iref) {
3149 BUG_ON(path->slots[0] != extent_slot);
3150 } else {
3151 BUG_ON(path->slots[0] != extent_slot + 1);
3152 path->slots[0] = extent_slot;
3153 num_to_del = 2;
3154 }
Chris Mason78fae272007-03-25 11:35:08 -04003155 }
Chris Masonb9473432009-03-13 11:00:37 -04003156
Josef Bacikfcebe452014-05-13 17:30:47 -07003157 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003158 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3159 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003160 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003161 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003162 goto out;
3163 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003164 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003166 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003167 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003168 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003169 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003170 goto out;
3171 }
Chris Mason459931e2008-12-10 09:10:46 -05003172 }
3173
Nikolay Borisove7355e52018-05-10 15:44:55 +03003174 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003175 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003176 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003177 goto out;
3178 }
3179
Josef Bacikade4b512019-06-20 15:38:01 -04003180 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003181 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003182 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003183 goto out;
3184 }
Chris Masona28ec192007-03-06 20:08:01 -05003185 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003186 btrfs_release_path(path);
3187
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003188out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003189 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003190 return ret;
3191}
3192
3193/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003194 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003195 * delayed ref for that extent as well. This searches the delayed ref tree for
3196 * a given extent, and if there are no other delayed refs to be processed, it
3197 * removes it from the tree.
3198 */
3199static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003200 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003201{
3202 struct btrfs_delayed_ref_head *head;
3203 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003204 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003205
3206 delayed_refs = &trans->transaction->delayed_refs;
3207 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003208 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003209 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003210 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003211
Josef Bacikd7df2c72014-01-23 09:21:38 -05003212 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003213 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003214 goto out;
3215
Josef Bacikbedc66172018-12-03 10:20:31 -05003216 if (cleanup_extent_op(head) != NULL)
3217 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003218
Chris Mason1887be62009-03-13 10:11:24 -04003219 /*
3220 * waiting for the lock here would deadlock. If someone else has it
3221 * locked they are already in the process of dropping it anyway
3222 */
3223 if (!mutex_trylock(&head->mutex))
3224 goto out;
3225
Josef Bacikd7baffd2018-12-03 10:20:29 -05003226 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003227 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003228
Josef Bacikd7df2c72014-01-23 09:21:38 -05003229 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003230 spin_unlock(&delayed_refs->lock);
3231
Yan, Zhengf0486c62010-05-16 10:46:25 -04003232 BUG_ON(head->extent_op);
3233 if (head->must_insert_reserved)
3234 ret = 1;
3235
Josef Bacik31890da2018-11-21 14:05:41 -05003236 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003237 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003238 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003239 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003240out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003241 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003242
3243out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003244 spin_unlock(&delayed_refs->lock);
3245 return 0;
3246}
3247
Yan, Zhengf0486c62010-05-16 10:46:25 -04003248void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3249 struct btrfs_root *root,
3250 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003251 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003253 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003254 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003255 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003256 int ret;
3257
Qu Wenruoed4f2552019-04-04 14:45:31 +08003258 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3259 buf->start, buf->len, parent);
3260 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3261 root->root_key.objectid);
3262
Yan, Zhengf0486c62010-05-16 10:46:25 -04003263 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003264 int old_ref_mod, new_ref_mod;
3265
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003266 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003267 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003268 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003269 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003270 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003271 }
3272
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003273 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00003274 struct btrfs_block_group_cache *cache;
3275
Yan, Zhengf0486c62010-05-16 10:46:25 -04003276 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003277 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003278 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003279 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003280 }
3281
Omar Sandoval4da8b762017-06-06 16:45:28 -07003282 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003283 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003284
Yan, Zhengf0486c62010-05-16 10:46:25 -04003285 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
David Sterbafdf08602019-03-20 12:12:32 +01003286 pin_down_extent(cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003287 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003288 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003289 }
3290
3291 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3292
3293 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003294 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003295 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003296 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003297 }
3298out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003299 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003300 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003301
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003302 if (last_ref) {
3303 /*
3304 * Deleting the buffer, clear the corrupt flag since it doesn't
3305 * matter anymore.
3306 */
3307 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3308 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003309}
3310
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003311/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003312int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003313{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003314 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003315 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003316 int ret;
3317
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003318 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003319 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003320
Chris Mason56bec292009-03-13 10:10:06 -04003321 /*
3322 * tree log blocks never actually go into the extent allocation
3323 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003324 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003325 if ((ref->type == BTRFS_REF_METADATA &&
3326 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3327 (ref->type == BTRFS_REF_DATA &&
3328 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003329 /* unlocks the pinned mutex */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003330 btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003331 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003332 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003333 } else if (ref->type == BTRFS_REF_METADATA) {
3334 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003335 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003337 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003338 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003339 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003340
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003341 if (!((ref->type == BTRFS_REF_METADATA &&
3342 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3343 (ref->type == BTRFS_REF_DATA &&
3344 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3345 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003346
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003347 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003348 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003349
Chris Mason925baed2008-06-25 16:01:30 -04003350 return ret;
3351}
3352
Josef Bacik817d52f2009-07-13 21:29:25 -04003353enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003354 LOOP_CACHING_NOWAIT,
3355 LOOP_CACHING_WAIT,
3356 LOOP_ALLOC_CHUNK,
3357 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003358};
3359
Miao Xiee570fd22014-06-19 10:42:50 +08003360static inline void
3361btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
3362 int delalloc)
3363{
3364 if (delalloc)
3365 down_read(&cache->data_rwsem);
3366}
3367
3368static inline void
3369btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
3370 int delalloc)
3371{
3372 btrfs_get_block_group(cache);
3373 if (delalloc)
3374 down_read(&cache->data_rwsem);
3375}
3376
3377static struct btrfs_block_group_cache *
3378btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
3379 struct btrfs_free_cluster *cluster,
3380 int delalloc)
3381{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05303382 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003383
Miao Xiee570fd22014-06-19 10:42:50 +08003384 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003385 while (1) {
3386 used_bg = cluster->block_group;
3387 if (!used_bg)
3388 return NULL;
3389
3390 if (used_bg == block_group)
3391 return used_bg;
3392
3393 btrfs_get_block_group(used_bg);
3394
3395 if (!delalloc)
3396 return used_bg;
3397
3398 if (down_read_trylock(&used_bg->data_rwsem))
3399 return used_bg;
3400
3401 spin_unlock(&cluster->refill_lock);
3402
Liu Boe321f8a2016-11-30 16:11:04 -08003403 /* We should only have one-level nested. */
3404 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003405
3406 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003407 if (used_bg == cluster->block_group)
3408 return used_bg;
3409
3410 up_read(&used_bg->data_rwsem);
3411 btrfs_put_block_group(used_bg);
3412 }
Miao Xiee570fd22014-06-19 10:42:50 +08003413}
3414
3415static inline void
3416btrfs_release_block_group(struct btrfs_block_group_cache *cache,
3417 int delalloc)
3418{
3419 if (delalloc)
3420 up_read(&cache->data_rwsem);
3421 btrfs_put_block_group(cache);
3422}
3423
Josef Bacik817d52f2009-07-13 21:29:25 -04003424/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003425 * Structure used internally for find_free_extent() function. Wraps needed
3426 * parameters.
3427 */
3428struct find_free_extent_ctl {
3429 /* Basic allocation info */
3430 u64 ram_bytes;
3431 u64 num_bytes;
3432 u64 empty_size;
3433 u64 flags;
3434 int delalloc;
3435
3436 /* Where to start the search inside the bg */
3437 u64 search_start;
3438
3439 /* For clustered allocation */
3440 u64 empty_cluster;
3441
3442 bool have_caching_bg;
3443 bool orig_have_caching_bg;
3444
3445 /* RAID index, converted from flags */
3446 int index;
3447
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003448 /*
3449 * Current loop number, check find_free_extent_update_loop() for details
3450 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003451 int loop;
3452
3453 /*
3454 * Whether we're refilling a cluster, if true we need to re-search
3455 * current block group but don't try to refill the cluster again.
3456 */
3457 bool retry_clustered;
3458
3459 /*
3460 * Whether we're updating free space cache, if true we need to re-search
3461 * current block group but don't try updating free space cache again.
3462 */
3463 bool retry_unclustered;
3464
3465 /* If current block group is cached */
3466 int cached;
3467
3468 /* Max contiguous hole found */
3469 u64 max_extent_size;
3470
3471 /* Total free space from free space cache, not always contiguous */
3472 u64 total_free_space;
3473
3474 /* Found result */
3475 u64 found_offset;
3476};
3477
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003478
3479/*
3480 * Helper function for find_free_extent().
3481 *
3482 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3483 * Return -EAGAIN to inform caller that we need to re-search this block group
3484 * Return >0 to inform caller that we find nothing
3485 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3486 */
3487static int find_free_extent_clustered(struct btrfs_block_group_cache *bg,
3488 struct btrfs_free_cluster *last_ptr,
3489 struct find_free_extent_ctl *ffe_ctl,
3490 struct btrfs_block_group_cache **cluster_bg_ret)
3491{
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003492 struct btrfs_block_group_cache *cluster_bg;
3493 u64 aligned_cluster;
3494 u64 offset;
3495 int ret;
3496
3497 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3498 if (!cluster_bg)
3499 goto refill_cluster;
3500 if (cluster_bg != bg && (cluster_bg->ro ||
3501 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3502 goto release_cluster;
3503
3504 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
3505 ffe_ctl->num_bytes, cluster_bg->key.objectid,
3506 &ffe_ctl->max_extent_size);
3507 if (offset) {
3508 /* We have a block, we're done */
3509 spin_unlock(&last_ptr->refill_lock);
3510 trace_btrfs_reserve_extent_cluster(cluster_bg,
3511 ffe_ctl->search_start, ffe_ctl->num_bytes);
3512 *cluster_bg_ret = cluster_bg;
3513 ffe_ctl->found_offset = offset;
3514 return 0;
3515 }
3516 WARN_ON(last_ptr->block_group != cluster_bg);
3517
3518release_cluster:
3519 /*
3520 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3521 * lets just skip it and let the allocator find whatever block it can
3522 * find. If we reach this point, we will have tried the cluster
3523 * allocator plenty of times and not have found anything, so we are
3524 * likely way too fragmented for the clustering stuff to find anything.
3525 *
3526 * However, if the cluster is taken from the current block group,
3527 * release the cluster first, so that we stand a better chance of
3528 * succeeding in the unclustered allocation.
3529 */
3530 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3531 spin_unlock(&last_ptr->refill_lock);
3532 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3533 return -ENOENT;
3534 }
3535
3536 /* This cluster didn't work out, free it and start over */
3537 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3538
3539 if (cluster_bg != bg)
3540 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3541
3542refill_cluster:
3543 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3544 spin_unlock(&last_ptr->refill_lock);
3545 return -ENOENT;
3546 }
3547
3548 aligned_cluster = max_t(u64,
3549 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3550 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003551 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3552 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003553 if (ret == 0) {
3554 /* Now pull our allocation out of this cluster */
3555 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3556 ffe_ctl->num_bytes, ffe_ctl->search_start,
3557 &ffe_ctl->max_extent_size);
3558 if (offset) {
3559 /* We found one, proceed */
3560 spin_unlock(&last_ptr->refill_lock);
3561 trace_btrfs_reserve_extent_cluster(bg,
3562 ffe_ctl->search_start,
3563 ffe_ctl->num_bytes);
3564 ffe_ctl->found_offset = offset;
3565 return 0;
3566 }
3567 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3568 !ffe_ctl->retry_clustered) {
3569 spin_unlock(&last_ptr->refill_lock);
3570
3571 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003572 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003573 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3574 return -EAGAIN;
3575 }
3576 /*
3577 * At this point we either didn't find a cluster or we weren't able to
3578 * allocate a block from our cluster. Free the cluster we've been
3579 * trying to use, and go to the next block group.
3580 */
3581 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3582 spin_unlock(&last_ptr->refill_lock);
3583 return 1;
3584}
3585
Qu Wenruob4bd7452018-11-02 09:39:47 +08003586/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003587 * Return >0 to inform caller that we find nothing
3588 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3589 * Return -EAGAIN to inform caller that we need to re-search this block group
3590 */
3591static int find_free_extent_unclustered(struct btrfs_block_group_cache *bg,
3592 struct btrfs_free_cluster *last_ptr,
3593 struct find_free_extent_ctl *ffe_ctl)
3594{
3595 u64 offset;
3596
3597 /*
3598 * We are doing an unclustered allocation, set the fragmented flag so
3599 * we don't bother trying to setup a cluster again until we get more
3600 * space.
3601 */
3602 if (unlikely(last_ptr)) {
3603 spin_lock(&last_ptr->lock);
3604 last_ptr->fragmented = 1;
3605 spin_unlock(&last_ptr->lock);
3606 }
3607 if (ffe_ctl->cached) {
3608 struct btrfs_free_space_ctl *free_space_ctl;
3609
3610 free_space_ctl = bg->free_space_ctl;
3611 spin_lock(&free_space_ctl->tree_lock);
3612 if (free_space_ctl->free_space <
3613 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3614 ffe_ctl->empty_size) {
3615 ffe_ctl->total_free_space = max_t(u64,
3616 ffe_ctl->total_free_space,
3617 free_space_ctl->free_space);
3618 spin_unlock(&free_space_ctl->tree_lock);
3619 return 1;
3620 }
3621 spin_unlock(&free_space_ctl->tree_lock);
3622 }
3623
3624 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3625 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3626 &ffe_ctl->max_extent_size);
3627
3628 /*
3629 * If we didn't find a chunk, and we haven't failed on this block group
3630 * before, and this block group is in the middle of caching and we are
3631 * ok with waiting, then go ahead and wait for progress to be made, and
3632 * set @retry_unclustered to true.
3633 *
3634 * If @retry_unclustered is true then we've already waited on this
3635 * block group once and should move on to the next block group.
3636 */
3637 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3638 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003639 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3640 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003641 ffe_ctl->retry_unclustered = true;
3642 return -EAGAIN;
3643 } else if (!offset) {
3644 return 1;
3645 }
3646 ffe_ctl->found_offset = offset;
3647 return 0;
3648}
3649
3650/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003651 * Return >0 means caller needs to re-search for free extent
3652 * Return 0 means we have the needed free extent.
3653 * Return <0 means we failed to locate any free extent.
3654 */
3655static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3656 struct btrfs_free_cluster *last_ptr,
3657 struct btrfs_key *ins,
3658 struct find_free_extent_ctl *ffe_ctl,
3659 int full_search, bool use_cluster)
3660{
3661 struct btrfs_root *root = fs_info->extent_root;
3662 int ret;
3663
3664 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3665 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3666 ffe_ctl->orig_have_caching_bg = true;
3667
3668 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3669 ffe_ctl->have_caching_bg)
3670 return 1;
3671
3672 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3673 return 1;
3674
3675 if (ins->objectid) {
3676 if (!use_cluster && last_ptr) {
3677 spin_lock(&last_ptr->lock);
3678 last_ptr->window_start = ins->objectid;
3679 spin_unlock(&last_ptr->lock);
3680 }
3681 return 0;
3682 }
3683
3684 /*
3685 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3686 * caching kthreads as we move along
3687 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3688 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3689 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3690 * again
3691 */
3692 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3693 ffe_ctl->index = 0;
3694 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3695 /*
3696 * We want to skip the LOOP_CACHING_WAIT step if we
3697 * don't have any uncached bgs and we've already done a
3698 * full search through.
3699 */
3700 if (ffe_ctl->orig_have_caching_bg || !full_search)
3701 ffe_ctl->loop = LOOP_CACHING_WAIT;
3702 else
3703 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3704 } else {
3705 ffe_ctl->loop++;
3706 }
3707
3708 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3709 struct btrfs_trans_handle *trans;
3710 int exist = 0;
3711
3712 trans = current->journal_info;
3713 if (trans)
3714 exist = 1;
3715 else
3716 trans = btrfs_join_transaction(root);
3717
3718 if (IS_ERR(trans)) {
3719 ret = PTR_ERR(trans);
3720 return ret;
3721 }
3722
Josef Bacikfc471cb2019-06-18 16:09:17 -04003723 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3724 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003725
3726 /*
3727 * If we can't allocate a new chunk we've already looped
3728 * through at least once, move on to the NO_EMPTY_SIZE
3729 * case.
3730 */
3731 if (ret == -ENOSPC)
3732 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3733
3734 /* Do not bail out on ENOSPC since we can do more. */
3735 if (ret < 0 && ret != -ENOSPC)
3736 btrfs_abort_transaction(trans, ret);
3737 else
3738 ret = 0;
3739 if (!exist)
3740 btrfs_end_transaction(trans);
3741 if (ret)
3742 return ret;
3743 }
3744
3745 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3746 /*
3747 * Don't loop again if we already have no empty_size and
3748 * no empty_cluster.
3749 */
3750 if (ffe_ctl->empty_size == 0 &&
3751 ffe_ctl->empty_cluster == 0)
3752 return -ENOSPC;
3753 ffe_ctl->empty_size = 0;
3754 ffe_ctl->empty_cluster = 0;
3755 }
3756 return 1;
3757 }
3758 return -ENOSPC;
3759}
3760
3761/*
Chris Masonfec577f2007-02-26 10:40:21 -05003762 * walks the btree of allocated extents and find a hole of a given size.
3763 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003764 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003765 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003766 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003767 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003768 *
3769 * If there is no suitable free space, we will record the max size of
3770 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003771 *
3772 * The overall logic and call chain:
3773 *
3774 * find_free_extent()
3775 * |- Iterate through all block groups
3776 * | |- Get a valid block group
3777 * | |- Try to do clustered allocation in that block group
3778 * | |- Try to do unclustered allocation in that block group
3779 * | |- Check if the result is valid
3780 * | | |- If valid, then exit
3781 * | |- Jump to next block group
3782 * |
3783 * |- Push harder to find free extents
3784 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003785 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003786static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003787 u64 ram_bytes, u64 num_bytes, u64 empty_size,
3788 u64 hint_byte, struct btrfs_key *ins,
3789 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003790{
Josef Bacik80eb2342008-10-29 14:49:05 -04003791 int ret = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003792 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04003793 struct btrfs_block_group_cache *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003794 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003795 struct btrfs_space_info *space_info;
Josef Bacik67377732010-09-16 16:19:09 -04003796 bool use_cluster = true;
Josef Bacika5e681d2015-10-01 14:54:10 -04003797 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003799 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003800
3801 ffe_ctl.ram_bytes = ram_bytes;
3802 ffe_ctl.num_bytes = num_bytes;
3803 ffe_ctl.empty_size = empty_size;
3804 ffe_ctl.flags = flags;
3805 ffe_ctl.search_start = 0;
3806 ffe_ctl.retry_clustered = false;
3807 ffe_ctl.retry_unclustered = false;
3808 ffe_ctl.delalloc = delalloc;
3809 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3810 ffe_ctl.have_caching_bg = false;
3811 ffe_ctl.orig_have_caching_bg = false;
3812 ffe_ctl.found_offset = 0;
3813
David Sterba962a2982014-06-04 18:41:45 +02003814 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003815 ins->objectid = 0;
3816 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003817
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003818 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003819
Josef Bacik280c29082019-06-18 16:09:19 -04003820 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003821 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003822 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003823 return -ENOSPC;
3824 }
Josef Bacik2552d172009-04-03 10:14:19 -04003825
Josef Bacik67377732010-09-16 16:19:09 -04003826 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04003827 * If our free space is heavily fragmented we may not be able to make
3828 * big contiguous allocations, so instead of doing the expensive search
3829 * for free space, simply return ENOSPC with our max_extent_size so we
3830 * can go ahead and search for a more manageable chunk.
3831 *
3832 * If our max_extent_size is large enough for our allocation simply
3833 * disable clustering since we will likely not be able to find enough
3834 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04003835 */
Josef Bacik4f4db212015-09-29 11:40:47 -04003836 if (unlikely(space_info->max_extent_size)) {
3837 spin_lock(&space_info->lock);
3838 if (space_info->max_extent_size &&
3839 num_bytes > space_info->max_extent_size) {
3840 ins->offset = space_info->max_extent_size;
3841 spin_unlock(&space_info->lock);
3842 return -ENOSPC;
3843 } else if (space_info->max_extent_size) {
3844 use_cluster = false;
3845 }
3846 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003847 }
3848
Qu Wenruob4bd7452018-11-02 09:39:47 +08003849 last_ptr = fetch_cluster_info(fs_info, space_info,
3850 &ffe_ctl.empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04003851 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003852 spin_lock(&last_ptr->lock);
3853 if (last_ptr->block_group)
3854 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003855 if (last_ptr->fragmented) {
3856 /*
3857 * We still set window_start so we can keep track of the
3858 * last place we found an allocation to try and save
3859 * some time.
3860 */
3861 hint_byte = last_ptr->window_start;
3862 use_cluster = false;
3863 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003864 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003865 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003866
Qu Wenruob4bd7452018-11-02 09:39:47 +08003867 ffe_ctl.search_start = max(ffe_ctl.search_start,
3868 first_logical_byte(fs_info, 0));
3869 ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte);
3870 if (ffe_ctl.search_start == hint_byte) {
3871 block_group = btrfs_lookup_block_group(fs_info,
3872 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003873 /*
3874 * we don't want to use the block group if it doesn't match our
3875 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003876 *
3877 * However if we are re-searching with an ideal block group
3878 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003879 */
David Sterbab6919a52013-04-29 13:39:40 +00003880 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003881 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003882 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003883 if (list_empty(&block_group->list) ||
3884 block_group->ro) {
3885 /*
3886 * someone is removing this block group,
3887 * we can't jump into the have_block_group
3888 * target because our list pointers are not
3889 * valid
3890 */
3891 btrfs_put_block_group(block_group);
3892 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05003893 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003894 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08003895 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08003896 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04003897 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05003898 }
Josef Bacik2552d172009-04-03 10:14:19 -04003899 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003900 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003901 }
Chris Mason42e70e72008-11-07 18:17:11 -05003902 }
Josef Bacik2552d172009-04-03 10:14:19 -04003903search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003904 ffe_ctl.have_caching_bg = false;
3905 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
3906 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04003907 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04003908 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003909 list_for_each_entry(block_group,
3910 &space_info->block_groups[ffe_ctl.index], list) {
Jeff Mahoney14443932017-07-19 23:25:51 -04003911 /* If the block group is read-only, we can skip it entirely. */
3912 if (unlikely(block_group->ro))
3913 continue;
3914
Miao Xiee570fd22014-06-19 10:42:50 +08003915 btrfs_grab_block_group(block_group, delalloc);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003916 ffe_ctl.search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05003917
Chris Mason83a50de2010-12-13 15:06:46 -05003918 /*
3919 * this can happen if we end up cycling through all the
3920 * raid types, but we want to make sure we only allocate
3921 * for the proper type.
3922 */
David Sterbab6919a52013-04-29 13:39:40 +00003923 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07003924 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02003925 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02003926 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05003927 BTRFS_BLOCK_GROUP_RAID10;
3928
3929 /*
3930 * if they asked for extra copies and this block group
3931 * doesn't provide them, bail. This does allow us to
3932 * fill raid0 from raid1.
3933 */
David Sterbab6919a52013-04-29 13:39:40 +00003934 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05003935 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08003936
3937 /*
3938 * This block group has different flags than we want.
3939 * It's possible that we have MIXED_GROUP flag but no
3940 * block group is mixed. Just skip such block group.
3941 */
3942 btrfs_release_block_group(block_group, delalloc);
3943 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05003944 }
3945
Josef Bacik2552d172009-04-03 10:14:19 -04003946have_block_group:
Josef Bacik676f1f72019-06-20 15:37:48 -04003947 ffe_ctl.cached = btrfs_block_group_cache_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003948 if (unlikely(!ffe_ctl.cached)) {
3949 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003950 ret = btrfs_cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04003951 BUG_ON(ret < 0);
3952 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05003953 }
3954
Josef Bacik36cce922013-08-05 11:15:21 -04003955 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
3956 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003957
Josef Bacik0a243252009-09-11 16:11:20 -04003958 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05003959 * Ok we want to try and use the cluster allocator, so
3960 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04003961 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04003962 if (last_ptr && use_cluster) {
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003963 struct btrfs_block_group_cache *cluster_bg = NULL;
Chris Mason44fb5512009-06-04 15:34:51 -04003964
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003965 ret = find_free_extent_clustered(block_group, last_ptr,
3966 &ffe_ctl, &cluster_bg);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05003967
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003968 if (ret == 0) {
3969 if (cluster_bg && cluster_bg != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08003970 btrfs_release_block_group(block_group,
3971 delalloc);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003972 block_group = cluster_bg;
Miao Xie215a63d2014-01-15 20:00:56 +08003973 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003974 goto checks;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003975 } else if (ret == -EAGAIN) {
Josef Bacik817d52f2009-07-13 21:29:25 -04003976 goto have_block_group;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003977 } else if (ret > 0) {
3978 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003979 }
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003980 /* ret == -ENOENT case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003981 }
3982
Qu Wenruoe1a41842018-11-02 09:39:49 +08003983 ret = find_free_extent_unclustered(block_group, last_ptr,
3984 &ffe_ctl);
3985 if (ret == -EAGAIN)
Josef Bacik817d52f2009-07-13 21:29:25 -04003986 goto have_block_group;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003987 else if (ret > 0)
Josef Bacik1cdda9b2009-10-06 10:04:28 -04003988 goto loop;
Qu Wenruoe1a41842018-11-02 09:39:49 +08003989 /* ret == 0 case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04003990checks:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003991 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
3992 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04003993
Josef Bacik2552d172009-04-03 10:14:19 -04003994 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003995 if (ffe_ctl.search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08003996 block_group->key.objectid + block_group->key.offset) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003997 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
3998 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04003999 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004000 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004001
Qu Wenruob4bd7452018-11-02 09:39:47 +08004002 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4003 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4004 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004005
Wang Xiaoguang18513092016-07-25 15:51:40 +08004006 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4007 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004008 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004009 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4010 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004011 goto loop;
4012 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004013 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004014
Josef Bacik2552d172009-04-03 10:14:19 -04004015 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004016 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004017 ins->offset = num_bytes;
4018
Qu Wenruob4bd7452018-11-02 09:39:47 +08004019 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4020 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004021 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004022 break;
4023loop:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004024 ffe_ctl.retry_clustered = false;
4025 ffe_ctl.retry_unclustered = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004026 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
Qu Wenruob4bd7452018-11-02 09:39:47 +08004027 ffe_ctl.index);
Miao Xiee570fd22014-06-19 10:42:50 +08004028 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004029 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004030 }
4031 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004032
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004033 ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl,
4034 full_search, use_cluster);
4035 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004036 goto search;
4037
Josef Bacik4f4db212015-09-29 11:40:47 -04004038 if (ret == -ENOSPC) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004039 /*
4040 * Use ffe_ctl->total_free_space as fallback if we can't find
4041 * any contiguous hole.
4042 */
4043 if (!ffe_ctl.max_extent_size)
4044 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004045 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004046 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004047 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004048 ins->offset = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004049 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004050 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004051}
Chris Masonec44a352008-04-28 15:29:52 -04004052
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004053/*
4054 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4055 * hole that is at least as big as @num_bytes.
4056 *
4057 * @root - The root that will contain this extent
4058 *
4059 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4060 * is used for accounting purposes. This value differs
4061 * from @num_bytes only in the case of compressed extents.
4062 *
4063 * @num_bytes - Number of bytes to allocate on-disk.
4064 *
4065 * @min_alloc_size - Indicates the minimum amount of space that the
4066 * allocator should try to satisfy. In some cases
4067 * @num_bytes may be larger than what is required and if
4068 * the filesystem is fragmented then allocation fails.
4069 * However, the presence of @min_alloc_size gives a
4070 * chance to try and satisfy the smaller allocation.
4071 *
4072 * @empty_size - A hint that you plan on doing more COW. This is the
4073 * size in bytes the allocator should try to find free
4074 * next to the block it returns. This is just a hint and
4075 * may be ignored by the allocator.
4076 *
4077 * @hint_byte - Hint to the allocator to start searching above the byte
4078 * address passed. It might be ignored.
4079 *
4080 * @ins - This key is modified to record the found hole. It will
4081 * have the following values:
4082 * ins->objectid == start position
4083 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4084 * ins->offset == the size of the hole.
4085 *
4086 * @is_data - Boolean flag indicating whether an extent is
4087 * allocated for data (true) or metadata (false)
4088 *
4089 * @delalloc - Boolean flag indicating whether this allocation is for
4090 * delalloc or not. If 'true' data_rwsem of block groups
4091 * is going to be acquired.
4092 *
4093 *
4094 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4095 * case -ENOSPC is returned then @ins->offset will contain the size of the
4096 * largest available hole the allocator managed to find.
4097 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004098int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004099 u64 num_bytes, u64 min_alloc_size,
4100 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004101 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004102{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004103 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004104 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004105 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004106 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004107
Jeff Mahoney1b868262017-05-17 11:38:35 -04004108 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004109again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004110 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004111 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004112 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004113 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004114 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004115 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004116 if (!final_tried && ins->offset) {
4117 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004118 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004119 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004120 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004121 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004122 if (num_bytes == min_alloc_size)
4123 final_tried = true;
4124 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004125 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004126 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004127
Josef Bacik280c29082019-06-18 16:09:19 -04004128 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004129 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004130 "allocation failed flags %llu, wanted %llu",
4131 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004132 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004133 btrfs_dump_space_info(fs_info, sinfo,
4134 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004135 }
Chris Mason925baed2008-06-25 16:01:30 -04004136 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004137
4138 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004139}
4140
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004141static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004142 u64 start, u64 len,
4143 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04004144{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004145 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05004146 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004147
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004148 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004149 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004150 btrfs_err(fs_info, "Unable to find block group for %llu",
4151 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004152 return -ENOSPC;
4153 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004154
Chris Masone688b7252011-10-31 20:52:39 -04004155 if (pin)
David Sterbafdf08602019-03-20 12:12:32 +01004156 pin_down_extent(cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04004157 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004158 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004159 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04004160 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004161 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004162 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04004163 }
Dongsheng Yang31193212014-12-12 16:44:35 +08004164
Chris Masonfa9c0d792009-04-03 09:47:43 -04004165 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004166 return ret;
4167}
4168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004169int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004170 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004171{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004172 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04004173}
4174
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004175int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04004176 u64 start, u64 len)
4177{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004178 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04004179}
4180
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004181static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004182 u64 parent, u64 root_objectid,
4183 u64 flags, u64 owner, u64 offset,
4184 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004185{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004186 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004187 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004188 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004189 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004191 struct extent_buffer *leaf;
4192 int type;
4193 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004195 if (parent > 0)
4196 type = BTRFS_SHARED_DATA_REF_KEY;
4197 else
4198 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004199
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004200 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004201
4202 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004203 if (!path)
4204 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004205
Chris Masonb9473432009-03-13 11:00:37 -04004206 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004207 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4208 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004209 if (ret) {
4210 btrfs_free_path(path);
4211 return ret;
4212 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004213
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004214 leaf = path->nodes[0];
4215 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004216 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4218 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4219 btrfs_set_extent_flags(leaf, extent_item,
4220 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4223 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4224 if (parent > 0) {
4225 struct btrfs_shared_data_ref *ref;
4226 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4227 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4228 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4229 } else {
4230 struct btrfs_extent_data_ref *ref;
4231 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4232 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4233 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4234 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4235 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4236 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004237
4238 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004239 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004240
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004241 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004242 if (ret)
4243 return ret;
4244
Josef Bacikade4b512019-06-20 15:38:01 -04004245 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004247 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004248 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004249 BUG();
4250 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004251 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004252 return ret;
4253}
4254
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004255static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004256 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004257 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004259 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004260 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004261 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004262 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004263 struct btrfs_tree_block_info *block_info;
4264 struct btrfs_extent_inline_ref *iref;
4265 struct btrfs_path *path;
4266 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004267 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004268 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004269 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004270 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004271 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004272
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004273 ref = btrfs_delayed_node_to_tree_ref(node);
4274
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004275 extent_key.objectid = node->bytenr;
4276 if (skinny_metadata) {
4277 extent_key.offset = ref->level;
4278 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4279 num_bytes = fs_info->nodesize;
4280 } else {
4281 extent_key.offset = node->num_bytes;
4282 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004283 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004284 num_bytes = node->num_bytes;
4285 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004286
4287 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004288 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004289 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290
4291 path->leave_spinning = 1;
4292 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004293 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004295 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004296 return ret;
4297 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298
4299 leaf = path->nodes[0];
4300 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4301 struct btrfs_extent_item);
4302 btrfs_set_extent_refs(leaf, extent_item, 1);
4303 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4304 btrfs_set_extent_flags(leaf, extent_item,
4305 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004306
Josef Bacik3173a182013-03-07 14:22:04 -05004307 if (skinny_metadata) {
4308 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4309 } else {
4310 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004311 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004312 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004313 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4314 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004315
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004316 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004317 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4318 btrfs_set_extent_inline_ref_type(leaf, iref,
4319 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004320 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004321 } else {
4322 btrfs_set_extent_inline_ref_type(leaf, iref,
4323 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004324 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325 }
4326
4327 btrfs_mark_buffer_dirty(leaf);
4328 btrfs_free_path(path);
4329
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004330 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4331 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004332 if (ret)
4333 return ret;
4334
Josef Bacikade4b512019-06-20 15:38:01 -04004335 ret = btrfs_update_block_group(trans, extent_key.objectid,
4336 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004337 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004338 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004339 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004340 BUG();
4341 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004342
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004343 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004345 return ret;
4346}
4347
4348int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004349 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004350 u64 offset, u64 ram_bytes,
4351 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352{
Qu Wenruo76675592019-04-04 14:45:32 +08004353 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004355
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004356 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004357
Qu Wenruo76675592019-04-04 14:45:32 +08004358 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4359 ins->objectid, ins->offset, 0);
4360 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004361 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004362 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4363 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 return ret;
4365}
Chris Masone02119d2008-09-05 16:13:11 -04004366
4367/*
4368 * this is used by the tree logging recovery code. It records that
4369 * an extent has been allocated and makes sure to clear the free
4370 * space cache bits as well
4371 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004372int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004373 u64 root_objectid, u64 owner, u64 offset,
4374 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004375{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004376 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004377 int ret;
4378 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004379 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004380
4381 /*
4382 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004383 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004384 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004386 ret = __exclude_logged_extent(fs_info, ins->objectid,
4387 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004388 if (ret)
4389 return ret;
4390 }
Chris Masone02119d2008-09-05 16:13:11 -04004391
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004392 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004393 if (!block_group)
4394 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004395
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004396 space_info = block_group->space_info;
4397 spin_lock(&space_info->lock);
4398 spin_lock(&block_group->lock);
4399 space_info->bytes_reserved += ins->offset;
4400 block_group->reserved += ins->offset;
4401 spin_unlock(&block_group->lock);
4402 spin_unlock(&space_info->lock);
4403
Nikolay Borisovef89b822018-06-20 15:48:58 +03004404 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4405 offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004406 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004407 return ret;
4408}
4409
Eric Sandeen48a3b632013-04-25 20:41:01 +00004410static struct extent_buffer *
4411btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004412 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004414 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004415 struct extent_buffer *buf;
4416
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004417 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004418 if (IS_ERR(buf))
4419 return buf;
4420
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004421 /*
4422 * Extra safety check in case the extent tree is corrupted and extent
4423 * allocator chooses to use a tree block which is already used and
4424 * locked.
4425 */
4426 if (buf->lock_owner == current->pid) {
4427 btrfs_err_rl(fs_info,
4428"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4429 buf->start, btrfs_header_owner(buf), current->pid);
4430 free_extent_buffer(buf);
4431 return ERR_PTR(-EUCLEAN);
4432 }
4433
Chris Mason85d4e462011-07-26 16:11:19 -04004434 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004435 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004436 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004437 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004438
David Sterba8bead252018-04-04 02:03:48 +02004439 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004440 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004441
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004442 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4443 btrfs_set_header_level(buf, level);
4444 btrfs_set_header_bytenr(buf, buf->start);
4445 btrfs_set_header_generation(buf, trans->transid);
4446 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4447 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004448 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004449 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004450 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004451 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004452 /*
4453 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004454 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004455 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004456 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004457 set_extent_dirty(&root->dirty_log_pages, buf->start,
4458 buf->start + buf->len - 1, GFP_NOFS);
4459 else
4460 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004461 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004462 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004463 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004464 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4465 buf->start + buf->len - 1, GFP_NOFS);
4466 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004467 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004468 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004469 return buf;
4470}
4471
Chris Masonfec577f2007-02-26 10:40:21 -05004472/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004473 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004474 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004475 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004476struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004477 struct btrfs_root *root,
4478 u64 parent, u64 root_objectid,
4479 const struct btrfs_disk_key *key,
4480 int level, u64 hint,
4481 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004483 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004484 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004485 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004486 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004487 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004488 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004489 u64 flags = 0;
4490 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004491 u32 blocksize = fs_info->nodesize;
4492 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004493
David Sterba05653ef2016-07-15 15:23:37 +02004494#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004495 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004496 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004497 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004498 if (!IS_ERR(buf))
4499 root->alloc_bytenr += blocksize;
4500 return buf;
4501 }
David Sterba05653ef2016-07-15 15:23:37 +02004502#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004503
Josef Bacik67f9c222019-06-19 13:47:23 -04004504 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004505 if (IS_ERR(block_rsv))
4506 return ERR_CAST(block_rsv);
4507
Wang Xiaoguang18513092016-07-25 15:51:40 +08004508 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004509 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004510 if (ret)
4511 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004512
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004513 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4514 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004515 if (IS_ERR(buf)) {
4516 ret = PTR_ERR(buf);
4517 goto out_free_reserved;
4518 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004519
4520 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4521 if (parent == 0)
4522 parent = ins.objectid;
4523 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4524 } else
4525 BUG_ON(parent > 0);
4526
4527 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004528 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004529 if (!extent_op) {
4530 ret = -ENOMEM;
4531 goto out_free_buf;
4532 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004533 if (key)
4534 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4535 else
4536 memset(&extent_op->key, 0, sizeof(extent_op->key));
4537 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004538 extent_op->update_key = skinny_metadata ? false : true;
4539 extent_op->update_flags = true;
4540 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004541 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004542
Qu Wenruoed4f2552019-04-04 14:45:31 +08004543 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4544 ins.objectid, ins.offset, parent);
4545 generic_ref.real_root = root->root_key.objectid;
4546 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004547 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004548 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004549 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004550 if (ret)
4551 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004552 }
Chris Masonfec577f2007-02-26 10:40:21 -05004553 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004554
4555out_free_delayed:
4556 btrfs_free_delayed_extent_op(extent_op);
4557out_free_buf:
4558 free_extent_buffer(buf);
4559out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004560 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004561out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004562 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004563 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004564}
Chris Masona28ec192007-03-06 20:08:01 -05004565
Yan Zheng2c47e6052009-06-27 21:07:35 -04004566struct walk_control {
4567 u64 refs[BTRFS_MAX_LEVEL];
4568 u64 flags[BTRFS_MAX_LEVEL];
4569 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004570 struct btrfs_key drop_progress;
4571 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004572 int stage;
4573 int level;
4574 int shared_level;
4575 int update_ref;
4576 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004577 int reada_slot;
4578 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004579 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004580};
4581
4582#define DROP_REFERENCE 1
4583#define UPDATE_BACKREF 2
4584
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004585static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4586 struct btrfs_root *root,
4587 struct walk_control *wc,
4588 struct btrfs_path *path)
4589{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004590 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004591 u64 bytenr;
4592 u64 generation;
4593 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004594 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004595 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004596 struct btrfs_key key;
4597 struct extent_buffer *eb;
4598 int ret;
4599 int slot;
4600 int nread = 0;
4601
4602 if (path->slots[wc->level] < wc->reada_slot) {
4603 wc->reada_count = wc->reada_count * 2 / 3;
4604 wc->reada_count = max(wc->reada_count, 2);
4605 } else {
4606 wc->reada_count = wc->reada_count * 3 / 2;
4607 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004609 }
4610
4611 eb = path->nodes[wc->level];
4612 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004613
4614 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4615 if (nread >= wc->reada_count)
4616 break;
4617
4618 cond_resched();
4619 bytenr = btrfs_node_blockptr(eb, slot);
4620 generation = btrfs_node_ptr_generation(eb, slot);
4621
4622 if (slot == path->slots[wc->level])
4623 goto reada;
4624
4625 if (wc->stage == UPDATE_BACKREF &&
4626 generation <= root->root_key.offset)
4627 continue;
4628
Yan, Zheng94fcca92009-10-09 09:25:16 -04004629 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004630 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004631 wc->level - 1, 1, &refs,
4632 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004633 /* We don't care about errors in readahead. */
4634 if (ret < 0)
4635 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004636 BUG_ON(refs == 0);
4637
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004638 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004639 if (refs == 1)
4640 goto reada;
4641
Yan, Zheng94fcca92009-10-09 09:25:16 -04004642 if (wc->level == 1 &&
4643 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4644 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004645 if (!wc->update_ref ||
4646 generation <= root->root_key.offset)
4647 continue;
4648 btrfs_node_key_to_cpu(eb, &key, slot);
4649 ret = btrfs_comp_cpu_keys(&key,
4650 &wc->update_progress);
4651 if (ret < 0)
4652 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004653 } else {
4654 if (wc->level == 1 &&
4655 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4656 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004657 }
4658reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004659 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004660 nread++;
4661 }
4662 wc->reada_slot = slot;
4663}
4664
Chris Mason9aca1d52007-03-13 11:09:37 -04004665/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004666 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004667 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004668 * when wc->stage == UPDATE_BACKREF, this function updates
4669 * back refs for pointers in the block.
4670 *
4671 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004672 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004673static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4674 struct btrfs_root *root,
4675 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004676 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004677{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004678 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004679 int level = wc->level;
4680 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004681 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4682 int ret;
4683
4684 if (wc->stage == UPDATE_BACKREF &&
4685 btrfs_header_owner(eb) != root->root_key.objectid)
4686 return 1;
4687
4688 /*
4689 * when reference count of tree block is 1, it won't increase
4690 * again. once full backref flag is set, we never clear it.
4691 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004692 if (lookup_info &&
4693 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4694 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004695 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004696 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004697 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004698 &wc->refs[level],
4699 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004700 BUG_ON(ret == -ENOMEM);
4701 if (ret)
4702 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004703 BUG_ON(wc->refs[level] == 0);
4704 }
4705
Yan Zheng2c47e6052009-06-27 21:07:35 -04004706 if (wc->stage == DROP_REFERENCE) {
4707 if (wc->refs[level] > 1)
4708 return 1;
4709
4710 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004711 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004712 path->locks[level] = 0;
4713 }
4714 return 0;
4715 }
4716
4717 /* wc->stage == UPDATE_BACKREF */
4718 if (!(wc->flags[level] & flag)) {
4719 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004720 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004721 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004722 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004723 BUG_ON(ret); /* -ENOMEM */
David Sterbaf5c8daa2019-03-20 11:43:36 +01004724 ret = btrfs_set_disk_extent_flags(trans, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004725 eb->len, flag,
4726 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004728 wc->flags[level] |= flag;
4729 }
4730
4731 /*
4732 * the block is shared by multiple trees, so it's not good to
4733 * keep the tree lock
4734 */
4735 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004736 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004737 path->locks[level] = 0;
4738 }
4739 return 0;
4740}
4741
4742/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004743 * This is used to verify a ref exists for this root to deal with a bug where we
4744 * would have a drop_progress key that hadn't been updated properly.
4745 */
4746static int check_ref_exists(struct btrfs_trans_handle *trans,
4747 struct btrfs_root *root, u64 bytenr, u64 parent,
4748 int level)
4749{
4750 struct btrfs_path *path;
4751 struct btrfs_extent_inline_ref *iref;
4752 int ret;
4753
4754 path = btrfs_alloc_path();
4755 if (!path)
4756 return -ENOMEM;
4757
4758 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4759 root->fs_info->nodesize, parent,
4760 root->root_key.objectid, level, 0);
4761 btrfs_free_path(path);
4762 if (ret == -ENOENT)
4763 return 0;
4764 if (ret < 0)
4765 return ret;
4766 return 1;
4767}
4768
4769/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004770 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004771 *
4772 * when wc->stage == DROP_REFERENCE, this function checks
4773 * reference count of the block pointed to. if the block
4774 * is shared and we need update back refs for the subtree
4775 * rooted at the block, this function changes wc->stage to
4776 * UPDATE_BACKREF. if the block is shared and there is no
4777 * need to update back, this function drops the reference
4778 * to the block.
4779 *
4780 * NOTE: return value 1 means we should stop walking down.
4781 */
4782static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4783 struct btrfs_root *root,
4784 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004785 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004786{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004788 u64 bytenr;
4789 u64 generation;
4790 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004791 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004792 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004793 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004794 struct extent_buffer *next;
4795 int level = wc->level;
4796 int reada = 0;
4797 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004798 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004799
4800 generation = btrfs_node_ptr_generation(path->nodes[level],
4801 path->slots[level]);
4802 /*
4803 * if the lower level block was created before the snapshot
4804 * was created, we know there is no need to update back refs
4805 * for the subtree
4806 */
4807 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004808 generation <= root->root_key.offset) {
4809 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004810 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004811 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004812
4813 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004814 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4815 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004816
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004817 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004818 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004819 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004820 if (IS_ERR(next))
4821 return PTR_ERR(next);
4822
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004823 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4824 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004825 reada = 1;
4826 }
4827 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004828 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004829
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004830 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004831 &wc->refs[level - 1],
4832 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004833 if (ret < 0)
4834 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004835
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004836 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004837 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004838 ret = -EIO;
4839 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004840 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004841 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004842
Yan, Zheng94fcca92009-10-09 09:25:16 -04004843 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004844 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004845 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004846 if (level == 1 &&
4847 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4848 goto skip;
4849
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004850 if (!wc->update_ref ||
4851 generation <= root->root_key.offset)
4852 goto skip;
4853
4854 btrfs_node_key_to_cpu(path->nodes[level], &key,
4855 path->slots[level]);
4856 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4857 if (ret < 0)
4858 goto skip;
4859
4860 wc->stage = UPDATE_BACKREF;
4861 wc->shared_level = level - 1;
4862 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004863 } else {
4864 if (level == 1 &&
4865 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4866 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004867 }
4868
Chris Masonb9fab912012-05-06 07:23:47 -04004869 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004870 btrfs_tree_unlock(next);
4871 free_extent_buffer(next);
4872 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004873 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004874 }
4875
4876 if (!next) {
4877 if (reada && level == 1)
4878 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004879 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4880 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004881 if (IS_ERR(next)) {
4882 return PTR_ERR(next);
4883 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004884 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004885 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004886 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004887 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004888 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004889 }
4890
4891 level--;
Josef Bacik48672682016-09-23 13:23:28 +02004892 ASSERT(level == btrfs_header_level(next));
4893 if (level != btrfs_header_level(next)) {
4894 btrfs_err(root->fs_info, "mismatched level");
4895 ret = -EIO;
4896 goto out_unlock;
4897 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004898 path->nodes[level] = next;
4899 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004900 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004901 wc->level = level;
4902 if (wc->level == 1)
4903 wc->reada_slot = 0;
4904 return 0;
4905skip:
4906 wc->refs[level - 1] = 0;
4907 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004908 if (wc->stage == DROP_REFERENCE) {
4909 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4910 parent = path->nodes[level]->start;
4911 } else {
Josef Bacik48672682016-09-23 13:23:28 +02004912 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04004913 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02004914 if (root->root_key.objectid !=
4915 btrfs_header_owner(path->nodes[level])) {
4916 btrfs_err(root->fs_info,
4917 "mismatched block owner");
4918 ret = -EIO;
4919 goto out_unlock;
4920 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004921 parent = 0;
4922 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004923
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004924 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05004925 * If we had a drop_progress we need to verify the refs are set
4926 * as expected. If we find our ref then we know that from here
4927 * on out everything should be correct, and we can clear the
4928 * ->restarted flag.
4929 */
4930 if (wc->restarted) {
4931 ret = check_ref_exists(trans, root, bytenr, parent,
4932 level - 1);
4933 if (ret < 0)
4934 goto out_unlock;
4935 if (ret == 0)
4936 goto no_delete;
4937 ret = 0;
4938 wc->restarted = 0;
4939 }
4940
4941 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004942 * Reloc tree doesn't contribute to qgroup numbers, and we have
4943 * already accounted them at merge time (replace_path),
4944 * thus we could skip expensive subtree trace here.
4945 */
4946 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
4947 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08004948 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08004949 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07004950 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004951 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004952 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
4953 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07004954 }
4955 }
Josef Bacikaea6f022019-02-06 15:46:15 -05004956
4957 /*
4958 * We need to update the next key in our walk control so we can
4959 * update the drop_progress key accordingly. We don't care if
4960 * find_next_key doesn't find a key because that means we're at
4961 * the end and are going to clean up now.
4962 */
4963 wc->drop_level = level;
4964 find_next_key(path, level, &wc->drop_progress);
4965
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004966 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
4967 fs_info->nodesize, parent);
4968 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
4969 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02004970 if (ret)
4971 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004972 }
Josef Bacik78c52d92019-02-06 15:46:14 -05004973no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02004974 *lookup_info = 1;
4975 ret = 1;
4976
4977out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004978 btrfs_tree_unlock(next);
4979 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02004980
4981 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004982}
4983
4984/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004985 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004986 *
4987 * when wc->stage == DROP_REFERENCE, this function drops
4988 * reference count on the block.
4989 *
4990 * when wc->stage == UPDATE_BACKREF, this function changes
4991 * wc->stage back to DROP_REFERENCE if we changed wc->stage
4992 * to UPDATE_BACKREF previously while processing the block.
4993 *
4994 * NOTE: return value 1 means we should stop walking up.
4995 */
4996static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
4997 struct btrfs_root *root,
4998 struct btrfs_path *path,
4999 struct walk_control *wc)
5000{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005001 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005002 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005003 int level = wc->level;
5004 struct extent_buffer *eb = path->nodes[level];
5005 u64 parent = 0;
5006
5007 if (wc->stage == UPDATE_BACKREF) {
5008 BUG_ON(wc->shared_level < level);
5009 if (level < wc->shared_level)
5010 goto out;
5011
Yan Zheng2c47e6052009-06-27 21:07:35 -04005012 ret = find_next_key(path, level + 1, &wc->update_progress);
5013 if (ret > 0)
5014 wc->update_ref = 0;
5015
5016 wc->stage = DROP_REFERENCE;
5017 wc->shared_level = -1;
5018 path->slots[level] = 0;
5019
5020 /*
5021 * check reference count again if the block isn't locked.
5022 * we should start walking down the tree again if reference
5023 * count is one.
5024 */
5025 if (!path->locks[level]) {
5026 BUG_ON(level == 0);
5027 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005028 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005029 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005030
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005031 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005032 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005033 &wc->refs[level],
5034 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005035 if (ret < 0) {
5036 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005037 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005038 return ret;
5039 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005040 BUG_ON(wc->refs[level] == 0);
5041 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005042 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005043 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005044 return 1;
5045 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005046 }
5047 }
5048
5049 /* wc->stage == DROP_REFERENCE */
5050 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5051
5052 if (wc->refs[level] == 1) {
5053 if (level == 0) {
5054 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005055 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005056 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005057 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005058 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005059 if (is_fstree(root->root_key.objectid)) {
5060 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5061 if (ret) {
5062 btrfs_err_rl(fs_info,
5063 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005064 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005065 }
Mark Fasheh11526512014-07-17 12:39:01 -07005066 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005067 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005068 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005069 if (!path->locks[level] &&
5070 btrfs_header_generation(eb) == trans->transid) {
5071 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005072 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005073 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005074 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005075 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005076 }
5077
5078 if (eb == root->node) {
5079 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5080 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005081 else if (root->root_key.objectid != btrfs_header_owner(eb))
5082 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005083 } else {
5084 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5085 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005086 else if (root->root_key.objectid !=
5087 btrfs_header_owner(path->nodes[level + 1]))
5088 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005089 }
5090
Jan Schmidt5581a512012-05-16 17:04:52 +02005091 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005092out:
5093 wc->refs[level] = 0;
5094 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005095 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005096
5097owner_mismatch:
5098 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5099 btrfs_header_owner(eb), root->root_key.objectid);
5100 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005101}
5102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005103static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5104 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005105 struct btrfs_path *path,
5106 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005107{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005108 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005109 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005110 int ret;
5111
Yan Zheng2c47e6052009-06-27 21:07:35 -04005112 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005113 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005114 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005115 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005116
Yan Zheng2c47e6052009-06-27 21:07:35 -04005117 if (level == 0)
5118 break;
5119
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005120 if (path->slots[level] >=
5121 btrfs_header_nritems(path->nodes[level]))
5122 break;
5123
Yan, Zheng94fcca92009-10-09 09:25:16 -04005124 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005125 if (ret > 0) {
5126 path->slots[level]++;
5127 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005128 } else if (ret < 0)
5129 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005130 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005131 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005132 return 0;
5133}
5134
Chris Masond3977122009-01-05 21:25:51 -05005135static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005136 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005137 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005138 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005139{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005140 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005141 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005142
Yan Zheng2c47e6052009-06-27 21:07:35 -04005143 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5144 while (level < max_level && path->nodes[level]) {
5145 wc->level = level;
5146 if (path->slots[level] + 1 <
5147 btrfs_header_nritems(path->nodes[level])) {
5148 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005149 return 0;
5150 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005151 ret = walk_up_proc(trans, root, path, wc);
5152 if (ret > 0)
5153 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005154 if (ret < 0)
5155 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005156
Yan Zheng2c47e6052009-06-27 21:07:35 -04005157 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005158 btrfs_tree_unlock_rw(path->nodes[level],
5159 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005160 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005161 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005162 free_extent_buffer(path->nodes[level]);
5163 path->nodes[level] = NULL;
5164 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005165 }
5166 }
5167 return 1;
5168}
5169
Chris Mason9aca1d52007-03-13 11:09:37 -04005170/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005171 * drop a subvolume tree.
5172 *
5173 * this function traverses the tree freeing any blocks that only
5174 * referenced by the tree.
5175 *
5176 * when a shared tree block is found. this function decreases its
5177 * reference count by one. if update_ref is true, this function
5178 * also make sure backrefs for the shared block and all lower level
5179 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005180 *
5181 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005182 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005183int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005184 struct btrfs_block_rsv *block_rsv, int update_ref,
5185 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005186{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005187 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005188 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005189 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005190 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005191 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005192 struct walk_control *wc;
5193 struct btrfs_key key;
5194 int err = 0;
5195 int ret;
5196 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005197 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005198
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005199 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005200
Chris Mason5caf2a02007-04-02 11:20:42 -04005201 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005202 if (!path) {
5203 err = -ENOMEM;
5204 goto out;
5205 }
Chris Mason20524f02007-03-10 06:35:47 -05005206
Yan Zheng2c47e6052009-06-27 21:07:35 -04005207 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005208 if (!wc) {
5209 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005210 err = -ENOMEM;
5211 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005212 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005213
Yan, Zhenga22285a2010-05-16 10:48:46 -04005214 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005215 if (IS_ERR(trans)) {
5216 err = PTR_ERR(trans);
5217 goto out_free;
5218 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005219
Josef Bacik0568e822018-11-30 11:52:14 -05005220 err = btrfs_run_delayed_items(trans);
5221 if (err)
5222 goto out_end_trans;
5223
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005224 if (block_rsv)
5225 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005226
Josef Bacik83354f02018-11-30 11:52:13 -05005227 /*
5228 * This will help us catch people modifying the fs tree while we're
5229 * dropping it. It is unsafe to mess with the fs tree while it's being
5230 * dropped as we unlock the root node and parent nodes as we walk down
5231 * the tree, assuming nothing will change. If something does change
5232 * then we'll have stale information and drop references to blocks we've
5233 * already dropped.
5234 */
5235 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005236 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005237 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005238 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005239 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005240 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005241 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005242 memset(&wc->update_progress, 0,
5243 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005244 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005245 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005246 memcpy(&wc->update_progress, &key,
5247 sizeof(wc->update_progress));
5248
Chris Mason6702ed42007-08-07 16:15:09 -04005249 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005250 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005251 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5253 path->lowest_level = 0;
5254 if (ret < 0) {
5255 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005256 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005257 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005258 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005259
Chris Mason7d9eb122008-07-08 14:19:17 -04005260 /*
5261 * unlock our path, this is safe because only this
5262 * function is allowed to delete this snapshot
5263 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005265
Yan Zheng2c47e6052009-06-27 21:07:35 -04005266 level = btrfs_header_level(root->node);
5267 while (1) {
5268 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005269 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005270 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005271
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005272 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005273 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005274 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005275 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005276 if (ret < 0) {
5277 err = ret;
5278 goto out_end_trans;
5279 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005280 BUG_ON(wc->refs[level] == 0);
5281
5282 if (level == root_item->drop_level)
5283 break;
5284
5285 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005286 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005287 WARN_ON(wc->refs[level] != 1);
5288 level--;
5289 }
5290 }
5291
Josef Bacik78c52d92019-02-06 15:46:14 -05005292 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005293 wc->level = level;
5294 wc->shared_level = -1;
5295 wc->stage = DROP_REFERENCE;
5296 wc->update_ref = update_ref;
5297 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005299
5300 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005301
Yan Zheng2c47e6052009-06-27 21:07:35 -04005302 ret = walk_down_tree(trans, root, path, wc);
5303 if (ret < 0) {
5304 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005305 break;
5306 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005307
5308 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5309 if (ret < 0) {
5310 err = ret;
5311 break;
5312 }
5313
5314 if (ret > 0) {
5315 BUG_ON(wc->stage != DROP_REFERENCE);
5316 break;
5317 }
5318
5319 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005320 wc->drop_level = wc->level;
5321 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5322 &wc->drop_progress,
5323 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005324 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005325 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5326 &wc->drop_progress);
5327 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005328
5329 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005330 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005331 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005332 ret = btrfs_update_root(trans, tree_root,
5333 &root->root_key,
5334 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005335 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005336 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005337 err = ret;
5338 goto out_end_trans;
5339 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005340
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005341 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005342 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005343 btrfs_debug(fs_info,
5344 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005345 err = -EAGAIN;
5346 goto out_free;
5347 }
5348
Yan, Zhenga22285a2010-05-16 10:48:46 -04005349 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005350 if (IS_ERR(trans)) {
5351 err = PTR_ERR(trans);
5352 goto out_free;
5353 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005354 if (block_rsv)
5355 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005356 }
Chris Mason20524f02007-03-10 06:35:47 -05005357 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005358 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005359 if (err)
5360 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005361
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005362 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005363 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005364 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005365 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005366 goto out_end_trans;
5367 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005368
Yan, Zheng76dda932009-09-21 16:00:26 -04005369 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005370 ret = btrfs_find_root(tree_root, &root->root_key, path,
5371 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005372 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005373 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005374 err = ret;
5375 goto out_end_trans;
5376 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005377 /* if we fail to delete the orphan item this time
5378 * around, it'll get picked up the next time.
5379 *
5380 * The most common failure here is just -ENOENT.
5381 */
5382 btrfs_del_orphan_item(trans, tree_root,
5383 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005384 }
5385 }
5386
Miao Xie27cdeb72014-04-02 19:51:05 +08005387 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005388 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005389 } else {
5390 free_extent_buffer(root->node);
5391 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00005392 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005393 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005394 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005395out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005396 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005397out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005398 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005399 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005400out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005401 /*
5402 * So if we need to stop dropping the snapshot for whatever reason we
5403 * need to make sure to add it back to the dead root list so that we
5404 * keep trying to do the work later. This also cleans up roots if we
5405 * don't have it in the radix (like when we recover after a power fail
5406 * or unmount) so we don't leak memory.
5407 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005408 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005409 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005410 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005411 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005412 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005413}
Chris Mason9078a3e2007-04-26 16:46:15 -04005414
Yan Zheng2c47e6052009-06-27 21:07:35 -04005415/*
5416 * drop subtree rooted at tree block 'node'.
5417 *
5418 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005419 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005420 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005421int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5422 struct btrfs_root *root,
5423 struct extent_buffer *node,
5424 struct extent_buffer *parent)
5425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005426 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005427 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005428 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005429 int level;
5430 int parent_level;
5431 int ret = 0;
5432 int wret;
5433
Yan Zheng2c47e6052009-06-27 21:07:35 -04005434 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5435
Yan Zhengf82d02d2008-10-29 14:49:05 -04005436 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005437 if (!path)
5438 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005439
Yan Zheng2c47e6052009-06-27 21:07:35 -04005440 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005441 if (!wc) {
5442 btrfs_free_path(path);
5443 return -ENOMEM;
5444 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005445
Chris Masonb9447ef82009-03-09 11:45:38 -04005446 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005447 parent_level = btrfs_header_level(parent);
5448 extent_buffer_get(parent);
5449 path->nodes[parent_level] = parent;
5450 path->slots[parent_level] = btrfs_header_nritems(parent);
5451
Chris Masonb9447ef82009-03-09 11:45:38 -04005452 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005453 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005454 path->nodes[level] = node;
5455 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005456 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005457
5458 wc->refs[parent_level] = 1;
5459 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5460 wc->level = level;
5461 wc->shared_level = -1;
5462 wc->stage = DROP_REFERENCE;
5463 wc->update_ref = 0;
5464 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005465 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005466
5467 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005468 wret = walk_down_tree(trans, root, path, wc);
5469 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005470 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005471 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005472 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005473
Yan Zheng2c47e6052009-06-27 21:07:35 -04005474 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005475 if (wret < 0)
5476 ret = wret;
5477 if (wret != 0)
5478 break;
5479 }
5480
Yan Zheng2c47e6052009-06-27 21:07:35 -04005481 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005482 btrfs_free_path(path);
5483 return ret;
5484}
5485
Miao Xie6d07bce2011-01-05 10:07:31 +00005486/*
5487 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005488 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005489 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005490u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005491{
5492 struct btrfs_block_group_cache *block_group;
5493 u64 free_bytes = 0;
5494 int factor;
5495
Nicholas D Steeves01327612016-05-19 21:18:45 -04005496 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005497 if (list_empty(&sinfo->ro_bgs))
5498 return 0;
5499
5500 spin_lock(&sinfo->lock);
5501 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005502 spin_lock(&block_group->lock);
5503
5504 if (!block_group->ro) {
5505 spin_unlock(&block_group->lock);
5506 continue;
5507 }
5508
David Sterba46df06b2018-07-13 20:46:30 +02005509 factor = btrfs_bg_type_to_factor(block_group->flags);
Miao Xie6d07bce2011-01-05 10:07:31 +00005510 free_bytes += (block_group->key.offset -
5511 btrfs_block_group_used(&block_group->item)) *
5512 factor;
5513
5514 spin_unlock(&block_group->lock);
5515 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005516 spin_unlock(&sinfo->lock);
5517
5518 return free_bytes;
5519}
5520
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005521int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5522 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005523{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005524 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005525}
5526
Jeff Mahoney499f3772015-06-15 09:41:17 -04005527/*
5528 * It used to be that old block groups would be left around forever.
5529 * Iterating over them would be enough to trim unused space. Since we
5530 * now automatically remove them, we also need to iterate over unallocated
5531 * space.
5532 *
5533 * We don't want a transaction for this since the discard may take a
5534 * substantial amount of time. We don't require that a transaction be
5535 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005536 * to ensure that we're not discarding chunks that were released or
5537 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005538 *
5539 * Holding the chunks lock will prevent other threads from allocating
5540 * or releasing chunks, but it won't prevent a running transaction
5541 * from committing and releasing the memory that the pending chunks
5542 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005543 * transaction and hold the commit root sem. We only need to hold
5544 * it while performing the free space search since we have already
5545 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005546 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005547static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005548{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005549 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005550 int ret;
5551
5552 *trimmed = 0;
5553
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005554 /* Discard not supported = nothing to do. */
5555 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5556 return 0;
5557
Andrea Gelmini52042d82018-11-28 12:05:13 +01005558 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005559 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005560 return 0;
5561
5562 /* No free space = nothing to do. */
5563 if (device->total_bytes <= device->bytes_used)
5564 return 0;
5565
5566 ret = 0;
5567
5568 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005569 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005570 u64 bytes;
5571
5572 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5573 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005574 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005575
Nikolay Borisov929be172019-03-27 14:24:18 +02005576 find_first_clear_extent_bit(&device->alloc_state, start,
5577 &start, &end,
5578 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005579
5580 /* Ensure we skip the reserved area in the first 1M */
5581 start = max_t(u64, start, SZ_1M);
5582
Nikolay Borisov929be172019-03-27 14:24:18 +02005583 /*
5584 * If find_first_clear_extent_bit find a range that spans the
5585 * end of the device it will set end to -1, in this case it's up
5586 * to the caller to trim the value to the size of the device.
5587 */
5588 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005589
Nikolay Borisov929be172019-03-27 14:24:18 +02005590 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005591
Nikolay Borisov929be172019-03-27 14:24:18 +02005592 /* We didn't find any extents */
5593 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005594 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005595 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005596 break;
5597 }
5598
Nikolay Borisov929be172019-03-27 14:24:18 +02005599 ret = btrfs_issue_discard(device->bdev, start, len,
5600 &bytes);
5601 if (!ret)
5602 set_extent_bits(&device->alloc_state, start,
5603 start + bytes - 1,
5604 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005605 mutex_unlock(&fs_info->chunk_mutex);
5606
5607 if (ret)
5608 break;
5609
5610 start += len;
5611 *trimmed += bytes;
5612
5613 if (fatal_signal_pending(current)) {
5614 ret = -ERESTARTSYS;
5615 break;
5616 }
5617
5618 cond_resched();
5619 }
5620
5621 return ret;
5622}
5623
Qu Wenruo93bba242018-09-07 14:16:23 +08005624/*
5625 * Trim the whole filesystem by:
5626 * 1) trimming the free space in each block group
5627 * 2) trimming the unallocated space on each device
5628 *
5629 * This will also continue trimming even if a block group or device encounters
5630 * an error. The return value will be the last error, or 0 if nothing bad
5631 * happens.
5632 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005633int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005634{
Li Dongyangf7039b12011-03-24 10:24:28 +00005635 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005636 struct btrfs_device *device;
5637 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005638 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005639 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005640 u64 start;
5641 u64 end;
5642 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005643 u64 bg_failed = 0;
5644 u64 dev_failed = 0;
5645 int bg_ret = 0;
5646 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005647 int ret = 0;
5648
Qu Wenruo07301df2019-05-28 16:21:54 +08005649 /*
5650 * Check range overflow if range->len is set.
5651 * The default range->len is U64_MAX.
5652 */
5653 if (range->len != U64_MAX &&
5654 check_add_overflow(range->start, range->len, &range_end))
5655 return -EINVAL;
5656
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005657 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005658 for (; cache; cache = btrfs_next_block_group(cache)) {
Qu Wenruo07301df2019-05-28 16:21:54 +08005659 if (cache->key.objectid >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005660 btrfs_put_block_group(cache);
5661 break;
5662 }
5663
5664 start = max(range->start, cache->key.objectid);
Qu Wenruo07301df2019-05-28 16:21:54 +08005665 end = min(range_end, cache->key.objectid + cache->key.offset);
Li Dongyangf7039b12011-03-24 10:24:28 +00005666
5667 if (end - start >= range->minlen) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005668 if (!btrfs_block_group_cache_done(cache)) {
5669 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005670 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005671 bg_failed++;
5672 bg_ret = ret;
5673 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005674 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005675 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005676 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005677 bg_failed++;
5678 bg_ret = ret;
5679 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005680 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005681 }
5682 ret = btrfs_trim_block_group(cache,
5683 &group_trimmed,
5684 start,
5685 end,
5686 range->minlen);
5687
5688 trimmed += group_trimmed;
5689 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005690 bg_failed++;
5691 bg_ret = ret;
5692 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005693 }
5694 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005695 }
5696
Qu Wenruo93bba242018-09-07 14:16:23 +08005697 if (bg_failed)
5698 btrfs_warn(fs_info,
5699 "failed to trim %llu block group(s), last error %d",
5700 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005701 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005702 devices = &fs_info->fs_devices->devices;
5703 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005704 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005705 if (ret) {
5706 dev_failed++;
5707 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005708 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005709 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005710
5711 trimmed += group_trimmed;
5712 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005713 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005714
Qu Wenruo93bba242018-09-07 14:16:23 +08005715 if (dev_failed)
5716 btrfs_warn(fs_info,
5717 "failed to trim %llu device(s), last error %d",
5718 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005719 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005720 if (bg_ret)
5721 return bg_ret;
5722 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005723}
Miao Xie8257b2d2014-03-06 13:38:19 +08005724
5725/*
David Sterbaea14b57f2017-06-22 02:19:11 +02005726 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005727 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
5728 * data into the page cache through nocow before the subvolume is snapshoted,
5729 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +02005730 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005731 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +08005732 */
David Sterbaea14b57f2017-06-22 02:19:11 +02005733void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005734{
5735 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +01005736 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +08005737}
5738
David Sterbaea14b57f2017-06-22 02:19:11 +02005739int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005740{
David Sterbaea14b57f2017-06-22 02:19:11 +02005741 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +08005742 return 0;
5743
5744 percpu_counter_inc(&root->subv_writers->counter);
5745 /*
5746 * Make sure counter is updated before we check for snapshot creation.
5747 */
5748 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +02005749 if (atomic_read(&root->will_be_snapshotted)) {
5750 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08005751 return 0;
5752 }
5753 return 1;
5754}
Zhao Lei0bc19f902016-01-06 18:56:36 +08005755
Zhao Lei0bc19f902016-01-06 18:56:36 +08005756void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
5757{
5758 while (true) {
5759 int ret;
5760
David Sterbaea14b57f2017-06-22 02:19:11 +02005761 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +08005762 if (ret)
5763 break;
Peter Zijlstra46259562018-03-15 11:43:08 +01005764 wait_var_event(&root->will_be_snapshotted,
5765 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +08005766 }
5767}