blob: 2b4c3ca5e65148ec66c1375a51300c22139a2584 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Zach Brownec6b9102007-07-11 10:00:37 -04006#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01007#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05008#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -04009#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010010#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050011#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040012#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040013#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020015#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040016#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040017#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
David Sterba602cbe92019-08-21 18:48:25 +020019#include "misc.h"
Miao Xie995946d2014-04-02 19:51:06 +080020#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050021#include "disk-io.h"
22#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040023#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040026#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070027#include "free-space-tree.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040028#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070029#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040030#include "ref-verify.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040031#include "space-info.h"
Josef Bacikd12ffdd2019-06-19 13:47:17 -040032#include "block-rsv.h"
Josef Bacik86736342019-06-19 15:12:00 -040033#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040034#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080035#include "discard.h"
Chris Masonfec577f2007-02-26 10:40:21 -050036
Arne Jansen709c0482011-09-12 12:22:57 +020037#undef SCRAMBLE_DELAYED_REFS
38
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080039
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030041 struct btrfs_delayed_ref_node *node, u64 parent,
42 u64 root_objectid, u64 owner_objectid,
43 u64 owner_offset, int refs_to_drop,
44 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040045static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
46 struct extent_buffer *leaf,
47 struct btrfs_extent_item *ei);
48static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040049 u64 parent, u64 root_objectid,
50 u64 flags, u64 owner, u64 offset,
51 struct btrfs_key *ins, int ref_mod);
52static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030053 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030054 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040055static int find_next_key(struct btrfs_path *path, int level,
56 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050057
David Sterba32da53862019-10-29 19:20:18 +010058static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
Josef Bacik0f9dd462008-09-23 13:14:11 -040059{
60 return (cache->flags & bits) == bits;
61}
62
Josef Bacik6f410d12019-06-20 15:37:49 -040063int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
64 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -040065{
Yan Zheng11833d62009-09-11 16:11:19 -040066 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040067 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +020068 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040069 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +020070 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040071 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -040072}
73
David Sterba32da53862019-10-29 19:20:18 +010074void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -040075{
Nikolay Borisov9e715da2018-06-20 15:49:08 +030076 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -040077 u64 start, end;
78
David Sterbab3470b52019-10-23 18:48:22 +020079 start = cache->start;
80 end = start + cache->length - 1;
Yan Zheng11833d62009-09-11 16:11:19 -040081
Jeff Mahoney0b246af2016-06-22 18:54:23 -040082 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +020083 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040084 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +020085 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -040086}
87
Qu Wenruo78192442019-05-15 07:33:48 +080088static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
Omar Sandoval0d9f8242017-06-06 16:45:26 -070089{
Qu Wenruoddf30cf2019-04-04 14:45:34 +080090 if (ref->type == BTRFS_REF_METADATA) {
91 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
Qu Wenruo78192442019-05-15 07:33:48 +080092 return BTRFS_BLOCK_GROUP_SYSTEM;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070093 else
Qu Wenruo78192442019-05-15 07:33:48 +080094 return BTRFS_BLOCK_GROUP_METADATA;
Omar Sandoval0d9f8242017-06-06 16:45:26 -070095 }
Qu Wenruo78192442019-05-15 07:33:48 +080096 return BTRFS_BLOCK_GROUP_DATA;
97}
98
99static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
100 struct btrfs_ref *ref)
101{
102 struct btrfs_space_info *space_info;
103 u64 flags = generic_ref_to_space_flags(ref);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700104
Josef Bacik280c29082019-06-18 16:09:19 -0400105 space_info = btrfs_find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700106 ASSERT(space_info);
Qu Wenruo78192442019-05-15 07:33:48 +0800107 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
108 BTRFS_TOTAL_BYTES_PINNED_BATCH);
109}
110
111static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
112 struct btrfs_ref *ref)
113{
114 struct btrfs_space_info *space_info;
115 u64 flags = generic_ref_to_space_flags(ref);
116
Josef Bacik280c29082019-06-18 16:09:19 -0400117 space_info = btrfs_find_space_info(fs_info, flags);
Qu Wenruo78192442019-05-15 07:33:48 +0800118 ASSERT(space_info);
119 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
Ethan Liendec59fa2018-07-13 16:50:42 +0800120 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700121}
122
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000123/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400124int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400125{
126 int ret;
127 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400128 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400129
Zheng Yan31840ae2008-09-23 13:14:14 -0400130 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700131 if (!path)
132 return -ENOMEM;
133
Chris Masone02119d2008-09-05 16:13:11 -0400134 key.objectid = start;
135 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500136 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400137 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400138 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500139 return ret;
140}
141
Chris Masond8d5f3e2007-12-11 12:42:00 -0500142/*
Josef Bacik3173a182013-03-07 14:22:04 -0500143 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400144 *
145 * the head node for delayed ref is used to store the sum of all the
146 * reference count modifications queued up in the rbtree. the head
147 * node may also store the extent flags to set. This way you can check
148 * to see what the reference count and extent flags would be if all of
149 * the delayed refs are not processed.
150 */
151int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400152 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500153 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400154{
155 struct btrfs_delayed_ref_head *head;
156 struct btrfs_delayed_ref_root *delayed_refs;
157 struct btrfs_path *path;
158 struct btrfs_extent_item *ei;
159 struct extent_buffer *leaf;
160 struct btrfs_key key;
161 u32 item_size;
162 u64 num_refs;
163 u64 extent_flags;
164 int ret;
165
Josef Bacik3173a182013-03-07 14:22:04 -0500166 /*
167 * If we don't have skinny metadata, don't bother doing anything
168 * different
169 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400170 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
171 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500172 metadata = 0;
173 }
174
Yan, Zhenga22285a2010-05-16 10:48:46 -0400175 path = btrfs_alloc_path();
176 if (!path)
177 return -ENOMEM;
178
Yan, Zhenga22285a2010-05-16 10:48:46 -0400179 if (!trans) {
180 path->skip_locking = 1;
181 path->search_commit_root = 1;
182 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000183
184search_again:
185 key.objectid = bytenr;
186 key.offset = offset;
187 if (metadata)
188 key.type = BTRFS_METADATA_ITEM_KEY;
189 else
190 key.type = BTRFS_EXTENT_ITEM_KEY;
191
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400192 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400193 if (ret < 0)
194 goto out_free;
195
Josef Bacik3173a182013-03-07 14:22:04 -0500196 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100197 if (path->slots[0]) {
198 path->slots[0]--;
199 btrfs_item_key_to_cpu(path->nodes[0], &key,
200 path->slots[0]);
201 if (key.objectid == bytenr &&
202 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400203 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100204 ret = 0;
205 }
Josef Bacik3173a182013-03-07 14:22:04 -0500206 }
207
Yan, Zhenga22285a2010-05-16 10:48:46 -0400208 if (ret == 0) {
209 leaf = path->nodes[0];
210 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
211 if (item_size >= sizeof(*ei)) {
212 ei = btrfs_item_ptr(leaf, path->slots[0],
213 struct btrfs_extent_item);
214 num_refs = btrfs_extent_refs(leaf, ei);
215 extent_flags = btrfs_extent_flags(leaf, ei);
216 } else {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300217 ret = -EINVAL;
218 btrfs_print_v0_err(fs_info);
219 if (trans)
220 btrfs_abort_transaction(trans, ret);
221 else
222 btrfs_handle_fs_error(fs_info, ret, NULL);
223
224 goto out_free;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400225 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300226
Yan, Zhenga22285a2010-05-16 10:48:46 -0400227 BUG_ON(num_refs == 0);
228 } else {
229 num_refs = 0;
230 extent_flags = 0;
231 ret = 0;
232 }
233
234 if (!trans)
235 goto out;
236
237 delayed_refs = &trans->transaction->delayed_refs;
238 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800239 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400240 if (head) {
241 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400242 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400243 spin_unlock(&delayed_refs->lock);
244
David Sterbab3b4aa72011-04-21 01:20:15 +0200245 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400246
David Sterba8cc33e52011-05-02 15:29:25 +0200247 /*
248 * Mutex was contended, block until it's released and try
249 * again
250 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400251 mutex_lock(&head->mutex);
252 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400253 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000254 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400255 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500256 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400257 if (head->extent_op && head->extent_op->update_flags)
258 extent_flags |= head->extent_op->flags_to_set;
259 else
260 BUG_ON(num_refs == 0);
261
Josef Bacikd2788502017-09-29 15:43:57 -0400262 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500263 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400264 mutex_unlock(&head->mutex);
265 }
266 spin_unlock(&delayed_refs->lock);
267out:
268 WARN_ON(num_refs == 0);
269 if (refs)
270 *refs = num_refs;
271 if (flags)
272 *flags = extent_flags;
273out_free:
274 btrfs_free_path(path);
275 return ret;
276}
277
278/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500279 * Back reference rules. Back refs have three main goals:
280 *
281 * 1) differentiate between all holders of references to an extent so that
282 * when a reference is dropped we can make sure it was a valid reference
283 * before freeing the extent.
284 *
285 * 2) Provide enough information to quickly find the holders of an extent
286 * if we notice a given block is corrupted or bad.
287 *
288 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
289 * maintenance. This is actually the same as #2, but with a slightly
290 * different use case.
291 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400292 * There are two kinds of back refs. The implicit back refs is optimized
293 * for pointers in non-shared tree blocks. For a given pointer in a block,
294 * back refs of this kind provide information about the block's owner tree
295 * and the pointer's key. These information allow us to find the block by
296 * b-tree searching. The full back refs is for pointers in tree blocks not
297 * referenced by their owner trees. The location of tree block is recorded
298 * in the back refs. Actually the full back refs is generic, and can be
299 * used in all cases the implicit back refs is used. The major shortcoming
300 * of the full back refs is its overhead. Every time a tree block gets
301 * COWed, we have to update back refs entry for all pointers in it.
302 *
303 * For a newly allocated tree block, we use implicit back refs for
304 * pointers in it. This means most tree related operations only involve
305 * implicit back refs. For a tree block created in old transaction, the
306 * only way to drop a reference to it is COW it. So we can detect the
307 * event that tree block loses its owner tree's reference and do the
308 * back refs conversion.
309 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400310 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400311 *
312 * The reference count of the block is one and the tree is the block's
313 * owner tree. Nothing to do in this case.
314 *
315 * The reference count of the block is one and the tree is not the
316 * block's owner tree. In this case, full back refs is used for pointers
317 * in the block. Remove these full back refs, add implicit back refs for
318 * every pointers in the new block.
319 *
320 * The reference count of the block is greater than one and the tree is
321 * the block's owner tree. In this case, implicit back refs is used for
322 * pointers in the block. Add full back refs for every pointers in the
323 * block, increase lower level extents' reference counts. The original
324 * implicit back refs are entailed to the new block.
325 *
326 * The reference count of the block is greater than one and the tree is
327 * not the block's owner tree. Add implicit back refs for every pointer in
328 * the new block, increase lower level extents' reference count.
329 *
330 * Back Reference Key composing:
331 *
332 * The key objectid corresponds to the first byte in the extent,
333 * The key type is used to differentiate between types of back refs.
334 * There are different meanings of the key offset for different types
335 * of back refs.
336 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500337 * File extents can be referenced by:
338 *
339 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400340 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500341 * - different offsets inside a file (bookend extents in file.c)
342 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400343 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500344 *
345 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500346 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400347 * - original offset in the file
348 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400349 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400350 * The key offset for the implicit back refs is hash of the first
351 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500352 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400353 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500354 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400355 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500356 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400357 * The key offset for the implicit back refs is the first byte of
358 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500359 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 * When a file extent is allocated, The implicit back refs is used.
361 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500362 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400363 * (root_key.objectid, inode objectid, offset in file, 1)
364 *
365 * When a file extent is removed file truncation, we find the
366 * corresponding implicit back refs and check the following fields:
367 *
368 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500369 *
370 * Btree extents can be referenced by:
371 *
372 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500373 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 * Both the implicit back refs and the full back refs for tree blocks
375 * only consist of key. The key offset for the implicit back refs is
376 * objectid of block's owner tree. The key offset for the full back refs
377 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500378 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400379 * When implicit back refs is used, information about the lowest key and
380 * level of the tree block are required. These information are stored in
381 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500382 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400383
Liu Bo167ce952017-08-18 15:15:18 -0600384/*
385 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100386 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600387 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
388 */
389int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
390 struct btrfs_extent_inline_ref *iref,
391 enum btrfs_inline_ref_type is_data)
392{
393 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -0600394 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600395
396 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
397 type == BTRFS_SHARED_BLOCK_REF_KEY ||
398 type == BTRFS_SHARED_DATA_REF_KEY ||
399 type == BTRFS_EXTENT_DATA_REF_KEY) {
400 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600401 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600402 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600403 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
404 ASSERT(eb->fs_info);
405 /*
406 * Every shared one has parent tree
407 * block, which must be aligned to
408 * nodesize.
409 */
410 if (offset &&
411 IS_ALIGNED(offset, eb->fs_info->nodesize))
412 return type;
413 }
Liu Bo167ce952017-08-18 15:15:18 -0600414 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600415 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600416 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600417 if (type == BTRFS_SHARED_DATA_REF_KEY) {
418 ASSERT(eb->fs_info);
419 /*
420 * Every shared one has parent tree
421 * block, which must be aligned to
422 * nodesize.
423 */
424 if (offset &&
425 IS_ALIGNED(offset, eb->fs_info->nodesize))
426 return type;
427 }
Liu Bo167ce952017-08-18 15:15:18 -0600428 } else {
429 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
430 return type;
431 }
432 }
433
434 btrfs_print_leaf((struct extent_buffer *)eb);
435 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
436 eb->start, type);
437 WARN_ON(1);
438
439 return BTRFS_REF_TYPE_INVALID;
440}
441
Qu Wenruo0785a9a2019-08-09 09:24:24 +0800442u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443{
444 u32 high_crc = ~(u32)0;
445 u32 low_crc = ~(u32)0;
446 __le64 lenum;
447
448 lenum = cpu_to_le64(root_objectid);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200449 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200451 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200453 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454
455 return ((u64)high_crc << 31) ^ (u64)low_crc;
456}
457
458static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
459 struct btrfs_extent_data_ref *ref)
460{
461 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
462 btrfs_extent_data_ref_objectid(leaf, ref),
463 btrfs_extent_data_ref_offset(leaf, ref));
464}
465
466static int match_extent_data_ref(struct extent_buffer *leaf,
467 struct btrfs_extent_data_ref *ref,
468 u64 root_objectid, u64 owner, u64 offset)
469{
470 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
471 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
472 btrfs_extent_data_ref_offset(leaf, ref) != offset)
473 return 0;
474 return 1;
475}
476
477static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 struct btrfs_path *path,
479 u64 bytenr, u64 parent,
480 u64 root_objectid,
481 u64 owner, u64 offset)
482{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300483 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400484 struct btrfs_key key;
485 struct btrfs_extent_data_ref *ref;
486 struct extent_buffer *leaf;
487 u32 nritems;
488 int ret;
489 int recow;
490 int err = -ENOENT;
491
492 key.objectid = bytenr;
493 if (parent) {
494 key.type = BTRFS_SHARED_DATA_REF_KEY;
495 key.offset = parent;
496 } else {
497 key.type = BTRFS_EXTENT_DATA_REF_KEY;
498 key.offset = hash_extent_data_ref(root_objectid,
499 owner, offset);
500 }
501again:
502 recow = 0;
503 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
504 if (ret < 0) {
505 err = ret;
506 goto fail;
507 }
508
509 if (parent) {
510 if (!ret)
511 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400512 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -0400513 }
514
515 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 nritems = btrfs_header_nritems(leaf);
517 while (1) {
518 if (path->slots[0] >= nritems) {
519 ret = btrfs_next_leaf(root, path);
520 if (ret < 0)
521 err = ret;
522 if (ret)
523 goto fail;
524
525 leaf = path->nodes[0];
526 nritems = btrfs_header_nritems(leaf);
527 recow = 1;
528 }
529
530 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
531 if (key.objectid != bytenr ||
532 key.type != BTRFS_EXTENT_DATA_REF_KEY)
533 goto fail;
534
535 ref = btrfs_item_ptr(leaf, path->slots[0],
536 struct btrfs_extent_data_ref);
537
538 if (match_extent_data_ref(leaf, ref, root_objectid,
539 owner, offset)) {
540 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200541 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400542 goto again;
543 }
544 err = 0;
545 break;
546 }
547 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549fail:
550 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -0400551}
552
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 struct btrfs_path *path,
555 u64 bytenr, u64 parent,
556 u64 root_objectid, u64 owner,
557 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400558{
Nikolay Borisov62b895a2018-06-20 15:48:44 +0300559 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -0400560 struct btrfs_key key;
561 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400562 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 u32 num_refs;
564 int ret;
565
566 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400567 if (parent) {
568 key.type = BTRFS_SHARED_DATA_REF_KEY;
569 key.offset = parent;
570 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400571 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400572 key.type = BTRFS_EXTENT_DATA_REF_KEY;
573 key.offset = hash_extent_data_ref(root_objectid,
574 owner, offset);
575 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400576 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577
578 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
579 if (ret && ret != -EEXIST)
580 goto fail;
581
582 leaf = path->nodes[0];
583 if (parent) {
584 struct btrfs_shared_data_ref *ref;
585 ref = btrfs_item_ptr(leaf, path->slots[0],
586 struct btrfs_shared_data_ref);
587 if (ret == 0) {
588 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
589 } else {
590 num_refs = btrfs_shared_data_ref_count(leaf, ref);
591 num_refs += refs_to_add;
592 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
593 }
594 } else {
595 struct btrfs_extent_data_ref *ref;
596 while (ret == -EEXIST) {
597 ref = btrfs_item_ptr(leaf, path->slots[0],
598 struct btrfs_extent_data_ref);
599 if (match_extent_data_ref(leaf, ref, root_objectid,
600 owner, offset))
601 break;
David Sterbab3b4aa72011-04-21 01:20:15 +0200602 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400603 key.offset++;
604 ret = btrfs_insert_empty_item(trans, root, path, &key,
605 size);
606 if (ret && ret != -EEXIST)
607 goto fail;
608
609 leaf = path->nodes[0];
610 }
611 ref = btrfs_item_ptr(leaf, path->slots[0],
612 struct btrfs_extent_data_ref);
613 if (ret == 0) {
614 btrfs_set_extent_data_ref_root(leaf, ref,
615 root_objectid);
616 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
617 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
618 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
619 } else {
620 num_refs = btrfs_extent_data_ref_count(leaf, ref);
621 num_refs += refs_to_add;
622 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
623 }
624 }
625 btrfs_mark_buffer_dirty(leaf);
626 ret = 0;
627fail:
David Sterbab3b4aa72011-04-21 01:20:15 +0200628 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500629 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500630}
631
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400633 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -0700634 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -0400635{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400636 struct btrfs_key key;
637 struct btrfs_extent_data_ref *ref1 = NULL;
638 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400639 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400640 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400641 int ret = 0;
642
643 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400644 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
645
646 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
647 ref1 = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_extent_data_ref);
649 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
650 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
651 ref2 = btrfs_item_ptr(leaf, path->slots[0],
652 struct btrfs_shared_data_ref);
653 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200654 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300655 btrfs_print_v0_err(trans->fs_info);
656 btrfs_abort_transaction(trans, -EINVAL);
657 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 } else {
659 BUG();
660 }
661
Chris Mason56bec292009-03-13 10:10:06 -0400662 BUG_ON(num_refs < refs_to_drop);
663 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +0300666 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -0700667 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400668 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
670 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
671 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
672 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
Zheng Yan31840ae2008-09-23 13:14:14 -0400673 btrfs_mark_buffer_dirty(leaf);
674 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 return ret;
676}
677
Zhaolei9ed0dea2015-08-06 22:16:24 +0800678static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 struct btrfs_extent_inline_ref *iref)
680{
681 struct btrfs_key key;
682 struct extent_buffer *leaf;
683 struct btrfs_extent_data_ref *ref1;
684 struct btrfs_shared_data_ref *ref2;
685 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -0600686 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400687
688 leaf = path->nodes[0];
689 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300690
691 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -0600693 /*
694 * If type is invalid, we should have bailed out earlier than
695 * this call.
696 */
697 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
698 ASSERT(type != BTRFS_REF_TYPE_INVALID);
699 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
701 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
702 } else {
703 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
704 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
705 }
706 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
707 ref1 = btrfs_item_ptr(leaf, path->slots[0],
708 struct btrfs_extent_data_ref);
709 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
710 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
711 ref2 = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_shared_data_ref);
713 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 } else {
715 WARN_ON(1);
716 }
717 return num_refs;
718}
719
720static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 struct btrfs_path *path,
722 u64 bytenr, u64 parent,
723 u64 root_objectid)
724{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +0300725 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400726 struct btrfs_key key;
727 int ret;
728
729 key.objectid = bytenr;
730 if (parent) {
731 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
732 key.offset = parent;
733 } else {
734 key.type = BTRFS_TREE_BLOCK_REF_KEY;
735 key.offset = root_objectid;
736 }
737
738 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
739 if (ret > 0)
740 ret = -ENOENT;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 return ret;
742}
743
744static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400745 struct btrfs_path *path,
746 u64 bytenr, u64 parent,
747 u64 root_objectid)
748{
749 struct btrfs_key key;
750 int ret;
751
752 key.objectid = bytenr;
753 if (parent) {
754 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
755 key.offset = parent;
756 } else {
757 key.type = BTRFS_TREE_BLOCK_REF_KEY;
758 key.offset = root_objectid;
759 }
760
Nikolay Borisov10728402018-06-20 15:48:43 +0300761 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500762 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +0200763 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -0400764 return ret;
765}
766
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400767static inline int extent_ref_type(u64 parent, u64 owner)
768{
769 int type;
770 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
771 if (parent > 0)
772 type = BTRFS_SHARED_BLOCK_REF_KEY;
773 else
774 type = BTRFS_TREE_BLOCK_REF_KEY;
775 } else {
776 if (parent > 0)
777 type = BTRFS_SHARED_DATA_REF_KEY;
778 else
779 type = BTRFS_EXTENT_DATA_REF_KEY;
780 }
781 return type;
782}
783
Yan Zheng2c47e6052009-06-27 21:07:35 -0400784static int find_next_key(struct btrfs_path *path, int level,
785 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400786
787{
Yan Zheng2c47e6052009-06-27 21:07:35 -0400788 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 if (!path->nodes[level])
790 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 if (path->slots[level] + 1 >=
792 btrfs_header_nritems(path->nodes[level]))
793 continue;
794 if (level == 0)
795 btrfs_item_key_to_cpu(path->nodes[level], key,
796 path->slots[level] + 1);
797 else
798 btrfs_node_key_to_cpu(path->nodes[level], key,
799 path->slots[level] + 1);
800 return 0;
801 }
802 return 1;
803}
804
805/*
806 * look for inline back ref. if back ref is found, *ref_ret is set
807 * to the address of inline back ref, and 0 is returned.
808 *
809 * if back ref isn't found, *ref_ret is set to the address where it
810 * should be inserted, and -ENOENT is returned.
811 *
812 * if insert is true and there are too many inline back refs, the path
813 * points to the extent item, and -EAGAIN is returned.
814 *
815 * NOTE: inline back refs are ordered in the same way that back ref
816 * items in the tree are ordered.
817 */
818static noinline_for_stack
819int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 struct btrfs_path *path,
821 struct btrfs_extent_inline_ref **ref_ret,
822 u64 bytenr, u64 num_bytes,
823 u64 parent, u64 root_objectid,
824 u64 owner, u64 offset, int insert)
825{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +0300826 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -0500827 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 struct btrfs_key key;
829 struct extent_buffer *leaf;
830 struct btrfs_extent_item *ei;
831 struct btrfs_extent_inline_ref *iref;
832 u64 flags;
833 u64 item_size;
834 unsigned long ptr;
835 unsigned long end;
836 int extra_size;
837 int type;
838 int want;
839 int ret;
840 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -0600842 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400843
844 key.objectid = bytenr;
845 key.type = BTRFS_EXTENT_ITEM_KEY;
846 key.offset = num_bytes;
847
848 want = extent_ref_type(parent, owner);
849 if (insert) {
850 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -0400851 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 } else
853 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -0500854
855 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +0300856 * Owner is our level, so we can just add one to get the level for the
857 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -0500858 */
859 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
860 key.type = BTRFS_METADATA_ITEM_KEY;
861 key.offset = owner;
862 }
863
864again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
866 if (ret < 0) {
867 err = ret;
868 goto out;
869 }
Josef Bacik3173a182013-03-07 14:22:04 -0500870
871 /*
872 * We may be a newly converted file system which still has the old fat
873 * extent entries for metadata, so try and see if we have one of those.
874 */
875 if (ret > 0 && skinny_metadata) {
876 skinny_metadata = false;
877 if (path->slots[0]) {
878 path->slots[0]--;
879 btrfs_item_key_to_cpu(path->nodes[0], &key,
880 path->slots[0]);
881 if (key.objectid == bytenr &&
882 key.type == BTRFS_EXTENT_ITEM_KEY &&
883 key.offset == num_bytes)
884 ret = 0;
885 }
886 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +0100887 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -0500888 key.type = BTRFS_EXTENT_ITEM_KEY;
889 key.offset = num_bytes;
890 btrfs_release_path(path);
891 goto again;
892 }
893 }
894
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 if (ret && !insert) {
896 err = -ENOENT;
897 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530898 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -0500899 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -0500900 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902
903 leaf = path->nodes[0];
904 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +0200905 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300906 err = -EINVAL;
907 btrfs_print_v0_err(fs_info);
908 btrfs_abort_transaction(trans, err);
909 goto out;
910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
913 flags = btrfs_extent_flags(leaf, ei);
914
915 ptr = (unsigned long)(ei + 1);
916 end = (unsigned long)ei + item_size;
917
Josef Bacik3173a182013-03-07 14:22:04 -0500918 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 ptr += sizeof(struct btrfs_tree_block_info);
920 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 }
922
Liu Bo3de28d52017-08-18 15:15:19 -0600923 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
924 needed = BTRFS_REF_TYPE_DATA;
925 else
926 needed = BTRFS_REF_TYPE_BLOCK;
927
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 err = -ENOENT;
929 while (1) {
930 if (ptr >= end) {
931 WARN_ON(ptr > end);
932 break;
933 }
934 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -0600935 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
936 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +0800937 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -0600938 goto out;
939 }
940
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 if (want < type)
942 break;
943 if (want > type) {
944 ptr += btrfs_extent_inline_ref_size(type);
945 continue;
946 }
947
948 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
949 struct btrfs_extent_data_ref *dref;
950 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
951 if (match_extent_data_ref(leaf, dref, root_objectid,
952 owner, offset)) {
953 err = 0;
954 break;
955 }
956 if (hash_extent_data_ref_item(leaf, dref) <
957 hash_extent_data_ref(root_objectid, owner, offset))
958 break;
959 } else {
960 u64 ref_offset;
961 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
962 if (parent > 0) {
963 if (parent == ref_offset) {
964 err = 0;
965 break;
966 }
967 if (ref_offset < parent)
968 break;
969 } else {
970 if (root_objectid == ref_offset) {
971 err = 0;
972 break;
973 }
974 if (ref_offset < root_objectid)
975 break;
976 }
977 }
978 ptr += btrfs_extent_inline_ref_size(type);
979 }
980 if (err == -ENOENT && insert) {
981 if (item_size + extra_size >=
982 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
983 err = -EAGAIN;
984 goto out;
985 }
986 /*
987 * To add new inline back ref, we have to make sure
988 * there is no corresponding back ref item.
989 * For simplicity, we just do not add new inline back
990 * ref if there is any kind of item for this block
991 */
Yan Zheng2c47e6052009-06-27 21:07:35 -0400992 if (find_next_key(path, 0, &key) == 0 &&
993 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -0400994 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 err = -EAGAIN;
996 goto out;
997 }
998 }
999 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1000out:
Yan Zheng85d41982009-06-11 08:51:10 -04001001 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 path->keep_locks = 0;
1003 btrfs_unlock_up_safe(path, 1);
1004 }
1005 return err;
1006}
1007
1008/*
1009 * helper to add new inline back ref
1010 */
1011static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001012void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001013 struct btrfs_path *path,
1014 struct btrfs_extent_inline_ref *iref,
1015 u64 parent, u64 root_objectid,
1016 u64 owner, u64 offset, int refs_to_add,
1017 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018{
1019 struct extent_buffer *leaf;
1020 struct btrfs_extent_item *ei;
1021 unsigned long ptr;
1022 unsigned long end;
1023 unsigned long item_offset;
1024 u64 refs;
1025 int size;
1026 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027
1028 leaf = path->nodes[0];
1029 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1030 item_offset = (unsigned long)iref - (unsigned long)ei;
1031
1032 type = extent_ref_type(parent, owner);
1033 size = btrfs_extent_inline_ref_size(type);
1034
David Sterbac71dd882019-03-20 14:51:10 +01001035 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036
1037 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1038 refs = btrfs_extent_refs(leaf, ei);
1039 refs += refs_to_add;
1040 btrfs_set_extent_refs(leaf, ei, refs);
1041 if (extent_op)
1042 __run_delayed_extent_op(extent_op, leaf, ei);
1043
1044 ptr = (unsigned long)ei + item_offset;
1045 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1046 if (ptr < end - size)
1047 memmove_extent_buffer(leaf, ptr + size, ptr,
1048 end - size - ptr);
1049
1050 iref = (struct btrfs_extent_inline_ref *)ptr;
1051 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1052 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1053 struct btrfs_extent_data_ref *dref;
1054 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1055 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1056 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1057 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1058 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1059 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1060 struct btrfs_shared_data_ref *sref;
1061 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1062 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1063 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1064 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1065 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1066 } else {
1067 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1068 }
1069 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070}
1071
1072static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073 struct btrfs_path *path,
1074 struct btrfs_extent_inline_ref **ref_ret,
1075 u64 bytenr, u64 num_bytes, u64 parent,
1076 u64 root_objectid, u64 owner, u64 offset)
1077{
1078 int ret;
1079
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001080 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1081 num_bytes, parent, root_objectid,
1082 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 if (ret != -ENOENT)
1084 return ret;
1085
David Sterbab3b4aa72011-04-21 01:20:15 +02001086 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087 *ref_ret = NULL;
1088
1089 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001090 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1091 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001093 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1094 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 }
1096 return ret;
1097}
1098
1099/*
1100 * helper to update/remove inline back ref
1101 */
1102static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001103void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001104 struct btrfs_extent_inline_ref *iref,
1105 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001106 struct btrfs_delayed_extent_op *extent_op,
1107 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001109 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001110 struct btrfs_extent_item *ei;
1111 struct btrfs_extent_data_ref *dref = NULL;
1112 struct btrfs_shared_data_ref *sref = NULL;
1113 unsigned long ptr;
1114 unsigned long end;
1115 u32 item_size;
1116 int size;
1117 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 u64 refs;
1119
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1121 refs = btrfs_extent_refs(leaf, ei);
1122 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1123 refs += refs_to_mod;
1124 btrfs_set_extent_refs(leaf, ei, refs);
1125 if (extent_op)
1126 __run_delayed_extent_op(extent_op, leaf, ei);
1127
Liu Bo3de28d52017-08-18 15:15:19 -06001128 /*
1129 * If type is invalid, we should have bailed out after
1130 * lookup_inline_extent_backref().
1131 */
1132 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1133 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134
1135 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1136 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1137 refs = btrfs_extent_data_ref_count(leaf, dref);
1138 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1139 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1140 refs = btrfs_shared_data_ref_count(leaf, sref);
1141 } else {
1142 refs = 1;
1143 BUG_ON(refs_to_mod != -1);
1144 }
1145
1146 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1147 refs += refs_to_mod;
1148
1149 if (refs > 0) {
1150 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1151 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1152 else
1153 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1154 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001155 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001156 size = btrfs_extent_inline_ref_size(type);
1157 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1158 ptr = (unsigned long)iref;
1159 end = (unsigned long)ei + item_size;
1160 if (ptr + size < end)
1161 memmove_extent_buffer(leaf, ptr, ptr + size,
1162 end - ptr - size);
1163 item_size -= size;
David Sterba78ac4f92019-03-20 14:49:12 +01001164 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165 }
1166 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001167}
1168
1169static noinline_for_stack
1170int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001171 struct btrfs_path *path,
1172 u64 bytenr, u64 num_bytes, u64 parent,
1173 u64 root_objectid, u64 owner,
1174 u64 offset, int refs_to_add,
1175 struct btrfs_delayed_extent_op *extent_op)
1176{
1177 struct btrfs_extent_inline_ref *iref;
1178 int ret;
1179
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001180 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1181 num_bytes, parent, root_objectid,
1182 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 if (ret == 0) {
1184 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001185 update_inline_extent_backref(path, iref, refs_to_add,
1186 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001188 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001189 root_objectid, owner, offset,
1190 refs_to_add, extent_op);
1191 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001192 }
1193 return ret;
1194}
1195
1196static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197 struct btrfs_path *path,
1198 u64 bytenr, u64 parent, u64 root_objectid,
1199 u64 owner, u64 offset, int refs_to_add)
1200{
1201 int ret;
1202 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1203 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001204 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1205 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001207 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1208 root_objectid, owner, offset,
1209 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210 }
1211 return ret;
1212}
1213
1214static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001215 struct btrfs_path *path,
1216 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001217 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001219 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220
1221 BUG_ON(!is_data && refs_to_drop != 1);
1222 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001223 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1224 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001226 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001227 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001229 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001230 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 }
1232 return ret;
1233}
1234
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001235static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1236 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001237{
Jeff Mahoney86557862015-06-15 09:41:16 -04001238 int j, ret = 0;
1239 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001240 u64 aligned_start = ALIGN(start, 1 << 9);
1241
1242 if (WARN_ON(start != aligned_start)) {
1243 len -= aligned_start - start;
1244 len = round_down(len, 1 << 9);
1245 start = aligned_start;
1246 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001247
1248 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001249
1250 if (!len)
1251 return 0;
1252
1253 end = start + len;
1254 bytes_left = len;
1255
1256 /* Skip any superblocks on this device. */
1257 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1258 u64 sb_start = btrfs_sb_offset(j);
1259 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1260 u64 size = sb_start - start;
1261
1262 if (!in_range(sb_start, start, bytes_left) &&
1263 !in_range(sb_end, start, bytes_left) &&
1264 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1265 continue;
1266
1267 /*
1268 * Superblock spans beginning of range. Adjust start and
1269 * try again.
1270 */
1271 if (sb_start <= start) {
1272 start += sb_end - start;
1273 if (start > end) {
1274 bytes_left = 0;
1275 break;
1276 }
1277 bytes_left = end - start;
1278 continue;
1279 }
1280
1281 if (size) {
1282 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1283 GFP_NOFS, 0);
1284 if (!ret)
1285 *discarded_bytes += size;
1286 else if (ret != -EOPNOTSUPP)
1287 return ret;
1288 }
1289
1290 start = sb_end;
1291 if (start > end) {
1292 bytes_left = 0;
1293 break;
1294 }
1295 bytes_left = end - start;
1296 }
1297
1298 if (bytes_left) {
1299 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001300 GFP_NOFS, 0);
1301 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001302 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001303 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001304 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001305}
Chris Mason15916de2008-11-19 21:17:22 -05001306
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001307int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001308 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001309{
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001310 int ret = 0;
Li Dongyang5378e602011-03-24 10:24:27 +00001311 u64 discarded_bytes = 0;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001312 u64 end = bytenr + num_bytes;
1313 u64 cur = bytenr;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001314 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001315
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001316
Filipe Manana29992412016-05-27 17:42:05 +01001317 /*
1318 * Avoid races with device replace and make sure our bbio has devices
1319 * associated to its stripes that don't go away while we are discarding.
1320 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001321 btrfs_bio_counter_inc_blocked(fs_info);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001322 while (cur < end) {
1323 struct btrfs_bio_stripe *stripe;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001324 int i;
1325
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001326 num_bytes = end - cur;
1327 /* Tell the block device(s) that the sectors can be discarded */
1328 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1329 &num_bytes, &bbio, 0);
1330 /*
1331 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1332 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1333 * thus we can't continue anyway.
1334 */
1335 if (ret < 0)
1336 goto out;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001337
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001338 stripe = bbio->stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001339 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001340 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001341 struct request_queue *req_q;
1342
Filipe Manana627e0872018-01-30 18:40:22 +00001343 if (!stripe->dev->bdev) {
1344 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1345 continue;
1346 }
Anand Jain38b5f682017-11-29 18:53:43 +08001347 req_q = bdev_get_queue(stripe->dev->bdev);
1348 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001349 continue;
1350
Li Dongyang5378e602011-03-24 10:24:27 +00001351 ret = btrfs_issue_discard(stripe->dev->bdev,
1352 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001353 stripe->length,
1354 &bytes);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001355 if (!ret) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001356 discarded_bytes += bytes;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001357 } else if (ret != -EOPNOTSUPP) {
1358 /*
1359 * Logic errors or -ENOMEM, or -EIO, but
1360 * unlikely to happen.
1361 *
1362 * And since there are two loops, explicitly
1363 * go to out to avoid confusion.
1364 */
1365 btrfs_put_bbio(bbio);
1366 goto out;
1367 }
Josef Bacikd5e20032011-08-04 14:52:27 +00001368
1369 /*
1370 * Just in case we get back EOPNOTSUPP for some reason,
1371 * just ignore the return value so we don't screw up
1372 * people calling discard_extent.
1373 */
1374 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001375 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001376 btrfs_put_bbio(bbio);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001377 cur += num_bytes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001378 }
Qu Wenruo6b7faad2019-10-23 21:57:27 +08001379out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001380 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001381
1382 if (actual_bytes)
1383 *actual_bytes = discarded_bytes;
1384
Liu Hui1f3c79a2009-01-05 15:57:51 -05001385
David Woodhouse53b381b2013-01-29 18:40:14 -05001386 if (ret == -EOPNOTSUPP)
1387 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001388 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001389}
1390
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001393 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001394{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001395 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001396 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001397 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001398
Qu Wenruo82fa1132019-04-04 14:45:35 +08001399 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1400 generic_ref->action);
1401 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1402 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001403
Qu Wenruo82fa1132019-04-04 14:45:35 +08001404 if (generic_ref->type == BTRFS_REF_METADATA)
1405 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001406 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001407 else
1408 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001409 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001410
Qu Wenruo82fa1132019-04-04 14:45:35 +08001411 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001412
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001413 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001414 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001415
Zheng Yan31840ae2008-09-23 13:14:14 -04001416 return ret;
1417}
1418
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001419/*
1420 * __btrfs_inc_extent_ref - insert backreference for a given extent
1421 *
1422 * @trans: Handle of transaction
1423 *
1424 * @node: The delayed ref node used to get the bytenr/length for
1425 * extent whose references are incremented.
1426 *
1427 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1428 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1429 * bytenr of the parent block. Since new extents are always
1430 * created with indirect references, this will only be the case
1431 * when relocating a shared extent. In that case, root_objectid
1432 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1433 * be 0
1434 *
1435 * @root_objectid: The id of the root where this modification has originated,
1436 * this can be either one of the well-known metadata trees or
1437 * the subvolume id which references this extent.
1438 *
1439 * @owner: For data extents it is the inode number of the owning file.
1440 * For metadata extents this parameter holds the level in the
1441 * tree of the extent.
1442 *
1443 * @offset: For metadata extents the offset is ignored and is currently
1444 * always passed as 0. For data extents it is the fileoffset
1445 * this extent belongs to.
1446 *
1447 * @refs_to_add Number of references to add
1448 *
1449 * @extent_op Pointer to a structure, holding information necessary when
1450 * updating a tree block's flags
1451 *
1452 */
Chris Mason925baed2008-06-25 16:01:30 -04001453static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001454 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455 u64 parent, u64 root_objectid,
1456 u64 owner, u64 offset, int refs_to_add,
1457 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001458{
Chris Mason5caf2a02007-04-02 11:20:42 -04001459 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001461 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001462 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001463 u64 bytenr = node->bytenr;
1464 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001465 u64 refs;
1466 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001467
Chris Mason5caf2a02007-04-02 11:20:42 -04001468 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001469 if (!path)
1470 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001471
David Sterbae4058b52015-11-27 16:31:35 +01001472 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001473 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001475 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1476 parent, root_objectid, owner,
1477 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001478 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001480
1481 /*
1482 * Ok we had -EAGAIN which means we didn't have space to insert and
1483 * inline extent ref, so just update the reference count and add a
1484 * normal backref.
1485 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001486 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001487 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1489 refs = btrfs_extent_refs(leaf, item);
1490 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1491 if (extent_op)
1492 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001493
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001494 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001495 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001496
David Sterbae4058b52015-11-27 16:31:35 +01001497 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001498 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001499 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001500 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1501 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001503 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504out:
Chris Mason74493f72007-12-11 09:25:06 -05001505 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001506 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001507}
1508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001509static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 struct btrfs_delayed_ref_node *node,
1511 struct btrfs_delayed_extent_op *extent_op,
1512 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001513{
Chris Mason56bec292009-03-13 10:10:06 -04001514 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515 struct btrfs_delayed_data_ref *ref;
1516 struct btrfs_key ins;
1517 u64 parent = 0;
1518 u64 ref_root = 0;
1519 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 ins.objectid = node->bytenr;
1522 ins.offset = node->num_bytes;
1523 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001525 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001526 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001527
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1529 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001530 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001531
1532 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001533 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001534 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03001535 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1536 flags, ref->objectid,
1537 ref->offset, &ins,
1538 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001540 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1541 ref->objectid, ref->offset,
1542 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001544 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001545 ref_root, ref->objectid,
1546 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001547 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001548 } else {
1549 BUG();
1550 }
Chris Mason56bec292009-03-13 10:10:06 -04001551 return ret;
1552}
1553
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1555 struct extent_buffer *leaf,
1556 struct btrfs_extent_item *ei)
1557{
1558 u64 flags = btrfs_extent_flags(leaf, ei);
1559 if (extent_op->update_flags) {
1560 flags |= extent_op->flags_to_set;
1561 btrfs_set_extent_flags(leaf, ei, flags);
1562 }
1563
1564 if (extent_op->update_key) {
1565 struct btrfs_tree_block_info *bi;
1566 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1567 bi = (struct btrfs_tree_block_info *)(ei + 1);
1568 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1569 }
1570}
1571
1572static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04001573 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001574 struct btrfs_delayed_extent_op *extent_op)
1575{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001576 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 struct btrfs_key key;
1578 struct btrfs_path *path;
1579 struct btrfs_extent_item *ei;
1580 struct extent_buffer *leaf;
1581 u32 item_size;
1582 int ret;
1583 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001584 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001585
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001586 if (trans->aborted)
1587 return 0;
1588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001589 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05001590 metadata = 0;
1591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592 path = btrfs_alloc_path();
1593 if (!path)
1594 return -ENOMEM;
1595
Josef Bacikd2788502017-09-29 15:43:57 -04001596 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001597
Josef Bacik3173a182013-03-07 14:22:04 -05001598 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05001599 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04001600 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05001601 } else {
1602 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04001603 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05001604 }
1605
1606again:
David Sterbae4058b52015-11-27 16:31:35 +01001607 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001608 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610 if (ret < 0) {
1611 err = ret;
1612 goto out;
1613 }
1614 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05001615 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001616 if (path->slots[0] > 0) {
1617 path->slots[0]--;
1618 btrfs_item_key_to_cpu(path->nodes[0], &key,
1619 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04001620 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001621 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04001622 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001623 ret = 0;
1624 }
1625 if (ret > 0) {
1626 btrfs_release_path(path);
1627 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001628
Josef Bacikd2788502017-09-29 15:43:57 -04001629 key.objectid = head->bytenr;
1630 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01001631 key.type = BTRFS_EXTENT_ITEM_KEY;
1632 goto again;
1633 }
1634 } else {
1635 err = -EIO;
1636 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05001637 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 }
1639
1640 leaf = path->nodes[0];
1641 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001642
David Sterba6d8ff4e2018-06-26 16:20:59 +02001643 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001644 err = -EINVAL;
1645 btrfs_print_v0_err(fs_info);
1646 btrfs_abort_transaction(trans, err);
1647 goto out;
1648 }
1649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001650 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1651 __run_delayed_extent_op(extent_op, leaf, ei);
1652
1653 btrfs_mark_buffer_dirty(leaf);
1654out:
1655 btrfs_free_path(path);
1656 return err;
1657}
1658
1659static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001660 struct btrfs_delayed_ref_node *node,
1661 struct btrfs_delayed_extent_op *extent_op,
1662 int insert_reserved)
1663{
1664 int ret = 0;
1665 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 u64 parent = 0;
1667 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668
1669 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001670 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08001671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001672 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1673 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07001674 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675
Liu Bo02794222016-09-14 19:19:05 -07001676 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001677 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07001678 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1679 node->bytenr, node->ref_mod, node->action, ref_root,
1680 parent);
1681 return -EIO;
1682 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05001684 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03001685 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03001687 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1688 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03001690 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001691 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001692 } else {
1693 BUG();
1694 }
1695 return ret;
1696}
1697
Chris Mason56bec292009-03-13 10:10:06 -04001698/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001700 struct btrfs_delayed_ref_node *node,
1701 struct btrfs_delayed_extent_op *extent_op,
1702 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04001703{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001704 int ret = 0;
1705
Josef Bacik857cc2f2013-10-07 15:21:08 -04001706 if (trans->aborted) {
1707 if (insert_reserved)
Nikolay Borisov5fac7f92018-06-20 15:49:11 +03001708 btrfs_pin_extent(trans->fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04001709 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001710 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04001711 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001713 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1714 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03001715 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 insert_reserved);
1717 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1718 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03001719 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 insert_reserved);
1721 else
1722 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04001723 if (ret && insert_reserved)
1724 btrfs_pin_extent(trans->fs_info, node->bytenr,
1725 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04001727}
1728
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001729static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04001730select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05001731{
Filipe Mananacffc3372015-07-09 13:13:44 +01001732 struct btrfs_delayed_ref_node *ref;
1733
Liu Boe3d03962018-08-23 03:51:50 +08001734 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08001735 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05001736
Filipe Mananacffc3372015-07-09 13:13:44 +01001737 /*
1738 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1739 * This is to prevent a ref count from going down to zero, which deletes
1740 * the extent item from the extent tree, when there still are references
1741 * to add, which would fail because they would not find the extent item.
1742 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001743 if (!list_empty(&head->ref_add_list))
1744 return list_first_entry(&head->ref_add_list,
1745 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01001746
Liu Boe3d03962018-08-23 03:51:50 +08001747 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04001748 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08001749 ASSERT(list_empty(&ref->add_list));
1750 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04001751}
1752
Josef Bacik2eadaa22017-09-29 15:43:52 -04001753static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1754 struct btrfs_delayed_ref_head *head)
1755{
1756 spin_lock(&delayed_refs->lock);
1757 head->processing = 0;
1758 delayed_refs->num_heads_ready++;
1759 spin_unlock(&delayed_refs->lock);
1760 btrfs_delayed_ref_unlock(head);
1761}
1762
Josef Bacikbedc66172018-12-03 10:20:31 -05001763static struct btrfs_delayed_extent_op *cleanup_extent_op(
1764 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04001765{
1766 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05001767
1768 if (!extent_op)
1769 return NULL;
1770
1771 if (head->must_insert_reserved) {
1772 head->extent_op = NULL;
1773 btrfs_free_delayed_extent_op(extent_op);
1774 return NULL;
1775 }
1776 return extent_op;
1777}
1778
1779static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1780 struct btrfs_delayed_ref_head *head)
1781{
1782 struct btrfs_delayed_extent_op *extent_op;
Josef Bacikb00e6252017-09-29 15:43:53 -04001783 int ret;
1784
Josef Bacikbedc66172018-12-03 10:20:31 -05001785 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04001786 if (!extent_op)
1787 return 0;
1788 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04001789 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03001790 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04001791 btrfs_free_delayed_extent_op(extent_op);
1792 return ret ? ret : 1;
1793}
1794
Josef Bacik31890da2018-11-21 14:05:41 -05001795void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1796 struct btrfs_delayed_ref_root *delayed_refs,
1797 struct btrfs_delayed_ref_head *head)
Josef Bacik07c47772018-12-03 10:20:30 -05001798{
Josef Bacikba2c4d42018-12-03 10:20:33 -05001799 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05001800
1801 if (head->total_ref_mod < 0) {
1802 struct btrfs_space_info *space_info;
1803 u64 flags;
1804
1805 if (head->is_data)
1806 flags = BTRFS_BLOCK_GROUP_DATA;
1807 else if (head->is_system)
1808 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1809 else
1810 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacik280c29082019-06-18 16:09:19 -04001811 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05001812 ASSERT(space_info);
1813 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1814 -head->num_bytes,
1815 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1816
Josef Bacikba2c4d42018-12-03 10:20:33 -05001817 /*
1818 * We had csum deletions accounted for in our delayed refs rsv,
1819 * we need to drop the csum leaves for this update from our
1820 * delayed_refs_rsv.
1821 */
Josef Bacik07c47772018-12-03 10:20:30 -05001822 if (head->is_data) {
1823 spin_lock(&delayed_refs->lock);
1824 delayed_refs->pending_csums -= head->num_bytes;
1825 spin_unlock(&delayed_refs->lock);
Josef Bacikba2c4d42018-12-03 10:20:33 -05001826 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1827 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05001828 }
1829 }
1830
Josef Bacikba2c4d42018-12-03 10:20:33 -05001831 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05001832}
1833
Josef Bacik194ab0b2017-09-29 15:43:54 -04001834static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04001835 struct btrfs_delayed_ref_head *head)
1836{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03001837
1838 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04001839 struct btrfs_delayed_ref_root *delayed_refs;
1840 int ret;
1841
1842 delayed_refs = &trans->transaction->delayed_refs;
1843
Josef Bacikbedc66172018-12-03 10:20:31 -05001844 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001845 if (ret < 0) {
1846 unselect_delayed_ref_head(delayed_refs, head);
1847 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1848 return ret;
1849 } else if (ret) {
1850 return ret;
1851 }
1852
1853 /*
1854 * Need to drop our head ref lock and re-acquire the delayed ref lock
1855 * and then re-check to make sure nobody got added.
1856 */
1857 spin_unlock(&head->lock);
1858 spin_lock(&delayed_refs->lock);
1859 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08001860 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04001861 spin_unlock(&head->lock);
1862 spin_unlock(&delayed_refs->lock);
1863 return 1;
1864 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05001865 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04001866 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03001867 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04001868
Josef Bacikc1103f72017-09-29 15:43:56 -04001869 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04001870 btrfs_pin_extent(fs_info, head->bytenr,
1871 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04001872 if (head->is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00001873 ret = btrfs_del_csums(trans, fs_info->csum_root,
1874 head->bytenr, head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04001875 }
1876 }
1877
Josef Bacik31890da2018-11-21 14:05:41 -05001878 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05001879
1880 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04001881 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04001882 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04001883 return 0;
1884}
1885
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001886static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1887 struct btrfs_trans_handle *trans)
1888{
1889 struct btrfs_delayed_ref_root *delayed_refs =
1890 &trans->transaction->delayed_refs;
1891 struct btrfs_delayed_ref_head *head = NULL;
1892 int ret;
1893
1894 spin_lock(&delayed_refs->lock);
Lu Fengqi5637c742018-10-11 13:40:33 +08001895 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001896 if (!head) {
1897 spin_unlock(&delayed_refs->lock);
1898 return head;
1899 }
1900
1901 /*
1902 * Grab the lock that says we are going to process all the refs for
1903 * this head
1904 */
Lu Fengqi9e920a62018-10-11 13:40:34 +08001905 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03001906 spin_unlock(&delayed_refs->lock);
1907
1908 /*
1909 * We may have dropped the spin lock to get the head mutex lock, and
1910 * that might have given someone else time to free the head. If that's
1911 * true, it has been removed from our list and we can move on.
1912 */
1913 if (ret == -EAGAIN)
1914 head = ERR_PTR(-EAGAIN);
1915
1916 return head;
1917}
1918
Nikolay Borisove7261382018-08-15 10:39:55 +03001919static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1920 struct btrfs_delayed_ref_head *locked_ref,
1921 unsigned long *run_refs)
1922{
1923 struct btrfs_fs_info *fs_info = trans->fs_info;
1924 struct btrfs_delayed_ref_root *delayed_refs;
1925 struct btrfs_delayed_extent_op *extent_op;
1926 struct btrfs_delayed_ref_node *ref;
1927 int must_insert_reserved = 0;
1928 int ret;
1929
1930 delayed_refs = &trans->transaction->delayed_refs;
1931
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03001932 lockdep_assert_held(&locked_ref->mutex);
1933 lockdep_assert_held(&locked_ref->lock);
1934
Nikolay Borisove7261382018-08-15 10:39:55 +03001935 while ((ref = select_delayed_ref(locked_ref))) {
1936 if (ref->seq &&
1937 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1938 spin_unlock(&locked_ref->lock);
1939 unselect_delayed_ref_head(delayed_refs, locked_ref);
1940 return -EAGAIN;
1941 }
1942
1943 (*run_refs)++;
1944 ref->in_tree = 0;
1945 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1946 RB_CLEAR_NODE(&ref->ref_node);
1947 if (!list_empty(&ref->add_list))
1948 list_del(&ref->add_list);
1949 /*
1950 * When we play the delayed ref, also correct the ref_mod on
1951 * head
1952 */
1953 switch (ref->action) {
1954 case BTRFS_ADD_DELAYED_REF:
1955 case BTRFS_ADD_DELAYED_EXTENT:
1956 locked_ref->ref_mod -= ref->ref_mod;
1957 break;
1958 case BTRFS_DROP_DELAYED_REF:
1959 locked_ref->ref_mod += ref->ref_mod;
1960 break;
1961 default:
1962 WARN_ON(1);
1963 }
1964 atomic_dec(&delayed_refs->num_entries);
1965
1966 /*
1967 * Record the must_insert_reserved flag before we drop the
1968 * spin lock.
1969 */
1970 must_insert_reserved = locked_ref->must_insert_reserved;
1971 locked_ref->must_insert_reserved = 0;
1972
1973 extent_op = locked_ref->extent_op;
1974 locked_ref->extent_op = NULL;
1975 spin_unlock(&locked_ref->lock);
1976
1977 ret = run_one_delayed_ref(trans, ref, extent_op,
1978 must_insert_reserved);
1979
1980 btrfs_free_delayed_extent_op(extent_op);
1981 if (ret) {
1982 unselect_delayed_ref_head(delayed_refs, locked_ref);
1983 btrfs_put_delayed_ref(ref);
1984 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1985 ret);
1986 return ret;
1987 }
1988
1989 btrfs_put_delayed_ref(ref);
1990 cond_resched();
1991
1992 spin_lock(&locked_ref->lock);
1993 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1994 }
1995
1996 return 0;
1997}
1998
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001999/*
2000 * Returns 0 on success or if called with an already aborted transaction.
2001 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2002 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002003static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002004 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002005{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002006 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002007 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002008 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002009 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002010 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002011 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002012 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002013
2014 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002015 do {
Chris Mason56bec292009-03-13 10:10:06 -04002016 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002017 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002018 if (IS_ERR_OR_NULL(locked_ref)) {
2019 if (PTR_ERR(locked_ref) == -EAGAIN) {
2020 continue;
2021 } else {
2022 break;
2023 }
Chris Mason56bec292009-03-13 10:10:06 -04002024 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002025 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002026 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002027 /*
2028 * We need to try and merge add/drops of the same ref since we
2029 * can run into issues with relocate dropping the implicit ref
2030 * and then it being added back again before the drop can
2031 * finish. If we merged anything we need to re-loop so we can
2032 * get a good ref.
2033 * Or we can get node references of the same type that weren't
2034 * merged when created due to bumps in the tree mod seq, and
2035 * we need to merge them to prevent adding an inline extent
2036 * backref before dropping it (triggering a BUG_ON at
2037 * insert_inline_extent_backref()).
2038 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002039 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002040 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002041
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002042 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2043 &actual_count);
2044 if (ret < 0 && ret != -EAGAIN) {
2045 /*
2046 * Error, btrfs_run_delayed_refs_for_head already
2047 * unlocked everything so just bail out
2048 */
2049 return ret;
2050 } else if (!ret) {
2051 /*
2052 * Success, perform the usual cleanup of a processed
2053 * head
2054 */
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002055 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002056 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002057 /* We dropped our lock, we need to loop. */
2058 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002059 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002060 } else if (ret) {
2061 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002062 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002063 }
Chris Mason56bec292009-03-13 10:10:06 -04002064
Josef Bacikb00e6252017-09-29 15:43:53 -04002065 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002066 * Either success case or btrfs_run_delayed_refs_for_head
2067 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002068 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002069
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002070 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002071 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002072 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002073
2074 /*
2075 * We don't want to include ref heads since we can have empty ref heads
2076 * and those will drastically skew our runtime down since we just do
2077 * accounting, no actual extent tree updates.
2078 */
2079 if (actual_count > 0) {
2080 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2081 u64 avg;
2082
2083 /*
2084 * We weigh the current average higher than our current runtime
2085 * to avoid large swings in the average.
2086 */
2087 spin_lock(&delayed_refs->lock);
2088 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002089 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002090 spin_unlock(&delayed_refs->lock);
2091 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002092 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002093}
2094
Arne Jansen709c0482011-09-12 12:22:57 +02002095#ifdef SCRAMBLE_DELAYED_REFS
2096/*
2097 * Normally delayed refs get processed in ascending bytenr order. This
2098 * correlates in most cases to the order added. To expose dependencies on this
2099 * order, we start to process the tree in the middle instead of the beginning
2100 */
2101static u64 find_middle(struct rb_root *root)
2102{
2103 struct rb_node *n = root->rb_node;
2104 struct btrfs_delayed_ref_node *entry;
2105 int alt = 1;
2106 u64 middle;
2107 u64 first = 0, last = 0;
2108
2109 n = rb_first(root);
2110 if (n) {
2111 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2112 first = entry->bytenr;
2113 }
2114 n = rb_last(root);
2115 if (n) {
2116 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2117 last = entry->bytenr;
2118 }
2119 n = root->rb_node;
2120
2121 while (n) {
2122 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2123 WARN_ON(!entry->in_tree);
2124
2125 middle = entry->bytenr;
2126
2127 if (alt)
2128 n = n->rb_left;
2129 else
2130 n = n->rb_right;
2131
2132 alt = 1 - alt;
2133 }
2134 return middle;
2135}
2136#endif
2137
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002138static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002139{
2140 u64 num_bytes;
2141
2142 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2143 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002145 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2146
2147 /*
2148 * 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 -04002149 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002150 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002151 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002152}
2153
Josef Bacik12621332015-02-03 07:50:16 -08002154/*
2155 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2156 * would require to store the csums for that many bytes.
2157 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002158u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002159{
2160 u64 csum_size;
2161 u64 num_csums_per_leaf;
2162 u64 num_csums;
2163
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002164 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002165 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002166 (u64)btrfs_super_csum_size(fs_info->super_copy));
2167 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002168 num_csums += num_csums_per_leaf - 1;
2169 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2170 return num_csums;
2171}
2172
Chris Masonc3e69d52009-03-13 10:17:05 -04002173/*
2174 * this starts processing the delayed reference count updates and
2175 * extent insertions we have queued up so far. count can be
2176 * 0, which means to process everything in the tree at the start
2177 * of the run (but not newly added entries), or it can be some target
2178 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179 *
2180 * Returns 0 on success or if called with an aborted transaction
2181 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002182 */
2183int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002184 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002185{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002186 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002187 struct rb_node *node;
2188 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002189 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002190 int ret;
2191 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002192
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002193 /* We'll clean this up in btrfs_cleanup_transaction */
2194 if (trans->aborted)
2195 return 0;
2196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002197 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002198 return 0;
2199
Chris Masonc3e69d52009-03-13 10:17:05 -04002200 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002201 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002202 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002203
Chris Masonc3e69d52009-03-13 10:17:05 -04002204again:
Arne Jansen709c0482011-09-12 12:22:57 +02002205#ifdef SCRAMBLE_DELAYED_REFS
2206 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2207#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002208 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002209 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002210 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002211 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002212 }
2213
Chris Mason56bec292009-03-13 10:10:06 -04002214 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002215 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002216
Josef Bacikd7df2c72014-01-23 09:21:38 -05002217 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002218 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002219 if (!node) {
2220 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002221 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002222 }
Josef Bacikd2788502017-09-29 15:43:57 -04002223 head = rb_entry(node, struct btrfs_delayed_ref_head,
2224 href_node);
2225 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002226 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002227
2228 /* Mutex was contended, block until it's released and retry. */
2229 mutex_lock(&head->mutex);
2230 mutex_unlock(&head->mutex);
2231
2232 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002233 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002234 goto again;
2235 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002236out:
Chris Masona28ec192007-03-06 20:08:01 -05002237 return 0;
2238}
2239
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002242 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243{
2244 struct btrfs_delayed_extent_op *extent_op;
2245 int ret;
2246
Miao Xie78a61842012-11-21 02:21:28 +00002247 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248 if (!extent_op)
2249 return -ENOMEM;
2250
2251 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002252 extent_op->update_flags = true;
2253 extent_op->update_key = false;
2254 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002255 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256
David Sterbac6e340b2019-03-20 11:42:34 +01002257 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002259 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 return ret;
2261}
2262
Liu Boe4c3b2d2017-01-30 12:25:28 -08002263static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002264 struct btrfs_path *path,
2265 u64 objectid, u64 offset, u64 bytenr)
2266{
2267 struct btrfs_delayed_ref_head *head;
2268 struct btrfs_delayed_ref_node *ref;
2269 struct btrfs_delayed_data_ref *data_ref;
2270 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08002271 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002272 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273 int ret = 0;
2274
ethanwu998ac6d2018-04-29 15:59:42 +08002275 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002276 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002277 if (cur_trans)
2278 refcount_inc(&cur_trans->use_count);
2279 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002280 if (!cur_trans)
2281 return 0;
2282
2283 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002284 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002285 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002286 if (!head) {
2287 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002288 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002289 return 0;
2290 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002291
2292 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002293 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294 spin_unlock(&delayed_refs->lock);
2295
David Sterbab3b4aa72011-04-21 01:20:15 +02002296 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297
David Sterba8cc33e52011-05-02 15:29:25 +02002298 /*
2299 * Mutex was contended, block until it's released and let
2300 * caller try again
2301 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002302 mutex_lock(&head->mutex);
2303 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002304 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002305 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002306 return -EAGAIN;
2307 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002308 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002309
2310 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002311 /*
2312 * XXX: We should replace this with a proper search function in the
2313 * future.
2314 */
Liu Boe3d03962018-08-23 03:51:50 +08002315 for (node = rb_first_cached(&head->ref_tree); node;
2316 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002317 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002318 /* If it's a shared ref we know a cross reference exists */
2319 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2320 ret = 1;
2321 break;
2322 }
2323
2324 data_ref = btrfs_delayed_node_to_data_ref(ref);
2325
2326 /*
2327 * If our ref doesn't match the one we're currently looking at
2328 * then we have a cross reference.
2329 */
2330 if (data_ref->root != root->root_key.objectid ||
2331 data_ref->objectid != objectid ||
2332 data_ref->offset != offset) {
2333 ret = 1;
2334 break;
2335 }
2336 }
2337 spin_unlock(&head->lock);
2338 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08002339 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340 return ret;
2341}
2342
Liu Boe4c3b2d2017-01-30 12:25:28 -08002343static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002344 struct btrfs_path *path,
2345 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002346{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002347 struct btrfs_fs_info *fs_info = root->fs_info;
2348 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002349 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002350 struct btrfs_extent_data_ref *ref;
2351 struct btrfs_extent_inline_ref *iref;
2352 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002353 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002354 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002355 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002356 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002357
Chris Masonbe20aa92007-12-17 20:14:01 -05002358 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002360 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002361
Chris Masonbe20aa92007-12-17 20:14:01 -05002362 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2363 if (ret < 0)
2364 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002365 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002366
2367 ret = -ENOENT;
2368 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002369 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002370
Zheng Yan31840ae2008-09-23 13:14:14 -04002371 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002372 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002376 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 ret = 1;
2379 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2381
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002382 /* If extent item has more than 1 inline ref then it's shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 if (item_size != sizeof(*ei) +
2384 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2385 goto out;
2386
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002387 /* If extent created before last snapshot => it's definitely shared */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002388 if (btrfs_extent_generation(leaf, ei) <=
2389 btrfs_root_last_snapshot(&root->root_item))
2390 goto out;
2391
2392 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06002393
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03002394 /* If this extent has SHARED_DATA_REF then it's shared */
Liu Bo3de28d52017-08-18 15:15:19 -06002395 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2396 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397 goto out;
2398
2399 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2400 if (btrfs_extent_refs(leaf, ei) !=
2401 btrfs_extent_data_ref_count(leaf, ref) ||
2402 btrfs_extent_data_ref_root(leaf, ref) !=
2403 root->root_key.objectid ||
2404 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2405 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2406 goto out;
2407
Yan Zhengf321e492008-07-30 09:26:11 -04002408 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002409out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 return ret;
2411}
2412
Liu Boe4c3b2d2017-01-30 12:25:28 -08002413int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2414 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415{
2416 struct btrfs_path *path;
2417 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418
2419 path = btrfs_alloc_path();
2420 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002421 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422
2423 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002424 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002425 offset, bytenr);
2426 if (ret && ret != -ENOENT)
2427 goto out;
2428
Misono Tomohiro380fd062018-08-30 10:59:16 +09002429 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2430 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002432out:
Yan Zhengf321e492008-07-30 09:26:11 -04002433 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002434 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2435 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002436 return ret;
2437}
2438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002439static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002440 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002441 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002442 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002444 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002446 u64 num_bytes;
2447 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002448 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002449 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002450 struct btrfs_key key;
2451 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002452 struct btrfs_ref generic_ref = { 0 };
2453 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002454 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002455 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002456 int level;
2457 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002458
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002459 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002460 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002461
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002463 nritems = btrfs_header_nritems(buf);
2464 level = btrfs_header_level(buf);
2465
Miao Xie27cdeb72014-04-02 19:51:05 +08002466 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002467 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002468
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002469 if (full_backref)
2470 parent = buf->start;
2471 else
2472 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002473 if (inc)
2474 action = BTRFS_ADD_DELAYED_REF;
2475 else
2476 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477
Zheng Yan31840ae2008-09-23 13:14:14 -04002478 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002479 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002480 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002481 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002482 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002483 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002484 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002485 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002486 BTRFS_FILE_EXTENT_INLINE)
2487 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002488 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2489 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002490 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002492 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2493 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002494 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2495 num_bytes, parent);
2496 generic_ref.real_root = root->root_key.objectid;
2497 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2498 key.offset);
2499 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002500 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002501 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002502 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002503 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 if (ret)
2505 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002506 } else {
2507 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002508 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002509 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2510 num_bytes, parent);
2511 generic_ref.real_root = root->root_key.objectid;
2512 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2513 generic_ref.skip_qgroup = for_reloc;
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002514 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002515 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002516 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002517 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002518 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002519 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002520 }
2521 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002522 return 0;
2523fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002524 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002525}
2526
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002528 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04002529{
Josef Bacike339a6b2014-07-02 10:54:25 -07002530 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531}
Zheng Yan31840ae2008-09-23 13:14:14 -04002532
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002533int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07002534 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535{
Josef Bacike339a6b2014-07-02 10:54:25 -07002536 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002537}
2538
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002539int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05002540{
David Sterba32da53862019-10-29 19:20:18 +01002541 struct btrfs_block_group *block_group;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002542 int readonly = 0;
2543
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002544 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002545 if (!block_group || block_group->ro)
2546 readonly = 1;
2547 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002548 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002549 return readonly;
2550}
2551
Jeff Mahoney1b868262017-05-17 11:38:35 -04002552static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002554 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04002555 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05002556 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002557
Yan, Zhengb742bb822010-05-16 10:46:24 -04002558 if (data)
2559 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002560 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04002561 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2562 else
2563 flags = BTRFS_BLOCK_GROUP_METADATA;
2564
Josef Bacik878d7b62019-06-20 15:38:05 -04002565 ret = btrfs_get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05002566 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002567}
2568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002569static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04002570{
David Sterba32da53862019-10-29 19:20:18 +01002571 struct btrfs_block_group *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002572 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002573
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002574 spin_lock(&fs_info->block_group_cache_lock);
2575 bytenr = fs_info->first_logical_byte;
2576 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00002577
2578 if (bytenr < (u64)-1)
2579 return bytenr;
2580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002581 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002582 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002583 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002584
David Sterbab3470b52019-10-23 18:48:22 +02002585 bytenr = cache->start;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002586 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002587
2588 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002589}
2590
David Sterba32da53862019-10-29 19:20:18 +01002591static int pin_down_extent(struct btrfs_block_group *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002592 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05002593{
David Sterbafdf08602019-03-20 12:12:32 +01002594 struct btrfs_fs_info *fs_info = cache->fs_info;
2595
Yan Zheng11833d62009-09-11 16:11:19 -04002596 spin_lock(&cache->space_info->lock);
2597 spin_lock(&cache->lock);
2598 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002599 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2600 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04002601 if (reserved) {
2602 cache->reserved -= num_bytes;
2603 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05002604 }
Yan Zheng11833d62009-09-11 16:11:19 -04002605 spin_unlock(&cache->lock);
2606 spin_unlock(&cache->space_info->lock);
2607
Ethan Liendec59fa2018-07-13 16:50:42 +08002608 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2609 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002610 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002611 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05002612 return 0;
2613}
Chris Mason9078a3e2007-04-26 16:46:15 -04002614
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002615int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002616 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04002617{
David Sterba32da53862019-10-29 19:20:18 +01002618 struct btrfs_block_group *cache;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002619
Nikolay Borisovce6d3eb2019-10-08 16:26:16 +03002620 ASSERT(fs_info->running_transaction);
2621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002622 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04002624
David Sterbafdf08602019-03-20 12:12:32 +01002625 pin_down_extent(cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002626
2627 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04002628 return 0;
2629}
Zheng Yane8569812008-09-26 10:05:48 -04002630
Yan, Zhengf0486c62010-05-16 10:46:25 -04002631/*
Chris Masone688b7252011-10-31 20:52:39 -04002632 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04002633 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002634int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04002635 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04002636{
David Sterba32da53862019-10-29 19:20:18 +01002637 struct btrfs_block_group *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002638 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04002639
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002640 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002641 if (!cache)
2642 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04002643
2644 /*
2645 * pull in the free space cache (if any) so that our pin
2646 * removes the free space from the cache. We have load_only set
2647 * to one because the slow code to read in the free extents does check
2648 * the pinned extents.
2649 */
Josef Bacik676f1f72019-06-20 15:37:48 -04002650 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04002651
David Sterbafdf08602019-03-20 12:12:32 +01002652 pin_down_extent(cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04002653
2654 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04002655 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04002656 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002657 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04002658}
2659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002660static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2661 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002662{
2663 int ret;
David Sterba32da53862019-10-29 19:20:18 +01002664 struct btrfs_block_group *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002665 struct btrfs_caching_control *caching_ctl;
2666
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002668 if (!block_group)
2669 return -EINVAL;
2670
Josef Bacik676f1f72019-06-20 15:37:48 -04002671 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04002672 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002673
2674 if (!caching_ctl) {
2675 /* Logic error */
David Sterba32da53862019-10-29 19:20:18 +01002676 BUG_ON(!btrfs_block_group_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002677 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2678 } else {
2679 mutex_lock(&caching_ctl->mutex);
2680
2681 if (start >= caching_ctl->progress) {
Josef Bacik6f410d12019-06-20 15:37:49 -04002682 ret = btrfs_add_excluded_extent(fs_info, start,
2683 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002684 } else if (start + num_bytes <= caching_ctl->progress) {
2685 ret = btrfs_remove_free_space(block_group,
2686 start, num_bytes);
2687 } else {
2688 num_bytes = caching_ctl->progress - start;
2689 ret = btrfs_remove_free_space(block_group,
2690 start, num_bytes);
2691 if (ret)
2692 goto out_lock;
2693
2694 num_bytes = (start + num_bytes) -
2695 caching_ctl->progress;
2696 start = caching_ctl->progress;
Josef Bacik6f410d12019-06-20 15:37:49 -04002697 ret = btrfs_add_excluded_extent(fs_info, start,
2698 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002699 }
2700out_lock:
2701 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04002702 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002703 }
2704 btrfs_put_block_group(block_group);
2705 return ret;
2706}
2707
David Sterbabcdc4282019-03-20 12:14:33 +01002708int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002709{
David Sterbabcdc4282019-03-20 12:14:33 +01002710 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002711 struct btrfs_file_extent_item *item;
2712 struct btrfs_key key;
2713 int found_type;
2714 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002715 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002716
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002717 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002718 return 0;
2719
2720 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2721 btrfs_item_key_to_cpu(eb, &key, i);
2722 if (key.type != BTRFS_EXTENT_DATA_KEY)
2723 continue;
2724 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2725 found_type = btrfs_file_extent_type(eb, item);
2726 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2727 continue;
2728 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2729 continue;
2730 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2731 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002732 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2733 if (ret)
2734 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002735 }
2736
Gu Jinxiangb89311e2018-05-22 17:46:51 +08002737 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04002738}
2739
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002740static void
David Sterba32da53862019-10-29 19:20:18 +01002741btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
Filipe Manana9cfa3e32016-04-26 15:39:32 +01002742{
2743 atomic_inc(&bg->reservations);
2744}
2745
David Sterba8b74c032017-02-10 19:20:56 +01002746void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04002747{
Yan Zheng11833d62009-09-11 16:11:19 -04002748 struct btrfs_caching_control *next;
2749 struct btrfs_caching_control *caching_ctl;
David Sterba32da53862019-10-29 19:20:18 +01002750 struct btrfs_block_group *cache;
Yan Zheng11833d62009-09-11 16:11:19 -04002751
Josef Bacik9e351cc2014-03-13 15:42:13 -04002752 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04002753
2754 list_for_each_entry_safe(caching_ctl, next,
2755 &fs_info->caching_block_groups, list) {
2756 cache = caching_ctl->block_group;
David Sterba32da53862019-10-29 19:20:18 +01002757 if (btrfs_block_group_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04002758 cache->last_byte_to_unpin = (u64)-1;
2759 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04002760 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04002761 } else {
2762 cache->last_byte_to_unpin = caching_ctl->progress;
2763 }
2764 }
2765
2766 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2767 fs_info->pinned_extents = &fs_info->freed_extents[1];
2768 else
2769 fs_info->pinned_extents = &fs_info->freed_extents[0];
2770
Josef Bacik9e351cc2014-03-13 15:42:13 -04002771 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002772
Josef Bacik67f9c222019-06-19 13:47:23 -04002773 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04002774}
2775
Josef Bacikc759c4e2015-10-02 15:25:10 -04002776/*
2777 * Returns the free cluster for the given space info and sets empty_cluster to
2778 * what it should be based on the mount options.
2779 */
2780static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002781fetch_cluster_info(struct btrfs_fs_info *fs_info,
2782 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04002783{
2784 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002785
2786 *empty_cluster = 0;
2787 if (btrfs_mixed_space_info(space_info))
2788 return ret;
2789
Josef Bacikc759c4e2015-10-02 15:25:10 -04002790 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002792 if (btrfs_test_opt(fs_info, SSD))
2793 *empty_cluster = SZ_2M;
2794 else
Byongho Leeee221842015-12-15 01:42:10 +09002795 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02002796 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2797 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2798 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002799 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002800 }
2801
2802 return ret;
2803}
2804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002805static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2806 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00002807 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04002808{
David Sterba32da53862019-10-29 19:20:18 +01002809 struct btrfs_block_group *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04002810 struct btrfs_space_info *space_info;
2811 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002812 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04002813 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002814 u64 total_unpinned = 0;
2815 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04002816 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04002817
2818 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04002819 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04002820 if (!cache ||
David Sterbab3470b52019-10-23 18:48:22 +02002821 start >= cache->start + cache->length) {
Yan Zheng11833d62009-09-11 16:11:19 -04002822 if (cache)
2823 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04002824 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04002825 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002826 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04002827
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002828 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04002829 cache->space_info,
2830 &empty_cluster);
2831 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04002832 }
2833
David Sterbab3470b52019-10-23 18:48:22 +02002834 len = cache->start + cache->length - start;
Yan Zheng11833d62009-09-11 16:11:19 -04002835 len = min(len, end + 1 - start);
2836
2837 if (start < cache->last_byte_to_unpin) {
2838 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00002839 if (return_free_space)
2840 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04002841 }
Josef Bacik25179202008-10-29 14:49:05 -04002842
Yan, Zhengf0486c62010-05-16 10:46:25 -04002843 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04002844 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04002845 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04002846
Josef Bacikc759c4e2015-10-02 15:25:10 -04002847 /*
2848 * If this space cluster has been marked as fragmented and we've
2849 * unpinned enough in this block group to potentially allow a
2850 * cluster to be created inside of it go ahead and clear the
2851 * fragmented check.
2852 */
2853 if (cluster && cluster->fragmented &&
2854 total_unpinned > empty_cluster) {
2855 spin_lock(&cluster->lock);
2856 cluster->fragmented = 0;
2857 spin_unlock(&cluster->lock);
2858 }
2859
Josef Bacik7b398f82012-10-22 15:52:28 -04002860 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002861 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002862 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002863 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacik4f4db212015-09-29 11:40:47 -04002864 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08002865 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2866 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04002867 if (cache->ro) {
2868 space_info->bytes_readonly += len;
2869 readonly = true;
2870 }
Josef Bacik25179202008-10-29 14:49:05 -04002871 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002872 if (!readonly && return_free_space &&
2873 global_rsv->space_info == space_info) {
2874 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03002875
Josef Bacik7b398f82012-10-22 15:52:28 -04002876 spin_lock(&global_rsv->lock);
2877 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04002878 to_add = min(len, global_rsv->size -
2879 global_rsv->reserved);
2880 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04002881 btrfs_space_info_update_bytes_may_use(fs_info,
2882 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04002883 if (global_rsv->reserved >= global_rsv->size)
2884 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04002885 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04002886 }
2887 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04002888 /* Add to any tickets we may have */
2889 if (len)
Josef Bacik18fa2282019-08-22 15:10:58 -04002890 btrfs_try_granting_tickets(fs_info,
2891 space_info);
Josef Bacik7b398f82012-10-22 15:52:28 -04002892 }
2893 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04002894 }
2895
2896 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002897 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04002898 return 0;
2899}
2900
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002901int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05002902{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02002903 struct btrfs_fs_info *fs_info = trans->fs_info;
David Sterba32da53862019-10-29 19:20:18 +01002904 struct btrfs_block_group *block_group, *tmp;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002905 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04002906 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04002907 u64 start;
2908 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002909 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002910
Yan Zheng11833d62009-09-11 16:11:19 -04002911 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
2912 unpin = &fs_info->freed_extents[1];
2913 else
2914 unpin = &fs_info->freed_extents[0];
2915
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002916 while (!trans->aborted) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00002917 struct extent_state *cached_state = NULL;
2918
Filipe Mananad4b450c2015-01-29 19:18:25 +00002919 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002920 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00002921 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002922 if (ret) {
2923 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002924 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00002925 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002926
Dennis Zhou46b27f52019-12-13 16:22:11 -08002927 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002928 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00002929 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002930
Filipe Manana0e6ec382018-11-16 13:04:44 +00002931 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002932 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00002933 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00002934 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04002935 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002936 }
Josef Bacik817d52f2009-07-13 21:29:25 -04002937
Dennis Zhoua2309302020-01-02 16:26:35 -05002938 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2939 btrfs_discard_calc_delay(&fs_info->discard_ctl);
Dennis Zhoub0643e52019-12-13 16:22:14 -08002940 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -05002941 }
Dennis Zhoub0643e52019-12-13 16:22:14 -08002942
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002943 /*
2944 * Transaction is finished. We don't need the lock anymore. We
2945 * do need to clean up the block groups in case of a transaction
2946 * abort.
2947 */
2948 deleted_bgs = &trans->transaction->deleted_bgs;
2949 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2950 u64 trimmed = 0;
2951
2952 ret = -EROFS;
2953 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002954 ret = btrfs_discard_extent(fs_info,
David Sterbab3470b52019-10-23 18:48:22 +02002955 block_group->start,
2956 block_group->length,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002957 &trimmed);
2958
2959 list_del_init(&block_group->bg_list);
2960 btrfs_put_block_group_trimming(block_group);
2961 btrfs_put_block_group(block_group);
2962
2963 if (ret) {
2964 const char *errstr = btrfs_decode_error(ret);
2965 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002966 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04002967 ret, errstr);
2968 }
2969 }
2970
Chris Masone20d96d2007-03-22 12:13:20 -04002971 return 0;
2972}
2973
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03002975 struct btrfs_delayed_ref_node *node, u64 parent,
2976 u64 root_objectid, u64 owner_objectid,
2977 u64 owner_offset, int refs_to_drop,
2978 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05002979{
Nikolay Borisove72cb922018-06-20 15:48:57 +03002980 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04002981 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002982 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04002983 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002984 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 struct btrfs_extent_item *ei;
2986 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05002987 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002988 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05002989 int extent_slot = 0;
2990 int found_extent = 0;
2991 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992 u32 item_size;
2993 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002994 u64 bytenr = node->bytenr;
2995 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07002996 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05002998
Chris Mason5caf2a02007-04-02 11:20:42 -04002999 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003000 if (!path)
3001 return -ENOMEM;
3002
David Sterbae4058b52015-11-27 16:31:35 +01003003 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04003004 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005
3006 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3007 BUG_ON(!is_data && refs_to_drop != 1);
3008
Josef Bacik3173a182013-03-07 14:22:04 -05003009 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02003010 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05003011
Nikolay Borisovfbe48012018-06-20 15:48:52 +03003012 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
3013 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05003015 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05003016 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 while (extent_slot >= 0) {
3018 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05003019 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05003021 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3023 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05003024 found_extent = 1;
3025 break;
3026 }
Josef Bacik3173a182013-03-07 14:22:04 -05003027 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3028 key.offset == owner_objectid) {
3029 found_extent = 1;
3030 break;
3031 }
Chris Mason952fcca2008-02-18 16:33:44 -05003032 if (path->slots[0] - extent_slot > 5)
3033 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003034 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05003035 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03003036
Zheng Yan31840ae2008-09-23 13:14:14 -04003037 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003038 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003039 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003040 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07003041 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003042 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003043 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003044 goto out;
3045 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003046 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04003047 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048
3049 key.objectid = bytenr;
3050 key.type = BTRFS_EXTENT_ITEM_KEY;
3051 key.offset = num_bytes;
3052
Josef Bacik3173a182013-03-07 14:22:04 -05003053 if (!is_data && skinny_metadata) {
3054 key.type = BTRFS_METADATA_ITEM_KEY;
3055 key.offset = owner_objectid;
3056 }
3057
Zheng Yan31840ae2008-09-23 13:14:14 -04003058 ret = btrfs_search_slot(trans, extent_root,
3059 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05003060 if (ret > 0 && skinny_metadata && path->slots[0]) {
3061 /*
3062 * Couldn't find our skinny metadata item,
3063 * see if we have ye olde extent item.
3064 */
3065 path->slots[0]--;
3066 btrfs_item_key_to_cpu(path->nodes[0], &key,
3067 path->slots[0]);
3068 if (key.objectid == bytenr &&
3069 key.type == BTRFS_EXTENT_ITEM_KEY &&
3070 key.offset == num_bytes)
3071 ret = 0;
3072 }
3073
3074 if (ret > 0 && skinny_metadata) {
3075 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01003076 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05003077 key.type = BTRFS_EXTENT_ITEM_KEY;
3078 key.offset = num_bytes;
3079 btrfs_release_path(path);
3080 ret = btrfs_search_slot(trans, extent_root,
3081 &key, path, -1, 1);
3082 }
3083
Josef Bacikf3465ca2008-11-12 14:19:50 -05003084 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003085 btrfs_err(info,
3086 "umm, got %d back from search, was looking for %llu",
3087 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00003088 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02003089 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003090 }
David Sterba005d6422012-09-18 07:52:32 -06003091 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003092 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003093 goto out;
3094 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003095 extent_slot = path->slots[0];
3096 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303097 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02003098 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003099 btrfs_err(info,
3100 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003101 bytenr, parent, root_objectid, owner_objectid,
3102 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04003103 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04003104 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003105 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04003106 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003107 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05003108 }
Chris Mason5f39d392007-10-15 16:14:19 -04003109
3110 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02003112 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03003113 ret = -EINVAL;
3114 btrfs_print_v0_err(info);
3115 btrfs_abort_transaction(trans, ret);
3116 goto out;
3117 }
Chris Mason952fcca2008-02-18 16:33:44 -05003118 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04003119 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05003120 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3121 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003122 struct btrfs_tree_block_info *bi;
3123 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3124 bi = (struct btrfs_tree_block_info *)(ei + 1);
3125 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05003126 }
3127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04003129 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003130 btrfs_err(info,
3131 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
3132 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04003133 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04003134 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04003135 goto out;
3136 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003137 refs -= refs_to_drop;
3138
3139 if (refs > 0) {
3140 if (extent_op)
3141 __run_delayed_extent_op(extent_op, leaf, ei);
3142 /*
3143 * In the case of inline back ref, reference count will
3144 * be updated by remove_extent_backref
3145 */
3146 if (iref) {
3147 BUG_ON(!found_extent);
3148 } else {
3149 btrfs_set_extent_refs(leaf, ei, refs);
3150 btrfs_mark_buffer_dirty(leaf);
3151 }
3152 if (found_extent) {
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03003153 ret = remove_extent_backref(trans, path, iref,
3154 refs_to_drop, is_data,
3155 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06003156 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003157 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003158 goto out;
3159 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003160 }
3161 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 if (found_extent) {
3163 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08003164 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 if (iref) {
3166 BUG_ON(path->slots[0] != extent_slot);
3167 } else {
3168 BUG_ON(path->slots[0] != extent_slot + 1);
3169 path->slots[0] = extent_slot;
3170 num_to_del = 2;
3171 }
Chris Mason78fae272007-03-25 11:35:08 -04003172 }
Chris Masonb9473432009-03-13 11:00:37 -04003173
Josef Bacikfcebe452014-05-13 17:30:47 -07003174 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05003175 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3176 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06003177 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003178 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003179 goto out;
3180 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003181 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01003182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003183 if (is_data) {
Filipe Manana40e046a2019-12-05 16:58:30 +00003184 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3185 num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06003186 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003187 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003188 goto out;
3189 }
Chris Mason459931e2008-12-10 09:10:46 -05003190 }
3191
Nikolay Borisove7355e52018-05-10 15:44:55 +03003192 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003193 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003194 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07003195 goto out;
3196 }
3197
Josef Bacikade4b512019-06-20 15:38:01 -04003198 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06003199 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003200 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06003201 goto out;
3202 }
Chris Masona28ec192007-03-06 20:08:01 -05003203 }
Josef Bacikfcebe452014-05-13 17:30:47 -07003204 btrfs_release_path(path);
3205
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003206out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003207 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05003208 return ret;
3209}
3210
3211/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04003212 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04003213 * delayed ref for that extent as well. This searches the delayed ref tree for
3214 * a given extent, and if there are no other delayed refs to be processed, it
3215 * removes it from the tree.
3216 */
3217static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003218 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04003219{
3220 struct btrfs_delayed_ref_head *head;
3221 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003222 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04003223
3224 delayed_refs = &trans->transaction->delayed_refs;
3225 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003226 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04003227 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08003228 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04003229
Josef Bacikd7df2c72014-01-23 09:21:38 -05003230 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08003231 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04003232 goto out;
3233
Josef Bacikbedc66172018-12-03 10:20:31 -05003234 if (cleanup_extent_op(head) != NULL)
3235 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003236
Chris Mason1887be62009-03-13 10:11:24 -04003237 /*
3238 * waiting for the lock here would deadlock. If someone else has it
3239 * locked they are already in the process of dropping it anyway
3240 */
3241 if (!mutex_trylock(&head->mutex))
3242 goto out;
3243
Josef Bacikd7baffd2018-12-03 10:20:29 -05003244 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003245 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05003246
Josef Bacikd7df2c72014-01-23 09:21:38 -05003247 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04003248 spin_unlock(&delayed_refs->lock);
3249
Yan, Zhengf0486c62010-05-16 10:46:25 -04003250 BUG_ON(head->extent_op);
3251 if (head->must_insert_reserved)
3252 ret = 1;
3253
Josef Bacik31890da2018-11-21 14:05:41 -05003254 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003255 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003256 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003257 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04003258out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05003259 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08003260
3261out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04003262 spin_unlock(&delayed_refs->lock);
3263 return 0;
3264}
3265
Yan, Zhengf0486c62010-05-16 10:46:25 -04003266void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3267 struct btrfs_root *root,
3268 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02003269 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003271 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08003272 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04003273 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003274 int ret;
3275
Qu Wenruoed4f2552019-04-04 14:45:31 +08003276 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3277 buf->start, buf->len, parent);
3278 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3279 root->root_key.objectid);
3280
Yan, Zhengf0486c62010-05-16 10:46:25 -04003281 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07003282 int old_ref_mod, new_ref_mod;
3283
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003284 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08003285 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003286 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003287 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07003288 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003289 }
3290
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003291 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
David Sterba32da53862019-10-29 19:20:18 +01003292 struct btrfs_block_group *cache;
Filipe Manana62198722015-01-06 20:18:45 +00003293
Yan, Zhengf0486c62010-05-16 10:46:25 -04003294 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003295 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003296 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04003297 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003298 }
3299
Omar Sandoval4da8b762017-06-06 16:45:28 -07003300 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003301 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00003302
Yan, Zhengf0486c62010-05-16 10:46:25 -04003303 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
David Sterbafdf08602019-03-20 12:12:32 +01003304 pin_down_extent(cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00003305 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04003306 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003307 }
3308
3309 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3310
3311 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08003312 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00003313 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003314 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003315 }
3316out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04003317 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08003318 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003319
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07003320 if (last_ref) {
3321 /*
3322 * Deleting the buffer, clear the corrupt flag since it doesn't
3323 * matter anymore.
3324 */
3325 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3326 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04003327}
3328
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003329/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003330int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04003331{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003332 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07003333 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04003334 int ret;
3335
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04003336 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003337 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003338
Chris Mason56bec292009-03-13 10:10:06 -04003339 /*
3340 * tree log blocks never actually go into the extent allocation
3341 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04003342 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003343 if ((ref->type == BTRFS_REF_METADATA &&
3344 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3345 (ref->type == BTRFS_REF_DATA &&
3346 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
Chris Masonb9473432009-03-13 11:00:37 -04003347 /* unlocks the pinned mutex */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003348 btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07003349 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04003350 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003351 } else if (ref->type == BTRFS_REF_METADATA) {
3352 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07003353 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003354 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003355 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07003356 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04003357 }
Omar Sandovald7eae342017-06-06 16:45:31 -07003358
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003359 if (!((ref->type == BTRFS_REF_METADATA &&
3360 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3361 (ref->type == BTRFS_REF_DATA &&
3362 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3363 btrfs_ref_tree_mod(fs_info, ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08003364
Qu Wenruoddf30cf2019-04-04 14:45:34 +08003365 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08003366 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07003367
Chris Mason925baed2008-06-25 16:01:30 -04003368 return ret;
3369}
3370
Josef Bacik817d52f2009-07-13 21:29:25 -04003371enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02003372 LOOP_CACHING_NOWAIT,
3373 LOOP_CACHING_WAIT,
3374 LOOP_ALLOC_CHUNK,
3375 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04003376};
3377
Miao Xiee570fd22014-06-19 10:42:50 +08003378static inline void
David Sterba32da53862019-10-29 19:20:18 +01003379btrfs_lock_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003380 int delalloc)
3381{
3382 if (delalloc)
3383 down_read(&cache->data_rwsem);
3384}
3385
David Sterba32da53862019-10-29 19:20:18 +01003386static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003387 int delalloc)
3388{
3389 btrfs_get_block_group(cache);
3390 if (delalloc)
3391 down_read(&cache->data_rwsem);
3392}
3393
David Sterba32da53862019-10-29 19:20:18 +01003394static struct btrfs_block_group *btrfs_lock_cluster(
3395 struct btrfs_block_group *block_group,
Miao Xiee570fd22014-06-19 10:42:50 +08003396 struct btrfs_free_cluster *cluster,
3397 int delalloc)
3398{
David Sterba32da53862019-10-29 19:20:18 +01003399 struct btrfs_block_group *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003400
Miao Xiee570fd22014-06-19 10:42:50 +08003401 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003402 while (1) {
3403 used_bg = cluster->block_group;
3404 if (!used_bg)
3405 return NULL;
3406
3407 if (used_bg == block_group)
3408 return used_bg;
3409
3410 btrfs_get_block_group(used_bg);
3411
3412 if (!delalloc)
3413 return used_bg;
3414
3415 if (down_read_trylock(&used_bg->data_rwsem))
3416 return used_bg;
3417
3418 spin_unlock(&cluster->refill_lock);
3419
Liu Boe321f8a2016-11-30 16:11:04 -08003420 /* We should only have one-level nested. */
3421 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02003422
3423 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08003424 if (used_bg == cluster->block_group)
3425 return used_bg;
3426
3427 up_read(&used_bg->data_rwsem);
3428 btrfs_put_block_group(used_bg);
3429 }
Miao Xiee570fd22014-06-19 10:42:50 +08003430}
3431
3432static inline void
David Sterba32da53862019-10-29 19:20:18 +01003433btrfs_release_block_group(struct btrfs_block_group *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08003434 int delalloc)
3435{
3436 if (delalloc)
3437 up_read(&cache->data_rwsem);
3438 btrfs_put_block_group(cache);
3439}
3440
Josef Bacik817d52f2009-07-13 21:29:25 -04003441/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08003442 * Structure used internally for find_free_extent() function. Wraps needed
3443 * parameters.
3444 */
3445struct find_free_extent_ctl {
3446 /* Basic allocation info */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003447 u64 num_bytes;
3448 u64 empty_size;
3449 u64 flags;
3450 int delalloc;
3451
3452 /* Where to start the search inside the bg */
3453 u64 search_start;
3454
3455 /* For clustered allocation */
3456 u64 empty_cluster;
3457
3458 bool have_caching_bg;
3459 bool orig_have_caching_bg;
3460
3461 /* RAID index, converted from flags */
3462 int index;
3463
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003464 /*
3465 * Current loop number, check find_free_extent_update_loop() for details
3466 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08003467 int loop;
3468
3469 /*
3470 * Whether we're refilling a cluster, if true we need to re-search
3471 * current block group but don't try to refill the cluster again.
3472 */
3473 bool retry_clustered;
3474
3475 /*
3476 * Whether we're updating free space cache, if true we need to re-search
3477 * current block group but don't try updating free space cache again.
3478 */
3479 bool retry_unclustered;
3480
3481 /* If current block group is cached */
3482 int cached;
3483
3484 /* Max contiguous hole found */
3485 u64 max_extent_size;
3486
3487 /* Total free space from free space cache, not always contiguous */
3488 u64 total_free_space;
3489
3490 /* Found result */
3491 u64 found_offset;
3492};
3493
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003494
3495/*
3496 * Helper function for find_free_extent().
3497 *
3498 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3499 * Return -EAGAIN to inform caller that we need to re-search this block group
3500 * Return >0 to inform caller that we find nothing
3501 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3502 */
David Sterba32da53862019-10-29 19:20:18 +01003503static int find_free_extent_clustered(struct btrfs_block_group *bg,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003504 struct btrfs_free_cluster *last_ptr,
3505 struct find_free_extent_ctl *ffe_ctl,
David Sterba32da53862019-10-29 19:20:18 +01003506 struct btrfs_block_group **cluster_bg_ret)
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003507{
David Sterba32da53862019-10-29 19:20:18 +01003508 struct btrfs_block_group *cluster_bg;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003509 u64 aligned_cluster;
3510 u64 offset;
3511 int ret;
3512
3513 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3514 if (!cluster_bg)
3515 goto refill_cluster;
3516 if (cluster_bg != bg && (cluster_bg->ro ||
3517 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3518 goto release_cluster;
3519
3520 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
David Sterbab3470b52019-10-23 18:48:22 +02003521 ffe_ctl->num_bytes, cluster_bg->start,
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003522 &ffe_ctl->max_extent_size);
3523 if (offset) {
3524 /* We have a block, we're done */
3525 spin_unlock(&last_ptr->refill_lock);
3526 trace_btrfs_reserve_extent_cluster(cluster_bg,
3527 ffe_ctl->search_start, ffe_ctl->num_bytes);
3528 *cluster_bg_ret = cluster_bg;
3529 ffe_ctl->found_offset = offset;
3530 return 0;
3531 }
3532 WARN_ON(last_ptr->block_group != cluster_bg);
3533
3534release_cluster:
3535 /*
3536 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3537 * lets just skip it and let the allocator find whatever block it can
3538 * find. If we reach this point, we will have tried the cluster
3539 * allocator plenty of times and not have found anything, so we are
3540 * likely way too fragmented for the clustering stuff to find anything.
3541 *
3542 * However, if the cluster is taken from the current block group,
3543 * release the cluster first, so that we stand a better chance of
3544 * succeeding in the unclustered allocation.
3545 */
3546 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3547 spin_unlock(&last_ptr->refill_lock);
3548 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3549 return -ENOENT;
3550 }
3551
3552 /* This cluster didn't work out, free it and start over */
3553 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3554
3555 if (cluster_bg != bg)
3556 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3557
3558refill_cluster:
3559 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3560 spin_unlock(&last_ptr->refill_lock);
3561 return -ENOENT;
3562 }
3563
3564 aligned_cluster = max_t(u64,
3565 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3566 bg->full_stripe_len);
David Sterba2ceeae22019-03-20 13:53:49 +01003567 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3568 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003569 if (ret == 0) {
3570 /* Now pull our allocation out of this cluster */
3571 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3572 ffe_ctl->num_bytes, ffe_ctl->search_start,
3573 &ffe_ctl->max_extent_size);
3574 if (offset) {
3575 /* We found one, proceed */
3576 spin_unlock(&last_ptr->refill_lock);
3577 trace_btrfs_reserve_extent_cluster(bg,
3578 ffe_ctl->search_start,
3579 ffe_ctl->num_bytes);
3580 ffe_ctl->found_offset = offset;
3581 return 0;
3582 }
3583 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3584 !ffe_ctl->retry_clustered) {
3585 spin_unlock(&last_ptr->refill_lock);
3586
3587 ffe_ctl->retry_clustered = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003588 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003589 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3590 return -EAGAIN;
3591 }
3592 /*
3593 * At this point we either didn't find a cluster or we weren't able to
3594 * allocate a block from our cluster. Free the cluster we've been
3595 * trying to use, and go to the next block group.
3596 */
3597 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3598 spin_unlock(&last_ptr->refill_lock);
3599 return 1;
3600}
3601
Qu Wenruob4bd7452018-11-02 09:39:47 +08003602/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08003603 * Return >0 to inform caller that we find nothing
3604 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3605 * Return -EAGAIN to inform caller that we need to re-search this block group
3606 */
David Sterba32da53862019-10-29 19:20:18 +01003607static int find_free_extent_unclustered(struct btrfs_block_group *bg,
Qu Wenruoe1a41842018-11-02 09:39:49 +08003608 struct btrfs_free_cluster *last_ptr,
3609 struct find_free_extent_ctl *ffe_ctl)
3610{
3611 u64 offset;
3612
3613 /*
3614 * We are doing an unclustered allocation, set the fragmented flag so
3615 * we don't bother trying to setup a cluster again until we get more
3616 * space.
3617 */
3618 if (unlikely(last_ptr)) {
3619 spin_lock(&last_ptr->lock);
3620 last_ptr->fragmented = 1;
3621 spin_unlock(&last_ptr->lock);
3622 }
3623 if (ffe_ctl->cached) {
3624 struct btrfs_free_space_ctl *free_space_ctl;
3625
3626 free_space_ctl = bg->free_space_ctl;
3627 spin_lock(&free_space_ctl->tree_lock);
3628 if (free_space_ctl->free_space <
3629 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3630 ffe_ctl->empty_size) {
3631 ffe_ctl->total_free_space = max_t(u64,
3632 ffe_ctl->total_free_space,
3633 free_space_ctl->free_space);
3634 spin_unlock(&free_space_ctl->tree_lock);
3635 return 1;
3636 }
3637 spin_unlock(&free_space_ctl->tree_lock);
3638 }
3639
3640 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3641 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3642 &ffe_ctl->max_extent_size);
3643
3644 /*
3645 * If we didn't find a chunk, and we haven't failed on this block group
3646 * before, and this block group is in the middle of caching and we are
3647 * ok with waiting, then go ahead and wait for progress to be made, and
3648 * set @retry_unclustered to true.
3649 *
3650 * If @retry_unclustered is true then we've already waited on this
3651 * block group once and should move on to the next block group.
3652 */
3653 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3654 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
Josef Bacik676f1f72019-06-20 15:37:48 -04003655 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3656 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08003657 ffe_ctl->retry_unclustered = true;
3658 return -EAGAIN;
3659 } else if (!offset) {
3660 return 1;
3661 }
3662 ffe_ctl->found_offset = offset;
3663 return 0;
3664}
3665
3666/*
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003667 * Return >0 means caller needs to re-search for free extent
3668 * Return 0 means we have the needed free extent.
3669 * Return <0 means we failed to locate any free extent.
3670 */
3671static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3672 struct btrfs_free_cluster *last_ptr,
3673 struct btrfs_key *ins,
3674 struct find_free_extent_ctl *ffe_ctl,
3675 int full_search, bool use_cluster)
3676{
3677 struct btrfs_root *root = fs_info->extent_root;
3678 int ret;
3679
3680 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3681 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3682 ffe_ctl->orig_have_caching_bg = true;
3683
3684 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3685 ffe_ctl->have_caching_bg)
3686 return 1;
3687
3688 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3689 return 1;
3690
3691 if (ins->objectid) {
3692 if (!use_cluster && last_ptr) {
3693 spin_lock(&last_ptr->lock);
3694 last_ptr->window_start = ins->objectid;
3695 spin_unlock(&last_ptr->lock);
3696 }
3697 return 0;
3698 }
3699
3700 /*
3701 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3702 * caching kthreads as we move along
3703 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3704 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3705 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3706 * again
3707 */
3708 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3709 ffe_ctl->index = 0;
3710 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3711 /*
3712 * We want to skip the LOOP_CACHING_WAIT step if we
3713 * don't have any uncached bgs and we've already done a
3714 * full search through.
3715 */
3716 if (ffe_ctl->orig_have_caching_bg || !full_search)
3717 ffe_ctl->loop = LOOP_CACHING_WAIT;
3718 else
3719 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3720 } else {
3721 ffe_ctl->loop++;
3722 }
3723
3724 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3725 struct btrfs_trans_handle *trans;
3726 int exist = 0;
3727
3728 trans = current->journal_info;
3729 if (trans)
3730 exist = 1;
3731 else
3732 trans = btrfs_join_transaction(root);
3733
3734 if (IS_ERR(trans)) {
3735 ret = PTR_ERR(trans);
3736 return ret;
3737 }
3738
Josef Bacikfc471cb2019-06-18 16:09:17 -04003739 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3740 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003741
3742 /*
3743 * If we can't allocate a new chunk we've already looped
3744 * through at least once, move on to the NO_EMPTY_SIZE
3745 * case.
3746 */
3747 if (ret == -ENOSPC)
3748 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3749
3750 /* Do not bail out on ENOSPC since we can do more. */
3751 if (ret < 0 && ret != -ENOSPC)
3752 btrfs_abort_transaction(trans, ret);
3753 else
3754 ret = 0;
3755 if (!exist)
3756 btrfs_end_transaction(trans);
3757 if (ret)
3758 return ret;
3759 }
3760
3761 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3762 /*
3763 * Don't loop again if we already have no empty_size and
3764 * no empty_cluster.
3765 */
3766 if (ffe_ctl->empty_size == 0 &&
3767 ffe_ctl->empty_cluster == 0)
3768 return -ENOSPC;
3769 ffe_ctl->empty_size = 0;
3770 ffe_ctl->empty_cluster = 0;
3771 }
3772 return 1;
3773 }
3774 return -ENOSPC;
3775}
3776
3777/*
Chris Masonfec577f2007-02-26 10:40:21 -05003778 * walks the btree of allocated extents and find a hole of a given size.
3779 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08003780 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04003781 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08003782 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05003783 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08003784 *
3785 * If there is no suitable free space, we will record the max size of
3786 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08003787 *
3788 * The overall logic and call chain:
3789 *
3790 * find_free_extent()
3791 * |- Iterate through all block groups
3792 * | |- Get a valid block group
3793 * | |- Try to do clustered allocation in that block group
3794 * | |- Try to do unclustered allocation in that block group
3795 * | |- Check if the result is valid
3796 * | | |- If valid, then exit
3797 * | |- Jump to next block group
3798 * |
3799 * |- Push harder to find free extents
3800 * |- If not found, re-iterate all block groups
Chris Masonfec577f2007-02-26 10:40:21 -05003801 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05003802static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08003803 u64 ram_bytes, u64 num_bytes, u64 empty_size,
3804 u64 hint_byte, struct btrfs_key *ins,
3805 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05003806{
Josef Bacik80eb2342008-10-29 14:49:05 -04003807 int ret = 0;
Josef Bacikdb8fe642019-11-19 13:59:00 -05003808 int cache_block_group_error = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04003809 struct btrfs_free_cluster *last_ptr = NULL;
David Sterba32da53862019-10-29 19:20:18 +01003810 struct btrfs_block_group *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08003811 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04003812 struct btrfs_space_info *space_info;
Josef Bacik67377732010-09-16 16:19:09 -04003813 bool use_cluster = true;
Josef Bacika5e681d2015-10-01 14:54:10 -04003814 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05003815
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003816 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003817
Qu Wenruob4bd7452018-11-02 09:39:47 +08003818 ffe_ctl.num_bytes = num_bytes;
3819 ffe_ctl.empty_size = empty_size;
3820 ffe_ctl.flags = flags;
3821 ffe_ctl.search_start = 0;
3822 ffe_ctl.retry_clustered = false;
3823 ffe_ctl.retry_unclustered = false;
3824 ffe_ctl.delalloc = delalloc;
3825 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
3826 ffe_ctl.have_caching_bg = false;
3827 ffe_ctl.orig_have_caching_bg = false;
3828 ffe_ctl.found_offset = 0;
3829
David Sterba962a2982014-06-04 18:41:45 +02003830 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04003831 ins->objectid = 0;
3832 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003833
Jeff Mahoney71ff6432016-09-06 16:00:42 -04003834 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05003835
Josef Bacik280c29082019-06-18 16:09:19 -04003836 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003837 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003838 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003839 return -ENOSPC;
3840 }
Josef Bacik2552d172009-04-03 10:14:19 -04003841
Josef Bacik67377732010-09-16 16:19:09 -04003842 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04003843 * If our free space is heavily fragmented we may not be able to make
3844 * big contiguous allocations, so instead of doing the expensive search
3845 * for free space, simply return ENOSPC with our max_extent_size so we
3846 * can go ahead and search for a more manageable chunk.
3847 *
3848 * If our max_extent_size is large enough for our allocation simply
3849 * disable clustering since we will likely not be able to find enough
3850 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04003851 */
Josef Bacik4f4db212015-09-29 11:40:47 -04003852 if (unlikely(space_info->max_extent_size)) {
3853 spin_lock(&space_info->lock);
3854 if (space_info->max_extent_size &&
3855 num_bytes > space_info->max_extent_size) {
3856 ins->offset = space_info->max_extent_size;
3857 spin_unlock(&space_info->lock);
3858 return -ENOSPC;
3859 } else if (space_info->max_extent_size) {
3860 use_cluster = false;
3861 }
3862 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003863 }
3864
Qu Wenruob4bd7452018-11-02 09:39:47 +08003865 last_ptr = fetch_cluster_info(fs_info, space_info,
3866 &ffe_ctl.empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04003867 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003868 spin_lock(&last_ptr->lock);
3869 if (last_ptr->block_group)
3870 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04003871 if (last_ptr->fragmented) {
3872 /*
3873 * We still set window_start so we can keep track of the
3874 * last place we found an allocation to try and save
3875 * some time.
3876 */
3877 hint_byte = last_ptr->window_start;
3878 use_cluster = false;
3879 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003880 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04003881 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04003882
Qu Wenruob4bd7452018-11-02 09:39:47 +08003883 ffe_ctl.search_start = max(ffe_ctl.search_start,
3884 first_logical_byte(fs_info, 0));
3885 ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte);
3886 if (ffe_ctl.search_start == hint_byte) {
3887 block_group = btrfs_lookup_block_group(fs_info,
3888 ffe_ctl.search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04003889 /*
3890 * we don't want to use the block group if it doesn't match our
3891 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05003892 *
3893 * However if we are re-searching with an ideal block group
3894 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04003895 */
David Sterbab6919a52013-04-29 13:39:40 +00003896 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05003897 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04003898 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04003899 if (list_empty(&block_group->list) ||
3900 block_group->ro) {
3901 /*
3902 * someone is removing this block group,
3903 * we can't jump into the have_block_group
3904 * target because our list pointers are not
3905 * valid
3906 */
3907 btrfs_put_block_group(block_group);
3908 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05003909 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08003910 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08003911 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08003912 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04003913 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05003914 }
Josef Bacik2552d172009-04-03 10:14:19 -04003915 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04003916 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04003917 }
Chris Mason42e70e72008-11-07 18:17:11 -05003918 }
Josef Bacik2552d172009-04-03 10:14:19 -04003919search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08003920 ffe_ctl.have_caching_bg = false;
3921 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
3922 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04003923 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04003924 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003925 list_for_each_entry(block_group,
3926 &space_info->block_groups[ffe_ctl.index], list) {
Jeff Mahoney14443932017-07-19 23:25:51 -04003927 /* If the block group is read-only, we can skip it entirely. */
3928 if (unlikely(block_group->ro))
3929 continue;
3930
Miao Xiee570fd22014-06-19 10:42:50 +08003931 btrfs_grab_block_group(block_group, delalloc);
David Sterbab3470b52019-10-23 18:48:22 +02003932 ffe_ctl.search_start = block_group->start;
Chris Mason42e70e72008-11-07 18:17:11 -05003933
Chris Mason83a50de2010-12-13 15:06:46 -05003934 /*
3935 * this can happen if we end up cycling through all the
3936 * raid types, but we want to make sure we only allocate
3937 * for the proper type.
3938 */
David Sterbab6919a52013-04-29 13:39:40 +00003939 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07003940 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02003941 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02003942 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05003943 BTRFS_BLOCK_GROUP_RAID10;
3944
3945 /*
3946 * if they asked for extra copies and this block group
3947 * doesn't provide them, bail. This does allow us to
3948 * fill raid0 from raid1.
3949 */
David Sterbab6919a52013-04-29 13:39:40 +00003950 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05003951 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08003952
3953 /*
3954 * This block group has different flags than we want.
3955 * It's possible that we have MIXED_GROUP flag but no
3956 * block group is mixed. Just skip such block group.
3957 */
3958 btrfs_release_block_group(block_group, delalloc);
3959 continue;
Chris Mason83a50de2010-12-13 15:06:46 -05003960 }
3961
Josef Bacik2552d172009-04-03 10:14:19 -04003962have_block_group:
David Sterba32da53862019-10-29 19:20:18 +01003963 ffe_ctl.cached = btrfs_block_group_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08003964 if (unlikely(!ffe_ctl.cached)) {
3965 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04003966 ret = btrfs_cache_block_group(block_group, 0);
Josef Bacikdb8fe642019-11-19 13:59:00 -05003967
3968 /*
3969 * If we get ENOMEM here or something else we want to
3970 * try other block groups, because it may not be fatal.
3971 * However if we can't find anything else we need to
3972 * save our return here so that we return the actual
3973 * error that caused problems, not ENOSPC.
3974 */
3975 if (ret < 0) {
3976 if (!cache_block_group_error)
3977 cache_block_group_error = ret;
3978 ret = 0;
3979 goto loop;
3980 }
Chris Mason1d4284b2012-03-28 20:31:37 -04003981 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05003982 }
3983
Josef Bacik36cce922013-08-05 11:15:21 -04003984 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
3985 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003986
Josef Bacik0a243252009-09-11 16:11:20 -04003987 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05003988 * Ok we want to try and use the cluster allocator, so
3989 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04003990 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04003991 if (last_ptr && use_cluster) {
David Sterba32da53862019-10-29 19:20:18 +01003992 struct btrfs_block_group *cluster_bg = NULL;
Chris Mason44fb5512009-06-04 15:34:51 -04003993
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003994 ret = find_free_extent_clustered(block_group, last_ptr,
3995 &ffe_ctl, &cluster_bg);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05003996
Qu Wenruod06e3bb2018-11-02 09:39:48 +08003997 if (ret == 0) {
3998 if (cluster_bg && cluster_bg != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08003999 btrfs_release_block_group(block_group,
4000 delalloc);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004001 block_group = cluster_bg;
Miao Xie215a63d2014-01-15 20:00:56 +08004002 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004003 goto checks;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004004 } else if (ret == -EAGAIN) {
Josef Bacik817d52f2009-07-13 21:29:25 -04004005 goto have_block_group;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004006 } else if (ret > 0) {
4007 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004008 }
Qu Wenruod06e3bb2018-11-02 09:39:48 +08004009 /* ret == -ENOENT case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004010 }
4011
Qu Wenruoe1a41842018-11-02 09:39:49 +08004012 ret = find_free_extent_unclustered(block_group, last_ptr,
4013 &ffe_ctl);
4014 if (ret == -EAGAIN)
Josef Bacik817d52f2009-07-13 21:29:25 -04004015 goto have_block_group;
Qu Wenruoe1a41842018-11-02 09:39:49 +08004016 else if (ret > 0)
Josef Bacik1cdda9b2009-10-06 10:04:28 -04004017 goto loop;
Qu Wenruoe1a41842018-11-02 09:39:49 +08004018 /* ret == 0 case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04004019checks:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004020 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4021 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04004022
Josef Bacik2552d172009-04-03 10:14:19 -04004023 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004024 if (ffe_ctl.search_start + num_bytes >
David Sterbab3470b52019-10-23 18:48:22 +02004025 block_group->start + block_group->length) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004026 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4027 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04004028 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04004029 }
Josef Bacik80eb2342008-10-29 14:49:05 -04004030
Qu Wenruob4bd7452018-11-02 09:39:47 +08004031 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4032 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4033 ffe_ctl.search_start - ffe_ctl.found_offset);
Josef Bacik6226cb02009-04-03 10:14:18 -04004034
Wang Xiaoguang18513092016-07-25 15:51:40 +08004035 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4036 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004037 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004038 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4039 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004040 goto loop;
4041 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004042 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04004043
Josef Bacik2552d172009-04-03 10:14:19 -04004044 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08004045 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004046 ins->offset = num_bytes;
4047
Qu Wenruob4bd7452018-11-02 09:39:47 +08004048 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4049 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004050 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04004051 break;
4052loop:
Qu Wenruob4bd7452018-11-02 09:39:47 +08004053 ffe_ctl.retry_clustered = false;
4054 ffe_ctl.retry_unclustered = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004055 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
Qu Wenruob4bd7452018-11-02 09:39:47 +08004056 ffe_ctl.index);
Miao Xiee570fd22014-06-19 10:42:50 +08004057 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04004058 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04004059 }
4060 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05004061
Qu Wenruoe72d79d2018-11-02 09:39:50 +08004062 ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl,
4063 full_search, use_cluster);
4064 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08004065 goto search;
4066
Josef Bacikdb8fe642019-11-19 13:59:00 -05004067 if (ret == -ENOSPC && !cache_block_group_error) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08004068 /*
4069 * Use ffe_ctl->total_free_space as fallback if we can't find
4070 * any contiguous hole.
4071 */
4072 if (!ffe_ctl.max_extent_size)
4073 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
Josef Bacik4f4db212015-09-29 11:40:47 -04004074 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004075 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04004076 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08004077 ins->offset = ffe_ctl.max_extent_size;
Josef Bacikdb8fe642019-11-19 13:59:00 -05004078 } else if (ret == -ENOSPC) {
4079 ret = cache_block_group_error;
Josef Bacik4f4db212015-09-29 11:40:47 -04004080 }
Chris Mason0f70abe2007-02-28 16:46:22 -05004081 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05004082}
Chris Masonec44a352008-04-28 15:29:52 -04004083
Nikolay Borisov6f47c702018-03-13 12:22:32 +02004084/*
4085 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4086 * hole that is at least as big as @num_bytes.
4087 *
4088 * @root - The root that will contain this extent
4089 *
4090 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4091 * is used for accounting purposes. This value differs
4092 * from @num_bytes only in the case of compressed extents.
4093 *
4094 * @num_bytes - Number of bytes to allocate on-disk.
4095 *
4096 * @min_alloc_size - Indicates the minimum amount of space that the
4097 * allocator should try to satisfy. In some cases
4098 * @num_bytes may be larger than what is required and if
4099 * the filesystem is fragmented then allocation fails.
4100 * However, the presence of @min_alloc_size gives a
4101 * chance to try and satisfy the smaller allocation.
4102 *
4103 * @empty_size - A hint that you plan on doing more COW. This is the
4104 * size in bytes the allocator should try to find free
4105 * next to the block it returns. This is just a hint and
4106 * may be ignored by the allocator.
4107 *
4108 * @hint_byte - Hint to the allocator to start searching above the byte
4109 * address passed. It might be ignored.
4110 *
4111 * @ins - This key is modified to record the found hole. It will
4112 * have the following values:
4113 * ins->objectid == start position
4114 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4115 * ins->offset == the size of the hole.
4116 *
4117 * @is_data - Boolean flag indicating whether an extent is
4118 * allocated for data (true) or metadata (false)
4119 *
4120 * @delalloc - Boolean flag indicating whether this allocation is for
4121 * delalloc or not. If 'true' data_rwsem of block groups
4122 * is going to be acquired.
4123 *
4124 *
4125 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4126 * case -ENOSPC is returned then @ins->offset will contain the size of the
4127 * largest available hole the allocator managed to find.
4128 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08004129int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04004130 u64 num_bytes, u64 min_alloc_size,
4131 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08004132 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05004133{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004134 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04004135 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00004136 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05004137 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04004138
Jeff Mahoney1b868262017-05-17 11:38:35 -04004139 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04004140again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004141 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004142 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004143 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004144 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004145 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004146 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08004147 if (!final_tried && ins->offset) {
4148 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004149 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004150 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05004151 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08004152 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05004153 if (num_bytes == min_alloc_size)
4154 final_tried = true;
4155 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04004156 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05004157 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004158
Josef Bacik280c29082019-06-18 16:09:19 -04004159 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004161 "allocation failed flags %llu, wanted %llu",
4162 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01004163 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04004164 btrfs_dump_space_info(fs_info, sinfo,
4165 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05004166 }
Chris Mason925baed2008-06-25 16:01:30 -04004167 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004168
4169 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004170}
4171
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004172int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08004173 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04004174{
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004175 struct btrfs_block_group *cache;
4176
4177 cache = btrfs_lookup_block_group(fs_info, start);
4178 if (!cache) {
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004179 btrfs_err(fs_info, "Unable to find block group for %llu",
4180 start);
Nikolay Borisov7ef54d52019-11-21 14:03:30 +02004181 return -ENOSPC;
4182 }
4183
4184 btrfs_add_free_space(cache, start, len);
4185 btrfs_free_reserved_bytes(cache, len, delalloc);
4186 trace_btrfs_reserved_extent_free(fs_info, start, len);
4187
4188 btrfs_put_block_group(cache);
4189 return 0;
Chris Masone688b7252011-10-31 20:52:39 -04004190}
4191
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004192int btrfs_pin_reserved_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone688b7252011-10-31 20:52:39 -04004193{
Nikolay Borisova0fbf732019-11-21 14:03:31 +02004194 struct btrfs_block_group *cache;
4195 int ret = 0;
4196
4197 cache = btrfs_lookup_block_group(fs_info, start);
4198 if (!cache) {
4199 btrfs_err(fs_info, "unable to find block group for %llu", start);
4200 return -ENOSPC;
4201 }
4202
4203 ret = pin_down_extent(cache, start, len, 1);
4204 btrfs_put_block_group(cache);
4205 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004206}
4207
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004208static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004209 u64 parent, u64 root_objectid,
4210 u64 flags, u64 owner, u64 offset,
4211 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212{
Nikolay Borisovef89b822018-06-20 15:48:58 +03004213 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004214 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004215 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004216 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004217 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004218 struct extent_buffer *leaf;
4219 int type;
4220 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04004221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 if (parent > 0)
4223 type = BTRFS_SHARED_DATA_REF_KEY;
4224 else
4225 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04004226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05004228
4229 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004230 if (!path)
4231 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05004232
Chris Masonb9473432009-03-13 11:00:37 -04004233 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004234 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4235 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004236 if (ret) {
4237 btrfs_free_path(path);
4238 return ret;
4239 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004240
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004241 leaf = path->nodes[0];
4242 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05004243 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004244 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4245 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4246 btrfs_set_extent_flags(leaf, extent_item,
4247 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05004248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004249 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4250 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4251 if (parent > 0) {
4252 struct btrfs_shared_data_ref *ref;
4253 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4254 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4255 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4256 } else {
4257 struct btrfs_extent_data_ref *ref;
4258 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4259 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4260 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4261 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4262 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4263 }
Chris Mason47e4bb92008-02-01 14:51:59 -05004264
4265 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05004266 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04004267
Nikolay Borisov25a356d2018-05-10 15:44:54 +03004268 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004269 if (ret)
4270 return ret;
4271
Josef Bacikade4b512019-06-20 15:38:01 -04004272 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004274 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004275 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05004276 BUG();
4277 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004278 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004279 return ret;
4280}
4281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004283 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004284 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004285{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03004286 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004287 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004288 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004289 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 struct btrfs_tree_block_info *block_info;
4291 struct btrfs_extent_inline_ref *iref;
4292 struct btrfs_path *path;
4293 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004294 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05004295 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004296 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004297 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004298 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05004299
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004300 ref = btrfs_delayed_node_to_tree_ref(node);
4301
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004302 extent_key.objectid = node->bytenr;
4303 if (skinny_metadata) {
4304 extent_key.offset = ref->level;
4305 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4306 num_bytes = fs_info->nodesize;
4307 } else {
4308 extent_key.offset = node->num_bytes;
4309 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05004310 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004311 num_bytes = node->num_bytes;
4312 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004313
4314 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04004315 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07004316 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004317
4318 path->leave_spinning = 1;
4319 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004320 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07004322 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004323 return ret;
4324 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325
4326 leaf = path->nodes[0];
4327 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4328 struct btrfs_extent_item);
4329 btrfs_set_extent_refs(leaf, extent_item, 1);
4330 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4331 btrfs_set_extent_flags(leaf, extent_item,
4332 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004333
Josef Bacik3173a182013-03-07 14:22:04 -05004334 if (skinny_metadata) {
4335 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4336 } else {
4337 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03004338 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004339 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05004340 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4341 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004342
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004343 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004344 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4345 btrfs_set_extent_inline_ref_type(leaf, iref,
4346 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03004347 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004348 } else {
4349 btrfs_set_extent_inline_ref_type(leaf, iref,
4350 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004351 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004352 }
4353
4354 btrfs_mark_buffer_dirty(leaf);
4355 btrfs_free_path(path);
4356
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004357 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4358 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004359 if (ret)
4360 return ret;
4361
Josef Bacikade4b512019-06-20 15:38:01 -04004362 ret = btrfs_update_block_group(trans, extent_key.objectid,
4363 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004364 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004365 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004366 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004367 BUG();
4368 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04004369
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03004370 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004371 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004372 return ret;
4373}
4374
4375int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004376 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08004377 u64 offset, u64 ram_bytes,
4378 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004379{
Qu Wenruo76675592019-04-04 14:45:32 +08004380 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04004381 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04004382
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004383 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04004384
Qu Wenruo76675592019-04-04 14:45:32 +08004385 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4386 ins->objectid, ins->offset, 0);
4387 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004388 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08004389 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4390 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004391 return ret;
4392}
Chris Masone02119d2008-09-05 16:13:11 -04004393
4394/*
4395 * this is used by the tree logging recovery code. It records that
4396 * an extent has been allocated and makes sure to clear the free
4397 * space cache bits as well
4398 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004399int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004400 u64 root_objectid, u64 owner, u64 offset,
4401 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04004402{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03004403 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004404 int ret;
David Sterba32da53862019-10-29 19:20:18 +01004405 struct btrfs_block_group *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004406 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004407
4408 /*
4409 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04004410 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004411 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004412 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004413 ret = __exclude_logged_extent(fs_info, ins->objectid,
4414 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004415 if (ret)
4416 return ret;
4417 }
Chris Masone02119d2008-09-05 16:13:11 -04004418
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004419 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004420 if (!block_group)
4421 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04004422
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08004423 space_info = block_group->space_info;
4424 spin_lock(&space_info->lock);
4425 spin_lock(&block_group->lock);
4426 space_info->bytes_reserved += ins->offset;
4427 block_group->reserved += ins->offset;
4428 spin_unlock(&block_group->lock);
4429 spin_unlock(&space_info->lock);
4430
Nikolay Borisovef89b822018-06-20 15:48:58 +03004431 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4432 offset, ins, 1);
Josef Bacikbd727172020-02-13 10:47:30 -05004433 if (ret)
4434 btrfs_pin_extent(fs_info, ins->objectid, ins->offset, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004435 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04004436 return ret;
4437}
4438
Eric Sandeen48a3b632013-04-25 20:41:01 +00004439static struct extent_buffer *
4440btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004441 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04004442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04004444 struct extent_buffer *buf;
4445
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004446 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004447 if (IS_ERR(buf))
4448 return buf;
4449
Qu Wenruob72c3ab2018-08-21 09:53:47 +08004450 /*
4451 * Extra safety check in case the extent tree is corrupted and extent
4452 * allocator chooses to use a tree block which is already used and
4453 * locked.
4454 */
4455 if (buf->lock_owner == current->pid) {
4456 btrfs_err_rl(fs_info,
4457"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4458 buf->start, btrfs_header_owner(buf), current->pid);
4459 free_extent_buffer(buf);
4460 return ERR_PTR(-EUCLEAN);
4461 }
4462
Chris Mason85d4e462011-07-26 16:11:19 -04004463 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04004464 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01004465 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05004466 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004467
David Sterba8bead252018-04-04 02:03:48 +02004468 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01004469 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004470
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004471 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4472 btrfs_set_header_level(buf, level);
4473 btrfs_set_header_bytenr(buf, buf->start);
4474 btrfs_set_header_generation(buf, trans->transid);
4475 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4476 btrfs_set_header_owner(buf, owner);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02004477 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004478 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04004479 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01004480 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004481 /*
4482 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01004483 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004484 */
Filipe Manana656f30d2014-09-26 12:25:56 +01004485 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00004486 set_extent_dirty(&root->dirty_log_pages, buf->start,
4487 buf->start + buf->len - 1, GFP_NOFS);
4488 else
4489 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02004490 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04004491 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01004492 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04004493 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4494 buf->start + buf->len - 1, GFP_NOFS);
4495 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04004496 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004497 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04004498 return buf;
4499}
4500
Chris Masonfec577f2007-02-26 10:40:21 -05004501/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004502 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08004503 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05004504 */
David Sterba4d75f8a2014-06-15 01:54:12 +02004505struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08004506 struct btrfs_root *root,
4507 u64 parent, u64 root_objectid,
4508 const struct btrfs_disk_key *key,
4509 int level, u64 hint,
4510 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05004511{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004512 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004513 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004514 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04004515 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004516 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004517 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04004518 u64 flags = 0;
4519 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004520 u32 blocksize = fs_info->nodesize;
4521 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004522
David Sterba05653ef2016-07-15 15:23:37 +02004523#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004524 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004525 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004526 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004527 if (!IS_ERR(buf))
4528 root->alloc_bytenr += blocksize;
4529 return buf;
4530 }
David Sterba05653ef2016-07-15 15:23:37 +02004531#endif
David Sterbafccb84c2014-09-29 23:53:21 +02004532
Josef Bacik67f9c222019-06-19 13:47:23 -04004533 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004534 if (IS_ERR(block_rsv))
4535 return ERR_CAST(block_rsv);
4536
Wang Xiaoguang18513092016-07-25 15:51:40 +08004537 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08004538 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004539 if (ret)
4540 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05004541
Nikolay Borisovbc877d22018-06-18 14:13:19 +03004542 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4543 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08004544 if (IS_ERR(buf)) {
4545 ret = PTR_ERR(buf);
4546 goto out_free_reserved;
4547 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004548
4549 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4550 if (parent == 0)
4551 parent = ins.objectid;
4552 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4553 } else
4554 BUG_ON(parent > 0);
4555
4556 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00004557 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08004558 if (!extent_op) {
4559 ret = -ENOMEM;
4560 goto out_free_buf;
4561 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004562 if (key)
4563 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4564 else
4565 memset(&extent_op->key, 0, sizeof(extent_op->key));
4566 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01004567 extent_op->update_key = skinny_metadata ? false : true;
4568 extent_op->update_flags = true;
4569 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04004570 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004571
Qu Wenruoed4f2552019-04-04 14:45:31 +08004572 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4573 ins.objectid, ins.offset, parent);
4574 generic_ref.real_root = root->root_key.objectid;
4575 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004576 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004577 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07004578 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08004579 if (ret)
4580 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004581 }
Chris Masonfec577f2007-02-26 10:40:21 -05004582 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08004583
4584out_free_delayed:
4585 btrfs_free_delayed_extent_op(extent_op);
4586out_free_buf:
4587 free_extent_buffer(buf);
4588out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004589 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08004590out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04004591 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08004592 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05004593}
Chris Masona28ec192007-03-06 20:08:01 -05004594
Yan Zheng2c47e6052009-06-27 21:07:35 -04004595struct walk_control {
4596 u64 refs[BTRFS_MAX_LEVEL];
4597 u64 flags[BTRFS_MAX_LEVEL];
4598 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05004599 struct btrfs_key drop_progress;
4600 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004601 int stage;
4602 int level;
4603 int shared_level;
4604 int update_ref;
4605 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004606 int reada_slot;
4607 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05004608 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004609};
4610
4611#define DROP_REFERENCE 1
4612#define UPDATE_BACKREF 2
4613
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004614static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4615 struct btrfs_root *root,
4616 struct walk_control *wc,
4617 struct btrfs_path *path)
4618{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004619 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004620 u64 bytenr;
4621 u64 generation;
4622 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004623 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004624 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004625 struct btrfs_key key;
4626 struct extent_buffer *eb;
4627 int ret;
4628 int slot;
4629 int nread = 0;
4630
4631 if (path->slots[wc->level] < wc->reada_slot) {
4632 wc->reada_count = wc->reada_count * 2 / 3;
4633 wc->reada_count = max(wc->reada_count, 2);
4634 } else {
4635 wc->reada_count = wc->reada_count * 3 / 2;
4636 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004637 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004638 }
4639
4640 eb = path->nodes[wc->level];
4641 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004642
4643 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4644 if (nread >= wc->reada_count)
4645 break;
4646
4647 cond_resched();
4648 bytenr = btrfs_node_blockptr(eb, slot);
4649 generation = btrfs_node_ptr_generation(eb, slot);
4650
4651 if (slot == path->slots[wc->level])
4652 goto reada;
4653
4654 if (wc->stage == UPDATE_BACKREF &&
4655 generation <= root->root_key.offset)
4656 continue;
4657
Yan, Zheng94fcca92009-10-09 09:25:16 -04004658 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004659 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05004660 wc->level - 1, 1, &refs,
4661 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004662 /* We don't care about errors in readahead. */
4663 if (ret < 0)
4664 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004665 BUG_ON(refs == 0);
4666
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004667 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004668 if (refs == 1)
4669 goto reada;
4670
Yan, Zheng94fcca92009-10-09 09:25:16 -04004671 if (wc->level == 1 &&
4672 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4673 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004674 if (!wc->update_ref ||
4675 generation <= root->root_key.offset)
4676 continue;
4677 btrfs_node_key_to_cpu(eb, &key, slot);
4678 ret = btrfs_comp_cpu_keys(&key,
4679 &wc->update_progress);
4680 if (ret < 0)
4681 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004682 } else {
4683 if (wc->level == 1 &&
4684 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4685 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004686 }
4687reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004688 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004689 nread++;
4690 }
4691 wc->reada_slot = slot;
4692}
4693
Chris Mason9aca1d52007-03-13 11:09:37 -04004694/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004695 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04004696 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04004697 * when wc->stage == UPDATE_BACKREF, this function updates
4698 * back refs for pointers in the block.
4699 *
4700 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04004701 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004702static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4703 struct btrfs_root *root,
4704 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004705 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04004706{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004707 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004708 int level = wc->level;
4709 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04004710 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4711 int ret;
4712
4713 if (wc->stage == UPDATE_BACKREF &&
4714 btrfs_header_owner(eb) != root->root_key.objectid)
4715 return 1;
4716
4717 /*
4718 * when reference count of tree block is 1, it won't increase
4719 * again. once full backref flag is set, we never clear it.
4720 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04004721 if (lookup_info &&
4722 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4723 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04004724 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004725 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05004726 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04004727 &wc->refs[level],
4728 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004729 BUG_ON(ret == -ENOMEM);
4730 if (ret)
4731 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04004732 BUG_ON(wc->refs[level] == 0);
4733 }
4734
Yan Zheng2c47e6052009-06-27 21:07:35 -04004735 if (wc->stage == DROP_REFERENCE) {
4736 if (wc->refs[level] > 1)
4737 return 1;
4738
4739 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04004740 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004741 path->locks[level] = 0;
4742 }
4743 return 0;
4744 }
4745
4746 /* wc->stage == UPDATE_BACKREF */
4747 if (!(wc->flags[level] & flag)) {
4748 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07004749 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004750 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07004751 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004752 BUG_ON(ret); /* -ENOMEM */
David Sterbaf5c8daa2019-03-20 11:43:36 +01004753 ret = btrfs_set_disk_extent_flags(trans, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04004754 eb->len, flag,
4755 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004756 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04004757 wc->flags[level] |= flag;
4758 }
4759
4760 /*
4761 * the block is shared by multiple trees, so it's not good to
4762 * keep the tree lock
4763 */
4764 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04004765 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04004766 path->locks[level] = 0;
4767 }
4768 return 0;
4769}
4770
4771/*
Josef Bacik78c52d92019-02-06 15:46:14 -05004772 * This is used to verify a ref exists for this root to deal with a bug where we
4773 * would have a drop_progress key that hadn't been updated properly.
4774 */
4775static int check_ref_exists(struct btrfs_trans_handle *trans,
4776 struct btrfs_root *root, u64 bytenr, u64 parent,
4777 int level)
4778{
4779 struct btrfs_path *path;
4780 struct btrfs_extent_inline_ref *iref;
4781 int ret;
4782
4783 path = btrfs_alloc_path();
4784 if (!path)
4785 return -ENOMEM;
4786
4787 ret = lookup_extent_backref(trans, path, &iref, bytenr,
4788 root->fs_info->nodesize, parent,
4789 root->root_key.objectid, level, 0);
4790 btrfs_free_path(path);
4791 if (ret == -ENOENT)
4792 return 0;
4793 if (ret < 0)
4794 return ret;
4795 return 1;
4796}
4797
4798/*
Liu Bo2c016dc2012-12-26 15:32:17 +08004799 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004800 *
4801 * when wc->stage == DROP_REFERENCE, this function checks
4802 * reference count of the block pointed to. if the block
4803 * is shared and we need update back refs for the subtree
4804 * rooted at the block, this function changes wc->stage to
4805 * UPDATE_BACKREF. if the block is shared and there is no
4806 * need to update back, this function drops the reference
4807 * to the block.
4808 *
4809 * NOTE: return value 1 means we should stop walking down.
4810 */
4811static noinline int do_walk_down(struct btrfs_trans_handle *trans,
4812 struct btrfs_root *root,
4813 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004814 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004815{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004816 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004817 u64 bytenr;
4818 u64 generation;
4819 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004820 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08004821 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004822 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004823 struct extent_buffer *next;
4824 int level = wc->level;
4825 int reada = 0;
4826 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07004827 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004828
4829 generation = btrfs_node_ptr_generation(path->nodes[level],
4830 path->slots[level]);
4831 /*
4832 * if the lower level block was created before the snapshot
4833 * was created, we know there is no need to update back refs
4834 * for the subtree
4835 */
4836 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04004837 generation <= root->root_key.offset) {
4838 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004839 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004840 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004841
4842 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08004843 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
4844 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004845
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004846 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004847 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004848 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07004849 if (IS_ERR(next))
4850 return PTR_ERR(next);
4851
Josef Bacikb2aaaa32013-07-05 17:05:38 -04004852 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
4853 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004854 reada = 1;
4855 }
4856 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004857 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004858
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004859 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04004860 &wc->refs[level - 1],
4861 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02004862 if (ret < 0)
4863 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004864
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004865 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004866 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02004867 ret = -EIO;
4868 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004869 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004870 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004871
Yan, Zheng94fcca92009-10-09 09:25:16 -04004872 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004873 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07004874 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004875 if (level == 1 &&
4876 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4877 goto skip;
4878
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004879 if (!wc->update_ref ||
4880 generation <= root->root_key.offset)
4881 goto skip;
4882
4883 btrfs_node_key_to_cpu(path->nodes[level], &key,
4884 path->slots[level]);
4885 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
4886 if (ret < 0)
4887 goto skip;
4888
4889 wc->stage = UPDATE_BACKREF;
4890 wc->shared_level = level - 1;
4891 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004892 } else {
4893 if (level == 1 &&
4894 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4895 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004896 }
4897
Chris Masonb9fab912012-05-06 07:23:47 -04004898 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004899 btrfs_tree_unlock(next);
4900 free_extent_buffer(next);
4901 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004902 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004903 }
4904
4905 if (!next) {
4906 if (reada && level == 1)
4907 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08004908 next = read_tree_block(fs_info, bytenr, generation, level - 1,
4909 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08004910 if (IS_ERR(next)) {
4911 return PTR_ERR(next);
4912 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04004913 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004914 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04004915 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004916 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02004917 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004918 }
4919
4920 level--;
Josef Bacik48672682016-09-23 13:23:28 +02004921 ASSERT(level == btrfs_header_level(next));
4922 if (level != btrfs_header_level(next)) {
4923 btrfs_err(root->fs_info, "mismatched level");
4924 ret = -EIO;
4925 goto out_unlock;
4926 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004927 path->nodes[level] = next;
4928 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004929 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004930 wc->level = level;
4931 if (wc->level == 1)
4932 wc->reada_slot = 0;
4933 return 0;
4934skip:
4935 wc->refs[level - 1] = 0;
4936 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04004937 if (wc->stage == DROP_REFERENCE) {
4938 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4939 parent = path->nodes[level]->start;
4940 } else {
Josef Bacik48672682016-09-23 13:23:28 +02004941 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04004942 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02004943 if (root->root_key.objectid !=
4944 btrfs_header_owner(path->nodes[level])) {
4945 btrfs_err(root->fs_info,
4946 "mismatched block owner");
4947 ret = -EIO;
4948 goto out_unlock;
4949 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04004950 parent = 0;
4951 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04004952
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004953 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05004954 * If we had a drop_progress we need to verify the refs are set
4955 * as expected. If we find our ref then we know that from here
4956 * on out everything should be correct, and we can clear the
4957 * ->restarted flag.
4958 */
4959 if (wc->restarted) {
4960 ret = check_ref_exists(trans, root, bytenr, parent,
4961 level - 1);
4962 if (ret < 0)
4963 goto out_unlock;
4964 if (ret == 0)
4965 goto no_delete;
4966 ret = 0;
4967 wc->restarted = 0;
4968 }
4969
4970 /*
Qu Wenruo2cd86d32018-09-27 14:42:33 +08004971 * Reloc tree doesn't contribute to qgroup numbers, and we have
4972 * already accounted them at merge time (replace_path),
4973 * thus we could skip expensive subtree trace here.
4974 */
4975 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
4976 need_account) {
Lu Fengqideb40622018-07-18 14:45:38 +08004977 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08004978 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07004979 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004980 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004981 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
4982 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07004983 }
4984 }
Josef Bacikaea6f022019-02-06 15:46:15 -05004985
4986 /*
4987 * We need to update the next key in our walk control so we can
4988 * update the drop_progress key accordingly. We don't care if
4989 * find_next_key doesn't find a key because that means we're at
4990 * the end and are going to clean up now.
4991 */
4992 wc->drop_level = level;
4993 find_next_key(path, level, &wc->drop_progress);
4994
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004995 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
4996 fs_info->nodesize, parent);
4997 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
4998 ret = btrfs_free_extent(trans, &ref);
Josef Bacik48672682016-09-23 13:23:28 +02004999 if (ret)
5000 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005001 }
Josef Bacik78c52d92019-02-06 15:46:14 -05005002no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02005003 *lookup_info = 1;
5004 ret = 1;
5005
5006out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005007 btrfs_tree_unlock(next);
5008 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02005009
5010 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005011}
5012
5013/*
Liu Bo2c016dc2012-12-26 15:32:17 +08005014 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04005015 *
5016 * when wc->stage == DROP_REFERENCE, this function drops
5017 * reference count on the block.
5018 *
5019 * when wc->stage == UPDATE_BACKREF, this function changes
5020 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5021 * to UPDATE_BACKREF previously while processing the block.
5022 *
5023 * NOTE: return value 1 means we should stop walking up.
5024 */
5025static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5026 struct btrfs_root *root,
5027 struct btrfs_path *path,
5028 struct walk_control *wc)
5029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005030 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005031 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005032 int level = wc->level;
5033 struct extent_buffer *eb = path->nodes[level];
5034 u64 parent = 0;
5035
5036 if (wc->stage == UPDATE_BACKREF) {
5037 BUG_ON(wc->shared_level < level);
5038 if (level < wc->shared_level)
5039 goto out;
5040
Yan Zheng2c47e6052009-06-27 21:07:35 -04005041 ret = find_next_key(path, level + 1, &wc->update_progress);
5042 if (ret > 0)
5043 wc->update_ref = 0;
5044
5045 wc->stage = DROP_REFERENCE;
5046 wc->shared_level = -1;
5047 path->slots[level] = 0;
5048
5049 /*
5050 * check reference count again if the block isn't locked.
5051 * we should start walking down the tree again if reference
5052 * count is one.
5053 */
5054 if (!path->locks[level]) {
5055 BUG_ON(level == 0);
5056 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005057 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005058 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005059
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005060 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05005061 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005062 &wc->refs[level],
5063 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005064 if (ret < 0) {
5065 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005066 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005067 return ret;
5068 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005069 BUG_ON(wc->refs[level] == 0);
5070 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04005071 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00005072 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005073 return 1;
5074 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005075 }
5076 }
5077
5078 /* wc->stage == DROP_REFERENCE */
5079 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5080
5081 if (wc->refs[level] == 1) {
5082 if (level == 0) {
5083 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07005084 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005085 else
Josef Bacike339a6b2014-07-02 10:54:25 -07005086 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005087 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005088 if (is_fstree(root->root_key.objectid)) {
5089 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5090 if (ret) {
5091 btrfs_err_rl(fs_info,
5092 "error %d accounting leaf items, quota is out of sync, rescan required",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005093 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08005094 }
Mark Fasheh11526512014-07-17 12:39:01 -07005095 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005096 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005097 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04005098 if (!path->locks[level] &&
5099 btrfs_header_generation(eb) == trans->transid) {
5100 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02005101 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04005102 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005103 }
David Sterba6a884d7d2019-03-20 14:30:02 +01005104 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005105 }
5106
5107 if (eb == root->node) {
5108 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5109 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005110 else if (root->root_key.objectid != btrfs_header_owner(eb))
5111 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005112 } else {
5113 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5114 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005115 else if (root->root_key.objectid !=
5116 btrfs_header_owner(path->nodes[level + 1]))
5117 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005118 }
5119
Jan Schmidt5581a512012-05-16 17:04:52 +02005120 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005121out:
5122 wc->refs[level] = 0;
5123 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005124 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005125
5126owner_mismatch:
5127 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5128 btrfs_header_owner(eb), root->root_key.objectid);
5129 return -EUCLEAN;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005130}
5131
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005132static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5133 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005134 struct btrfs_path *path,
5135 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005136{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005137 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04005138 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005139 int ret;
5140
Yan Zheng2c47e6052009-06-27 21:07:35 -04005141 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04005142 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005143 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04005144 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005145
Yan Zheng2c47e6052009-06-27 21:07:35 -04005146 if (level == 0)
5147 break;
5148
Yan, Zheng7a7965f2010-02-01 02:41:17 +00005149 if (path->slots[level] >=
5150 btrfs_header_nritems(path->nodes[level]))
5151 break;
5152
Yan, Zheng94fcca92009-10-09 09:25:16 -04005153 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005154 if (ret > 0) {
5155 path->slots[level]++;
5156 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00005157 } else if (ret < 0)
5158 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005159 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005160 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005161 return 0;
5162}
5163
Chris Masond3977122009-01-05 21:25:51 -05005164static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005165 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005166 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005167 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05005168{
Yan Zheng2c47e6052009-06-27 21:07:35 -04005169 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05005170 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04005171
Yan Zheng2c47e6052009-06-27 21:07:35 -04005172 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5173 while (level < max_level && path->nodes[level]) {
5174 wc->level = level;
5175 if (path->slots[level] + 1 <
5176 btrfs_header_nritems(path->nodes[level])) {
5177 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05005178 return 0;
5179 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005180 ret = walk_up_proc(trans, root, path, wc);
5181 if (ret > 0)
5182 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08005183 if (ret < 0)
5184 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05005185
Yan Zheng2c47e6052009-06-27 21:07:35 -04005186 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04005187 btrfs_tree_unlock_rw(path->nodes[level],
5188 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005189 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005190 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005191 free_extent_buffer(path->nodes[level]);
5192 path->nodes[level] = NULL;
5193 level++;
Chris Mason20524f02007-03-10 06:35:47 -05005194 }
5195 }
5196 return 1;
5197}
5198
Chris Mason9aca1d52007-03-13 11:09:37 -04005199/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04005200 * drop a subvolume tree.
5201 *
5202 * this function traverses the tree freeing any blocks that only
5203 * referenced by the tree.
5204 *
5205 * when a shared tree block is found. this function decreases its
5206 * reference count by one. if update_ref is true, this function
5207 * also make sure backrefs for the shared block and all lower level
5208 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00005209 *
5210 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04005211 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04005212int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005213 struct btrfs_block_rsv *block_rsv, int update_ref,
5214 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05005215{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005216 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04005217 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005218 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005219 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04005220 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005221 struct walk_control *wc;
5222 struct btrfs_key key;
5223 int err = 0;
5224 int ret;
5225 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04005226 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05005227
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09005228 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07005229
Chris Mason5caf2a02007-04-02 11:20:42 -04005230 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005231 if (!path) {
5232 err = -ENOMEM;
5233 goto out;
5234 }
Chris Mason20524f02007-03-10 06:35:47 -05005235
Yan Zheng2c47e6052009-06-27 21:07:35 -04005236 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07005237 if (!wc) {
5238 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005239 err = -ENOMEM;
5240 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07005241 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005242
Yan, Zhenga22285a2010-05-16 10:48:46 -04005243 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005244 if (IS_ERR(trans)) {
5245 err = PTR_ERR(trans);
5246 goto out_free;
5247 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00005248
Josef Bacik0568e822018-11-30 11:52:14 -05005249 err = btrfs_run_delayed_items(trans);
5250 if (err)
5251 goto out_end_trans;
5252
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005253 if (block_rsv)
5254 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005255
Josef Bacik83354f02018-11-30 11:52:13 -05005256 /*
5257 * This will help us catch people modifying the fs tree while we're
5258 * dropping it. It is unsafe to mess with the fs tree while it's being
5259 * dropped as we unlock the root node and parent nodes as we walk down
5260 * the tree, assuming nothing will change. If something does change
5261 * then we'll have stale information and drop references to blocks we've
5262 * already dropped.
5263 */
5264 set_bit(BTRFS_ROOT_DELETING, &root->state);
Chris Mason9f3a7422007-08-07 15:52:19 -04005265 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005266 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005267 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02005268 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04005269 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005270 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005271 memset(&wc->update_progress, 0,
5272 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04005273 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04005274 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005275 memcpy(&wc->update_progress, &key,
5276 sizeof(wc->update_progress));
5277
Chris Mason6702ed42007-08-07 16:15:09 -04005278 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005279 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04005280 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005281 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5282 path->lowest_level = 0;
5283 if (ret < 0) {
5284 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005285 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04005286 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04005287 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005288
Chris Mason7d9eb122008-07-08 14:19:17 -04005289 /*
5290 * unlock our path, this is safe because only this
5291 * function is allowed to delete this snapshot
5292 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04005294
Yan Zheng2c47e6052009-06-27 21:07:35 -04005295 level = btrfs_header_level(root->node);
5296 while (1) {
5297 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02005298 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005299 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005301 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04005302 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05005303 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04005304 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005305 if (ret < 0) {
5306 err = ret;
5307 goto out_end_trans;
5308 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005309 BUG_ON(wc->refs[level] == 0);
5310
5311 if (level == root_item->drop_level)
5312 break;
5313
5314 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04005315 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005316 WARN_ON(wc->refs[level] != 1);
5317 level--;
5318 }
5319 }
5320
Josef Bacik78c52d92019-02-06 15:46:14 -05005321 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005322 wc->level = level;
5323 wc->shared_level = -1;
5324 wc->stage = DROP_REFERENCE;
5325 wc->update_ref = update_ref;
5326 wc->keep_locks = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005327 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005328
5329 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00005330
Yan Zheng2c47e6052009-06-27 21:07:35 -04005331 ret = walk_down_tree(trans, root, path, wc);
5332 if (ret < 0) {
5333 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04005334 break;
5335 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005336
5337 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5338 if (ret < 0) {
5339 err = ret;
5340 break;
5341 }
5342
5343 if (ret > 0) {
5344 BUG_ON(wc->stage != DROP_REFERENCE);
5345 break;
5346 }
5347
5348 if (wc->stage == DROP_REFERENCE) {
Josef Bacikaea6f022019-02-06 15:46:15 -05005349 wc->drop_level = wc->level;
5350 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5351 &wc->drop_progress,
5352 path->slots[wc->drop_level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04005353 }
Josef Bacikaea6f022019-02-06 15:46:15 -05005354 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5355 &wc->drop_progress);
5356 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005357
5358 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005359 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005360 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005361 ret = btrfs_update_root(trans, tree_root,
5362 &root->root_key,
5363 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005364 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005365 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005366 err = ret;
5367 goto out_end_trans;
5368 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005369
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005370 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005371 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005372 btrfs_debug(fs_info,
5373 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04005374 err = -EAGAIN;
5375 goto out_free;
5376 }
5377
Yan, Zhenga22285a2010-05-16 10:48:46 -04005378 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005379 if (IS_ERR(trans)) {
5380 err = PTR_ERR(trans);
5381 goto out_free;
5382 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04005383 if (block_rsv)
5384 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04005385 }
Chris Mason20524f02007-03-10 06:35:47 -05005386 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005387 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005388 if (err)
5389 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005390
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08005391 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005392 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005393 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05005394 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005395 goto out_end_trans;
5396 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005397
Yan, Zheng76dda932009-09-21 16:00:26 -04005398 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00005399 ret = btrfs_find_root(tree_root, &root->root_key, path,
5400 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005401 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005402 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005403 err = ret;
5404 goto out_end_trans;
5405 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05005406 /* if we fail to delete the orphan item this time
5407 * around, it'll get picked up the next time.
5408 *
5409 * The most common failure here is just -ENOENT.
5410 */
5411 btrfs_del_orphan_item(trans, tree_root,
5412 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04005413 }
5414 }
5415
Miao Xie27cdeb72014-04-02 19:51:05 +08005416 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04005417 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005418 } else {
5419 free_extent_buffer(root->node);
5420 free_extent_buffer(root->commit_root);
Josef Bacik00246522020-01-24 09:33:01 -05005421 btrfs_put_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005422 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04005423 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005424out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005425 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005426out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04005427 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04005428 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00005429out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04005430 /*
5431 * So if we need to stop dropping the snapshot for whatever reason we
5432 * need to make sure to add it back to the dead root list so that we
5433 * keep trying to do the work later. This also cleans up roots if we
5434 * don't have it in the radix (like when we recover after a power fail
5435 * or unmount) so we don't leak memory.
5436 */
Thomas Meyer897ca812017-10-07 16:02:21 +02005437 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04005438 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08005439 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005440 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04005441 return err;
Chris Mason20524f02007-03-10 06:35:47 -05005442}
Chris Mason9078a3e2007-04-26 16:46:15 -04005443
Yan Zheng2c47e6052009-06-27 21:07:35 -04005444/*
5445 * drop subtree rooted at tree block 'node'.
5446 *
5447 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005448 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04005449 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04005450int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5451 struct btrfs_root *root,
5452 struct extent_buffer *node,
5453 struct extent_buffer *parent)
5454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005455 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005456 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005457 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005458 int level;
5459 int parent_level;
5460 int ret = 0;
5461 int wret;
5462
Yan Zheng2c47e6052009-06-27 21:07:35 -04005463 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5464
Yan Zhengf82d02d2008-10-29 14:49:05 -04005465 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005466 if (!path)
5467 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005468
Yan Zheng2c47e6052009-06-27 21:07:35 -04005469 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005470 if (!wc) {
5471 btrfs_free_path(path);
5472 return -ENOMEM;
5473 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04005474
Chris Masonb9447ef82009-03-09 11:45:38 -04005475 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005476 parent_level = btrfs_header_level(parent);
David Sterba67439da2019-10-08 13:28:47 +02005477 atomic_inc(&parent->refs);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005478 path->nodes[parent_level] = parent;
5479 path->slots[parent_level] = btrfs_header_nritems(parent);
5480
Chris Masonb9447ef82009-03-09 11:45:38 -04005481 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005482 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005483 path->nodes[level] = node;
5484 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04005485 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005486
5487 wc->refs[parent_level] = 1;
5488 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5489 wc->level = level;
5490 wc->shared_level = -1;
5491 wc->stage = DROP_REFERENCE;
5492 wc->update_ref = 0;
5493 wc->keep_locks = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005494 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005495
5496 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04005497 wret = walk_down_tree(trans, root, path, wc);
5498 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005499 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005500 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04005501 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04005502
Yan Zheng2c47e6052009-06-27 21:07:35 -04005503 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005504 if (wret < 0)
5505 ret = wret;
5506 if (wret != 0)
5507 break;
5508 }
5509
Yan Zheng2c47e6052009-06-27 21:07:35 -04005510 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005511 btrfs_free_path(path);
5512 return ret;
5513}
5514
Miao Xie6d07bce2011-01-05 10:07:31 +00005515/*
5516 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04005517 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00005518 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005519u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00005520{
David Sterba32da53862019-10-29 19:20:18 +01005521 struct btrfs_block_group *block_group;
Miao Xie6d07bce2011-01-05 10:07:31 +00005522 u64 free_bytes = 0;
5523 int factor;
5524
Nicholas D Steeves01327612016-05-19 21:18:45 -04005525 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04005526 if (list_empty(&sinfo->ro_bgs))
5527 return 0;
5528
5529 spin_lock(&sinfo->lock);
5530 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00005531 spin_lock(&block_group->lock);
5532
5533 if (!block_group->ro) {
5534 spin_unlock(&block_group->lock);
5535 continue;
5536 }
5537
David Sterba46df06b2018-07-13 20:46:30 +02005538 factor = btrfs_bg_type_to_factor(block_group->flags);
David Sterbab3470b52019-10-23 18:48:22 +02005539 free_bytes += (block_group->length -
David Sterbabf38be62019-10-23 18:48:11 +02005540 block_group->used) * factor;
Miao Xie6d07bce2011-01-05 10:07:31 +00005541
5542 spin_unlock(&block_group->lock);
5543 }
Miao Xie6d07bce2011-01-05 10:07:31 +00005544 spin_unlock(&sinfo->lock);
5545
5546 return free_bytes;
5547}
5548
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005549int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5550 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08005551{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005552 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08005553}
5554
Jeff Mahoney499f3772015-06-15 09:41:17 -04005555/*
5556 * It used to be that old block groups would be left around forever.
5557 * Iterating over them would be enough to trim unused space. Since we
5558 * now automatically remove them, we also need to iterate over unallocated
5559 * space.
5560 *
5561 * We don't want a transaction for this since the discard may take a
5562 * substantial amount of time. We don't require that a transaction be
5563 * running, but we do need to take a running transaction into account
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005564 * to ensure that we're not discarding chunks that were released or
5565 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005566 *
5567 * Holding the chunks lock will prevent other threads from allocating
5568 * or releasing chunks, but it won't prevent a running transaction
5569 * from committing and releasing the memory that the pending chunks
5570 * list head uses. For that, we need to take a reference to the
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005571 * transaction and hold the commit root sem. We only need to hold
5572 * it while performing the free space search since we have already
5573 * held back allocations.
Jeff Mahoney499f3772015-06-15 09:41:17 -04005574 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03005575static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04005576{
Nikolay Borisov8103d102019-06-03 13:06:00 +03005577 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005578 int ret;
5579
5580 *trimmed = 0;
5581
Jeff Mahoney0be88e32018-09-06 17:18:15 -04005582 /* Discard not supported = nothing to do. */
5583 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5584 return 0;
5585
Andrea Gelmini52042d82018-11-28 12:05:13 +01005586 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08005587 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04005588 return 0;
5589
5590 /* No free space = nothing to do. */
5591 if (device->total_bytes <= device->bytes_used)
5592 return 0;
5593
5594 ret = 0;
5595
5596 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04005597 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005598 u64 bytes;
5599
5600 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5601 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04005602 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005603
Nikolay Borisov929be172019-03-27 14:24:18 +02005604 find_first_clear_extent_bit(&device->alloc_state, start,
5605 &start, &end,
5606 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005607
5608 /* Ensure we skip the reserved area in the first 1M */
5609 start = max_t(u64, start, SZ_1M);
5610
Nikolay Borisov929be172019-03-27 14:24:18 +02005611 /*
5612 * If find_first_clear_extent_bit find a range that spans the
5613 * end of the device it will set end to -1, in this case it's up
5614 * to the caller to trim the value to the size of the device.
5615 */
5616 end = min(end, device->total_bytes - 1);
Nikolay Borisov53460a42019-06-03 13:06:01 +03005617
Nikolay Borisov929be172019-03-27 14:24:18 +02005618 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005619
Nikolay Borisov929be172019-03-27 14:24:18 +02005620 /* We didn't find any extents */
5621 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04005622 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02005623 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005624 break;
5625 }
5626
Nikolay Borisov929be172019-03-27 14:24:18 +02005627 ret = btrfs_issue_discard(device->bdev, start, len,
5628 &bytes);
5629 if (!ret)
5630 set_extent_bits(&device->alloc_state, start,
5631 start + bytes - 1,
5632 CHUNK_TRIMMED);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005633 mutex_unlock(&fs_info->chunk_mutex);
5634
5635 if (ret)
5636 break;
5637
5638 start += len;
5639 *trimmed += bytes;
5640
5641 if (fatal_signal_pending(current)) {
5642 ret = -ERESTARTSYS;
5643 break;
5644 }
5645
5646 cond_resched();
5647 }
5648
5649 return ret;
5650}
5651
Qu Wenruo93bba242018-09-07 14:16:23 +08005652/*
5653 * Trim the whole filesystem by:
5654 * 1) trimming the free space in each block group
5655 * 2) trimming the unallocated space on each device
5656 *
5657 * This will also continue trimming even if a block group or device encounters
5658 * an error. The return value will be the last error, or 0 if nothing bad
5659 * happens.
5660 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005661int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00005662{
David Sterba32da53862019-10-29 19:20:18 +01005663 struct btrfs_block_group *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005664 struct btrfs_device *device;
5665 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00005666 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08005667 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00005668 u64 start;
5669 u64 end;
5670 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08005671 u64 bg_failed = 0;
5672 u64 dev_failed = 0;
5673 int bg_ret = 0;
5674 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00005675 int ret = 0;
5676
Qu Wenruo07301df2019-05-28 16:21:54 +08005677 /*
5678 * Check range overflow if range->len is set.
5679 * The default range->len is U64_MAX.
5680 */
5681 if (range->len != U64_MAX &&
5682 check_add_overflow(range->start, range->len, &range_end))
5683 return -EINVAL;
5684
Qu Wenruo6ba9fc82018-09-07 14:16:24 +08005685 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04005686 for (; cache; cache = btrfs_next_block_group(cache)) {
David Sterbab3470b52019-10-23 18:48:22 +02005687 if (cache->start >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00005688 btrfs_put_block_group(cache);
5689 break;
5690 }
5691
David Sterbab3470b52019-10-23 18:48:22 +02005692 start = max(range->start, cache->start);
5693 end = min(range_end, cache->start + cache->length);
Li Dongyangf7039b12011-03-24 10:24:28 +00005694
5695 if (end - start >= range->minlen) {
David Sterba32da53862019-10-29 19:20:18 +01005696 if (!btrfs_block_group_done(cache)) {
Josef Bacik676f1f72019-06-20 15:37:48 -04005697 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04005698 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005699 bg_failed++;
5700 bg_ret = ret;
5701 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005702 }
Josef Bacik676f1f72019-06-20 15:37:48 -04005703 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04005704 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005705 bg_failed++;
5706 bg_ret = ret;
5707 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04005708 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005709 }
5710 ret = btrfs_trim_block_group(cache,
5711 &group_trimmed,
5712 start,
5713 end,
5714 range->minlen);
5715
5716 trimmed += group_trimmed;
5717 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08005718 bg_failed++;
5719 bg_ret = ret;
5720 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00005721 }
5722 }
Li Dongyangf7039b12011-03-24 10:24:28 +00005723 }
5724
Qu Wenruo93bba242018-09-07 14:16:23 +08005725 if (bg_failed)
5726 btrfs_warn(fs_info,
5727 "failed to trim %llu block group(s), last error %d",
5728 bg_failed, bg_ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005729 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04005730 devices = &fs_info->fs_devices->devices;
5731 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03005732 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08005733 if (ret) {
5734 dev_failed++;
5735 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04005736 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08005737 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04005738
5739 trimmed += group_trimmed;
5740 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005741 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04005742
Qu Wenruo93bba242018-09-07 14:16:23 +08005743 if (dev_failed)
5744 btrfs_warn(fs_info,
5745 "failed to trim %llu device(s), last error %d",
5746 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00005747 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08005748 if (bg_ret)
5749 return bg_ret;
5750 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00005751}
Miao Xie8257b2d2014-03-06 13:38:19 +08005752
5753/*
David Sterbaea14b57f2017-06-22 02:19:11 +02005754 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005755 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
5756 * data into the page cache through nocow before the subvolume is snapshoted,
5757 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +02005758 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005759 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +08005760 */
David Sterbaea14b57f2017-06-22 02:19:11 +02005761void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005762{
5763 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +01005764 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +08005765}
5766
David Sterbaea14b57f2017-06-22 02:19:11 +02005767int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08005768{
David Sterbaea14b57f2017-06-22 02:19:11 +02005769 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +08005770 return 0;
5771
5772 percpu_counter_inc(&root->subv_writers->counter);
5773 /*
5774 * Make sure counter is updated before we check for snapshot creation.
5775 */
5776 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +02005777 if (atomic_read(&root->will_be_snapshotted)) {
5778 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08005779 return 0;
5780 }
5781 return 1;
5782}
Zhao Lei0bc19f902016-01-06 18:56:36 +08005783
Zhao Lei0bc19f902016-01-06 18:56:36 +08005784void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
5785{
5786 while (true) {
5787 int ret;
5788
David Sterbaea14b57f2017-06-22 02:19:11 +02005789 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +08005790 if (ret)
5791 break;
Peter Zijlstra46259562018-03-15 11:43:08 +01005792 wait_var_event(&root->will_be_snapshotted,
5793 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +08005794 }
5795}