blob: b4ef478e0de3e2029123defe90aa825dc2e012d7 [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 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
Christoph Hellwig348332e2021-09-20 14:33:12 +02009#include <linux/blk-cgroup.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040010#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040011#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050020#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040021#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040022#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020024#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000025#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050026#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040027#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080028#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040029#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050030#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070031#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080032#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030033#include <linux/sched/mm.h>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050034#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020035#include <asm/unaligned.h>
Boris Burkov14605402021-06-30 13:01:49 -070036#include <linux/fsverity.h>
David Sterba602cbe92019-08-21 18:48:25 +020037#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "ctree.h"
39#include "disk-io.h"
40#include "transaction.h"
41#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040042#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040043#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040044#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040045#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020046#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040047#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050048#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050049#include "free-space-cache.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000050#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080051#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040052#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040053#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010054#include "space-info.h"
Naohiro Aotad8e3fb12021-02-04 19:22:05 +090055#include "zoned.h"
Qu Wenruob945a462021-05-31 16:50:46 +080056#include "subpage.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020059 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040060 struct btrfs_root *root;
61};
62
Filipe Mananaf28a4922015-12-08 19:23:20 +000063struct btrfs_dio_data {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050064 ssize_t submitted;
65 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000066};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070070static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
76struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000079struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
Eric Sandeen3972f262013-01-12 02:57:22 +000081static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000082static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040083static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030084static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050085 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030086 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030087 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030088static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
89 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080090 u64 block_len, u64 orig_block_len,
91 u64 ram_bytes, int compress_type,
92 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030094static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080095 const u64 offset, const u64 bytes,
96 const bool uptodate);
97
98/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050099 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
100 *
101 * ilock_flags can have the following bit set:
102 *
103 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
104 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
105 * return -EAGAIN
Josef Bacik8318ba72021-02-10 17:14:33 -0500106 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500107 */
108int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
109{
110 if (ilock_flags & BTRFS_ILOCK_SHARED) {
111 if (ilock_flags & BTRFS_ILOCK_TRY) {
112 if (!inode_trylock_shared(inode))
113 return -EAGAIN;
114 else
115 return 0;
116 }
117 inode_lock_shared(inode);
118 } else {
119 if (ilock_flags & BTRFS_ILOCK_TRY) {
120 if (!inode_trylock(inode))
121 return -EAGAIN;
122 else
123 return 0;
124 }
125 inode_lock(inode);
126 }
Josef Bacik8318ba72021-02-10 17:14:33 -0500127 if (ilock_flags & BTRFS_ILOCK_MMAP)
128 down_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500129 return 0;
130}
131
132/*
133 * btrfs_inode_unlock - unock inode i_rwsem
134 *
135 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
136 * to decide whether the lock acquired is shared or exclusive.
137 */
138void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
139{
Josef Bacik8318ba72021-02-10 17:14:33 -0500140 if (ilock_flags & BTRFS_ILOCK_MMAP)
141 up_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500142 if (ilock_flags & BTRFS_ILOCK_SHARED)
143 inode_unlock_shared(inode);
144 else
145 inode_unlock(inode);
146}
147
148/*
Qu Wenruo524272602017-03-08 10:25:52 +0800149 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100150 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800151 *
152 * NOTE: caller must ensure that when an error happens, it can not call
153 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
154 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
155 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200156 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800157 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300158static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200159 struct page *locked_page,
160 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800161{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900162 unsigned long index = offset >> PAGE_SHIFT;
163 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200164 u64 page_start = page_offset(locked_page);
165 u64 page_end = page_start + PAGE_SIZE - 1;
166
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900167 struct page *page;
168
169 while (index <= end_index) {
Qu Wenruo968f2562021-05-18 15:09:41 +0800170 /*
171 * For locked page, we will call end_extent_writepage() on it
172 * in run_delalloc_range() for the error handling. That
173 * end_extent_writepage() function will call
174 * btrfs_mark_ordered_io_finished() to clear page Ordered and
175 * run the ordered extent accounting.
176 *
177 * Here we can't just clear the Ordered bit, or
178 * btrfs_mark_ordered_io_finished() would skip the accounting
179 * for the page range, and the ordered extent will never finish.
180 */
181 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
182 index++;
183 continue;
184 }
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300185 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900186 index++;
187 if (!page)
188 continue;
Qu Wenruo968f2562021-05-18 15:09:41 +0800189
190 /*
191 * Here we just clear all Ordered bits for every page in the
192 * range, then __endio_write_update_ordered() will handle
193 * the ordered extent accounting for the range.
194 */
Qu Wenruob945a462021-05-31 16:50:46 +0800195 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
196 offset, bytes);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900197 put_page(page);
198 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200199
Qu Wenruob945a462021-05-31 16:50:46 +0800200 /* The locked page covers the full range, nothing needs to be done */
201 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
202 return;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200203 /*
204 * In case this page belongs to the delalloc range being instantiated
205 * then skip it, since the first page of a range is going to be
206 * properly cleaned up by the caller of run_delalloc_range
207 */
208 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
Qu Wenruob945a462021-05-31 16:50:46 +0800209 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
210 offset = page_offset(locked_page) + PAGE_SIZE;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200211 }
212
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300213 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800214}
215
Eric Sandeen48a3b632013-04-25 20:41:01 +0000216static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400217
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000218static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500219 struct inode *inode, struct inode *dir,
220 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500221{
222 int err;
223
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000224 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500225 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500226 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500227 return err;
228}
229
Chris Masond352ac62008-09-29 15:18:18 -0400230/*
Chris Masonc8b97812008-10-29 14:49:59 -0400231 * this does all the hard work for inserting an inline extent into
232 * the btree. The caller should have done a btrfs_drop_extents so that
233 * no overlapping inline items exist in the btree
234 */
Chris Mason40f76582014-05-21 13:35:51 -0700235static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000236 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400237 struct btrfs_root *root, struct inode *inode,
238 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000239 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400240 struct page **compressed_pages)
241{
Chris Masonc8b97812008-10-29 14:49:59 -0400242 struct extent_buffer *leaf;
243 struct page *page = NULL;
244 char *kaddr;
245 unsigned long ptr;
246 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400247 int ret;
248 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400249 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400250
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800251 ASSERT((compressed_size > 0 && compressed_pages) ||
252 (compressed_size == 0 && !compressed_pages));
253
Li Zefanfe3f5662011-03-28 08:30:38 +0000254 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400255 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000257 if (!extent_inserted) {
258 struct btrfs_key key;
259 size_t datasize;
260
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200261 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000262 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200263 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000264
265 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 ret = btrfs_insert_empty_item(trans, root, path, &key,
267 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200268 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400270 }
271 leaf = path->nodes[0];
272 ei = btrfs_item_ptr(leaf, path->slots[0],
273 struct btrfs_file_extent_item);
274 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
275 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
276 btrfs_set_file_extent_encryption(leaf, ei, 0);
277 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
278 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
279 ptr = btrfs_file_extent_inline_start(ei);
280
Li Zefan261507a02010-12-17 14:21:50 +0800281 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400282 struct page *cpage;
283 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500284 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400285 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500286 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400288
David Sterba3a60f652021-10-27 10:39:03 +0200289 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400290 write_extent_buffer(leaf, kaddr, ptr, cur_size);
David Sterba3a60f652021-10-27 10:39:03 +0200291 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400292
293 i++;
294 ptr += cur_size;
295 compressed_size -= cur_size;
296 }
297 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800298 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400299 } else {
300 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300301 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400302 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800303 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100304 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400305 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800306 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300307 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400308 }
309 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400311
Yan, Zhengc2167752009-11-12 09:34:21 +0000312 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500313 * We align size to sectorsize for inline extents just for simplicity
314 * sake.
315 */
316 size = ALIGN(size, root->fs_info->sectorsize);
317 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
318 if (ret)
319 goto fail;
320
321 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000322 * we're an inline extent, so nobody can
323 * extend the file past i_size without locking
324 * a page we already have locked.
325 *
326 * We must do any isize and inode updates
327 * before we unlock the pages. Otherwise we
328 * could end up racing with unlink.
329 */
Chris Masonc8b97812008-10-29 14:49:59 -0400330 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400331fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200332 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400333}
334
335
336/*
337 * conditionally insert an inline extent into the file. This
338 * does the checks required to make sure the data is small enough
339 * to fit as an inline extent.
340 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300341static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400342 u64 end, size_t compressed_size,
343 int compress_type,
344 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400345{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000346 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300347 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400348 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400349 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300350 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400351 u64 actual_end = min(end + 1, isize);
352 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400353 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400354 u64 data_len = inline_len;
355 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000356 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400357
358 if (compressed_size)
359 data_len = compressed_size;
360
361 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400362 actual_end > fs_info->sectorsize ||
363 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400364 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400365 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400366 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400367 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400368 return 1;
369 }
370
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000371 path = btrfs_alloc_path();
372 if (!path)
373 return -ENOMEM;
374
Josef Bacik00361582013-08-14 14:02:47 -0400375 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000376 if (IS_ERR(trans)) {
377 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400378 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000379 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300380 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400381
Filipe Manana5893dfb2020-11-04 11:07:32 +0000382 drop_args.path = path;
383 drop_args.start = start;
384 drop_args.end = aligned_end;
385 drop_args.drop_cache = true;
386 drop_args.replace_extent = true;
387
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000388 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000389 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000390 compressed_size);
391 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000392 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000393 inline_len);
394
Filipe Manana5893dfb2020-11-04 11:07:32 +0000395 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400396 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400397 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400398 goto out;
399 }
Chris Masonc8b97812008-10-29 14:49:59 -0400400
401 if (isize > actual_end)
402 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000403 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300404 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400405 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000406 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400407 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400408 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400409 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400410 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400411 ret = 1;
412 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100413 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400414
Filipe Manana2766ff62020-11-04 11:07:34 +0000415 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200416 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000417 if (ret && ret != -ENOSPC) {
418 btrfs_abort_transaction(trans, ret);
419 goto out;
420 } else if (ret == -ENOSPC) {
421 ret = 1;
422 goto out;
423 }
424
Nikolay Borisova0349402020-06-03 08:55:12 +0300425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400426out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800427 /*
428 * Don't forget to free the reserved space, as for inlined extent
429 * it won't count as data extent, free them directly here.
430 * And at reserve time, it's always aligned to page size, so
431 * just free one page here.
432 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300433 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000434 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400435 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400436 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400437}
438
Chris Mason771ed682008-11-06 22:02:51 -0500439struct async_extent {
440 u64 start;
441 u64 ram_size;
442 u64 compressed_size;
443 struct page **pages;
444 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800445 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500446 struct list_head list;
447};
448
Nikolay Borisov97db1202019-03-12 17:20:24 +0200449struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500450 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500451 struct page *locked_page;
452 u64 start;
453 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600454 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500455 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700456 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500457 struct btrfs_work work;
Qu Wenruo9e895a82021-09-27 15:21:45 +0800458 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -0500459};
460
Nikolay Borisov97db1202019-03-12 17:20:24 +0200461struct async_cow {
Nikolay Borisov97db1202019-03-12 17:20:24 +0200462 atomic_t num_chunks;
463 struct async_chunk chunks[];
464};
465
466static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500467 u64 start, u64 ram_size,
468 u64 compressed_size,
469 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800470 unsigned long nr_pages,
471 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500472{
473 struct async_extent *async_extent;
474
475 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100476 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500477 async_extent->start = start;
478 async_extent->ram_size = ram_size;
479 async_extent->compressed_size = compressed_size;
480 async_extent->pages = pages;
481 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800482 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500483 list_add_tail(&async_extent->list, &cow->extents);
484 return 0;
485}
486
Qu Wenruo42c16da2019-07-01 05:12:46 +0000487/*
488 * Check if the inode has flags compatible with compression
489 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300490static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000491{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300492 if (inode->flags & BTRFS_INODE_NODATACOW ||
493 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000494 return false;
495 return true;
496}
497
498/*
499 * Check if the inode needs to be submitted to compression, based on mount
500 * options, defragmentation, properties or heuristics.
501 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300502static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
503 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800504{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300505 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800506
Nikolay Borisov808a1292020-06-03 08:55:27 +0300507 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000508 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
509 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300510 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000511 return 0;
512 }
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800513 /*
514 * Special check for subpage.
515 *
516 * We lock the full page then run each delalloc range in the page, thus
517 * for the following case, we will hit some subpage specific corner case:
518 *
519 * 0 32K 64K
520 * | |///////| |///////|
521 * \- A \- B
522 *
523 * In above case, both range A and range B will try to unlock the full
524 * page [0, 64K), causing the one finished later will have page
525 * unlocked already, triggering various page lock requirement BUG_ON()s.
526 *
527 * So here we add an artificial limit that subpage compression can only
528 * if the range is fully page aligned.
529 *
530 * In theory we only need to ensure the first page is fully covered, but
531 * the tailing partial page will be locked until the full compression
532 * finishes, delaying the write of other range.
533 *
534 * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range
535 * first to prevent any submitted async extent to unlock the full page.
536 * By this, we can ensure for subpage case that only the last async_cow
537 * will unlock the full page.
538 */
539 if (fs_info->sectorsize < PAGE_SIZE) {
540 if (!IS_ALIGNED(start, PAGE_SIZE) ||
541 !IS_ALIGNED(end + 1, PAGE_SIZE))
542 return 0;
543 }
544
Wang Shilongf79707b2014-07-17 11:44:09 +0800545 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400546 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800547 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200548 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300549 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200550 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800551 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300552 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800553 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400554 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300555 inode->flags & BTRFS_INODE_COMPRESS ||
556 inode->prop_compress)
557 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800558 return 0;
559}
560
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200561static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800562 u64 start, u64 end, u64 num_bytes, u64 small_write)
563{
564 /* If this is a small write inside eof, kick off a defrag */
565 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200566 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800567 btrfs_add_inode_defrag(NULL, inode);
568}
569
Chris Masonc8b97812008-10-29 14:49:59 -0400570/*
Chris Mason771ed682008-11-06 22:02:51 -0500571 * we create compressed extents in two phases. The first
572 * phase compresses a range of pages that have already been
573 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400574 *
Chris Mason771ed682008-11-06 22:02:51 -0500575 * This is done inside an ordered work queue, and the compression
576 * is spread across many cpus. The actual IO submission is step
577 * two, and the ordered work queue takes care of making sure that
578 * happens in the same order things were put onto the queue by
579 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400580 *
Chris Mason771ed682008-11-06 22:02:51 -0500581 * If this code finds it can't get good compression, it puts an
582 * entry onto the work queue to write the uncompressed bytes. This
583 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300584 * are written in the same order that the flusher thread sent them
585 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400586 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300587static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400588{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200589 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400590 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400591 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200592 u64 start = async_chunk->start;
593 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400594 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400595 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400596 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400597 struct page **pages = NULL;
598 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400599 unsigned long total_compressed = 0;
600 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400601 int i;
602 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400603 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300604 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400605 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400606
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200607 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
608 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400609
Josef Bacikd98da492019-10-11 09:03:54 -0400610 /*
611 * We need to save i_size before now because it could change in between
612 * us evaluating the size and assigning it. This is because we lock and
613 * unlock the page in truncate and fallocate, and then modify the i_size
614 * later on.
615 *
616 * The barriers are to emulate READ_ONCE, remove that once i_size_read
617 * does that for us.
618 */
619 barrier();
620 i_size = i_size_read(inode);
621 barrier();
622 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400623again:
624 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300625 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100626 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
627 nr_pages = min_t(unsigned long, nr_pages,
628 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400629
Chris Masonf03d9301f2009-02-04 09:31:06 -0500630 /*
631 * we don't want to send crud past the end of i_size through
632 * compression, that's just a waste of CPU time. So, if the
633 * end of the file is before the start of our current
634 * requested range of bytes, we bail out to the uncompressed
635 * cleanup code that can deal with all of this.
636 *
637 * It isn't really the fastest way to fix things, but this is a
638 * very uncommon corner.
639 */
640 if (actual_end <= start)
641 goto cleanup_and_bail_uncompressed;
642
Chris Masonc8b97812008-10-29 14:49:59 -0400643 total_compressed = actual_end - start;
644
Shilong Wang4bcbb332014-10-07 18:44:35 -0400645 /*
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800646 * Skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400647 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400648 */
649 if (total_compressed <= blocksize &&
650 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
651 goto cleanup_and_bail_uncompressed;
652
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800653 /*
654 * For subpage case, we require full page alignment for the sector
655 * aligned range.
656 * Thus we must also check against @actual_end, not just @end.
657 */
658 if (blocksize < PAGE_SIZE) {
659 if (!IS_ALIGNED(start, PAGE_SIZE) ||
660 !IS_ALIGNED(round_up(actual_end, blocksize), PAGE_SIZE))
661 goto cleanup_and_bail_uncompressed;
662 }
663
David Sterba069eac72017-02-14 19:36:54 +0100664 total_compressed = min_t(unsigned long, total_compressed,
665 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400666 total_in = 0;
667 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400668
Chris Mason771ed682008-11-06 22:02:51 -0500669 /*
670 * we do compression for mount -o compress and when the
671 * inode has not been flagged as nocompress. This flag can
672 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400673 */
Qu Wenruo4e965572021-08-25 13:41:42 +0800674 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400675 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100676 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800677 if (!pages) {
678 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100679 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800680 goto cont;
681 }
Chris Mason179e29e2007-11-01 11:28:41 -0400682
David Sterbaeec63c62017-07-17 19:41:31 +0200683 if (BTRFS_I(inode)->defrag_compress)
684 compress_type = BTRFS_I(inode)->defrag_compress;
685 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200686 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800687
Chris Mason4adaa612013-03-26 13:07:00 -0400688 /*
689 * we need to call clear_page_dirty_for_io on each
690 * page in the range. Otherwise applications with the file
691 * mmap'd can wander in and change the page contents while
692 * we are compressing them.
693 *
694 * If the compression fails for any reason, we set the pages
695 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300696 *
697 * Note that the remaining part is redirtied, the start pointer
698 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400699 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300700 if (!redirty) {
701 extent_range_clear_dirty_for_io(inode, start, end);
702 redirty = 1;
703 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200704
705 /* Compression level is applied here and only here */
706 ret = btrfs_compress_pages(
707 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800708 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100709 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100710 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800711 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100712 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400713
714 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100715 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100716 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400717
718 /* zero the tail end of the last page, we might be
719 * sending it down to disk
720 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700721 if (offset)
722 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400723 will_compress = 1;
724 }
725 }
Li Zefan560f7d72011-09-08 10:22:01 +0800726cont:
Qu Wenruo73672532021-07-26 14:34:59 +0800727 /*
728 * Check cow_file_range() for why we don't even try to create inline
729 * extent for subpage case.
730 */
731 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400732 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300733 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400734 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500735 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400736 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300737 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
738 0, BTRFS_COMPRESS_NONE,
739 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400740 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500741 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300742 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000743 total_compressed,
744 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400745 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100746 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400747 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400748 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
749 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100750 unsigned long page_error_op;
751
Filipe Mananae6eb4312014-10-10 10:45:12 +0100752 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400753
Chris Mason771ed682008-11-06 22:02:51 -0500754 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100755 * inline extent creation worked or returned error,
756 * we don't need to create any more async work items.
757 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400758 *
759 * We use DO_ACCOUNTING here because we need the
760 * delalloc_release_metadata to be done _after_ we drop
761 * our outstanding extent for clearing delalloc for this
762 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500763 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300764 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
765 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300766 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800767 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800768 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100769 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400770 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300771
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800772 /*
773 * Ensure we only free the compressed pages if we have
774 * them allocated, as we can still reach here with
775 * inode_need_compress() == false.
776 */
777 if (pages) {
778 for (i = 0; i < nr_pages; i++) {
779 WARN_ON(pages[i]->mapping);
780 put_page(pages[i]);
781 }
782 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300783 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300784 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400785 }
786 }
787
788 if (will_compress) {
789 /*
790 * we aren't doing an inline extent round the compressed size
791 * up to a block size boundary so the allocator does sane
792 * things
793 */
Qu Wenruofda28322013-02-26 08:10:22 +0000794 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400795
796 /*
797 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300798 * win, compare the page count read with the blocks on disk,
799 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400800 */
Qu Wenruo4c162772021-09-27 15:21:59 +0800801 total_in = round_up(total_in, fs_info->sectorsize);
Timofey Titovets170607e2017-06-06 14:41:15 +0300802 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300803 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700804
805 /*
806 * The async work queues will take care of doing actual
807 * allocation on disk for these compressed pages, and
808 * will submit them to the elevator.
809 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200810 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100811 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700812 compress_type);
813
Timofey Titovets11708622017-10-03 18:06:01 +0300814 if (start + total_in < end) {
815 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700816 pages = NULL;
817 cond_resched();
818 goto again;
819 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300820 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400821 }
822 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700823 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400824 /*
825 * the compression code ran but failed to make things smaller,
826 * free any pages it allocated and our page pointer array
827 */
David Sterba4d3a8002017-02-14 19:04:07 +0100828 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400829 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300830 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400831 }
832 kfree(pages);
833 pages = NULL;
834 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100835 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400836
837 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400838 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200839 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500840 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500841 }
Chris Masonc8b97812008-10-29 14:49:59 -0400842 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500843cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700844 /*
845 * No compression, but we still need to write the pages in the file
846 * we've been given so far. redirty the locked page if it corresponds
847 * to our extent and set things up for the async work queue to run
848 * cow_file_range to do the normal delalloc dance.
849 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700850 if (async_chunk->locked_page &&
851 (page_offset(async_chunk->locked_page) >= start &&
852 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200853 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700854 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700855 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700856
857 if (redirty)
858 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200859 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700860 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300861 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500862
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300863 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500864}
Chris Mason771ed682008-11-06 22:02:51 -0500865
Filipe Manana40ae8372014-10-06 22:14:24 +0100866static void free_async_extent_pages(struct async_extent *async_extent)
867{
868 int i;
869
870 if (!async_extent->pages)
871 return;
872
873 for (i = 0; i < async_extent->nr_pages; i++) {
874 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300875 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100876 }
877 kfree(async_extent->pages);
878 async_extent->nr_pages = 0;
879 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500880}
881
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800882static int submit_uncompressed_range(struct btrfs_inode *inode,
883 struct async_extent *async_extent,
884 struct page *locked_page)
Chris Mason771ed682008-11-06 22:02:51 -0500885{
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800886 u64 start = async_extent->start;
887 u64 end = async_extent->start + async_extent->ram_size - 1;
888 unsigned long nr_written = 0;
889 int page_started = 0;
890 int ret;
Chris Mason771ed682008-11-06 22:02:51 -0500891
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800892 /*
893 * Call cow_file_range() to run the delalloc range directly, since we
894 * won't go to NOCOW or async path again.
895 *
896 * Also we call cow_file_range() with @unlock_page == 0, so that we
897 * can directly submit them without interruption.
898 */
899 ret = cow_file_range(inode, locked_page, start, end, &page_started,
900 &nr_written, 0);
901 /* Inline extent inserted, page gets unlocked and everything is done */
902 if (page_started) {
903 ret = 0;
904 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500905 }
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800906 if (ret < 0) {
907 if (locked_page)
908 unlock_page(locked_page);
909 goto out;
910 }
911
912 ret = extent_write_locked_range(&inode->vfs_inode, start, end);
913 /* All pages will be unlocked, including @locked_page */
914out:
915 kfree(async_extent);
916 return ret;
917}
918
Qu Wenruob4ccace2021-09-27 15:21:57 +0800919static int submit_one_async_extent(struct btrfs_inode *inode,
920 struct async_chunk *async_chunk,
921 struct async_extent *async_extent,
922 u64 *alloc_hint)
Chris Mason771ed682008-11-06 22:02:51 -0500923{
Qu Wenruob4ccace2021-09-27 15:21:57 +0800924 struct extent_io_tree *io_tree = &inode->io_tree;
925 struct btrfs_root *root = inode->root;
926 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500927 struct btrfs_key ins;
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800928 struct page *locked_page = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500929 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500930 int ret = 0;
Qu Wenruob4ccace2021-09-27 15:21:57 +0800931 u64 start = async_extent->start;
932 u64 end = async_extent->start + async_extent->ram_size - 1;
Chris Mason771ed682008-11-06 22:02:51 -0500933
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800934 /*
935 * If async_chunk->locked_page is in the async_extent range, we need to
936 * handle it.
937 */
938 if (async_chunk->locked_page) {
939 u64 locked_page_start = page_offset(async_chunk->locked_page);
940 u64 locked_page_end = locked_page_start + PAGE_SIZE - 1;
941
942 if (!(start >= locked_page_end || end <= locked_page_start))
943 locked_page = async_chunk->locked_page;
944 }
Qu Wenruob4ccace2021-09-27 15:21:57 +0800945 lock_extent(io_tree, start, end);
Chris Mason771ed682008-11-06 22:02:51 -0500946
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800947 /* We have fall back to uncompressed write */
948 if (!async_extent->pages)
949 return submit_uncompressed_range(inode, async_extent, locked_page);
Qu Wenruob4ccace2021-09-27 15:21:57 +0800950
951 ret = btrfs_reserve_extent(root, async_extent->ram_size,
952 async_extent->compressed_size,
953 async_extent->compressed_size,
954 0, *alloc_hint, &ins, 1, 1);
955 if (ret) {
956 free_async_extent_pages(async_extent);
957 /*
958 * Here we used to try again by going back to non-compressed
959 * path for ENOSPC. But we can't reserve space even for
960 * compressed size, how could it work for uncompressed size
961 * which requires larger size? So here we directly go error
962 * path.
963 */
964 goto out_free;
965 }
966
967 /* Here we're doing allocation and writeback of the compressed pages */
968 em = create_io_em(inode, start,
969 async_extent->ram_size, /* len */
970 start, /* orig_start */
971 ins.objectid, /* block_start */
972 ins.offset, /* block_len */
973 ins.offset, /* orig_block_len */
974 async_extent->ram_size, /* ram_bytes */
975 async_extent->compress_type,
976 BTRFS_ORDERED_COMPRESSED);
977 if (IS_ERR(em)) {
978 ret = PTR_ERR(em);
979 goto out_free_reserve;
980 }
981 free_extent_map(em);
982
983 ret = btrfs_add_ordered_extent_compress(inode, start, /* file_offset */
984 ins.objectid, /* disk_bytenr */
985 async_extent->ram_size, /* num_bytes */
986 ins.offset, /* disk_num_bytes */
987 async_extent->compress_type);
988 if (ret) {
989 btrfs_drop_extent_cache(inode, start, end, 0);
990 goto out_free_reserve;
991 }
992 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
993
994 /* Clear dirty, set writeback and unlock the pages. */
995 extent_clear_unlock_delalloc(inode, start, end,
996 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
997 PAGE_UNLOCK | PAGE_START_WRITEBACK);
998 if (btrfs_submit_compressed_write(inode, start, /* file_offset */
999 async_extent->ram_size, /* num_bytes */
1000 ins.objectid, /* disk_bytenr */
1001 ins.offset, /* compressed_len */
1002 async_extent->pages, /* compressed_pages */
1003 async_extent->nr_pages,
1004 async_chunk->write_flags,
1005 async_chunk->blkcg_css)) {
1006 const u64 start = async_extent->start;
1007 const u64 end = start + async_extent->ram_size - 1;
1008
1009 btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0);
1010
1011 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
1012 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
1013 free_async_extent_pages(async_extent);
1014 }
1015 *alloc_hint = ins.objectid + ins.offset;
1016 kfree(async_extent);
1017 return ret;
1018
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001019out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001020 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001021 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001022out_free:
Qu Wenruob4ccace2021-09-27 15:21:57 +08001023 extent_clear_unlock_delalloc(inode, start, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001025 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -04001026 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001027 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1028 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +01001029 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 kfree(async_extent);
Qu Wenruob4ccace2021-09-27 15:21:57 +08001031 return ret;
1032}
1033
1034/*
1035 * Phase two of compressed writeback. This is the ordered portion of the code,
1036 * which only gets called in the order the work was queued. We walk all the
1037 * async extents created by compress_file_range and send them down to the disk.
1038 */
1039static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
1040{
1041 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
1042 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1043 struct async_extent *async_extent;
1044 u64 alloc_hint = 0;
1045 int ret = 0;
1046
1047 while (!list_empty(&async_chunk->extents)) {
1048 u64 extent_start;
1049 u64 ram_size;
1050
1051 async_extent = list_entry(async_chunk->extents.next,
1052 struct async_extent, list);
1053 list_del(&async_extent->list);
1054 extent_start = async_extent->start;
1055 ram_size = async_extent->ram_size;
1056
1057 ret = submit_one_async_extent(inode, async_chunk, async_extent,
1058 &alloc_hint);
1059 btrfs_debug(fs_info,
1060"async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1061 inode->root->root_key.objectid,
1062 btrfs_ino(inode), extent_start, ram_size, ret);
1063 }
Chris Mason771ed682008-11-06 22:02:51 -05001064}
1065
Nikolay Borisov43c69842020-06-03 08:55:02 +03001066static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04001067 u64 num_bytes)
1068{
Nikolay Borisov43c69842020-06-03 08:55:02 +03001069 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -04001070 struct extent_map *em;
1071 u64 alloc_hint = 0;
1072
1073 read_lock(&em_tree->lock);
1074 em = search_extent_mapping(em_tree, start, num_bytes);
1075 if (em) {
1076 /*
1077 * if block start isn't an actual block number then find the
1078 * first block in this inode and use that as a hint. If that
1079 * block is also bogus then just don't worry about it.
1080 */
1081 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1082 free_extent_map(em);
1083 em = search_extent_mapping(em_tree, 0, 0);
1084 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1085 alloc_hint = em->block_start;
1086 if (em)
1087 free_extent_map(em);
1088 } else {
1089 alloc_hint = em->block_start;
1090 free_extent_map(em);
1091 }
1092 }
1093 read_unlock(&em_tree->lock);
1094
1095 return alloc_hint;
1096}
1097
Chris Mason771ed682008-11-06 22:02:51 -05001098/*
1099 * when extent_io.c finds a delayed allocation range in the file,
1100 * the call backs end up in this code. The basic idea is to
1101 * allocate extents on disk for the range, and create ordered data structs
1102 * in ram to track those extents.
1103 *
1104 * locked_page is the page that writepage had locked already. We use
1105 * it to make sure we don't do extra locks or unlocks.
1106 *
1107 * *page_started is set to one if we unlock locked_page and do everything
1108 * required to start IO on it. It may be clean and already done with
1109 * IO when we return.
1110 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001111static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001112 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001113 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001114 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001115{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001116 struct btrfs_root *root = inode->root;
1117 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001118 u64 alloc_hint = 0;
1119 u64 num_bytes;
1120 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001122 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001123 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001124 struct btrfs_key ins;
1125 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001126 unsigned clear_bits;
1127 unsigned long page_ops;
1128 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001129 int ret = 0;
1130
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001131 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001132 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001133 ret = -EINVAL;
1134 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001135 }
Chris Mason771ed682008-11-06 22:02:51 -05001136
Qu Wenruofda28322013-02-26 08:10:22 +00001137 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001138 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001139 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001140
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001141 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001142
Qu Wenruo73672532021-07-26 14:34:59 +08001143 /*
1144 * Due to the page size limit, for subpage we can only trigger the
1145 * writeback for the dirty sectors of page, that means data writeback
1146 * is doing more writeback than what we want.
1147 *
1148 * This is especially unexpected for some call sites like fallocate,
1149 * where we only increase i_size after everything is done.
1150 * This means we can trigger inline extent even if we didn't want to.
1151 * So here we skip inline extent creation completely.
1152 */
1153 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Mason771ed682008-11-06 22:02:51 -05001154 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001155 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001156 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001157 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001158 /*
1159 * We use DO_ACCOUNTING here because we need the
1160 * delalloc_release_metadata to be run _after_ we drop
1161 * our outstanding extent for clearing delalloc for this
1162 * range.
1163 */
Qu Wenruo4750af32021-05-31 16:50:48 +08001164 extent_clear_unlock_delalloc(inode, start, end,
1165 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001166 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001167 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1168 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001169 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001170 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001171 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001172 *page_started = 1;
Qu Wenruo4750af32021-05-31 16:50:48 +08001173 /*
1174 * locked_page is locked by the caller of
1175 * writepage_delalloc(), not locked by
1176 * __process_pages_contig().
1177 *
1178 * We can't let __process_pages_contig() to unlock it,
1179 * as it doesn't have any subpage::writers recorded.
1180 *
1181 * Here we manually unlock the page, since the caller
1182 * can't use page_started to determine if it's an
1183 * inline extent or a compressed extent.
1184 */
1185 unlock_page(locked_page);
Chris Mason771ed682008-11-06 22:02:51 -05001186 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001189 }
1190 }
Chris Masonc8b97812008-10-29 14:49:59 -04001191
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001192 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1193 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001194
Filipe Manana432cd2a2020-06-08 13:32:55 +01001195 /*
1196 * Relocation relies on the relocated extents to have exactly the same
1197 * size as the original extents. Normally writeback for relocation data
1198 * extents follows a NOCOW path because relocation preallocates the
1199 * extents. However, due to an operation such as scrub turning a block
1200 * group to RO mode, it may fallback to COW mode, so we must make sure
1201 * an extent allocated during COW has exactly the requested size and can
1202 * not be split into smaller extents, otherwise relocation breaks and
1203 * fails during the stage where it updates the bytenr of file extent
1204 * items.
1205 */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001206 if (btrfs_is_data_reloc_root(root))
Filipe Manana432cd2a2020-06-08 13:32:55 +01001207 min_alloc_size = num_bytes;
1208 else
1209 min_alloc_size = fs_info->sectorsize;
1210
Anand Jain3752d222018-02-15 12:29:38 +08001211 while (num_bytes > 0) {
1212 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001213 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001214 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001215 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001216 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001218 cur_alloc_size = ins.offset;
1219 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001220
Chris Mason771ed682008-11-06 22:02:51 -05001221 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001222 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001223 start, /* orig_start */
1224 ins.objectid, /* block_start */
1225 ins.offset, /* block_len */
1226 ins.offset, /* orig_block_len */
1227 ram_size, /* ram_bytes */
1228 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001229 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001230 if (IS_ERR(em)) {
1231 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001232 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001233 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001234 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001235
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001236 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001237 ram_size, cur_alloc_size,
1238 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001239 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001240 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001241
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001242 if (btrfs_is_data_reloc_root(root)) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001243 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001245 /*
1246 * Only drop cache here, and process as normal.
1247 *
1248 * We must not allow extent_clear_unlock_delalloc()
1249 * at out_unlock label to free meta of this ordered
1250 * extent, as its meta should be freed by
1251 * btrfs_finish_ordered_io().
1252 *
1253 * So we must continue until @start is increased to
1254 * skip current ordered extent.
1255 */
Josef Bacik00361582013-08-14 14:02:47 -04001256 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001257 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001258 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001259 }
1260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001261 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001262
Qu Wenruof57ad932021-04-07 19:22:13 +08001263 /*
1264 * We're not doing compressed IO, don't unlock the first page
1265 * (which the caller expects to stay locked), don't clear any
1266 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001267 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001268 * Do set the Ordered (Private2) bit so we know this page was
1269 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001270 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001271 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001272 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001273
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001274 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001275 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001276 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001277 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001278 if (num_bytes < cur_alloc_size)
1279 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001280 else
Anand Jain3752d222018-02-15 12:29:38 +08001281 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001282 alloc_hint = ins.objectid + ins.offset;
1283 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001284 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001285
1286 /*
1287 * btrfs_reloc_clone_csums() error, since start is increased
1288 * extent_clear_unlock_delalloc() at out_unlock label won't
1289 * free metadata of current ordered extent, we're OK to exit.
1290 */
1291 if (ret)
1292 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001293 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001295 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001296
Filipe Mananad9f85962014-08-25 10:43:00 +01001297out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001298 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001299out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001300 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001301 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001303 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1304 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001305 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001306 /*
1307 * If we reserved an extent for our delalloc range (or a subrange) and
1308 * failed to create the respective ordered extent, then it means that
1309 * when we reserved the extent we decremented the extent's size from
1310 * the data space_info's bytes_may_use counter and incremented the
1311 * space_info's bytes_reserved counter by the same amount. We must make
1312 * sure extent_clear_unlock_delalloc() does not try to decrement again
1313 * the data space_info's bytes_may_use counter, therefore we do not pass
1314 * it the flag EXTENT_CLEAR_DATA_RESV.
1315 */
1316 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001317 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001318 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001319 locked_page,
1320 clear_bits,
1321 page_ops);
1322 start += cur_alloc_size;
1323 if (start >= end)
1324 goto out;
1325 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001326 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001327 clear_bits | EXTENT_CLEAR_DATA_RESV,
1328 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001329 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001330}
Chris Masonc8b97812008-10-29 14:49:59 -04001331
Chris Mason771ed682008-11-06 22:02:51 -05001332/*
1333 * work queue call back to started compression on a file and pages
1334 */
1335static noinline void async_cow_start(struct btrfs_work *work)
1336{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001337 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001338 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001339
Nikolay Borisovb5326272019-03-12 17:20:25 +02001340 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001341
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001342 compressed_extents = compress_file_range(async_chunk);
1343 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001344 btrfs_add_delayed_iput(async_chunk->inode);
1345 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001346 }
Chris Mason771ed682008-11-06 22:02:51 -05001347}
1348
1349/*
1350 * work queue call back to submit previously compressed pages
1351 */
1352static noinline void async_cow_submit(struct btrfs_work *work)
1353{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001354 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1355 work);
1356 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001357 unsigned long nr_pages;
1358
Nikolay Borisovb5326272019-03-12 17:20:25 +02001359 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001361
Nikolay Borisov4546d172019-01-03 10:50:03 +02001362 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001363 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001364 * in which case we don't have anything to submit, yet we need to
1365 * always adjust ->async_delalloc_pages as its paired with the init
1366 * happening in cow_file_range_async
1367 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001368 if (async_chunk->inode)
1369 submit_compressed_extents(async_chunk);
Josef Bacikac981412021-07-14 14:47:17 -04001370
1371 /* atomic_sub_return implies a barrier */
1372 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1373 5 * SZ_1M)
1374 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001375}
Chris Masonc8b97812008-10-29 14:49:59 -04001376
Chris Mason771ed682008-11-06 22:02:51 -05001377static noinline void async_cow_free(struct btrfs_work *work)
1378{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001379 struct async_chunk *async_chunk;
Qu Wenruo9e895a82021-09-27 15:21:45 +08001380 struct async_cow *async_cow;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001381
Nikolay Borisovb5326272019-03-12 17:20:25 +02001382 async_chunk = container_of(work, struct async_chunk, work);
1383 if (async_chunk->inode)
1384 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001385 if (async_chunk->blkcg_css)
1386 css_put(async_chunk->blkcg_css);
Qu Wenruo9e895a82021-09-27 15:21:45 +08001387
1388 async_cow = async_chunk->async_cow;
1389 if (atomic_dec_and_test(&async_cow->num_chunks))
1390 kvfree(async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001391}
1392
Nikolay Borisov751b6432020-06-03 08:55:22 +03001393static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001394 struct writeback_control *wbc,
1395 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001396 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001397 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001398{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001399 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001400 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001401 struct async_cow *ctx;
1402 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001403 unsigned long nr_pages;
1404 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001405 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1406 int i;
1407 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001408 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001409 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001410
Nikolay Borisov751b6432020-06-03 08:55:22 +03001411 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001412
Nikolay Borisov751b6432020-06-03 08:55:22 +03001413 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001414 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1415 num_chunks = 1;
1416 should_compress = false;
1417 } else {
1418 should_compress = true;
1419 }
1420
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001421 nofs_flag = memalloc_nofs_save();
1422 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1423 memalloc_nofs_restore(nofs_flag);
1424
Nikolay Borisov97db1202019-03-12 17:20:24 +02001425 if (!ctx) {
1426 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1427 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1428 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001429 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1430 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001431
Nikolay Borisov751b6432020-06-03 08:55:22 +03001432 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1433 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001434 return -ENOMEM;
1435 }
1436
1437 async_chunk = ctx->chunks;
1438 atomic_set(&ctx->num_chunks, num_chunks);
1439
1440 for (i = 0; i < num_chunks; i++) {
1441 if (should_compress)
1442 cur_end = min(end, start + SZ_512K - 1);
1443 else
1444 cur_end = end;
1445
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001446 /*
1447 * igrab is called higher up in the call chain, take only the
1448 * lightweight reference for the callback lifetime
1449 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001450 ihold(&inode->vfs_inode);
Qu Wenruo9e895a82021-09-27 15:21:45 +08001451 async_chunk[i].async_cow = ctx;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001452 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001453 async_chunk[i].start = start;
1454 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001455 async_chunk[i].write_flags = write_flags;
1456 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001457
Chris Mason1d53c9e2019-07-10 12:28:16 -07001458 /*
1459 * The locked_page comes all the way from writepage and its
1460 * the original page we were actually given. As we spread
1461 * this large delalloc region across multiple async_chunk
1462 * structs, only the first struct needs a pointer to locked_page
1463 *
1464 * This way we don't need racey decisions about who is supposed
1465 * to unlock it.
1466 */
1467 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001468 /*
1469 * Depending on the compressibility, the pages might or
1470 * might not go through async. We want all of them to
1471 * be accounted against wbc once. Let's do it here
1472 * before the paths diverge. wbc accounting is used
1473 * only for foreign writeback detection and doesn't
1474 * need full accuracy. Just account the whole thing
1475 * against the first page.
1476 */
1477 wbc_account_cgroup_owner(wbc, locked_page,
1478 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001479 async_chunk[i].locked_page = locked_page;
1480 locked_page = NULL;
1481 } else {
1482 async_chunk[i].locked_page = NULL;
1483 }
1484
Chris Masonec39f762019-07-10 12:28:17 -07001485 if (blkcg_css != blkcg_root_css) {
1486 css_get(blkcg_css);
1487 async_chunk[i].blkcg_css = blkcg_css;
1488 } else {
1489 async_chunk[i].blkcg_css = NULL;
1490 }
1491
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001492 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1493 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001494
Nikolay Borisov97db1202019-03-12 17:20:24 +02001495 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001496 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001497
Nikolay Borisov97db1202019-03-12 17:20:24 +02001498 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001499
Chris Mason771ed682008-11-06 22:02:51 -05001500 *nr_written += nr_pages;
1501 start = cur_end + 1;
1502 }
1503 *page_started = 1;
1504 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001505}
1506
Naohiro Aota42c01102021-02-04 19:22:07 +09001507static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1508 struct page *locked_page, u64 start,
1509 u64 end, int *page_started,
1510 unsigned long *nr_written)
1511{
1512 int ret;
1513
1514 ret = cow_file_range(inode, locked_page, start, end, page_started,
1515 nr_written, 0);
1516 if (ret)
1517 return ret;
1518
1519 if (*page_started)
1520 return 0;
1521
1522 __set_page_dirty_nobuffers(locked_page);
1523 account_page_redirty(locked_page);
Qu Wenruo2bd0fc92021-09-27 15:21:58 +08001524 extent_write_locked_range(&inode->vfs_inode, start, end);
Naohiro Aota42c01102021-02-04 19:22:07 +09001525 *page_started = 1;
1526
1527 return 0;
1528}
1529
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001530static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001531 u64 bytenr, u64 num_bytes)
1532{
1533 int ret;
1534 struct btrfs_ordered_sum *sums;
1535 LIST_HEAD(list);
1536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001537 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001538 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001539 if (ret == 0 && list_empty(&list))
1540 return 0;
1541
1542 while (!list_empty(&list)) {
1543 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1544 list_del(&sums->list);
1545 kfree(sums);
1546 }
Liu Bo58113752018-01-31 17:09:13 -07001547 if (ret < 0)
1548 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001549 return 1;
1550}
1551
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001552static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001553 const u64 start, const u64 end,
1554 int *page_started, unsigned long *nr_written)
1555{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001556 const bool is_space_ino = btrfs_is_free_space_inode(inode);
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001557 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001558 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001559 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001560 u64 range_start = start;
1561 u64 count;
1562
1563 /*
1564 * If EXTENT_NORESERVE is set it means that when the buffered write was
1565 * made we had not enough available data space and therefore we did not
1566 * reserve data space for it, since we though we could do NOCOW for the
1567 * respective file range (either there is prealloc extent or the inode
1568 * has the NOCOW bit set).
1569 *
1570 * However when we need to fallback to COW mode (because for example the
1571 * block group for the corresponding extent was turned to RO mode by a
1572 * scrub or relocation) we need to do the following:
1573 *
1574 * 1) We increment the bytes_may_use counter of the data space info.
1575 * If COW succeeds, it allocates a new data extent and after doing
1576 * that it decrements the space info's bytes_may_use counter and
1577 * increments its bytes_reserved counter by the same amount (we do
1578 * this at btrfs_add_reserved_bytes()). So we need to increment the
1579 * bytes_may_use counter to compensate (when space is reserved at
1580 * buffered write time, the bytes_may_use counter is incremented);
1581 *
1582 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1583 * that if the COW path fails for any reason, it decrements (through
1584 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1585 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001586 *
1587 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001588 * space cache inode or an inode of the data relocation tree, we must
1589 * also increment bytes_may_use of the data space_info for the same
1590 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001591 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001592 * group that contains that extent to RO mode and therefore force COW
1593 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001594 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001595 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001596 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001597 if (count > 0 || is_space_ino || is_reloc_ino) {
1598 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001599 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001600 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1601
Filipe Manana6bd335b2020-06-08 13:33:05 +01001602 if (is_space_ino || is_reloc_ino)
1603 bytes = range_bytes;
1604
Filipe Manana467dc472020-05-27 11:16:07 +01001605 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001606 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001607 spin_unlock(&sinfo->lock);
1608
Filipe Manana2166e5e2020-05-27 11:16:19 +01001609 if (count > 0)
1610 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1611 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001612 }
1613
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001614 return cow_file_range(inode, locked_page, start, end, page_started,
1615 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001616}
1617
Chris Masond352ac62008-09-29 15:18:18 -04001618/*
1619 * when nowcow writeback call back. This checks for snapshots or COW copies
1620 * of the extents that exist in the file, and COWs the file as required.
1621 *
1622 * If no cow copies or snapshots exist, we write directly to the existing
1623 * blocks on disk
1624 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001625static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001626 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001627 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001628 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001629 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001630{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001631 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1632 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001633 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001634 u64 cow_start = (u64)-1;
1635 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001636 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001637 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001638 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1639 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001640 bool nocow = false;
1641 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001642 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001643
1644 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001645 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001646 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001647 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001648 EXTENT_DO_ACCOUNTING |
1649 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001650 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001651 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001652 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001653 }
Li Zefan82d59022011-04-20 10:33:24 +08001654
Yan Zheng80ff3852008-10-30 14:20:02 -04001655 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001656 struct btrfs_key found_key;
1657 struct btrfs_file_extent_item *fi;
1658 struct extent_buffer *leaf;
1659 u64 extent_end;
1660 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001661 u64 num_bytes = 0;
1662 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001663 u64 ram_bytes;
1664 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001665
1666 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001667
Liu Boe4c3b2d2017-01-30 12:25:28 -08001668 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001670 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001671 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001672
1673 /*
1674 * If there is no extent for our range when doing the initial
1675 * search, then go back to the previous slot as it will be the
1676 * one containing the search offset
1677 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1679 leaf = path->nodes[0];
1680 btrfs_item_key_to_cpu(leaf, &found_key,
1681 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001682 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001683 found_key.type == BTRFS_EXTENT_DATA_KEY)
1684 path->slots[0]--;
1685 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001686 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001687next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001688 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001689 leaf = path->nodes[0];
1690 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1691 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001692 if (ret < 0) {
1693 if (cow_start != (u64)-1)
1694 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001695 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001696 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001697 if (ret > 0)
1698 break;
1699 leaf = path->nodes[0];
1700 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001701
Yan Zheng80ff3852008-10-30 14:20:02 -04001702 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001703
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001704 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001705 if (found_key.objectid > ino)
1706 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001707 /*
1708 * Keep searching until we find an EXTENT_ITEM or there are no
1709 * more extents for this inode
1710 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001711 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1712 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1713 path->slots[0]++;
1714 goto next_slot;
1715 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001716
1717 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001718 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001719 found_key.offset > end)
1720 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001721
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001722 /*
1723 * If the found extent starts after requested offset, then
1724 * adjust extent_end to be right before this extent begins
1725 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001726 if (found_key.offset > cur_offset) {
1727 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001728 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001729 goto out_check;
1730 }
Chris Masonc31f8832008-01-08 15:46:31 -05001731
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001732 /*
1733 * Found extent which begins before our range and potentially
1734 * intersect it
1735 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001736 fi = btrfs_item_ptr(leaf, path->slots[0],
1737 struct btrfs_file_extent_item);
1738 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001739
Josef Bacikcc95bef2013-04-04 14:31:27 -04001740 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001741 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1742 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001743 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001745 extent_end = found_key.offset +
1746 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001747 disk_num_bytes =
1748 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001749 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001750 * If the extent we got ends before our current offset,
1751 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001752 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001753 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001754 path->slots[0]++;
1755 goto next_slot;
1756 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001757 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001758 if (disk_bytenr == 0)
1759 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001760 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001761 if (btrfs_file_extent_compression(leaf, fi) ||
1762 btrfs_file_extent_encryption(leaf, fi) ||
1763 btrfs_file_extent_other_encoding(leaf, fi))
1764 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001765 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001766 * If extent is created before the last volume's snapshot
1767 * this implies the extent is shared, hence we can't do
1768 * nocow. This is the same check as in
1769 * btrfs_cross_ref_exist but without calling
1770 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001771 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001772 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001773 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001774 btrfs_root_last_snapshot(&root->root_item))
1775 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001776 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1777 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001778
1779 /*
1780 * The following checks can be expensive, as they need to
1781 * take other locks and do btree or rbtree searches, so
1782 * release the path to avoid blocking other tasks for too
1783 * long.
1784 */
1785 btrfs_release_path(path);
1786
Liu Bo58113752018-01-31 17:09:13 -07001787 ret = btrfs_cross_ref_exist(root, ino,
1788 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001789 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001790 if (ret) {
1791 /*
1792 * ret could be -EIO if the above fails to read
1793 * metadata.
1794 */
1795 if (ret < 0) {
1796 if (cow_start != (u64)-1)
1797 cur_offset = cow_start;
1798 goto error;
1799 }
1800
Nikolay Borisov3e024842019-08-21 10:42:03 +03001801 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001802 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001803 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001804 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001805 disk_bytenr += cur_offset - found_key.offset;
1806 num_bytes = min(end + 1, extent_end) - cur_offset;
1807 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001808 * If there are pending snapshots for this root, we
1809 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001810 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001811 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001812 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001813 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001814 * force cow if csum exists in the range.
1815 * this ensure that csum for a given extent are
1816 * either valid or do not exist.
1817 */
Liu Bo58113752018-01-31 17:09:13 -07001818 ret = csum_exist_in_range(fs_info, disk_bytenr,
1819 num_bytes);
1820 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001821 /*
1822 * ret could be -EIO if the above fails to read
1823 * metadata.
1824 */
1825 if (ret < 0) {
1826 if (cow_start != (u64)-1)
1827 cur_offset = cow_start;
1828 goto error;
1829 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001830 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001831 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001832 }
Filipe Manana20903032021-02-05 12:55:36 +00001833 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001834 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001835 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001836 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001837 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001838 extent_end = found_key.offset + ram_bytes;
1839 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001840 /* Skip extents outside of our requested range */
1841 if (extent_end <= start) {
1842 path->slots[0]++;
1843 goto next_slot;
1844 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001845 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001846 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001847 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001848 }
1849out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001850 /*
1851 * If nocow is false then record the beginning of the range
1852 * that needs to be COWed
1853 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001854 if (!nocow) {
1855 if (cow_start == (u64)-1)
1856 cow_start = cur_offset;
1857 cur_offset = extent_end;
1858 if (cur_offset > end)
1859 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001860 if (!path->nodes[0])
1861 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001862 path->slots[0]++;
1863 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001864 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001865
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001866 /*
1867 * COW range from cow_start to found_key.offset - 1. As the key
1868 * will contain the beginning of the first extent that can be
1869 * NOCOW, following one which needs to be COW'ed
1870 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001871 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001872 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001873 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001874 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001875 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001876 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001877 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001878 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001879
Yan Zhengd899e052008-10-30 14:25:28 -04001880 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001881 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001882 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001883
Nikolay Borisov968322c2020-06-03 08:55:21 +03001884 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001885 orig_start,
1886 disk_bytenr, /* block_start */
1887 num_bytes, /* block_len */
1888 disk_num_bytes, /* orig_block_len */
1889 ram_bytes, BTRFS_COMPRESS_NONE,
1890 BTRFS_ORDERED_PREALLOC);
1891 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001892 ret = PTR_ERR(em);
1893 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001894 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001895 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001896 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001897 disk_bytenr, num_bytes,
1898 num_bytes,
1899 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001900 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001901 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001902 cur_offset + num_bytes - 1,
1903 0);
1904 goto error;
1905 }
Yan Zhengd899e052008-10-30 14:25:28 -04001906 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001907 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001908 disk_bytenr, num_bytes,
1909 num_bytes,
1910 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001911 if (ret)
1912 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001913 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001914
Filipe Mananaf78c4362016-05-09 13:15:41 +01001915 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001916 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001917 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001918
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001919 if (btrfs_is_data_reloc_root(root))
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001920 /*
1921 * Error handled later, as we must prevent
1922 * extent_clear_unlock_delalloc() in error handler
1923 * from freeing metadata of created ordered extent.
1924 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001925 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001926 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001927
Nikolay Borisov968322c2020-06-03 08:55:21 +03001928 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001929 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001930 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001931 EXTENT_DELALLOC |
1932 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001933 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001934
Yan Zheng80ff3852008-10-30 14:20:02 -04001935 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001936
1937 /*
1938 * btrfs_reloc_clone_csums() error, now we're OK to call error
1939 * handler, as metadata for created ordered extent will only
1940 * be freed by btrfs_finish_ordered_io().
1941 */
1942 if (ret)
1943 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001944 if (cur_offset > end)
1945 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001946 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001947 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001948
Robbie Ko506481b2018-10-30 18:04:04 +08001949 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001950 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001951
Yan Zheng80ff3852008-10-30 14:20:02 -04001952 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001953 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001954 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1955 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001956 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001957 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001958 }
1959
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001960error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001961 if (nocow)
1962 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1963
Josef Bacik17ca04a2012-05-31 15:58:55 -04001964 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001965 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001966 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001967 EXTENT_DELALLOC | EXTENT_DEFRAG |
1968 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001969 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001970 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001971 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001972 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001973}
1974
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001975static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001976{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001977 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1978 if (inode->defrag_bytes &&
1979 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1980 0, NULL))
1981 return false;
1982 return true;
1983 }
1984 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001985}
1986
Chris Masond352ac62008-09-29 15:18:18 -04001987/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001988 * Function to process delayed allocation (create CoW) for ranges which are
1989 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001990 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001991int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001992 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1993 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001994{
Chris Masonbe20aa92007-12-17 20:14:01 -05001995 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001996 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001997
Qu Wenruo2749f7e2021-09-27 15:22:07 +08001998 /*
1999 * The range must cover part of the @locked_page, or the returned
2000 * @page_started can confuse the caller.
2001 */
2002 ASSERT(!(end <= page_offset(locked_page) ||
2003 start >= page_offset(locked_page) + PAGE_SIZE));
2004
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06002005 if (should_nocow(inode, start, end)) {
Johannes Thumshirn2adada82021-09-09 01:19:29 +09002006 /*
2007 * Normally on a zoned device we're only doing COW writes, but
2008 * in case of relocation on a zoned filesystem we have taken
2009 * precaution, that we're only writing sequentially. It's safe
2010 * to use run_delalloc_nocow() here, like for regular
2011 * preallocated inodes.
2012 */
2013 ASSERT(!zoned ||
2014 (zoned && btrfs_is_data_reloc_root(inode->root)));
Nikolay Borisov98456b92020-06-03 08:55:29 +03002015 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06002016 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03002017 } else if (!inode_can_compress(inode) ||
2018 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09002019 if (zoned)
2020 ret = run_delalloc_zoned(inode, locked_page, start, end,
2021 page_started, nr_written);
2022 else
2023 ret = cow_file_range(inode, locked_page, start, end,
2024 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04002025 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03002026 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
2027 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01002028 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04002029 }
Qu Wenruo7361b4a2021-07-28 14:05:05 +08002030 ASSERT(ret <= 0);
Qu Wenruo524272602017-03-08 10:25:52 +08002031 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03002032 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02002033 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04002034 return ret;
2035}
2036
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02002037void btrfs_split_delalloc_extent(struct inode *inode,
2038 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002039{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002040 u64 size;
2041
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002042 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002043 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002044 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002045
Josef Bacikdcab6a32015-02-11 15:08:59 -05002046 size = orig->end - orig->start + 1;
2047 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01002048 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002049 u64 new_size;
2050
2051 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002052 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04002053 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002054 */
2055 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01002056 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04002057 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01002058 num_extents += count_max_extents(new_size);
2059 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002060 return;
2061 }
2062
Josef Bacik9e0baf62011-07-15 15:16:44 +00002063 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002064 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002065 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002066}
2067
2068/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002069 * Handle merged delayed allocation extents so we can keep track of new extents
2070 * that are just merged onto old extents, such as when we are doing sequential
2071 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04002072 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02002073void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2074 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002075{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002076 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01002077 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002078
Josef Bacik9ed74f22009-09-11 16:12:44 -04002079 /* not delalloc, ignore it */
2080 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002081 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002082
Josef Bacik8461a3d2015-03-13 15:12:08 -04002083 if (new->start > other->start)
2084 new_size = new->end - other->start + 1;
2085 else
2086 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002087
2088 /* we're not bigger than the max, unreserve the space and go */
2089 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2090 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002091 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002092 spin_unlock(&BTRFS_I(inode)->lock);
2093 return;
2094 }
2095
2096 /*
Josef Bacikba117212015-03-13 15:01:24 -04002097 * We have to add up either side to figure out how many extents were
2098 * accounted for before we merged into one big extent. If the number of
2099 * extents we accounted for is <= the amount we need for the new range
2100 * then we can return, otherwise drop. Think of it like this
2101 *
2102 * [ 4k][MAX_SIZE]
2103 *
2104 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2105 * need 2 outstanding extents, on one side we have 1 and the other side
2106 * we have 1 so they are == and we can return. But in this case
2107 *
2108 * [MAX_SIZE+4k][MAX_SIZE+4k]
2109 *
2110 * Each range on their own accounts for 2 extents, but merged together
2111 * they are only 3 extents worth of accounting, so we need to drop in
2112 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002113 */
Josef Bacikba117212015-03-13 15:01:24 -04002114 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002115 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002116 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002117 num_extents += count_max_extents(old_size);
2118 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002119 return;
2120
Josef Bacik9e0baf62011-07-15 15:16:44 +00002121 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002122 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002123 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002124}
2125
Miao Xieeb73c1b2013-05-15 07:48:22 +00002126static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2127 struct inode *inode)
2128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2130
Miao Xieeb73c1b2013-05-15 07:48:22 +00002131 spin_lock(&root->delalloc_lock);
2132 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2133 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2134 &root->delalloc_inodes);
2135 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2136 &BTRFS_I(inode)->runtime_flags);
2137 root->nr_delalloc_inodes++;
2138 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002139 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002140 BUG_ON(!list_empty(&root->delalloc_root));
2141 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002142 &fs_info->delalloc_roots);
2143 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002144 }
2145 }
2146 spin_unlock(&root->delalloc_lock);
2147}
2148
Nikolay Borisov2b877332018-04-27 12:21:51 +03002149
2150void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2151 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002152{
David Sterba3ffbd682018-06-29 10:56:42 +02002153 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002154
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002155 if (!list_empty(&inode->delalloc_inodes)) {
2156 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002157 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002158 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002159 root->nr_delalloc_inodes--;
2160 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002161 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002162 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002163 BUG_ON(list_empty(&root->delalloc_root));
2164 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002165 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002166 }
2167 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002168}
2169
2170static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2171 struct btrfs_inode *inode)
2172{
2173 spin_lock(&root->delalloc_lock);
2174 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002175 spin_unlock(&root->delalloc_lock);
2176}
2177
Chris Masond352ac62008-09-29 15:18:18 -04002178/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002179 * Properly track delayed allocation bytes in the inode and to maintain the
2180 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002181 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002182void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2183 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002185 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2186
Wang Shilong47059d92014-07-03 18:22:07 +08002187 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2188 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002189 /*
2190 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002191 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002192 * bit, which is only set or cleared with irqs on
2193 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002194 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002195 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002196 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002197 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002198 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002199
Josef Bacik8b62f872017-10-19 14:15:55 -04002200 spin_lock(&BTRFS_I(inode)->lock);
2201 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2202 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002203
Josef Bacik6a3891c2015-03-16 17:38:52 -04002204 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002206 return;
2207
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002208 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2209 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002210 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002211 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002212 if (*bits & EXTENT_DEFRAG)
2213 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002214 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002215 &BTRFS_I(inode)->runtime_flags))
2216 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002217 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002218 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002219
2220 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2221 (*bits & EXTENT_DELALLOC_NEW)) {
2222 spin_lock(&BTRFS_I(inode)->lock);
2223 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2224 state->start;
2225 spin_unlock(&BTRFS_I(inode)->lock);
2226 }
Chris Mason291d6732008-01-29 15:55:23 -05002227}
2228
Chris Masond352ac62008-09-29 15:18:18 -04002229/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002230 * Once a range is no longer delalloc this function ensures that proper
2231 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002232 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002233void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2234 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002235{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002236 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2237 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002238 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002239 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002240
Filipe Manana4a4b9642017-07-27 19:52:55 +01002241 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2242 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002243 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002244 spin_unlock(&inode->lock);
2245 }
Wang Shilong47059d92014-07-03 18:22:07 +08002246
Chris Mason75eff682008-12-15 15:54:40 -05002247 /*
2248 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002249 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002250 * bit, which is only set or cleared with irqs on
2251 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002252 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002253 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002254 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002255
Josef Bacik8b62f872017-10-19 14:15:55 -04002256 spin_lock(&inode->lock);
2257 btrfs_mod_outstanding_extents(inode, -num_extents);
2258 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002259
Josef Bacikb6d08f02013-09-27 14:57:43 -04002260 /*
2261 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002262 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002263 * error.
2264 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002265 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002266 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002267 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002268
Josef Bacik6a3891c2015-03-16 17:38:52 -04002269 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002270 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002271 return;
2272
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002273 if (!btrfs_is_data_reloc_root(root) &&
Filipe Mananaa315e682017-03-06 23:04:20 +00002274 do_list && !(state->state & EXTENT_NORESERVE) &&
2275 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002276 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002277
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002278 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2279 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002280 spin_lock(&inode->lock);
2281 inode->delalloc_bytes -= len;
2282 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002283 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002284 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002285 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002286 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002287 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002288
2289 if ((state->state & EXTENT_DELALLOC_NEW) &&
2290 (*bits & EXTENT_DELALLOC_NEW)) {
2291 spin_lock(&inode->lock);
2292 ASSERT(inode->new_delalloc_bytes >= len);
2293 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002294 if (*bits & EXTENT_ADD_INODE_BYTES)
2295 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002296 spin_unlock(&inode->lock);
2297 }
Chris Mason291d6732008-01-29 15:55:23 -05002298}
2299
Chris Masond352ac62008-09-29 15:18:18 -04002300/*
Chris Masond352ac62008-09-29 15:18:18 -04002301 * in order to insert checksums into the metadata in large chunks,
2302 * we wait until bio submission time. All the pages in the bio are
2303 * checksummed and sums are attached onto the ordered extent record.
2304 *
2305 * At IO completion time the cums attached on the ordered extent record
2306 * are inserted into the btree
2307 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002308static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002309 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002310{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002311 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002312}
Chris Masone0156402008-04-16 11:15:20 -04002313
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002314/*
2315 * Split an extent_map at [start, start + len]
2316 *
2317 * This function is intended to be used only for extract_ordered_extent().
2318 */
2319static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2320 u64 pre, u64 post)
2321{
2322 struct extent_map_tree *em_tree = &inode->extent_tree;
2323 struct extent_map *em;
2324 struct extent_map *split_pre = NULL;
2325 struct extent_map *split_mid = NULL;
2326 struct extent_map *split_post = NULL;
2327 int ret = 0;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002328 unsigned long flags;
2329
2330 /* Sanity check */
2331 if (pre == 0 && post == 0)
2332 return 0;
2333
2334 split_pre = alloc_extent_map();
2335 if (pre)
2336 split_mid = alloc_extent_map();
2337 if (post)
2338 split_post = alloc_extent_map();
2339 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2340 ret = -ENOMEM;
2341 goto out;
2342 }
2343
2344 ASSERT(pre + post < len);
2345
2346 lock_extent(&inode->io_tree, start, start + len - 1);
2347 write_lock(&em_tree->lock);
2348 em = lookup_extent_mapping(em_tree, start, len);
2349 if (!em) {
2350 ret = -EIO;
2351 goto out_unlock;
2352 }
2353
2354 ASSERT(em->len == len);
2355 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2356 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
Naohiro Aota63fb5872021-08-09 09:29:18 +09002357 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2358 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2359 ASSERT(!list_empty(&em->list));
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002360
2361 flags = em->flags;
2362 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002363
2364 /* First, replace the em with a new extent_map starting from * em->start */
2365 split_pre->start = em->start;
2366 split_pre->len = (pre ? pre : em->len - post);
2367 split_pre->orig_start = split_pre->start;
2368 split_pre->block_start = em->block_start;
2369 split_pre->block_len = split_pre->len;
2370 split_pre->orig_block_len = split_pre->block_len;
2371 split_pre->ram_bytes = split_pre->len;
2372 split_pre->flags = flags;
2373 split_pre->compress_type = em->compress_type;
2374 split_pre->generation = em->generation;
2375
Naohiro Aota63fb5872021-08-09 09:29:18 +09002376 replace_extent_mapping(em_tree, em, split_pre, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002377
2378 /*
2379 * Now we only have an extent_map at:
2380 * [em->start, em->start + pre] if pre != 0
2381 * [em->start, em->start + em->len - post] if pre == 0
2382 */
2383
2384 if (pre) {
2385 /* Insert the middle extent_map */
2386 split_mid->start = em->start + pre;
2387 split_mid->len = em->len - pre - post;
2388 split_mid->orig_start = split_mid->start;
2389 split_mid->block_start = em->block_start + pre;
2390 split_mid->block_len = split_mid->len;
2391 split_mid->orig_block_len = split_mid->block_len;
2392 split_mid->ram_bytes = split_mid->len;
2393 split_mid->flags = flags;
2394 split_mid->compress_type = em->compress_type;
2395 split_mid->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002396 add_extent_mapping(em_tree, split_mid, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002397 }
2398
2399 if (post) {
2400 split_post->start = em->start + em->len - post;
2401 split_post->len = post;
2402 split_post->orig_start = split_post->start;
2403 split_post->block_start = em->block_start + em->len - post;
2404 split_post->block_len = split_post->len;
2405 split_post->orig_block_len = split_post->block_len;
2406 split_post->ram_bytes = split_post->len;
2407 split_post->flags = flags;
2408 split_post->compress_type = em->compress_type;
2409 split_post->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002410 add_extent_mapping(em_tree, split_post, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002411 }
2412
2413 /* Once for us */
2414 free_extent_map(em);
2415 /* Once for the tree */
2416 free_extent_map(em);
2417
2418out_unlock:
2419 write_unlock(&em_tree->lock);
2420 unlock_extent(&inode->io_tree, start, start + len - 1);
2421out:
2422 free_extent_map(split_pre);
2423 free_extent_map(split_mid);
2424 free_extent_map(split_post);
2425
2426 return ret;
2427}
2428
Naohiro Aotad22002f2021-02-04 19:22:00 +09002429static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2430 struct bio *bio, loff_t file_offset)
2431{
2432 struct btrfs_ordered_extent *ordered;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002433 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002434 u64 file_len;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002435 u64 len = bio->bi_iter.bi_size;
2436 u64 end = start + len;
2437 u64 ordered_end;
2438 u64 pre, post;
2439 int ret = 0;
2440
2441 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2442 if (WARN_ON_ONCE(!ordered))
2443 return BLK_STS_IOERR;
2444
2445 /* No need to split */
2446 if (ordered->disk_num_bytes == len)
2447 goto out;
2448
2449 /* We cannot split once end_bio'd ordered extent */
2450 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2451 ret = -EINVAL;
2452 goto out;
2453 }
2454
2455 /* We cannot split a compressed ordered extent */
2456 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2457 ret = -EINVAL;
2458 goto out;
2459 }
2460
2461 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2462 /* bio must be in one ordered extent */
2463 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2464 ret = -EINVAL;
2465 goto out;
2466 }
2467
2468 /* Checksum list should be empty */
2469 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2470 ret = -EINVAL;
2471 goto out;
2472 }
2473
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002474 file_len = ordered->num_bytes;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002475 pre = start - ordered->disk_bytenr;
2476 post = ordered_end - end;
2477
2478 ret = btrfs_split_ordered_extent(ordered, pre, post);
2479 if (ret)
2480 goto out;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002481 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
Naohiro Aotad22002f2021-02-04 19:22:00 +09002482
2483out:
Naohiro Aotad22002f2021-02-04 19:22:00 +09002484 btrfs_put_ordered_extent(ordered);
2485
2486 return errno_to_blk_status(ret);
2487}
2488
Chris Mason4a69a412008-11-06 22:03:00 -05002489/*
Chris Masoncad321a2008-12-17 14:51:42 -05002490 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002491 * on write, or reading the csums from the tree before a read.
2492 *
2493 * Rules about async/sync submit,
2494 * a) read: sync submit
2495 *
2496 * b) write without checksum: sync submit
2497 *
2498 * c) write with checksum:
2499 * c-1) if bio is issued by fsync: sync submit
2500 * (sync_writers != 0)
2501 *
2502 * c-2) if root is reloc root: sync submit
2503 * (only in case of buffered IO)
2504 *
2505 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002506 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002507blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2508 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002509
Chris Mason44b8bd72008-04-16 11:14:51 -04002510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002511 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002512 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302513 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002514 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002515 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002516 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002517
Josef Bacik42437a62020-10-16 11:29:18 -04002518 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2519 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002520
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002521 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302522 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002523
Naohiro Aotad22002f2021-02-04 19:22:00 +09002524 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2525 struct page *page = bio_first_bvec_all(bio)->bv_page;
2526 loff_t file_offset = page_offset(page);
2527
2528 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2529 if (ret)
2530 goto out;
2531 }
2532
Naohiro Aotacfe94442021-02-04 19:21:59 +09002533 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002534 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002535 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002536 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002537
Chris Masond20f7042008-12-08 16:58:54 -05002538 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002539 ret = btrfs_submit_compressed_read(inode, bio,
2540 mirror_num,
2541 bio_flags);
2542 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002543 } else {
2544 /*
2545 * Lookup bio sums does extra checks around whether we
2546 * need to csum or not, which is why we ignore skip_sum
2547 * here.
2548 */
Qu Wenruo62751932020-12-02 14:48:06 +08002549 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002550 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002551 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002552 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002553 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002554 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002555 /* csum items have already been cloned */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002556 if (btrfs_is_data_reloc_root(root))
Yan Zheng17d217f2008-12-12 10:03:38 -05002557 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002558 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002559 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2560 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002561 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002562 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002563 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002564 if (ret)
2565 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002566 }
2567
Chris Mason0b86a832008-03-24 15:01:56 -04002568mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002569 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002570
2571out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002572 if (ret) {
2573 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002574 bio_endio(bio);
2575 }
Stefan Behrens61891922012-11-05 18:51:52 +01002576 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002577}
Chris Mason6885f302008-02-20 16:11:05 -05002578
Chris Masond352ac62008-09-29 15:18:18 -04002579/*
2580 * given a list of ordered sums record them in the inode. This happens
2581 * at IO completion time based on sums calculated at bio submission time.
2582 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002583static int add_pending_csums(struct btrfs_trans_handle *trans,
2584 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002587 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002589 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002590 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002591 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002592 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002593 if (ret)
2594 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002595 }
2596 return 0;
2597}
2598
Filipe Mananac3347302020-11-04 11:07:31 +00002599static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2600 const u64 start,
2601 const u64 len,
2602 struct extent_state **cached_state)
2603{
2604 u64 search_start = start;
2605 const u64 end = start + len - 1;
2606
2607 while (search_start < end) {
2608 const u64 search_len = end - search_start + 1;
2609 struct extent_map *em;
2610 u64 em_len;
2611 int ret = 0;
2612
2613 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2614 if (IS_ERR(em))
2615 return PTR_ERR(em);
2616
2617 if (em->block_start != EXTENT_MAP_HOLE)
2618 goto next;
2619
2620 em_len = em->len;
2621 if (em->start < search_start)
2622 em_len -= search_start - em->start;
2623 if (em_len > search_len)
2624 em_len = search_len;
2625
2626 ret = set_extent_bit(&inode->io_tree, search_start,
2627 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002628 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2629 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002630next:
2631 search_start = extent_map_end(em);
2632 free_extent_map(em);
2633 if (ret)
2634 return ret;
2635 }
2636 return 0;
2637}
2638
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002639int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002640 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002641 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002642{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002643 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002644
2645 if (start >= i_size_read(&inode->vfs_inode) &&
2646 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2647 /*
2648 * There can't be any extents following eof in this case so just
2649 * set the delalloc new bit for the range directly.
2650 */
2651 extra_bits |= EXTENT_DELALLOC_NEW;
2652 } else {
2653 int ret;
2654
2655 ret = btrfs_find_new_delalloc_bytes(inode, start,
2656 end + 1 - start,
2657 cached_state);
2658 if (ret)
2659 return ret;
2660 }
2661
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002662 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2663 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002664}
2665
Chris Masond352ac62008-09-29 15:18:18 -04002666/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002667struct btrfs_writepage_fixup {
2668 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002669 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002670 struct btrfs_work work;
2671};
2672
Christoph Hellwigb2950862008-12-02 09:54:17 -05002673static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002674{
2675 struct btrfs_writepage_fixup *fixup;
2676 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002677 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002678 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002679 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002680 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002681 u64 page_start;
2682 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002683 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002684 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002685
2686 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2687 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002688 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002689 page_start = page_offset(page);
2690 page_end = page_offset(page) + PAGE_SIZE - 1;
2691
2692 /*
2693 * This is similar to page_mkwrite, we need to reserve the space before
2694 * we take the page lock.
2695 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002696 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2697 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002698again:
Chris Mason247e7432008-07-17 12:53:51 -04002699 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002700
Chris Mason25f3c502020-01-21 11:51:42 -05002701 /*
2702 * Before we queued this fixup, we took a reference on the page.
2703 * page->mapping may go NULL, but it shouldn't be moved to a different
2704 * address space.
2705 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002706 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2707 /*
2708 * Unfortunately this is a little tricky, either
2709 *
2710 * 1) We got here and our page had already been dealt with and
2711 * we reserved our space, thus ret == 0, so we need to just
2712 * drop our space reservation and bail. This can happen the
2713 * first time we come into the fixup worker, or could happen
2714 * while waiting for the ordered extent.
2715 * 2) Our page was already dealt with, but we happened to get an
2716 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2717 * this case we obviously don't have anything to release, but
2718 * because the page was already dealt with we don't want to
2719 * mark the page with an error, so make sure we're resetting
2720 * ret to 0. This is why we have this check _before_ the ret
2721 * check, because we do not want to have a surprise ENOSPC
2722 * when the page was already properly dealt with.
2723 */
2724 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002725 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2726 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002727 page_start, PAGE_SIZE,
2728 true);
2729 }
2730 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002731 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002732 }
Chris Mason25f3c502020-01-21 11:51:42 -05002733
2734 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002735 * We can't mess with the page state unless it is locked, so now that
2736 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002737 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002738 if (ret)
2739 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002740
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002741 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002742
2743 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002744 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002745 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002746
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002747 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002748 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002749 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2750 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002751 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002752 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002753 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002754 goto again;
2755 }
Chris Mason247e7432008-07-17 12:53:51 -04002756
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002757 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002758 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002759 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002760 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002761
Chris Mason25f3c502020-01-21 11:51:42 -05002762 /*
2763 * Everything went as planned, we're now the owner of a dirty page with
2764 * delayed allocation bits set and space reserved for our COW
2765 * destination.
2766 *
2767 * The page was dirty when we started, nothing should have cleaned it.
2768 */
2769 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002770 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002771out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002772 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002773 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002774 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2775 PAGE_SIZE, true);
2776 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002777 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002778out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002779 if (ret) {
2780 /*
2781 * We hit ENOSPC or other errors. Update the mapping and page
2782 * to reflect the errors and clean the page.
2783 */
2784 mapping_set_error(page->mapping, ret);
2785 end_extent_writepage(page, ret, page_start, page_end);
2786 clear_page_dirty_for_io(page);
2787 SetPageError(page);
2788 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08002789 btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002790 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002791 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002792 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002793 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002794 /*
2795 * As a precaution, do a delayed iput in case it would be the last iput
2796 * that could need flushing space. Recursing back to fixup worker would
2797 * deadlock.
2798 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002799 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002800}
2801
2802/*
2803 * There are a few paths in the higher layers of the kernel that directly
2804 * set the page dirty bit without asking the filesystem if it is a
2805 * good idea. This causes problems because we want to make sure COW
2806 * properly happens and the data=ordered rules are followed.
2807 *
Chris Masonc8b97812008-10-29 14:49:59 -04002808 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002809 * hasn't been properly setup for IO. We kick off an async process
2810 * to fix it up. The async helper will wait for ordered extents, set
2811 * the delalloc bit and make it safe to write the page.
2812 */
Qu Wenruoa129ffb2021-07-27 13:41:32 +08002813int btrfs_writepage_cow_fixup(struct page *page)
Chris Mason247e7432008-07-17 12:53:51 -04002814{
2815 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002817 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002818
Qu Wenruof57ad932021-04-07 19:22:13 +08002819 /* This page has ordered extent covering it already */
2820 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002821 return 0;
2822
Chris Mason25f3c502020-01-21 11:51:42 -05002823 /*
2824 * PageChecked is set below when we create a fixup worker for this page,
2825 * don't try to create another one if we're already PageChecked()
2826 *
2827 * The extent_io writepage code will redirty the page if we send back
2828 * EAGAIN.
2829 */
Chris Mason247e7432008-07-17 12:53:51 -04002830 if (PageChecked(page))
2831 return -EAGAIN;
2832
2833 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2834 if (!fixup)
2835 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002836
Josef Bacikf4b13632020-01-21 14:34:52 -05002837 /*
2838 * We are already holding a reference to this inode from
2839 * write_cache_pages. We need to hold it because the space reservation
2840 * takes place outside of the page lock, and we can't trust
2841 * page->mapping outside of the page lock.
2842 */
2843 ihold(inode);
Qu Wenruoe4f94342021-09-27 15:21:49 +08002844 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002845 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002846 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002847 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002848 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002849 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002850
2851 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002852}
2853
Yan Zhengd899e052008-10-30 14:25:28 -04002854static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002855 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002856 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002857 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002858 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002859{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002860 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002861 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002862 struct btrfs_path *path;
2863 struct extent_buffer *leaf;
2864 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002865 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2866 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2867 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2868 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002869 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002870 int ret;
2871
2872 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002873 if (!path)
2874 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002875
Chris Masona1ed8352009-09-11 12:27:37 -04002876 /*
2877 * we may be replacing one extent in the tree with another.
2878 * The new extent is pinned in the extent map, and we don't want
2879 * to drop it from the cache until it is completely in the btree.
2880 *
2881 * So, tell btrfs_drop_extents to leave this extent in the cache.
2882 * the caller is expected to unpin it and allow it to be merged
2883 * with the others.
2884 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002885 drop_args.path = path;
2886 drop_args.start = file_pos;
2887 drop_args.end = file_pos + num_bytes;
2888 drop_args.replace_extent = true;
2889 drop_args.extent_item_size = sizeof(*stack_fi);
2890 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 if (ret)
2892 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002893
Filipe Manana5893dfb2020-11-04 11:07:32 +00002894 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002895 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002896 ins.offset = file_pos;
2897 ins.type = BTRFS_EXTENT_DATA_KEY;
2898
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002899 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002900 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002901 if (ret)
2902 goto out;
2903 }
Yan Zhengd899e052008-10-30 14:25:28 -04002904 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002905 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2906 write_extent_buffer(leaf, stack_fi,
2907 btrfs_item_ptr_offset(leaf, path->slots[0]),
2908 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002909
Yan Zhengd899e052008-10-30 14:25:28 -04002910 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002911 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002912
Filipe Manana2766ff62020-11-04 11:07:34 +00002913 /*
2914 * If we dropped an inline extent here, we know the range where it is
2915 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
David Sterba1a9fd412021-05-21 17:42:23 +02002916 * number of bytes only for that range containing the inline extent.
Filipe Manana2766ff62020-11-04 11:07:34 +00002917 * The remaining of the range will be processed when clearning the
2918 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2919 */
2920 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2921 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2922
2923 inline_size = drop_args.bytes_found - inline_size;
2924 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2925 drop_args.bytes_found -= inline_size;
2926 num_bytes -= sectorsize;
2927 }
2928
2929 if (update_inode_bytes)
2930 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002931
2932 ins.objectid = disk_bytenr;
2933 ins.offset = disk_num_bytes;
2934 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002935
Nikolay Borisovc553f942020-06-03 08:55:19 +03002936 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002937 if (ret)
2938 goto out;
2939
Nikolay Borisovc553f942020-06-03 08:55:19 +03002940 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002941 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002942out:
Yan Zhengd899e052008-10-30 14:25:28 -04002943 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002944
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002945 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002946}
2947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002948static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002949 u64 start, u64 len)
2950{
David Sterba32da53862019-10-29 19:20:18 +01002951 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002952
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002953 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002954 ASSERT(cache);
2955
2956 spin_lock(&cache->lock);
2957 cache->delalloc_bytes -= len;
2958 spin_unlock(&cache->lock);
2959
2960 btrfs_put_block_group(cache);
2961}
2962
Qu Wenruo203f44c52020-06-10 09:04:40 +08002963static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002964 struct btrfs_ordered_extent *oe)
2965{
2966 struct btrfs_file_extent_item stack_fi;
2967 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002968 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002969
2970 memset(&stack_fi, 0, sizeof(stack_fi));
2971 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2972 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2973 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2974 oe->disk_num_bytes);
2975 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2976 logical_len = oe->truncated_len;
2977 else
2978 logical_len = oe->num_bytes;
2979 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2980 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2981 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2982 /* Encryption and other encoding is reserved and all 0 */
2983
Filipe Manana2766ff62020-11-04 11:07:34 +00002984 /*
2985 * For delalloc, when completing an ordered extent we update the inode's
2986 * bytes when clearing the range in the inode's io tree, so pass false
2987 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2988 * except if the ordered extent was truncated.
2989 */
2990 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2991 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2992
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002993 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2994 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002995 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002996}
2997
2998/*
2999 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04003000 * an ordered extent if the range of bytes in the file it covers are
3001 * fully written.
3002 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003003static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003004{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003005 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3006 struct btrfs_root *root = inode->root;
3007 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003008 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003009 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003010 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003011 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08003012 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003013 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003014 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03003015 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003016 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003017 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00003018 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003019
Omar Sandovalbffe6332019-12-02 17:34:19 -08003020 start = ordered_extent->file_offset;
3021 end = start + ordered_extent->num_bytes - 1;
3022
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003023 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3024 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3025 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00003026 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003028 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003029
Josef Bacik5fd02042012-05-02 14:00:54 -04003030 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3031 ret = -EIO;
3032 goto out;
3033 }
3034
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003035 /* A valid bdev implies a write on a sequential zone */
3036 if (ordered_extent->bdev) {
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003037 btrfs_rewrite_logical_zoned(ordered_extent);
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003038 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3039 ordered_extent->disk_num_bytes);
3040 }
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003041
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003042 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08003043
Josef Bacik77cef2e2013-08-29 13:57:21 -04003044 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3045 truncated = true;
3046 logical_len = ordered_extent->truncated_len;
3047 /* Truncated the entire extent, don't bother adding */
3048 if (!logical_len)
3049 goto out;
3050 }
3051
Yan, Zhengc2167752009-11-12 09:34:21 +00003052 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003054
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003055 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003056 if (freespace_inode)
3057 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003058 else
3059 trans = btrfs_join_transaction(root);
3060 if (IS_ERR(trans)) {
3061 ret = PTR_ERR(trans);
3062 trans = NULL;
3063 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003064 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003065 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02003066 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003067 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003068 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003069 goto out;
3070 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003071
Filipe Manana2766ff62020-11-04 11:07:34 +00003072 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003073 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003074
Nikolay Borisov8d510122019-10-08 20:43:06 +03003075 if (freespace_inode)
3076 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003077 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003078 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003079 if (IS_ERR(trans)) {
3080 ret = PTR_ERR(trans);
3081 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003082 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083 }
Chris Masona79b7d42014-05-22 16:18:52 -07003084
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003085 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003086
Chris Masonc8b97812008-10-29 14:49:59 -04003087 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003088 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003089 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003090 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003091 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04003092 ordered_extent->file_offset,
3093 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003094 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003095 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003096 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03003097 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04003098 if (!ret) {
3099 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003100 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003101 ordered_extent->disk_bytenr,
3102 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04003103 }
Yan Zhengd899e052008-10-30 14:25:28 -04003104 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003105 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003106 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003107 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003108 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003109 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003110 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003111
Nikolay Borisov510f85e2020-09-18 12:15:52 +03003112 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02003113 if (ret) {
3114 btrfs_abort_transaction(trans, ret);
3115 goto out;
3116 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003117
Filipe Manana2766ff62020-11-04 11:07:34 +00003118 /*
3119 * If this is a new delalloc range, clear its new delalloc flag to
3120 * update the inode's number of bytes. This needs to be done first
3121 * before updating the inode item.
3122 */
3123 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3124 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003125 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00003126 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3127 0, 0, &cached_state);
3128
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003129 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003130 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003131 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003132 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003133 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003134 }
3135 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003136out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003137 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003138 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003139 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003140
Miao Xiea698d0752012-09-20 01:51:59 -06003141 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003142 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003143
Josef Bacik77cef2e2013-08-29 13:57:21 -04003144 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003145 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003146
Josef Bacikd61bec02021-05-19 09:38:27 -04003147 /*
3148 * If we failed to finish this ordered extent for any reason we
3149 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3150 * extent, and mark the inode with the error if it wasn't
3151 * already set. Any error during writeback would have already
3152 * set the mapping error, so we need to set it if we're the ones
3153 * marking this ordered extent as failed.
3154 */
3155 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3156 &ordered_extent->flags))
3157 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3158
Josef Bacik77cef2e2013-08-29 13:57:21 -04003159 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003160 unwritten_start += logical_len;
3161 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003162
3163 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003164 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003165
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003166 /*
3167 * If the ordered extent had an IOERR or something else went
3168 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003169 * back to the allocator. We only free the extent in the
3170 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003171 *
3172 * If we made it past insert_reserved_file_extent before we
3173 * errored out then we don't need to do this as the accounting
3174 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003175 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003176 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003177 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003178 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003179 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3180 /*
3181 * Discard the range before returning it back to the
3182 * free space pool
3183 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003184 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003185 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003186 ordered_extent->disk_bytenr,
3187 ordered_extent->disk_num_bytes,
3188 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003189 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003190 ordered_extent->disk_bytenr,
3191 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003192 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003193 }
3194
Josef Bacik5fd02042012-05-02 14:00:54 -04003195 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003196 * This needs to be done to make sure anybody waiting knows we are done
3197 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003198 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003199 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003200
Chris Masone6dcd2d2008-07-17 12:53:50 -04003201 /* once for us */
3202 btrfs_put_ordered_extent(ordered_extent);
3203 /* once for the tree */
3204 btrfs_put_ordered_extent(ordered_extent);
3205
Josef Bacik5fd02042012-05-02 14:00:54 -04003206 return ret;
3207}
3208
3209static void finish_ordered_fn(struct btrfs_work *work)
3210{
3211 struct btrfs_ordered_extent *ordered_extent;
3212 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3213 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003214}
3215
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003216void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3217 struct page *page, u64 start,
David Sterba25c12522021-07-26 14:15:08 +02003218 u64 end, bool uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003219{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003220 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003221
Qu Wenruoe65f1522021-04-01 15:15:06 +08003222 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3223 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003224}
3225
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003226/*
3227 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003228 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003229 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003230 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003231 * @page: page where is the data to be verified
3232 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003233 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003234 *
3235 * The length of such check is always one sector size.
3236 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003237static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003238 u32 bio_offset, struct page *page, u32 pgoff,
3239 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003240{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3242 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003243 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003244 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003245 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003246 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003247 u8 *csum_expected;
3248 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003249
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003250 ASSERT(pgoff + len <= PAGE_SIZE);
3251
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003252 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003253 csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003254
3255 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003256 shash->tfm = fs_info->csum_shash;
3257
Eric Biggersfd080012020-04-30 23:51:59 -07003258 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003259
3260 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003261 goto zeroit;
3262
3263 kunmap_atomic(kaddr);
3264 return 0;
3265zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003266 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003267 bbio->mirror_num);
3268 if (bbio->device)
3269 btrfs_dev_stat_inc_and_print(bbio->device,
Nikolay Borisov814723e2020-07-02 15:23:32 +03003270 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003271 memset(kaddr + pgoff, 1, len);
3272 flush_dcache_page(page);
3273 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003274 return -EIO;
3275}
3276
Chris Masond352ac62008-09-29 15:18:18 -04003277/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003278 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003279 * if there's a match, we allow the bio to finish. If not, the code in
3280 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003281 *
3282 * @bio_offset: offset to the beginning of the bio (in bytes)
3283 * @start: file offset of the range start
3284 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003285 *
3286 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3287 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003288 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003289unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio,
3290 u32 bio_offset, struct page *page,
3291 u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003292{
Chris Mason07157aa2007-08-30 08:50:51 -04003293 struct inode *inode = page->mapping->host;
Qu Wenruoe4f94342021-09-27 15:21:49 +08003294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masond1310b22008-01-24 16:13:08 -05003295 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003296 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003297 const u32 sectorsize = root->fs_info->sectorsize;
3298 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003299 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003300
Qu Wenruoe4f94342021-09-27 15:21:49 +08003301 if (btrfs_page_test_checked(fs_info, page, start, end + 1 - start)) {
3302 btrfs_page_clear_checked(fs_info, page, start, end + 1 - start);
Miao Xiedc380ae2014-09-12 18:43:55 +08003303 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003304 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003305
Qu Wenruo3670e642021-07-26 14:34:51 +08003306 /*
Qu Wenruoe4f94342021-09-27 15:21:49 +08003307 * This only happens for NODATASUM or compressed read.
3308 * Normally this should be covered by above check for compressed read
3309 * or the next check for NODATASUM. Just do a quicker exit here.
Qu Wenruo3670e642021-07-26 14:34:51 +08003310 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003311 if (bbio->csum == NULL)
Qu Wenruo3670e642021-07-26 14:34:51 +08003312 return 0;
3313
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003314 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003315 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003316
Josef Bacik42437a62020-10-16 11:29:18 -04003317 if (!root->fs_info->csum_root)
3318 return 0;
3319
Qu Wenruof44cf412020-12-02 14:47:59 +08003320 ASSERT(page_offset(page) <= start &&
3321 end <= page_offset(page) + PAGE_SIZE - 1);
3322 for (pg_off = offset_in_page(start);
3323 pg_off < offset_in_page(end);
3324 pg_off += sectorsize, bio_offset += sectorsize) {
Qu Wenruoe3c62322021-07-26 14:35:04 +08003325 u64 file_offset = pg_off + page_offset(page);
Qu Wenruof44cf412020-12-02 14:47:59 +08003326 int ret;
3327
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09003328 if (btrfs_is_data_reloc_root(root) &&
Qu Wenruoe3c62322021-07-26 14:35:04 +08003329 test_range_bit(io_tree, file_offset,
3330 file_offset + sectorsize - 1,
3331 EXTENT_NODATASUM, 1, NULL)) {
3332 /* Skip the range without csum for data reloc inode */
3333 clear_extent_bits(io_tree, file_offset,
3334 file_offset + sectorsize - 1,
3335 EXTENT_NODATASUM);
3336 continue;
3337 }
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003338 ret = check_data_csum(inode, bbio, bio_offset, page, pg_off,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003339 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003340 if (ret < 0) {
3341 const int nr_bit = (pg_off - offset_in_page(start)) >>
3342 root->fs_info->sectorsize_bits;
3343
3344 result |= (1U << nr_bit);
3345 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003346 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003347 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003348}
Chris Masonb888db22007-08-27 16:49:44 -04003349
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003350/*
3351 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3352 *
3353 * @inode: The inode we want to perform iput on
3354 *
3355 * This function uses the generic vfs_inode::i_count to track whether we should
3356 * just decrement it (in case it's > 1) or if this is the last iput then link
3357 * the inode to the delayed iput machinery. Delayed iputs are processed at
3358 * transaction commit time/superblock commit/cleaner kthread.
3359 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003360void btrfs_add_delayed_iput(struct inode *inode)
3361{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003362 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003363 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003364
3365 if (atomic_add_unless(&inode->i_count, -1, 1))
3366 return;
3367
Josef Bacik034f7842018-12-03 11:06:52 -05003368 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003369 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003370 ASSERT(list_empty(&binode->delayed_iput));
3371 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003372 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003373 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3374 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003375}
3376
Josef Bacik63611e72019-06-18 10:59:18 -04003377static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3378 struct btrfs_inode *inode)
3379{
3380 list_del_init(&inode->delayed_iput);
3381 spin_unlock(&fs_info->delayed_iput_lock);
3382 iput(&inode->vfs_inode);
3383 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3384 wake_up(&fs_info->delayed_iputs_wait);
3385 spin_lock(&fs_info->delayed_iput_lock);
3386}
3387
3388static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3389 struct btrfs_inode *inode)
3390{
3391 if (!list_empty(&inode->delayed_iput)) {
3392 spin_lock(&fs_info->delayed_iput_lock);
3393 if (!list_empty(&inode->delayed_iput))
3394 run_delayed_iput_locked(fs_info, inode);
3395 spin_unlock(&fs_info->delayed_iput_lock);
3396 }
3397}
3398
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003399void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003400{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003401
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003402 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003403 while (!list_empty(&fs_info->delayed_iputs)) {
3404 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003405
David Sterba8089fe62015-11-19 14:15:51 +01003406 inode = list_first_entry(&fs_info->delayed_iputs,
3407 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003408 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003409 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003410 }
David Sterba8089fe62015-11-19 14:15:51 +01003411 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003412}
3413
Josef Bacik034f7842018-12-03 11:06:52 -05003414/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003415 * Wait for flushing all delayed iputs
3416 *
3417 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003418 *
3419 * This will wait on any delayed iputs that are currently running with KILLABLE
3420 * set. Once they are all done running we will return, unless we are killed in
3421 * which case we return EINTR. This helps in user operations like fallocate etc
3422 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003423 *
3424 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003425 */
3426int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3427{
3428 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3429 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3430 if (ret)
3431 return -EINTR;
3432 return 0;
3433}
3434
Yan, Zhengd68fc572010-05-16 10:49:58 -04003435/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003436 * This creates an orphan entry for the given inode in case something goes wrong
3437 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003438 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003439int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003440 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003441{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003442 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003443
Omar Sandoval27919062018-05-11 13:13:37 -07003444 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3445 if (ret && ret != -EEXIST) {
3446 btrfs_abort_transaction(trans, ret);
3447 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003448 }
3449
Yan, Zhengd68fc572010-05-16 10:49:58 -04003450 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003451}
3452
3453/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003454 * We have done the delete so we can go ahead and remove the orphan item for
3455 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003456 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003457static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003458 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003459{
Omar Sandoval27919062018-05-11 13:13:37 -07003460 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003461}
3462
3463/*
3464 * this cleans up any orphans that may be left on the list from the last use
3465 * of this root.
3466 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003467int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003469 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003470 struct btrfs_path *path;
3471 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003472 struct btrfs_key key, found_key;
3473 struct btrfs_trans_handle *trans;
3474 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003475 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003476 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003477
Josef Bacik54230012021-11-09 10:12:06 -05003478 if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003479 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003480
3481 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003482 if (!path) {
3483 ret = -ENOMEM;
3484 goto out;
3485 }
David Sterbae4058b52015-11-27 16:31:35 +01003486 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003489 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 key.offset = (u64)-1;
3491
Josef Bacik7b128762008-07-24 12:17:14 -04003492 while (1) {
3493 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003494 if (ret < 0)
3495 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003496
3497 /*
3498 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003499 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003500 * find the key and see if we have stuff that matches
3501 */
3502 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003503 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003504 if (path->slots[0] == 0)
3505 break;
3506 path->slots[0]--;
3507 }
3508
3509 /* pull out the item */
3510 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003511 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3512
3513 /* make sure the item matches what we want */
3514 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3515 break;
David Sterba962a2982014-06-04 18:41:45 +02003516 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003517 break;
3518
3519 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003520 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003521
3522 /*
3523 * this is where we are basically btrfs_lookup, without the
3524 * crossing root thing. we store the inode number in the
3525 * offset of the orphan item.
3526 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003527
3528 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003529 btrfs_err(fs_info,
3530 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003531 ret = -EINVAL;
3532 goto out;
3533 }
3534
3535 last_objectid = found_key.offset;
3536
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003537 found_key.objectid = found_key.offset;
3538 found_key.type = BTRFS_INODE_ITEM_KEY;
3539 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003540 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303541 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003542 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003543 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003544
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003545 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003546 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003547 int is_dead_root = 0;
3548
3549 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003550 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003551 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003552 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003553 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003554 * We need to distinguish those two, as the orphan item
3555 * for a root must not get deleted before the deletion
3556 * of the snapshot/subvolume's tree completes.
3557 *
3558 * btrfs_find_orphan_roots() ran before us, which has
3559 * found all deleted roots and loaded them into
3560 * fs_info->fs_roots_radix. So here we can find if an
3561 * orphan item corresponds to a deleted root by looking
3562 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003563 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003564
3565 spin_lock(&fs_info->fs_roots_radix_lock);
3566 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3567 (unsigned long)found_key.objectid);
3568 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3569 is_dead_root = 1;
3570 spin_unlock(&fs_info->fs_roots_radix_lock);
3571
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003572 if (is_dead_root) {
3573 /* prevent this orphan from being found again */
3574 key.offset = found_key.objectid - 1;
3575 continue;
3576 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003577
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003578 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003579
Josef Bacika8c9e572011-09-21 16:55:59 -04003580 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003581 * If we have an inode with links, there are a couple of
Boris Burkov70524252021-06-30 13:01:50 -07003582 * possibilities:
3583 *
3584 * 1. We were halfway through creating fsverity metadata for the
3585 * file. In that case, the orphan item represents incomplete
3586 * fsverity metadata which must be cleaned up with
3587 * btrfs_drop_verity_items and deleting the orphan item.
3588
3589 * 2. Old kernels (before v3.12) used to create an
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003590 * orphan item for truncate indicating that there were possibly
3591 * extent items past i_size that needed to be deleted. In v3.12,
3592 * truncate was changed to update i_size in sync with the extent
3593 * items, but the (useless) orphan item was still created. Since
3594 * v4.18, we don't create the orphan item for truncate at all.
3595 *
3596 * So, this item could mean that we need to do a truncate, but
3597 * only if this filesystem was last used on a pre-v3.12 kernel
3598 * and was not cleanly unmounted. The odds of that are quite
3599 * slim, and it's a pain to do the truncate now, so just delete
3600 * the orphan item.
3601 *
3602 * It's also possible that this orphan item was supposed to be
3603 * deleted but wasn't. The inode number may have been reused,
3604 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003605 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003606 if (ret == -ENOENT || inode->i_nlink) {
Boris Burkov70524252021-06-30 13:01:50 -07003607 if (!ret) {
3608 ret = btrfs_drop_verity_items(BTRFS_I(inode));
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003609 iput(inode);
Boris Burkov70524252021-06-30 13:01:50 -07003610 if (ret)
3611 goto out;
3612 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003613 trans = btrfs_start_transaction(root, 1);
3614 if (IS_ERR(trans)) {
3615 ret = PTR_ERR(trans);
3616 goto out;
3617 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 btrfs_debug(fs_info, "auto deleting %Lu",
3619 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003620 ret = btrfs_del_orphan_item(trans, root,
3621 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003622 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003623 if (ret)
3624 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003625 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003626 }
Josef Bacik7b128762008-07-24 12:17:14 -04003627
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003628 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003629
3630 /* this will do delete_inode and everything for us */
3631 iput(inode);
3632 }
Miao Xie3254c872011-11-10 20:45:05 -05003633 /* release the path since we're done with it */
3634 btrfs_release_path(path);
3635
Omar Sandovala575cee2018-05-11 13:13:38 -07003636 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003637 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003638 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003639 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003640 }
Josef Bacik7b128762008-07-24 12:17:14 -04003641
3642 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003643 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003644
3645out:
3646 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003647 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003648 btrfs_free_path(path);
3649 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003650}
3651
Chris Masond352ac62008-09-29 15:18:18 -04003652/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003653 * very simple check to peek ahead in the leaf looking for xattrs. If we
3654 * don't find any xattrs, we know there can't be any acls.
3655 *
3656 * slot is the slot the inode is in, objectid is the objectid of the inode
3657 */
3658static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003659 int slot, u64 objectid,
3660 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003661{
3662 u32 nritems = btrfs_header_nritems(leaf);
3663 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003664 static u64 xattr_access = 0;
3665 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003666 int scanned = 0;
3667
Josef Bacikf23b5a52013-06-19 10:16:26 -04003668 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003669 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3670 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3671 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3672 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003673 }
3674
Chris Mason46a53cc2009-04-27 11:47:50 -04003675 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003676 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003677 while (slot < nritems) {
3678 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3679
3680 /* we found a different objectid, there must not be acls */
3681 if (found_key.objectid != objectid)
3682 return 0;
3683
3684 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003685 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 if (*first_xattr_slot == -1)
3687 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003688 if (found_key.offset == xattr_access ||
3689 found_key.offset == xattr_default)
3690 return 1;
3691 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003692
3693 /*
3694 * we found a key greater than an xattr key, there can't
3695 * be any acls later on
3696 */
3697 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3698 return 0;
3699
3700 slot++;
3701 scanned++;
3702
3703 /*
3704 * it goes inode, inode backrefs, xattrs, extents,
3705 * so if there are a ton of hard links to an inode there can
3706 * be a lot of backrefs. Don't waste time searching too hard,
3707 * this is just an optimization
3708 */
3709 if (scanned >= 8)
3710 break;
3711 }
3712 /* we hit the end of the leaf before we found an xattr or
3713 * something larger than an xattr. We have to assume the inode
3714 * has acls
3715 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003716 if (*first_xattr_slot == -1)
3717 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003718 return 1;
3719}
3720
3721/*
Chris Masond352ac62008-09-29 15:18:18 -04003722 * read an inode from the btree into the in-memory inode
3723 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003724static int btrfs_read_locked_inode(struct inode *inode,
3725 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003728 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003729 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003730 struct btrfs_inode_item *inode_item;
3731 struct btrfs_root *root = BTRFS_I(inode)->root;
3732 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003733 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003734 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003735 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003737 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003738 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003739
3740 ret = btrfs_fill_inode(inode, &rdev);
3741 if (!ret)
3742 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003743
Filipe Manana4222ea72018-10-24 10:13:03 +01003744 if (!path) {
3745 path = btrfs_alloc_path();
3746 if (!path)
3747 return -ENOMEM;
3748 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003749
Chris Mason39279cc2007-06-12 06:35:45 -04003750 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003753 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003754 if (path != in_path)
3755 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003756 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003757 }
Chris Mason39279cc2007-06-12 06:35:45 -04003758
Chris Mason5f39d392007-10-15 16:14:19 -04003759 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003760
3761 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003762 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003763
Chris Mason5f39d392007-10-15 16:14:19 -04003764 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3765 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003766 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003767 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003768 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3769 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003770 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003771 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3772 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003773
David Sterbaa937b972014-12-12 17:39:12 +01003774 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3775 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
David Sterbaa937b972014-12-12 17:39:12 +01003777 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3778 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003779
David Sterbaa937b972014-12-12 17:39:12 +01003780 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3781 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
chandan r9cc97d62012-07-04 12:48:07 +05303783 BTRFS_I(inode)->i_otime.tv_sec =
3784 btrfs_timespec_sec(leaf, &inode_item->otime);
3785 BTRFS_I(inode)->i_otime.tv_nsec =
3786 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003787
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003788 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003789 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003790 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3791
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003792 inode_set_iversion_queried(inode,
3793 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003794 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003795 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003796 rdev = btrfs_inode_rdev(leaf, inode_item);
3797
Josef Bacikaec74772008-07-24 12:12:38 -04003798 BTRFS_I(inode)->index_cnt = (u64)-1;
Boris Burkov77eea052021-06-30 13:01:48 -07003799 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3800 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
Miao Xie67de1172013-12-26 13:07:06 +08003801
3802cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003803 /*
3804 * If we were modified in the current generation and evicted from memory
3805 * and then re-read we need to do a full sync since we don't have any
3806 * idea about which extents were modified before we were evicted from
3807 * cache.
3808 *
3809 * This is required for both inode re-read from disk and delayed inode
3810 * in delayed_nodes_tree.
3811 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003812 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003813 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3814 &BTRFS_I(inode)->runtime_flags);
3815
Filipe Mananabde6c242015-07-24 00:00:19 +01003816 /*
3817 * We don't persist the id of the transaction where an unlink operation
3818 * against the inode was last made. So here we assume the inode might
3819 * have been evicted, and therefore the exact value of last_unlink_trans
3820 * lost, and set it to last_trans to avoid metadata inconsistencies
3821 * between the inode and its parent if the inode is fsync'ed and the log
3822 * replayed. For example, in the scenario:
3823 *
3824 * touch mydir/foo
3825 * ln mydir/foo mydir/bar
3826 * sync
3827 * unlink mydir/bar
3828 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3829 * xfs_io -c fsync mydir/foo
3830 * <power failure>
3831 * mount fs, triggers fsync log replay
3832 *
3833 * We must make sure that when we fsync our inode foo we also log its
3834 * parent inode, otherwise after log replay the parent still has the
3835 * dentry with the "bar" name but our inode foo has a link count of 1
3836 * and doesn't have an inode ref with the name "bar" anymore.
3837 *
3838 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003839 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003840 * transaction commits on fsync if our inode is a directory, or if our
3841 * inode is not a directory, logging its parent unnecessarily.
3842 */
3843 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3844
Filipe Manana3ebac172020-07-15 12:30:43 +01003845 /*
3846 * Same logic as for last_unlink_trans. We don't persist the generation
3847 * of the last transaction where this inode was used for a reflink
3848 * operation, so after eviction and reloading the inode we must be
3849 * pessimistic and assume the last transaction that modified the inode.
3850 */
3851 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3852
Miao Xie67de1172013-12-26 13:07:06 +08003853 path->slots[0]++;
3854 if (inode->i_nlink != 1 ||
3855 path->slots[0] >= btrfs_header_nritems(leaf))
3856 goto cache_acl;
3857
3858 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003859 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003860 goto cache_acl;
3861
3862 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3863 if (location.type == BTRFS_INODE_REF_KEY) {
3864 struct btrfs_inode_ref *ref;
3865
3866 ref = (struct btrfs_inode_ref *)ptr;
3867 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3868 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3869 struct btrfs_inode_extref *extref;
3870
3871 extref = (struct btrfs_inode_extref *)ptr;
3872 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3873 extref);
3874 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003875cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003876 /*
3877 * try to precache a NULL acl entry for files that don't have
3878 * any xattrs or acls
3879 */
Li Zefan33345d012011-04-20 10:31:50 +08003880 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003881 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003882 if (first_xattr_slot != -1) {
3883 path->slots[0] = first_xattr_slot;
3884 ret = btrfs_load_inode_props(inode, path);
3885 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003887 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003888 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003889 root->root_key.objectid, ret);
3890 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003891 if (path != in_path)
3892 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003893
Al Viro72c04902009-06-24 16:58:48 -04003894 if (!maybe_acls)
3895 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003896
Chris Mason39279cc2007-06-12 06:35:45 -04003897 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003898 case S_IFREG:
3899 inode->i_mapping->a_ops = &btrfs_aops;
3900 inode->i_fop = &btrfs_file_operations;
3901 inode->i_op = &btrfs_file_inode_operations;
3902 break;
3903 case S_IFDIR:
3904 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003905 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 break;
3907 case S_IFLNK:
3908 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003909 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003910 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003912 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003913 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003914 init_special_inode(inode, inode->i_mode, rdev);
3915 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003916 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003917
David Sterba7b6a2212018-03-26 18:40:21 +02003918 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003919 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003920}
3921
Chris Masond352ac62008-09-29 15:18:18 -04003922/*
3923 * given a leaf and an inode, copy the inode fields into the leaf
3924 */
Chris Masone02119d2008-09-05 16:13:11 -04003925static void fill_inode_item(struct btrfs_trans_handle *trans,
3926 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003927 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003928 struct inode *inode)
3929{
Liu Bo51fab692012-12-27 09:01:21 +00003930 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07003931 u64 flags;
Chris Mason5f39d392007-10-15 16:14:19 -04003932
David Sterbac82f8232019-08-09 17:48:21 +02003933 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003934
David Sterbacc4c13d2020-04-29 02:15:56 +02003935 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3936 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3937 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3938 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3939 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003940
David Sterbacc4c13d2020-04-29 02:15:56 +02003941 btrfs_set_token_timespec_sec(&token, &item->atime,
3942 inode->i_atime.tv_sec);
3943 btrfs_set_token_timespec_nsec(&token, &item->atime,
3944 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003945
David Sterbacc4c13d2020-04-29 02:15:56 +02003946 btrfs_set_token_timespec_sec(&token, &item->mtime,
3947 inode->i_mtime.tv_sec);
3948 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3949 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003950
David Sterbacc4c13d2020-04-29 02:15:56 +02003951 btrfs_set_token_timespec_sec(&token, &item->ctime,
3952 inode->i_ctime.tv_sec);
3953 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3954 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003955
David Sterbacc4c13d2020-04-29 02:15:56 +02003956 btrfs_set_token_timespec_sec(&token, &item->otime,
3957 BTRFS_I(inode)->i_otime.tv_sec);
3958 btrfs_set_token_timespec_nsec(&token, &item->otime,
3959 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303960
David Sterbacc4c13d2020-04-29 02:15:56 +02003961 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3962 btrfs_set_token_inode_generation(&token, item,
3963 BTRFS_I(inode)->generation);
3964 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3965 btrfs_set_token_inode_transid(&token, item, trans->transid);
3966 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07003967 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3968 BTRFS_I(inode)->ro_flags);
3969 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02003970 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003971}
3972
Chris Masond352ac62008-09-29 15:18:18 -04003973/*
3974 * copy everything in the in-memory inode into the btree.
3975 */
Chris Mason21151332011-11-10 20:39:08 -05003976static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003977 struct btrfs_root *root,
3978 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003979{
3980 struct btrfs_inode_item *inode_item;
3981 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003982 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003983 int ret;
3984
3985 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003986 if (!path)
3987 return -ENOMEM;
3988
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003989 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003990 if (ret) {
3991 if (ret > 0)
3992 ret = -ENOENT;
3993 goto failed;
3994 }
3995
Chris Mason5f39d392007-10-15 16:14:19 -04003996 leaf = path->nodes[0];
3997 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003998 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003999
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02004000 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004001 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02004002 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004003 ret = 0;
4004failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004005 btrfs_free_path(path);
4006 return ret;
4007}
4008
Chris Masond352ac62008-09-29 15:18:18 -04004009/*
Chris Mason21151332011-11-10 20:39:08 -05004010 * copy everything in the in-memory inode into the btree.
4011 */
4012noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004013 struct btrfs_root *root,
4014 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004016 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004017 int ret;
4018
4019 /*
4020 * If the inode is a free space inode, we can deadlock during commit
4021 * if we put it into the delayed code.
4022 *
4023 * The data relocation inode should also be directly updated
4024 * without delay
4025 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004026 if (!btrfs_is_free_space_inode(inode)
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09004027 && !btrfs_is_data_reloc_root(root)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004028 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004029 btrfs_update_root_times(trans, root);
4030
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004031 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004032 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004033 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05004034 return ret;
4035 }
4036
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004037 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004038}
4039
Nikolay Borisov729f7962020-11-02 16:49:06 +02004040int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4041 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004042{
4043 int ret;
4044
Nikolay Borisov729f7962020-11-02 16:49:06 +02004045 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004046 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02004047 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004048 return ret;
4049}
4050
4051/*
Chris Masond352ac62008-09-29 15:18:18 -04004052 * unlink helper that gets used here in inode.c and in the tree logging
4053 * recovery code. It remove a link in a directory with a given name, and
4054 * also drops the back refs in the inode to the directory
4055 */
Al Viro92986792011-03-04 17:14:37 +00004056static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004057 struct btrfs_inode *dir,
4058 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004059 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004060{
Filipe Manana4467af82021-10-25 17:31:50 +01004061 struct btrfs_root *root = dir->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004062 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004063 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004064 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004066 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004067 u64 ino = btrfs_ino(inode);
4068 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004069
4070 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004071 if (!path) {
4072 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004073 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004074 }
4075
Li Zefan33345d012011-04-20 10:31:50 +08004076 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004077 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004078 if (IS_ERR_OR_NULL(di)) {
4079 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 goto err;
4081 }
Chris Mason39279cc2007-06-12 06:35:45 -04004082 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004083 if (ret)
4084 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004085 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004086
Miao Xie67de1172013-12-26 13:07:06 +08004087 /*
4088 * If we don't have dir index, we have to get it by looking up
4089 * the inode ref, since we get the inode ref, remove it directly,
4090 * it is unnecessary to do delayed deletion.
4091 *
4092 * But if we have dir index, needn't search inode ref to get it.
4093 * Since the inode ref is close to the inode item, it is better
4094 * that we delay to delete it, and just do this deletion when
4095 * we update the inode item.
4096 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004097 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004098 ret = btrfs_delayed_delete_inode_ref(inode);
4099 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004100 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004101 goto skip_backref;
4102 }
4103 }
4104
Li Zefan33345d012011-04-20 10:31:50 +08004105 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4106 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004107 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004109 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004110 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004111 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004112 goto err;
4113 }
Miao Xie67de1172013-12-26 13:07:06 +08004114skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004115 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004118 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 }
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Josef Bacik9a35fc92021-10-05 16:35:24 -04004121 btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4122 dir_ino);
4123 btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, index);
Josef Bacik63611e72019-06-18 10:59:18 -04004124
4125 /*
4126 * If we have a pending delayed iput we could end up with the final iput
4127 * being run in btrfs-cleaner context. If we have enough of these built
4128 * up we can end up burning a lot of time in btrfs-cleaner without any
4129 * way to throttle the unlinks. Since we're currently holding a ref on
4130 * the inode we can run the delayed iput here without any issues as the
4131 * final iput won't be done until after we drop the ref we're currently
4132 * holding.
4133 */
4134 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004135err:
4136 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004137 if (ret)
4138 goto out;
4139
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004140 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004141 inode_inc_iversion(&inode->vfs_inode);
4142 inode_inc_iversion(&dir->vfs_inode);
4143 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4144 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004145 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004146out:
Chris Mason39279cc2007-06-12 06:35:45 -04004147 return ret;
4148}
4149
Al Viro92986792011-03-04 17:14:37 +00004150int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004151 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004152 const char *name, int name_len)
4153{
4154 int ret;
Filipe Manana4467af82021-10-25 17:31:50 +01004155 ret = __btrfs_unlink_inode(trans, dir, inode, name, name_len);
Al Viro92986792011-03-04 17:14:37 +00004156 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 drop_nlink(&inode->vfs_inode);
Filipe Manana4467af82021-10-25 17:31:50 +01004158 ret = btrfs_update_inode(trans, inode->root, inode);
Al Viro92986792011-03-04 17:14:37 +00004159 }
4160 return ret;
4161}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162
4163/*
4164 * helper to start transaction for unlink and rmdir.
4165 *
Josef Bacikd52be812013-05-29 14:54:47 -04004166 * unlink and rmdir are special in btrfs, they do not always free space, so
4167 * if we cannot make our reservations the normal way try and see if there is
4168 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4169 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170 */
Josef Bacikd52be812013-05-29 14:54:47 -04004171static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004172{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004173 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174
Josef Bacike70bea52011-10-11 14:18:24 -04004175 /*
4176 * 1 for the possible orphan item
4177 * 1 for the dir item
4178 * 1 for the dir index
4179 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004180 * 1 for the inode
4181 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004182 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183}
4184
Chris Mason39279cc2007-06-12 06:35:45 -04004185static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4186{
Chris Mason39279cc2007-06-12 06:35:45 -04004187 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004188 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004189 int ret;
4190
Josef Bacikd52be812013-05-29 14:54:47 -04004191 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004192 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004193 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004194
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004195 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4196 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004197
Filipe Manana4467af82021-10-25 17:31:50 +01004198 ret = btrfs_unlink_inode(trans, BTRFS_I(dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004199 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4200 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004201 if (ret)
4202 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004203
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004205 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004206 if (ret)
4207 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208 }
Josef Bacik7b128762008-07-24 12:17:14 -04004209
Tsutomu Itohb5324022011-07-19 07:27:20 +00004210out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004211 btrfs_end_transaction(trans);
Filipe Manana4467af82021-10-25 17:31:50 +01004212 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004213 return ret;
4214}
4215
Misono Tomohirof60a2362018-04-18 11:34:52 +09004216static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004217 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004219 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004220 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221 struct btrfs_path *path;
4222 struct extent_buffer *leaf;
4223 struct btrfs_dir_item *di;
4224 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004225 const char *name = dentry->d_name.name;
4226 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 u64 index;
4228 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004229 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004230 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231
Josef Bacik045d3962019-12-18 17:20:27 -05004232 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4233 objectid = inode->root->root_key.objectid;
4234 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4235 objectid = inode->location.objectid;
4236 } else {
4237 WARN_ON(1);
4238 return -EINVAL;
4239 }
4240
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 path = btrfs_alloc_path();
4242 if (!path)
4243 return -ENOMEM;
4244
Li Zefan33345d012011-04-20 10:31:50 +08004245 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004248 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 goto out;
4250 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251
4252 leaf = path->nodes[0];
4253 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4254 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4255 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004257 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004258 goto out;
4259 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004260 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
Josef Bacikd49d3282019-12-18 17:20:28 -05004262 /*
4263 * This is a placeholder inode for a subvolume we didn't have a
4264 * reference to at the time of the snapshot creation. In the meantime
4265 * we could have renamed the real subvol link into our snapshot, so
David Sterba1a9fd412021-05-21 17:42:23 +02004266 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
Josef Bacikd49d3282019-12-18 17:20:28 -05004267 * Instead simply lookup the dir_index_item for this entry so we can
4268 * remove it. Otherwise we know we have a ref to the root and we can
4269 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4270 */
4271 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004272 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004273 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 if (IS_ERR_OR_NULL(di)) {
4275 if (!di)
4276 ret = -ENOENT;
4277 else
4278 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004279 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 goto out;
4281 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004282
4283 leaf = path->nodes[0];
4284 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004285 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004286 btrfs_release_path(path);
4287 } else {
4288 ret = btrfs_del_root_ref(trans, objectid,
4289 root->root_key.objectid, dir_ino,
4290 &index, name, name_len);
4291 if (ret) {
4292 btrfs_abort_transaction(trans, ret);
4293 goto out;
4294 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004295 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296
Lu Fengqi9add2942018-08-01 11:32:26 +08004297 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004299 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 goto out;
4301 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004302
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004303 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004304 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004305 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004306 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004308 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004309out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004310 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004312}
4313
Misono Tomohiroec42f162018-04-18 11:34:13 +09004314/*
4315 * Helper to check if the subvolume references other subvolumes or if it's
4316 * default.
4317 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004318static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004319{
4320 struct btrfs_fs_info *fs_info = root->fs_info;
4321 struct btrfs_path *path;
4322 struct btrfs_dir_item *di;
4323 struct btrfs_key key;
4324 u64 dir_id;
4325 int ret;
4326
4327 path = btrfs_alloc_path();
4328 if (!path)
4329 return -ENOMEM;
4330
4331 /* Make sure this root isn't set as the default subvol */
4332 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4333 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4334 dir_id, "default", 7, 0);
4335 if (di && !IS_ERR(di)) {
4336 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4337 if (key.objectid == root->root_key.objectid) {
4338 ret = -EPERM;
4339 btrfs_err(fs_info,
4340 "deleting default subvolume %llu is not allowed",
4341 key.objectid);
4342 goto out;
4343 }
4344 btrfs_release_path(path);
4345 }
4346
4347 key.objectid = root->root_key.objectid;
4348 key.type = BTRFS_ROOT_REF_KEY;
4349 key.offset = (u64)-1;
4350
4351 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4352 if (ret < 0)
4353 goto out;
4354 BUG_ON(ret == 0);
4355
4356 ret = 0;
4357 if (path->slots[0] > 0) {
4358 path->slots[0]--;
4359 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4360 if (key.objectid == root->root_key.objectid &&
4361 key.type == BTRFS_ROOT_REF_KEY)
4362 ret = -ENOTEMPTY;
4363 }
4364out:
4365 btrfs_free_path(path);
4366 return ret;
4367}
4368
Nikolay Borisov20a68002018-04-27 14:36:24 +03004369/* Delete all dentries for inodes belonging to the root */
4370static void btrfs_prune_dentries(struct btrfs_root *root)
4371{
4372 struct btrfs_fs_info *fs_info = root->fs_info;
4373 struct rb_node *node;
4374 struct rb_node *prev;
4375 struct btrfs_inode *entry;
4376 struct inode *inode;
4377 u64 objectid = 0;
4378
Josef Bacik84961532021-10-05 16:35:25 -04004379 if (!BTRFS_FS_ERROR(fs_info))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004380 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4381
4382 spin_lock(&root->inode_lock);
4383again:
4384 node = root->inode_tree.rb_node;
4385 prev = NULL;
4386 while (node) {
4387 prev = node;
4388 entry = rb_entry(node, struct btrfs_inode, rb_node);
4389
David Sterba37508512018-06-29 10:56:40 +02004390 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004391 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004392 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004393 node = node->rb_right;
4394 else
4395 break;
4396 }
4397 if (!node) {
4398 while (prev) {
4399 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004400 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004401 node = prev;
4402 break;
4403 }
4404 prev = rb_next(prev);
4405 }
4406 }
4407 while (node) {
4408 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004409 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004410 inode = igrab(&entry->vfs_inode);
4411 if (inode) {
4412 spin_unlock(&root->inode_lock);
4413 if (atomic_read(&inode->i_count) > 1)
4414 d_prune_aliases(inode);
4415 /*
4416 * btrfs_drop_inode will have it removed from the inode
4417 * cache when its usage count hits zero.
4418 */
4419 iput(inode);
4420 cond_resched();
4421 spin_lock(&root->inode_lock);
4422 goto again;
4423 }
4424
4425 if (cond_resched_lock(&root->inode_lock))
4426 goto again;
4427
4428 node = rb_next(node);
4429 }
4430 spin_unlock(&root->inode_lock);
4431}
4432
Misono Tomohirof60a2362018-04-18 11:34:52 +09004433int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4434{
4435 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4436 struct btrfs_root *root = BTRFS_I(dir)->root;
4437 struct inode *inode = d_inode(dentry);
4438 struct btrfs_root *dest = BTRFS_I(inode)->root;
4439 struct btrfs_trans_handle *trans;
4440 struct btrfs_block_rsv block_rsv;
4441 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004442 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004443
4444 /*
4445 * Don't allow to delete a subvolume with send in progress. This is
4446 * inside the inode lock so the error handling that has to drop the bit
4447 * again is not run concurrently.
4448 */
4449 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004450 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004451 spin_unlock(&dest->root_item_lock);
4452 btrfs_warn(fs_info,
4453 "attempt to delete subvolume %llu during send",
4454 dest->root_key.objectid);
4455 return -EPERM;
4456 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004457 root_flags = btrfs_root_flags(&dest->root_item);
4458 btrfs_set_root_flags(&dest->root_item,
4459 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4460 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004461
4462 down_write(&fs_info->subvol_sem);
4463
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004464 ret = may_destroy_subvol(dest);
4465 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004466 goto out_up_write;
4467
4468 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4469 /*
4470 * One for dir inode,
4471 * two for dir entries,
4472 * two for root ref/backref.
4473 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004474 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4475 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004476 goto out_up_write;
4477
4478 trans = btrfs_start_transaction(root, 0);
4479 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004480 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004481 goto out_release;
4482 }
4483 trans->block_rsv = &block_rsv;
4484 trans->bytes_reserved = block_rsv.size;
4485
4486 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4487
Josef Bacik045d3962019-12-18 17:20:27 -05004488 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004489 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004490 btrfs_abort_transaction(trans, ret);
4491 goto out_end_trans;
4492 }
4493
Josef Bacik2731f512021-03-12 15:25:04 -05004494 ret = btrfs_record_root_in_trans(trans, dest);
4495 if (ret) {
4496 btrfs_abort_transaction(trans, ret);
4497 goto out_end_trans;
4498 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004499
4500 memset(&dest->root_item.drop_progress, 0,
4501 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004502 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004503 btrfs_set_root_refs(&dest->root_item, 0);
4504
4505 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4506 ret = btrfs_insert_orphan_item(trans,
4507 fs_info->tree_root,
4508 dest->root_key.objectid);
4509 if (ret) {
4510 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004511 goto out_end_trans;
4512 }
4513 }
4514
Lu Fengqid1957792018-05-29 15:01:54 +08004515 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004516 BTRFS_UUID_KEY_SUBVOL,
4517 dest->root_key.objectid);
4518 if (ret && ret != -ENOENT) {
4519 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004520 goto out_end_trans;
4521 }
4522 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004523 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004524 dest->root_item.received_uuid,
4525 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4526 dest->root_key.objectid);
4527 if (ret && ret != -ENOENT) {
4528 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004529 goto out_end_trans;
4530 }
4531 }
4532
Qu Wenruo082b6c92020-06-16 10:17:37 +08004533 free_anon_bdev(dest->anon_dev);
4534 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004535out_end_trans:
4536 trans->block_rsv = NULL;
4537 trans->bytes_reserved = 0;
4538 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004539 inode->i_flags |= S_DEAD;
4540out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004541 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004542out_up_write:
4543 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004544 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004545 spin_lock(&dest->root_item_lock);
4546 root_flags = btrfs_root_flags(&dest->root_item);
4547 btrfs_set_root_flags(&dest->root_item,
4548 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4549 spin_unlock(&dest->root_item_lock);
4550 } else {
4551 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004552 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004553 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004554 }
4555
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004556 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004557}
4558
Chris Mason39279cc2007-06-12 06:35:45 -04004559static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4560{
David Howells2b0143b2015-03-17 22:25:59 +00004561 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004562 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004563 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004564 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004565
David Sterbab3ae2442012-09-13 16:04:34 -06004566 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004567 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004568 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004569 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004570
Josef Bacikd52be812013-05-29 14:54:47 -04004571 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004572 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004573 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004574
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004575 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004576 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004577 goto out;
4578 }
4579
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004580 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004581 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004582 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004583
Filipe Manana44f714d2016-06-06 16:11:13 +01004584 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4585
Chris Mason39279cc2007-06-12 06:35:45 -04004586 /* now the directory is empty */
Filipe Manana4467af82021-10-25 17:31:50 +01004587 err = btrfs_unlink_inode(trans, BTRFS_I(dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004588 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4589 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004590 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004591 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004592 /*
4593 * Propagate the last_unlink_trans value of the deleted dir to
4594 * its parent directory. This is to prevent an unrecoverable
4595 * log tree in the case we do something like this:
4596 * 1) create dir foo
4597 * 2) create snapshot under dir foo
4598 * 3) delete the snapshot
4599 * 4) rmdir foo
4600 * 5) mkdir foo
4601 * 6) fsync foo or some file inside foo
4602 */
4603 if (last_unlink_trans >= trans->transid)
4604 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4605 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004606out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004607 btrfs_end_transaction(trans);
Filipe Manana4467af82021-10-25 17:31:50 +01004608 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004609
Chris Mason39279cc2007-06-12 06:35:45 -04004610 return err;
4611}
4612
Josef Bacikddfae632017-10-19 14:16:02 -04004613/*
4614 * Return this if we need to call truncate_block for the last bit of the
4615 * truncate.
4616 */
4617#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004618
Chris Mason323ac952008-10-01 19:05:46 -04004619/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004620 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004621 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004622 * @trans: A transaction handle.
4623 * @root: The root from which to remove items.
4624 * @inode: The inode whose items we want to remove.
4625 * @new_size: The new i_size for the inode. This is only applicable when
4626 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4627 * @min_type: The minimum key type to remove. All keys with a type
4628 * greater than this value are removed and all keys with
4629 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004630 * @extents_found: Output parameter that will contain the number of file
4631 * extent items that were removed or adjusted to the new
4632 * inode i_size. The caller is responsible for initializing
4633 * the counter. Also, it can be NULL if the caller does not
4634 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004635 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004636 * Remove all keys associated with the inode from the given root that have a key
4637 * with a type greater than or equals to @min_type. When @min_type has a value of
4638 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4639 * greater than or equals to @new_size. If a file extent item that starts before
4640 * @new_size and ends after it is found, its length is adjusted.
4641 *
4642 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4643 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004644 */
Yan, Zheng80825102009-11-12 09:35:36 +00004645int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4646 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004647 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004648 u64 new_size, u32 min_type,
4649 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004652 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004653 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004654 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004655 struct btrfs_key key;
4656 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004657 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004658 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004659 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004660 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004661 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004662 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004663 int found_extent;
4664 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004665 int pending_del_nr = 0;
4666 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004667 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004668 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004669 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004670 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004671 bool be_nice = false;
4672 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004673 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4674 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004675
4676 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004677
Chris Mason28ed1342014-12-17 09:41:04 -08004678 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004679 * For non-free space inodes and non-shareable roots, we want to back
4680 * off from time to time. This means all inodes in subvolume roots,
4681 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004682 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004683 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004684 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004685 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004686
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004687 path = btrfs_alloc_path();
4688 if (!path)
4689 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004690 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004691
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004692 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004693 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004694 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004695
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004696 /*
4697 * We want to drop from the next block forward in case this
4698 * new size is not block aligned since we will be keeping the
4699 * last block of the extent just the way it is.
4700 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004701 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004702 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004703 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004704 }
Yan, Zheng80825102009-11-12 09:35:36 +00004705
Miao Xie16cdcec2011-04-22 18:12:22 +08004706 /*
4707 * This function is also used to drop the items in the log tree before
4708 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004709 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004710 * items.
4711 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004712 if (min_type == 0 && root == inode->root)
4713 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004714
Li Zefan33345d012011-04-20 10:31:50 +08004715 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004717 key.type = (u8)-1;
4718
Chris Mason85e21ba2008-01-29 15:11:36 -05004719search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004720 /*
4721 * with a 16K leaf size and 128MB extents, you can actually queue
4722 * up a huge file in a single leaf. Most of the time that
4723 * bytes_deleted is > 0, it will be huge by the time we get here
4724 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004725 if (be_nice && bytes_deleted > SZ_32M &&
4726 btrfs_should_end_transaction(trans)) {
4727 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004728 goto out;
4729 }
Chris Masond3977122009-01-05 21:25:51 -05004730
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004731 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4732 if (ret < 0)
4733 goto out;
4734
Chris Mason85e21ba2008-01-29 15:11:36 -05004735 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004736 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004737 /* there are no items in the tree for us to truncate, we're
4738 * done
4739 */
Yan, Zheng80825102009-11-12 09:35:36 +00004740 if (path->slots[0] == 0)
4741 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004742 path->slots[0]--;
4743 }
4744
Chris Masond3977122009-01-05 21:25:51 -05004745 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004746 u64 clear_start = 0, clear_len = 0;
4747
Chris Mason39279cc2007-06-12 06:35:45 -04004748 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004749 leaf = path->nodes[0];
4750 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004751 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004752
Li Zefan33345d012011-04-20 10:31:50 +08004753 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004754 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004755
Chris Mason85e21ba2008-01-29 15:11:36 -05004756 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004757 break;
4758
Chris Mason5f39d392007-10-15 16:14:19 -04004759 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004760 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004761 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004762 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004763 extent_type = btrfs_file_extent_type(leaf, fi);
4764 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004765 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004766 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004767
4768 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004769 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004770 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004771 item_end += btrfs_file_extent_ram_bytes(leaf,
4772 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004773
4774 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004775 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004776 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004777 }
Yan008630c2007-11-07 13:31:09 -05004778 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004779 }
Yan, Zheng80825102009-11-12 09:35:36 +00004780 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004781 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004782 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004783 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004784 break;
4785 if (found_key.offset >= new_size)
4786 del_item = 1;
4787 else
4788 del_item = 0;
4789 }
Chris Mason39279cc2007-06-12 06:35:45 -04004790 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004791 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004792 if (found_type != BTRFS_EXTENT_DATA_KEY)
4793 goto delete;
4794
Filipe Manana0d7d3162021-05-24 11:35:55 +01004795 if (extents_found != NULL)
4796 (*extents_found)++;
4797
Chris Mason179e29e2007-11-01 11:28:41 -04004798 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004799 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004800
4801 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004802 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004803 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004804 u64 orig_num_bytes =
4805 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004806 extent_num_bytes = ALIGN(new_size -
4807 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004809 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004810 btrfs_set_file_extent_num_bytes(leaf, fi,
4811 extent_num_bytes);
4812 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004813 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004814 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004815 &root->state) &&
4816 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004817 inode_sub_bytes(&inode->vfs_inode,
4818 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004819 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004820 } else {
Chris Masondb945352007-10-15 16:15:53 -04004821 extent_num_bytes =
4822 btrfs_file_extent_disk_num_bytes(leaf,
4823 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004824 extent_offset = found_key.offset -
4825 btrfs_file_extent_offset(leaf, fi);
4826
Chris Mason39279cc2007-06-12 06:35:45 -04004827 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004828 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004829 if (extent_start != 0) {
4830 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004831 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004832 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004833 inode_sub_bytes(&inode->vfs_inode,
4834 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004835 }
4836 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004837 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004838 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004839 /*
4840 * we can't truncate inline items that have had
4841 * special encodings
4842 */
4843 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004844 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004845 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4846 btrfs_file_extent_compression(leaf, fi) == 0) {
4847 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004848
Josef Bacikddfae632017-10-19 14:16:02 -04004849 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4850 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004851 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004852 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004853 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004854 * We have to bail so the last_size is set to
4855 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004856 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004857 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004858 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004859 } else {
4860 /*
4861 * Inline extents are special, we just treat
4862 * them as a full sector worth in the file
4863 * extent tree just for simplicity sake.
4864 */
4865 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004866 }
Josef Bacikddfae632017-10-19 14:16:02 -04004867
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004868 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004869 inode_sub_bytes(&inode->vfs_inode,
4870 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004871 }
Chris Mason179e29e2007-11-01 11:28:41 -04004872delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004873 /*
4874 * We use btrfs_truncate_inode_items() to clean up log trees for
4875 * multiple fsyncs, and in this case we don't want to clear the
4876 * file extent range because it's just the log.
4877 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004878 if (root == inode->root) {
4879 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004880 clear_start, clear_len);
4881 if (ret) {
4882 btrfs_abort_transaction(trans, ret);
4883 break;
4884 }
4885 }
4886
Josef Bacikddfae632017-10-19 14:16:02 -04004887 if (del_item)
4888 last_size = found_key.offset;
4889 else
4890 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004891 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004892 if (!pending_del_nr) {
4893 /* no pending yet, add ourselves */
4894 pending_del_slot = path->slots[0];
4895 pending_del_nr = 1;
4896 } else if (pending_del_nr &&
4897 path->slots[0] + 1 == pending_del_slot) {
4898 /* hop on the pending chunk */
4899 pending_del_nr++;
4900 pending_del_slot = path->slots[0];
4901 } else {
Chris Masond3977122009-01-05 21:25:51 -05004902 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004903 }
Chris Mason39279cc2007-06-12 06:35:45 -04004904 } else {
4905 break;
4906 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004907 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004908
Miao Xie27cdeb72014-04-02 19:51:05 +08004909 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004910 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004911 struct btrfs_ref ref = { 0 };
4912
Chris Mason28ed1342014-12-17 09:41:04 -08004913 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004914
4915 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4916 extent_start, extent_num_bytes, 0);
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004917 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
Nikolay Borisovf42c5da2021-10-12 11:21:35 +03004918 ino, extent_offset,
4919 root->root_key.objectid, false);
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004920 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004921 if (ret) {
4922 btrfs_abort_transaction(trans, ret);
4923 break;
4924 }
Chris Mason28f75a02015-02-04 06:59:29 -08004925 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004926 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004927 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004928 }
Chris Mason39279cc2007-06-12 06:35:45 -04004929 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004930
Yan, Zheng80825102009-11-12 09:35:36 +00004931 if (found_type == BTRFS_INODE_ITEM_KEY)
4932 break;
4933
4934 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004935 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004936 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004937 if (pending_del_nr) {
4938 ret = btrfs_del_items(trans, root, path,
4939 pending_del_slot,
4940 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004941 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004942 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004943 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004944 }
Yan, Zheng80825102009-11-12 09:35:36 +00004945 pending_del_nr = 0;
4946 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004947 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004948
Chris Mason28f75a02015-02-04 06:59:29 -08004949 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004950 * We can generate a lot of delayed refs, so we need to
4951 * throttle every once and a while and make sure we're
4952 * adding enough space to keep up with the work we are
4953 * generating. Since we hold a transaction here we
4954 * can't flush, and we don't want to FLUSH_LIMIT because
4955 * we could have generated too many delayed refs to
4956 * actually allocate, so just bail if we're short and
4957 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004958 */
Josef Bacik28bad212018-12-03 10:20:38 -05004959 if (should_throttle) {
4960 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4961 BTRFS_RESERVE_NO_FLUSH);
4962 if (ret) {
4963 ret = -EAGAIN;
4964 break;
4965 }
Chris Mason28f75a02015-02-04 06:59:29 -08004966 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004967 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004968 } else {
4969 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 }
Chris Mason39279cc2007-06-12 06:35:45 -04004971 }
Yan, Zheng80825102009-11-12 09:35:36 +00004972out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004973 if (ret >= 0 && pending_del_nr) {
4974 int err;
4975
4976 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004977 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004978 if (err) {
4979 btrfs_abort_transaction(trans, err);
4980 ret = err;
4981 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004982 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004983 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4984 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004985 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004986 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004987 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4988 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4989 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004990 }
Chris Mason28ed1342014-12-17 09:41:04 -08004991
Chris Mason39279cc2007-06-12 06:35:45 -04004992 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004993 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004994}
4995
Chris Masona52d9a82007-08-27 16:49:44 -04004996/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304997 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004998 * @inode - inode that we're zeroing
4999 * @from - the offset to start zeroing
5000 * @len - the length to zero, 0 to zero the entire range respective to the
5001 * offset
5002 * @front - zero up to the offset instead of from the offset on
5003 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305004 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04005005 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04005006 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005007int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
5008 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04005009{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005010 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5011 struct address_space *mapping = inode->vfs_inode.i_mapping;
5012 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005013 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005014 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08005015 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005016 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005017 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005018 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305019 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04005020 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005021 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005022 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04005023 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305024 u64 block_start;
5025 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04005026
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02005027 if (IS_ALIGNED(offset, blocksize) &&
5028 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04005029 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305030
Josef Bacik8b62f872017-10-19 14:15:55 -04005031 block_start = round_down(from, blocksize);
5032 block_end = block_start + blocksize - 1;
5033
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005034 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5035 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005036 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005037 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005038 /* For nocow case, no need to reserve data space */
5039 only_release_metadata = true;
5040 } else {
5041 goto out;
5042 }
5043 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005044 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005045 if (ret < 0) {
5046 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005047 btrfs_free_reserved_data_space(inode, data_reserved,
5048 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005049 goto out;
5050 }
Chris Mason211c17f2008-05-15 09:13:45 -04005051again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005052 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005053 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005054 btrfs_delalloc_release_space(inode, data_reserved, block_start,
5055 blocksize, true);
5056 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005057 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005058 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005059 }
Qu Wenruo32443de2021-01-26 16:34:00 +08005060 ret = set_page_extent_mapped(page);
5061 if (ret < 0)
5062 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005063
Chris Masona52d9a82007-08-27 16:49:44 -04005064 if (!PageUptodate(page)) {
5065 ret = btrfs_readpage(NULL, page);
5066 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005067 if (page->mapping != mapping) {
5068 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005069 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005070 goto again;
5071 }
Chris Masona52d9a82007-08-27 16:49:44 -04005072 if (!PageUptodate(page)) {
5073 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005074 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005075 }
5076 }
Chris Mason211c17f2008-05-15 09:13:45 -04005077 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305079 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005080
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005081 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005082 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305083 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005084 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005085 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005086 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03005087 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005088 btrfs_put_ordered_extent(ordered);
5089 goto again;
5090 }
5091
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005092 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005093 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5094 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005095
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005096 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005097 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005098 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305099 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005100 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005101 goto out_unlock;
5102 }
5103
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305104 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005105 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305106 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04005107 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07005108 memzero_page(page, (block_start - page_offset(page)),
5109 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005110 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07005111 memzero_page(page, (block_start - page_offset(page)) + offset,
5112 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005113 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005114 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08005115 btrfs_page_clear_checked(fs_info, page, block_start,
5116 block_end + 1 - block_start);
Qu Wenruo6c9ac8b2021-05-31 16:50:51 +08005117 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
David Sterbae43bbe52017-12-12 21:43:52 +01005118 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005119
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005120 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005121 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02005122 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005123
Chris Mason89642222008-07-24 09:41:53 -04005124out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005125 if (ret) {
5126 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005127 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005128 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005129 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005130 block_start, blocksize, true);
5131 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005132 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005134 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005135out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005136 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005137 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08005138 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005139 return ret;
5140}
5141
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005142static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04005143 u64 offset, u64 len)
5144{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005145 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04005146 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00005147 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04005148 int ret;
5149
5150 /*
Filipe Mananacceaa892021-07-20 16:03:40 +01005151 * If NO_HOLES is enabled, we don't need to do anything.
5152 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5153 * or btrfs_update_inode() will be called, which guarantee that the next
5154 * fsync will know this inode was changed and needs to be logged.
Josef Bacik16e75492013-10-22 12:18:51 -04005155 */
Filipe Mananacceaa892021-07-20 16:03:40 +01005156 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04005157 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005158
5159 /*
5160 * 1 - for the one we're dropping
5161 * 1 - for the one we're adding
5162 * 1 - for updating the inode.
5163 */
5164 trans = btrfs_start_transaction(root, 3);
5165 if (IS_ERR(trans))
5166 return PTR_ERR(trans);
5167
Filipe Manana5893dfb2020-11-04 11:07:32 +00005168 drop_args.start = offset;
5169 drop_args.end = offset + len;
5170 drop_args.drop_cache = true;
5171
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005172 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005173 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005174 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005175 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005176 return ret;
5177 }
5178
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005179 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005180 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005181 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005182 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005183 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005184 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5185 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005186 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005187 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005188 return ret;
5189}
5190
Josef Bacik695a0d02011-03-04 15:46:53 -05005191/*
5192 * This function puts in dummy file extents for the area we're creating a hole
5193 * for. So if we are truncating this file to a larger size we need to insert
5194 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5195 * the range between oldsize and size
5196 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005197int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005198{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005199 struct btrfs_root *root = inode->root;
5200 struct btrfs_fs_info *fs_info = root->fs_info;
5201 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005202 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005203 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005204 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005205 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5206 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005207 u64 last_byte;
5208 u64 cur_offset;
5209 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005210 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005211
Josef Bacika71754f2013-06-17 17:14:39 -04005212 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305213 * If our size started in the middle of a block we need to zero out the
5214 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005215 * expose stale data.
5216 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005217 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005218 if (err)
5219 return err;
5220
Yan Zheng9036c102008-10-30 14:19:41 -04005221 if (size <= hole_start)
5222 return 0;
5223
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005224 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5225 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005226 cur_offset = hole_start;
5227 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005228 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005229 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005230 if (IS_ERR(em)) {
5231 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005232 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005233 break;
5234 }
Yan Zheng9036c102008-10-30 14:19:41 -04005235 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005236 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005237 hole_size = last_byte - cur_offset;
5238
Yan, Zheng80825102009-11-12 09:35:36 +00005239 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005240 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005241
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005242 err = maybe_insert_hole(root, inode, cur_offset,
5243 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005244 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005245 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005246
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005247 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005248 cur_offset, hole_size);
5249 if (err)
5250 break;
5251
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005252 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005253 cur_offset + hole_size - 1, 0);
5254 hole_em = alloc_extent_map();
5255 if (!hole_em) {
5256 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005257 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005258 goto next;
5259 }
5260 hole_em->start = cur_offset;
5261 hole_em->len = hole_size;
5262 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005263
Josef Bacik5dc562c2012-08-17 13:14:17 -04005264 hole_em->block_start = EXTENT_MAP_HOLE;
5265 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005266 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005267 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005268 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005269 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005270
5271 while (1) {
5272 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005273 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005274 write_unlock(&em_tree->lock);
5275 if (err != -EEXIST)
5276 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005277 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005278 cur_offset +
5279 hole_size - 1, 0);
5280 }
5281 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005282 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005283 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005284 cur_offset, hole_size);
5285 if (err)
5286 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005287 }
Josef Bacik16e75492013-10-22 12:18:51 -04005288next:
Yan Zheng9036c102008-10-30 14:19:41 -04005289 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005290 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005291 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005292 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005293 break;
5294 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005295 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005296 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005297 return err;
5298}
5299
Eric Sandeen3972f262013-01-12 02:57:22 +00005300static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005301{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005302 struct btrfs_root *root = BTRFS_I(inode)->root;
5303 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005304 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005305 loff_t newsize = attr->ia_size;
5306 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005307 int ret;
5308
Eric Sandeen3972f262013-01-12 02:57:22 +00005309 /*
5310 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5311 * special case where we need to update the times despite not having
5312 * these flags set. For all other operations the VFS set these flags
5313 * explicitly if it wants a timestamp update.
5314 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005315 if (newsize != oldsize) {
5316 inode_inc_iversion(inode);
5317 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5318 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005319 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005320 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005321
Josef Bacika41ad392011-01-31 15:30:16 -05005322 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005323 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005324 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005325 * This is to ensure the snapshot captures a fully consistent
5326 * state of this file - if the snapshot captures this expanding
5327 * truncation, it must capture all writes that happened before
5328 * this truncation.
5329 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005330 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005331 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005332 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005333 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005334 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005335 }
Yan, Zheng80825102009-11-12 09:35:36 +00005336
Miao Xief4a2f4c2011-12-14 20:12:01 -05005337 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005338 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005339 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005340 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005341 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005342
5343 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005344 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305345 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005346 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005347 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005348 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005349 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005350 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5351
5352 if (btrfs_is_zoned(fs_info)) {
5353 ret = btrfs_wait_ordered_range(inode,
5354 ALIGN(newsize, fs_info->sectorsize),
5355 (u64)-1);
5356 if (ret)
5357 return ret;
5358 }
Yan, Zheng80825102009-11-12 09:35:36 +00005359
Josef Bacika41ad392011-01-31 15:30:16 -05005360 /*
5361 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005362 * zero. Make sure any new writes to the file get on disk
5363 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005364 */
5365 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005366 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005367 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005368
Josef Bacika41ad392011-01-31 15:30:16 -05005369 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005370
Miao Xie2e60a512013-02-08 07:01:08 +00005371 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005372
Filipe Manana213e8c52018-02-06 20:40:31 +00005373 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005374 if (ret && inode->i_nlink) {
5375 int err;
5376
5377 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005378 * Truncate failed, so fix up the in-memory size. We
5379 * adjusted disk_i_size down as we removed extents, so
5380 * wait for disk_i_size to be stable and then update the
5381 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005382 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005383 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005384 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005385 return err;
5386 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005387 }
Yan, Zheng80825102009-11-12 09:35:36 +00005388 }
5389
Josef Bacika41ad392011-01-31 15:30:16 -05005390 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005391}
5392
Christian Brauner549c7292021-01-21 14:19:43 +01005393static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5394 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005395{
David Howells2b0143b2015-03-17 22:25:59 +00005396 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005397 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005398 int err;
5399
Li Zefanb83cc962010-12-20 16:04:08 +08005400 if (btrfs_root_readonly(root))
5401 return -EROFS;
5402
Christian Braunerd4d09462021-07-27 12:48:49 +02005403 err = setattr_prepare(mnt_userns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005404 if (err)
5405 return err;
5406
Chris Mason5a3f23d2009-03-31 13:27:11 -04005407 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005408 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005409 if (err)
5410 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005411 }
Yan Zheng9036c102008-10-30 14:19:41 -04005412
Christoph Hellwig10257742010-06-04 11:30:02 +02005413 if (attr->ia_valid) {
Christian Braunerd4d09462021-07-27 12:48:49 +02005414 setattr_copy(mnt_userns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005415 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005416 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005417
Josef Bacik22c44fe2011-11-30 10:45:38 -05005418 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunerd4d09462021-07-27 12:48:49 +02005419 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005420 }
5421
Chris Mason39279cc2007-06-12 06:35:45 -04005422 return err;
5423}
Chris Mason61295eb2008-01-14 16:24:38 -05005424
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005425/*
5426 * While truncating the inode pages during eviction, we get the VFS calling
5427 * btrfs_invalidatepage() against each page of the inode. This is slow because
5428 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5429 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5430 * extent_state structures over and over, wasting lots of time.
5431 *
5432 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5433 * those expensive operations on a per page basis and do only the ordered io
5434 * finishing, while we release here the extent_map and extent_state structures,
5435 * without the excessive merging and splitting.
5436 */
5437static void evict_inode_truncate_pages(struct inode *inode)
5438{
5439 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5440 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5441 struct rb_node *node;
5442
5443 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005444 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005445
5446 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005447 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005448 struct extent_map *em;
5449
Liu Bo07e1ce02018-08-23 03:51:52 +08005450 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005451 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005452 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5453 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005454 remove_extent_mapping(map_tree, em);
5455 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005456 if (need_resched()) {
5457 write_unlock(&map_tree->lock);
5458 cond_resched();
5459 write_lock(&map_tree->lock);
5460 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005461 }
5462 write_unlock(&map_tree->lock);
5463
Filipe Manana6ca07092015-05-26 00:55:42 +01005464 /*
5465 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005466 * We can have ongoing bios started by readahead that have
5467 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005468 * still in progress (unlocked the pages in the bio but did not yet
5469 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005470 * ranges can still be locked and eviction started because before
5471 * submitting those bios, which are executed by a separate task (work
5472 * queue kthread), inode references (inode->i_count) were not taken
5473 * (which would be dropped in the end io callback of each bio).
5474 * Therefore here we effectively end up waiting for those bios and
5475 * anyone else holding locked ranges without having bumped the inode's
5476 * reference count - if we don't do it, when they access the inode's
5477 * io_tree to unlock a range it may be too late, leading to an
5478 * use-after-free issue.
5479 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005480 spin_lock(&io_tree->lock);
5481 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5482 struct extent_state *state;
5483 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005484 u64 start;
5485 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005486 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005487
5488 node = rb_first(&io_tree->state);
5489 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005490 start = state->start;
5491 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005492 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005493 spin_unlock(&io_tree->lock);
5494
David Sterbaff13db42015-12-03 14:30:40 +01005495 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005496
5497 /*
5498 * If still has DELALLOC flag, the extent didn't reach disk,
5499 * and its reserved space won't be freed by delayed_ref.
5500 * So we need to free its reserved space here.
5501 * (Refer to comment in btrfs_invalidatepage, case 2)
5502 *
5503 * Note, end is the bytenr of last byte, so we need + 1 here.
5504 */
Filipe Manana421f0922018-10-12 13:02:48 +01005505 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005506 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5507 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005508
Filipe Manana6ca07092015-05-26 00:55:42 +01005509 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005510 EXTENT_LOCKED | EXTENT_DELALLOC |
5511 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5512 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005513
Filipe Manana7064dd52014-08-08 02:47:05 +01005514 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005515 spin_lock(&io_tree->lock);
5516 }
5517 spin_unlock(&io_tree->lock);
5518}
5519
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005520static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005521 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005522{
5523 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikd3984c92019-08-01 18:19:37 -04005524 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005525 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005526 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005527
Josef Bacikd3984c92019-08-01 18:19:37 -04005528 /*
5529 * Eviction should be taking place at some place safe because of our
5530 * delayed iputs. However the normal flushing code will run delayed
5531 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5532 *
5533 * We reserve the delayed_refs_extra here again because we can't use
5534 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5535 * above. We reserve our extra bit here because we generate a ton of
5536 * delayed refs activity by truncating.
5537 *
Josef Bacikee6adbf2021-11-09 10:12:04 -05005538 * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can,
5539 * if we fail to make this reservation we can re-try without the
5540 * delayed_refs_extra so we can make some forward progress.
Josef Bacikd3984c92019-08-01 18:19:37 -04005541 */
5542 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5543 BTRFS_RESERVE_FLUSH_EVICT);
5544 if (ret) {
Josef Bacikee6adbf2021-11-09 10:12:04 -05005545 ret = btrfs_block_rsv_refill(root, rsv, rsv->size,
5546 BTRFS_RESERVE_FLUSH_EVICT);
5547 if (ret) {
Josef Bacikd3984c92019-08-01 18:19:37 -04005548 btrfs_warn(fs_info,
5549 "could not allocate space for delete; will truncate on mount");
5550 return ERR_PTR(-ENOSPC);
5551 }
5552 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005553 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005554
5555 trans = btrfs_join_transaction(root);
5556 if (IS_ERR(trans))
5557 return trans;
5558
5559 if (delayed_refs_extra) {
5560 trans->block_rsv = &fs_info->trans_block_rsv;
5561 trans->bytes_reserved = delayed_refs_extra;
5562 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5563 delayed_refs_extra, 1);
5564 }
5565 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005566}
5567
Al Virobd555972010-06-07 11:35:40 -04005568void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005571 struct btrfs_trans_handle *trans;
5572 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005573 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005574 int ret;
5575
liubo1abe9b82011-03-24 11:18:59 +00005576 trace_btrfs_inode_evict(inode);
5577
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005578 if (!root) {
Boris Burkov14605402021-06-30 13:01:49 -07005579 fsverity_cleanup_inode(inode);
Liu Boe8f1bc12018-01-25 11:02:53 -07005580 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005581 return;
5582 }
5583
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005584 evict_inode_truncate_pages(inode);
5585
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005586 if (inode->i_nlink &&
5587 ((btrfs_root_refs(&root->root_item) != 0 &&
5588 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005589 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005590 goto no_delete;
5591
Omar Sandoval27919062018-05-11 13:13:37 -07005592 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005593 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005594
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005595 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005596
Omar Sandoval7b40b692018-05-11 13:13:33 -07005597 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005598 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005599
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005601 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5602 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005603 goto no_delete;
5604 }
5605
Nikolay Borisovaa790212017-01-10 20:35:40 +02005606 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005607 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005608 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005610 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005611 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005612 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005613 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005614 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005615
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005616 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Yan, Zheng80825102009-11-12 09:35:36 +00005618 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005619 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005620 if (IS_ERR(trans))
5621 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005622
5623 trans->block_rsv = rsv;
5624
Nikolay Borisov50743392020-11-02 16:48:55 +02005625 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005626 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005627 trans->block_rsv = &fs_info->trans_block_rsv;
5628 btrfs_end_transaction(trans);
5629 btrfs_btree_balance_dirty(fs_info);
5630 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5631 goto free_rsv;
5632 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005633 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005634 }
5635
Josef Bacik4ef31a42013-08-13 14:10:08 -04005636 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005637 * Errors here aren't a big deal, it just means we leave orphan items in
5638 * the tree. They will be cleaned up on the next mount. If the inode
5639 * number gets reused, cleanup deletes the orphan item without doing
5640 * anything, and unlink reuses the existing orphan item.
5641 *
5642 * If it turns out that we are dropping too many of these, we might want
5643 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005644 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005645 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005646 if (!IS_ERR(trans)) {
5647 trans->block_rsv = rsv;
5648 btrfs_orphan_del(trans, BTRFS_I(inode));
5649 trans->block_rsv = &fs_info->trans_block_rsv;
5650 btrfs_end_transaction(trans);
5651 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005652
Omar Sandoval27919062018-05-11 13:13:37 -07005653free_rsv:
5654 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005655no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005656 /*
5657 * If we didn't successfully delete, the orphan item will still be in
5658 * the tree and we'll retry on the next mount. Again, we might also want
5659 * to retry these periodically in the future.
5660 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005661 btrfs_remove_delayed_node(BTRFS_I(inode));
Boris Burkov14605402021-06-30 13:01:49 -07005662 fsverity_cleanup_inode(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005663 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005664}
5665
5666/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005667 * Return the key found in the dir entry in the location pointer, fill @type
5668 * with BTRFS_FT_*, and return 0.
5669 *
Su Yue005d6712018-03-05 17:13:37 +08005670 * If no dir entries were found, returns -ENOENT.
5671 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005672 */
5673static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005674 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005675{
5676 const char *name = dentry->d_name.name;
5677 int namelen = dentry->d_name.len;
5678 struct btrfs_dir_item *di;
5679 struct btrfs_path *path;
5680 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005681 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005682
5683 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005684 if (!path)
5685 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005686
David Sterbaf85b7372017-01-20 14:54:07 +01005687 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5688 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005689 if (IS_ERR_OR_NULL(di)) {
5690 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005691 goto out;
5692 }
Chris Masond3977122009-01-05 21:25:51 -05005693
Chris Mason5f39d392007-10-15 16:14:19 -04005694 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005695 if (location->type != BTRFS_INODE_ITEM_KEY &&
5696 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005697 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005698 btrfs_warn(root->fs_info,
5699"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5700 __func__, name, btrfs_ino(BTRFS_I(dir)),
5701 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005702 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005703 if (!ret)
5704 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005705out:
Chris Mason39279cc2007-06-12 06:35:45 -04005706 btrfs_free_path(path);
5707 return ret;
5708}
5709
5710/*
5711 * when we hit a tree root in a directory, the btrfs part of the inode
5712 * needs to be changed to reflect the root directory of the tree root. This
5713 * is kind of like crossing a mount point.
5714 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005715static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005716 struct inode *dir,
5717 struct dentry *dentry,
5718 struct btrfs_key *location,
5719 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005720{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721 struct btrfs_path *path;
5722 struct btrfs_root *new_root;
5723 struct btrfs_root_ref *ref;
5724 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005725 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726 int ret;
5727 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005728
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729 path = btrfs_alloc_path();
5730 if (!path) {
5731 err = -ENOMEM;
5732 goto out;
5733 }
Chris Mason39279cc2007-06-12 06:35:45 -04005734
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005736 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5737 key.type = BTRFS_ROOT_REF_KEY;
5738 key.offset = location->objectid;
5739
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005740 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005741 if (ret) {
5742 if (ret < 0)
5743 err = ret;
5744 goto out;
5745 }
Chris Mason39279cc2007-06-12 06:35:45 -04005746
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747 leaf = path->nodes[0];
5748 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005749 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5751 goto out;
5752
5753 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5754 (unsigned long)(ref + 1),
5755 dentry->d_name.len);
5756 if (ret)
5757 goto out;
5758
David Sterbab3b4aa72011-04-21 01:20:15 +02005759 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760
David Sterba56e93572020-05-15 19:35:55 +02005761 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 if (IS_ERR(new_root)) {
5763 err = PTR_ERR(new_root);
5764 goto out;
5765 }
5766
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 *sub_root = new_root;
5768 location->objectid = btrfs_root_dirid(&new_root->root_item);
5769 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005770 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 err = 0;
5772out:
5773 btrfs_free_path(path);
5774 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005775}
5776
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005777static void inode_tree_add(struct inode *inode)
5778{
5779 struct btrfs_root *root = BTRFS_I(inode)->root;
5780 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005781 struct rb_node **p;
5782 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005783 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005784 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005785
Al Viro1d3382cb2010-10-23 15:19:20 -04005786 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005787 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005788 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005789 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005790 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005791 while (*p) {
5792 parent = *p;
5793 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5794
David Sterba37508512018-06-29 10:56:40 +02005795 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005796 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005797 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005798 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005799 else {
5800 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005801 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005802 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005803 RB_CLEAR_NODE(parent);
5804 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005805 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005806 }
5807 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005808 rb_link_node(new, parent, p);
5809 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005810 spin_unlock(&root->inode_lock);
5811}
5812
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005813static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005814{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005815 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005816 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005817
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005818 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005819 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5820 rb_erase(&inode->rb_node, &root->inode_tree);
5821 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005822 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005823 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005824 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005825
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005826 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005827 spin_lock(&root->inode_lock);
5828 empty = RB_EMPTY_ROOT(&root->inode_tree);
5829 spin_unlock(&root->inode_lock);
5830 if (empty)
5831 btrfs_add_dead_root(root);
5832 }
5833}
5834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005835
Chris Masone02119d2008-09-05 16:13:11 -04005836static int btrfs_init_locked_inode(struct inode *inode, void *p)
5837{
5838 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005839
5840 inode->i_ino = args->ino;
5841 BTRFS_I(inode)->location.objectid = args->ino;
5842 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5843 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005844 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5845 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 return 0;
5847}
5848
5849static int btrfs_find_actor(struct inode *inode, void *opaque)
5850{
5851 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005852
5853 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005854 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005855}
5856
David Sterba0202e832020-05-15 19:35:59 +02005857static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005858 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005859{
5860 struct inode *inode;
5861 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005862 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005863
David Sterba0202e832020-05-15 19:35:59 +02005864 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005865 args.root = root;
5866
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005867 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005868 btrfs_init_locked_inode,
5869 (void *)&args);
5870 return inode;
5871}
5872
David Sterba4c66e0d2019-10-03 19:09:35 +02005873/*
David Sterba0202e832020-05-15 19:35:59 +02005874 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005875 * Path can be preallocated to prevent recursing back to iget through
5876 * allocator. NULL is also valid but may require an additional allocation
5877 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305878 */
David Sterba0202e832020-05-15 19:35:59 +02005879struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005880 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305881{
5882 struct inode *inode;
5883
David Sterba0202e832020-05-15 19:35:59 +02005884 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305885 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005886 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305887
5888 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005889 int ret;
5890
Filipe Manana4222ea72018-10-24 10:13:03 +01005891 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005892 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005893 inode_tree_add(inode);
5894 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005895 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005896 iget_failed(inode);
5897 /*
5898 * ret > 0 can come from btrfs_search_slot called by
5899 * btrfs_read_locked_inode, this means the inode item
5900 * was not found.
5901 */
5902 if (ret > 0)
5903 ret = -ENOENT;
5904 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005905 }
5906 }
5907
Balaji Rao1a54ef82008-07-21 02:01:04 +05305908 return inode;
5909}
5910
David Sterba0202e832020-05-15 19:35:59 +02005911struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005912{
David Sterba0202e832020-05-15 19:35:59 +02005913 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005914}
5915
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005916static struct inode *new_simple_dir(struct super_block *s,
5917 struct btrfs_key *key,
5918 struct btrfs_root *root)
5919{
5920 struct inode *inode = new_inode(s);
5921
5922 if (!inode)
5923 return ERR_PTR(-ENOMEM);
5924
Josef Bacik5c8fd992020-02-14 16:11:43 -05005925 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005926 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005927 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005928
5929 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005930 /*
5931 * We only need lookup, the rest is read-only and there's no inode
5932 * associated with the dentry
5933 */
5934 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005935 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005936 inode->i_fop = &simple_dir_operations;
5937 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005938 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305939 inode->i_atime = inode->i_mtime;
5940 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005941 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005942
5943 return inode;
5944}
5945
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005946static inline u8 btrfs_inode_type(struct inode *inode)
5947{
5948 /*
5949 * Compile-time asserts that generic FT_* types still match
5950 * BTRFS_FT_* types
5951 */
5952 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5953 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5954 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5955 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5956 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5957 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5958 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5959 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5960
5961 return fs_umode_to_ftype(inode->i_mode);
5962}
5963
Chris Mason3de45862008-11-17 21:02:50 -05005964struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005965{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005966 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005967 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005968 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005969 struct btrfs_root *sub_root = root;
5970 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005971 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005972 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005973
5974 if (dentry->d_name.len > BTRFS_NAME_LEN)
5975 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005976
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005977 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005978 if (ret < 0)
5979 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005980
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005981 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005982 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005983 if (IS_ERR(inode))
5984 return inode;
5985
5986 /* Do extra check against inode mode with di_type */
5987 if (btrfs_inode_type(inode) != di_type) {
5988 btrfs_crit(fs_info,
5989"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5990 inode->i_mode, btrfs_inode_type(inode),
5991 di_type);
5992 iput(inode);
5993 return ERR_PTR(-EUCLEAN);
5994 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005996 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005997
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005998 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005999 &location, &sub_root);
6000 if (ret < 0) {
6001 if (ret != -ENOENT)
6002 inode = ERR_PTR(ret);
6003 else
6004 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6005 } else {
David Sterba0202e832020-05-15 19:35:59 +02006006 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04006007 }
Josef Bacik87270022020-01-24 09:32:31 -05006008 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05006009 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04006010
Julia Lawall34d19ba2011-01-24 19:55:19 +00006011 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006012 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006013 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006014 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006015 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006016 if (ret) {
6017 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006018 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006019 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006020 }
6021
Chris Mason3de45862008-11-17 21:02:50 -05006022 return inode;
6023}
6024
Nick Pigginfe15ce42011-01-07 17:49:23 +11006025static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006026{
6027 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006028 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006029
Li Zefan848cce02012-02-21 17:04:28 +08006030 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006031 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006032
Li Zefan848cce02012-02-21 17:04:28 +08006033 if (inode) {
6034 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006035 if (btrfs_root_refs(&root->root_item) == 0)
6036 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006037
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006038 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006039 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006040 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006041 return 0;
6042}
6043
Chris Mason3de45862008-11-17 21:02:50 -05006044static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006045 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006046{
Al Viro3837d202018-10-10 16:38:27 -04006047 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04006048
Al Viro3837d202018-10-10 16:38:27 -04006049 if (inode == ERR_PTR(-ENOENT))
6050 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04006051 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006052}
6053
Josef Bacik23b5ec72017-07-24 15:14:25 -04006054/*
6055 * All this infrastructure exists because dir_emit can fault, and we are holding
6056 * the tree lock when doing readdir. For now just allocate a buffer and copy
6057 * our information into that, and then dir_emit from the buffer. This is
6058 * similar to what NFS does, only we don't keep the buffer around in pagecache
6059 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6060 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6061 * tree lock.
6062 */
6063static int btrfs_opendir(struct inode *inode, struct file *file)
6064{
6065 struct btrfs_file_private *private;
6066
6067 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6068 if (!private)
6069 return -ENOMEM;
6070 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6071 if (!private->filldir_buf) {
6072 kfree(private);
6073 return -ENOMEM;
6074 }
6075 file->private_data = private;
6076 return 0;
6077}
6078
6079struct dir_entry {
6080 u64 ino;
6081 u64 offset;
6082 unsigned type;
6083 int name_len;
6084};
6085
6086static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6087{
6088 while (entries--) {
6089 struct dir_entry *entry = addr;
6090 char *name = (char *)(entry + 1);
6091
David Sterba92d32172018-04-16 21:10:14 +02006092 ctx->pos = get_unaligned(&entry->offset);
6093 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6094 get_unaligned(&entry->ino),
6095 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006096 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006097 addr += sizeof(struct dir_entry) +
6098 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006099 ctx->pos++;
6100 }
6101 return 0;
6102}
6103
Al Viro9cdda8d2013-05-22 16:48:09 -04006104static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006105{
Al Viro9cdda8d2013-05-22 16:48:09 -04006106 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006107 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006108 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006109 struct btrfs_dir_item *di;
6110 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006111 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006112 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006113 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006114 struct list_head ins_list;
6115 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006116 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006117 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006118 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006119 char *name_ptr;
6120 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006121 int entries = 0;
6122 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006123 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006124 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006125
Al Viro9cdda8d2013-05-22 16:48:09 -04006126 if (!dir_emit_dots(file, ctx))
6127 return 0;
6128
David Woodhouse49593bf2008-08-17 17:08:36 +01006129 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006130 if (!path)
6131 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006132
Josef Bacik23b5ec72017-07-24 15:14:25 -04006133 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006134 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006135
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006136 INIT_LIST_HEAD(&ins_list);
6137 INIT_LIST_HEAD(&del_list);
6138 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006139
Josef Bacik23b5ec72017-07-24 15:14:25 -04006140again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006141 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006142 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006143 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006144
Chris Mason39279cc2007-06-12 06:35:45 -04006145 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6146 if (ret < 0)
6147 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006148
6149 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006150 struct dir_entry *entry;
6151
Chris Mason5f39d392007-10-15 16:14:19 -04006152 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006153 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006154 if (slot >= btrfs_header_nritems(leaf)) {
6155 ret = btrfs_next_leaf(root, path);
6156 if (ret < 0)
6157 goto err;
6158 else if (ret > 0)
6159 break;
6160 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006161 }
Chris Mason3de45862008-11-17 21:02:50 -05006162
Chris Mason5f39d392007-10-15 16:14:19 -04006163 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6164
6165 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006166 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006167 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006168 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006169 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006170 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006171 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006172 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006173 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006174 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006175 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6176 PAGE_SIZE) {
6177 btrfs_release_path(path);
6178 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6179 if (ret)
6180 goto nopos;
6181 addr = private->filldir_buf;
6182 entries = 0;
6183 total_len = 0;
6184 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006185 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006186
6187 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006188 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006189 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006190 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6191 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006192 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006193 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006194 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006195 put_unaligned(location.objectid, &entry->ino);
6196 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006197 entries++;
6198 addr += sizeof(struct dir_entry) + name_len;
6199 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006200next:
6201 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006202 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006203 btrfs_release_path(path);
6204
6205 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6206 if (ret)
6207 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006208
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006209 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006210 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006211 goto nopos;
6212
Zach Browndb62efb2013-07-11 16:19:42 -07006213 /*
6214 * Stop new entries from being returned after we return the last
6215 * entry.
6216 *
6217 * New directory entries are assigned a strictly increasing
6218 * offset. This means that new entries created during readdir
6219 * are *guaranteed* to be seen in the future by that readdir.
6220 * This has broken buggy programs which operate on names as
6221 * they're returned by readdir. Until we re-use freed offsets
6222 * we have this hack to stop new entries from being returned
6223 * under the assumption that they'll never reach this huge
6224 * offset.
6225 *
6226 * This is being careful not to overflow 32bit loff_t unless the
6227 * last entry requires it because doing so has broken 32bit apps
6228 * in the past.
6229 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006230 if (ctx->pos >= INT_MAX)
6231 ctx->pos = LLONG_MAX;
6232 else
6233 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006234nopos:
6235 ret = 0;
6236err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006237 if (put)
6238 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006239 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006240 return ret;
6241}
6242
Chris Mason39279cc2007-06-12 06:35:45 -04006243/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006244 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006245 * inode changes. But, it is most likely to find the inode in cache.
6246 * FIXME, needs more benchmarking...there are no reasons other than performance
6247 * to keep or drop this code.
6248 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006249static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006250{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006252 struct btrfs_root *root = BTRFS_I(inode)->root;
6253 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006254 int ret;
6255
Josef Bacik72ac3c02012-05-23 14:13:11 -04006256 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006257 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006258
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006259 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006260 if (IS_ERR(trans))
6261 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006262
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006263 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006264 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006265 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006266 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006267 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006268 if (IS_ERR(trans))
6269 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006270
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006271 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006272 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006273 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006274 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006275 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006276
6277 return ret;
6278}
6279
6280/*
6281 * This is a copy of file_update_time. We need this so we can return error on
6282 * ENOSPC for updating the inode in the case of file write and mmap writes.
6283 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006284static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006285 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006286{
Alexander Block2bc5565282012-06-15 09:49:33 +02006287 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006288 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006289
6290 if (btrfs_root_readonly(root))
6291 return -EROFS;
6292
Josef Bacike41f9412012-03-26 09:46:47 -04006293 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006294 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006295 if (flags & S_CTIME)
6296 inode->i_ctime = *now;
6297 if (flags & S_MTIME)
6298 inode->i_mtime = *now;
6299 if (flags & S_ATIME)
6300 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006301 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006302}
6303
Chris Masond352ac62008-09-29 15:18:18 -04006304/*
6305 * find the highest existing sequence number in a directory
6306 * and then set the in-memory index_cnt variable to reflect
6307 * free sequence numbers
6308 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006309static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006310{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006311 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006312 struct btrfs_key key, found_key;
6313 struct btrfs_path *path;
6314 struct extent_buffer *leaf;
6315 int ret;
6316
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006317 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006318 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006319 key.offset = (u64)-1;
6320
6321 path = btrfs_alloc_path();
6322 if (!path)
6323 return -ENOMEM;
6324
6325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6326 if (ret < 0)
6327 goto out;
6328 /* FIXME: we should be able to handle this */
6329 if (ret == 0)
6330 goto out;
6331 ret = 0;
6332
6333 /*
6334 * MAGIC NUMBER EXPLANATION:
6335 * since we search a directory based on f_pos we have to start at 2
6336 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6337 * else has to start at 2
6338 */
6339 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006340 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006341 goto out;
6342 }
6343
6344 path->slots[0]--;
6345
6346 leaf = path->nodes[0];
6347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6348
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006349 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006350 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006351 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006352 goto out;
6353 }
6354
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006355 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006356out:
6357 btrfs_free_path(path);
6358 return ret;
6359}
6360
Chris Masond352ac62008-09-29 15:18:18 -04006361/*
6362 * helper to find a free sequence number in a given directory. This current
6363 * code is very simple, later versions will do smarter things in the btree
6364 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006365int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006366{
6367 int ret = 0;
6368
Nikolay Borisov877574e2017-02-20 13:50:33 +02006369 if (dir->index_cnt == (u64)-1) {
6370 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006371 if (ret) {
6372 ret = btrfs_set_inode_index_count(dir);
6373 if (ret)
6374 return ret;
6375 }
Josef Bacikaec74772008-07-24 12:12:38 -04006376 }
6377
Nikolay Borisov877574e2017-02-20 13:50:33 +02006378 *index = dir->index_cnt;
6379 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006380
6381 return ret;
6382}
6383
Chris Masonb0d5d102014-09-08 13:08:51 -07006384static int btrfs_insert_inode_locked(struct inode *inode)
6385{
6386 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006387
6388 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006389 args.root = BTRFS_I(inode)->root;
6390
6391 return insert_inode_locked4(inode,
6392 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6393 btrfs_find_actor, &args);
6394}
6395
Anand Jain19aee8d2017-07-18 17:37:05 +08006396/*
6397 * Inherit flags from the parent inode.
6398 *
6399 * Currently only the compression flags and the cow flags are inherited.
6400 */
6401static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6402{
6403 unsigned int flags;
6404
6405 if (!dir)
6406 return;
6407
6408 flags = BTRFS_I(dir)->flags;
6409
6410 if (flags & BTRFS_INODE_NOCOMPRESS) {
6411 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6412 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6413 } else if (flags & BTRFS_INODE_COMPRESS) {
6414 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6415 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6416 }
6417
6418 if (flags & BTRFS_INODE_NODATACOW) {
6419 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6420 if (S_ISREG(inode->i_mode))
6421 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6422 }
6423
David Sterba7b6a2212018-03-26 18:40:21 +02006424 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006425}
6426
Chris Mason39279cc2007-06-12 06:35:45 -04006427static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6428 struct btrfs_root *root,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006429 struct user_namespace *mnt_userns,
Josef Bacikaec74772008-07-24 12:12:38 -04006430 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006431 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006432 u64 ref_objectid, u64 objectid,
6433 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006434{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006435 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006436 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006437 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006438 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006439 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006440 struct btrfs_inode_ref *ref;
6441 struct btrfs_key key[2];
6442 u32 sizes[2];
Filipe Mananab7ef5f32021-09-24 12:28:13 +01006443 struct btrfs_item_batch batch;
Chris Mason9c583092008-01-29 15:15:18 -05006444 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006445 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006446 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006447
Chris Mason5f39d392007-10-15 16:14:19 -04006448 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006449 if (!path)
6450 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006451
Josef Bacik11a19a92019-09-09 10:12:04 -04006452 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006453 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006454 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006455 if (!inode) {
6456 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006457 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006458 }
Chris Mason39279cc2007-06-12 06:35:45 -04006459
Li Zefan581bb052011-04-20 10:06:11 +08006460 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006461 * O_TMPFILE, set link count to 0, so that after this point,
6462 * we fill in an inode item with the correct link count.
6463 */
6464 if (!name)
6465 set_nlink(inode, 0);
6466
6467 /*
Li Zefan581bb052011-04-20 10:06:11 +08006468 * we have to initialize this early, so we can reclaim the inode
6469 * number if we fail afterwards in this function.
6470 */
6471 inode->i_ino = objectid;
6472
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006473 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006474 trace_btrfs_inode_request(dir);
6475
Nikolay Borisov877574e2017-02-20 13:50:33 +02006476 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006477 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006478 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006479 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006480 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006481 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006482 } else if (dir) {
6483 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006484 }
6485 /*
6486 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006487 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006488 * number
6489 */
6490 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006491 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006492 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006493 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006494 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006495
Josef Bacik5dc562c2012-08-17 13:14:17 -04006496 /*
6497 * We could have gotten an inode number from somebody who was fsynced
6498 * and then removed in this same transaction, so let's just set full
6499 * sync since it will be a full sync anyway and this will blow away the
6500 * old info in the log.
6501 */
6502 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6503
Chris Mason9c583092008-01-29 15:15:18 -05006504 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006505 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006506 key[0].offset = 0;
6507
Chris Mason9c583092008-01-29 15:15:18 -05006508 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006509
6510 if (name) {
6511 /*
6512 * Start new inodes with an inode_ref. This is slightly more
6513 * efficient for small numbers of hard links since they will
6514 * be packed into one item. Extended refs will kick in if we
6515 * add more hard links than can fit in the ref item.
6516 */
6517 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006518 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006519 key[1].offset = ref_objectid;
6520
6521 sizes[1] = name_len + sizeof(*ref);
6522 }
Chris Mason9c583092008-01-29 15:15:18 -05006523
Chris Masonb0d5d102014-09-08 13:08:51 -07006524 location = &BTRFS_I(inode)->location;
6525 location->objectid = objectid;
6526 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006527 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006528
6529 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006530 if (ret < 0) {
6531 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006532 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006533 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006534
Filipe Mananab7ef5f32021-09-24 12:28:13 +01006535 batch.keys = &key[0];
6536 batch.data_sizes = &sizes[0];
6537 batch.total_data_size = sizes[0] + (name ? sizes[1] : 0);
6538 batch.nr = name ? 2 : 1;
6539 ret = btrfs_insert_empty_items(trans, root, path, &batch);
Chris Mason9c583092008-01-29 15:15:18 -05006540 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006541 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006542
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006543 inode_init_owner(mnt_userns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006544 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306545
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006546 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306547 inode->i_atime = inode->i_mtime;
6548 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006549 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306550
Chris Mason5f39d392007-10-15 16:14:19 -04006551 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6552 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006553 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006554 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006555 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006556
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006557 if (name) {
6558 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6559 struct btrfs_inode_ref);
6560 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6561 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6562 ptr = (unsigned long)(ref + 1);
6563 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6564 }
Chris Mason9c583092008-01-29 15:15:18 -05006565
Chris Mason5f39d392007-10-15 16:14:19 -04006566 btrfs_mark_buffer_dirty(path->nodes[0]);
6567 btrfs_free_path(path);
6568
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006569 btrfs_inherit_iflags(inode, dir);
6570
Al Viro569254b2011-07-24 17:08:40 -04006571 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006572 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006573 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006574 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006575 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6576 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006577 }
6578
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006579 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006580
6581 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006582 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006583
Alexander Block8ea05e32012-07-25 17:35:53 +02006584 btrfs_update_root_times(trans, root);
6585
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006586 ret = btrfs_inode_inherit_props(trans, inode, dir);
6587 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006588 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006589 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006590 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006591
Chris Mason39279cc2007-06-12 06:35:45 -04006592 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006593
6594fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006595 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006596fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006597 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006598 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006599 btrfs_free_path(path);
6600 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006601}
6602
Chris Masond352ac62008-09-29 15:18:18 -04006603/*
6604 * utility function to add 'inode' into 'parent_inode' with
6605 * a give name and a given sequence number.
6606 * if 'add_backref' is true, also insert a backref from the
6607 * inode to the parent directory.
6608 */
Chris Masone02119d2008-09-05 16:13:11 -04006609int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006610 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006611 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006612{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006613 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006614 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006615 struct btrfs_root *root = parent_inode->root;
6616 u64 ino = btrfs_ino(inode);
6617 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006618
Li Zefan33345d012011-04-20 10:31:50 +08006619 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006620 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006621 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006622 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006623 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006624 key.offset = 0;
6625 }
Chris Mason39279cc2007-06-12 06:35:45 -04006626
Li Zefan33345d012011-04-20 10:31:50 +08006627 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006628 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006629 root->root_key.objectid, parent_ino,
6630 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006631 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006632 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6633 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006634 }
6635
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006636 /* Nothing to clean up yet */
6637 if (ret)
6638 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006639
Lu Fengqi684572d2018-08-04 21:10:57 +08006640 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006641 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006642 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006643 goto fail_dir_item;
6644 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006645 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006646 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006647 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006648
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006649 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006650 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006651 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006652 /*
6653 * If we are replaying a log tree, we do not want to update the mtime
6654 * and ctime of the parent directory with the current time, since the
6655 * log replay procedure is responsible for setting them to their correct
6656 * values (the ones it had when the fsync was done).
6657 */
6658 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6659 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6660
6661 parent_inode->vfs_inode.i_mtime = now;
6662 parent_inode->vfs_inode.i_ctime = now;
6663 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006664 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006665 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006666 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006667 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006668
6669fail_dir_item:
6670 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6671 u64 local_index;
6672 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006673 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006674 root->root_key.objectid, parent_ino,
6675 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006676 if (err)
6677 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006678 } else if (add_backref) {
6679 u64 local_index;
6680 int err;
6681
6682 err = btrfs_del_inode_ref(trans, root, name, name_len,
6683 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006684 if (err)
6685 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006686 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006687
6688 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006689 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006690}
6691
6692static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006693 struct btrfs_inode *dir, struct dentry *dentry,
6694 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006695{
Josef Bacika1b075d2010-11-19 20:36:11 +00006696 int err = btrfs_add_link(trans, dir, inode,
6697 dentry->d_name.name, dentry->d_name.len,
6698 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006699 if (err > 0)
6700 err = -EEXIST;
6701 return err;
6702}
6703
Christian Brauner549c7292021-01-21 14:19:43 +01006704static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6705 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006706{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006707 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006708 struct btrfs_trans_handle *trans;
6709 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006710 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006711 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006712 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006713 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006714
Josef Bacik9ed74f22009-09-11 16:12:44 -04006715 /*
6716 * 2 for inode item and ref
6717 * 2 for dir items
6718 * 1 for xattr if selinux is on
6719 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006720 trans = btrfs_start_transaction(root, 5);
6721 if (IS_ERR(trans))
6722 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006723
Nikolay Borisov543068a2020-12-07 17:32:33 +02006724 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006725 if (err)
6726 goto out_unlock;
6727
Christian Brauner72105272021-07-27 12:48:44 +02006728 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006729 dentry->d_name.name, dentry->d_name.len,
6730 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006731 if (IS_ERR(inode)) {
6732 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006733 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006734 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006735 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006736
Casey Schauflerad19db72011-12-15 10:09:07 -05006737 /*
6738 * If the active LSM wants to access the inode during
6739 * d_instantiate it needs these. Smack checks to see
6740 * if the filesystem supports xattrs by looking at the
6741 * ops vector.
6742 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006743 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006744 init_special_inode(inode, inode->i_mode, rdev);
6745
6746 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006747 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006748 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006749
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006750 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6751 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006752 if (err)
6753 goto out_unlock;
6754
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006755 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006756 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006757
Josef Bacik618e21d2007-07-11 10:18:17 -04006758out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006759 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006760 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006761 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006762 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006763 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006764 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006765 return err;
6766}
6767
Christian Brauner549c7292021-01-21 14:19:43 +01006768static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6769 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006770{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006771 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006772 struct btrfs_trans_handle *trans;
6773 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006774 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006775 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006776 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006777 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006778
Josef Bacik9ed74f22009-09-11 16:12:44 -04006779 /*
6780 * 2 for inode item and ref
6781 * 2 for dir items
6782 * 1 for xattr if selinux is on
6783 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006784 trans = btrfs_start_transaction(root, 5);
6785 if (IS_ERR(trans))
6786 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006787
Nikolay Borisov543068a2020-12-07 17:32:33 +02006788 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006789 if (err)
6790 goto out_unlock;
6791
Christian Braunere93ca492021-07-27 12:48:45 +02006792 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006793 dentry->d_name.name, dentry->d_name.len,
6794 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006795 if (IS_ERR(inode)) {
6796 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006797 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006798 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006799 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006800 /*
6801 * If the active LSM wants to access the inode during
6802 * d_instantiate it needs these. Smack checks to see
6803 * if the filesystem supports xattrs by looking at the
6804 * ops vector.
6805 */
6806 inode->i_fop = &btrfs_file_operations;
6807 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006808 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006809
6810 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6811 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006812 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006813
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006814 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006815 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006816 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006817
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006818 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6819 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006820 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006821 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006822
Al Viro1e2e5472018-05-04 08:23:01 -04006823 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006824
Chris Mason39279cc2007-06-12 06:35:45 -04006825out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006826 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006827 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006828 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006829 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006830 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006831 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006832 return err;
6833}
6834
6835static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6836 struct dentry *dentry)
6837{
Filipe Manana271dba452016-01-05 16:24:05 +00006838 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006839 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006840 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006842 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006843 int err;
6844 int drop_inode = 0;
6845
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006846 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006847 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006848 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006849
Mark Fashehf1863732012-08-08 11:32:27 -07006850 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006851 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006852
Nikolay Borisov877574e2017-02-20 13:50:33 +02006853 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006854 if (err)
6855 goto fail;
6856
Yan, Zhenga22285a2010-05-16 10:48:46 -04006857 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006858 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006859 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006860 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006861 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006862 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006863 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006864 if (IS_ERR(trans)) {
6865 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006866 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006867 goto fail;
6868 }
Chris Mason5f39d392007-10-15 16:14:19 -04006869
Miao Xie67de1172013-12-26 13:07:06 +08006870 /* There are several dir indexes for this inode, clear the cache. */
6871 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006872 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006873 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006874 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006875 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006876 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006877
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006878 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6879 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006880
Yan, Zhenga5719522009-09-24 09:17:31 -04006881 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006882 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006883 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006884 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006885
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006886 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006887 if (err)
6888 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006889 if (inode->i_nlink == 1) {
6890 /*
6891 * If new hard link count is 1, it's a file created
6892 * with open(2) O_TMPFILE flag.
6893 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006894 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006895 if (err)
6896 goto fail;
6897 }
Al Viro08c422c2011-12-23 07:58:13 -05006898 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006899 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006900 }
Chris Mason39279cc2007-06-12 06:35:45 -04006901
Chris Mason1832a6d2007-12-21 16:27:21 -05006902fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006903 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006904 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006905 if (drop_inode) {
6906 inode_dec_link_count(inode);
6907 iput(inode);
6908 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006909 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006910 return err;
6911}
6912
Christian Brauner549c7292021-01-21 14:19:43 +01006913static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6914 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006915{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006916 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006917 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006918 struct btrfs_trans_handle *trans;
6919 struct btrfs_root *root = BTRFS_I(dir)->root;
6920 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006921 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006922 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006923
Josef Bacik9ed74f22009-09-11 16:12:44 -04006924 /*
6925 * 2 items for inode and ref
6926 * 2 items for dir items
6927 * 1 for xattr if selinux is on
6928 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006929 trans = btrfs_start_transaction(root, 5);
6930 if (IS_ERR(trans))
6931 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006932
Nikolay Borisov543068a2020-12-07 17:32:33 +02006933 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006934 if (err)
6935 goto out_fail;
6936
Christian Braunerb0b3e442021-07-27 12:48:46 +02006937 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006938 dentry->d_name.name, dentry->d_name.len,
6939 btrfs_ino(BTRFS_I(dir)), objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01006940 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006941 if (IS_ERR(inode)) {
6942 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006943 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006944 goto out_fail;
6945 }
Chris Mason5f39d392007-10-15 16:14:19 -04006946
Chris Masonb0d5d102014-09-08 13:08:51 -07006947 /* these must be set before we unlock the inode */
6948 inode->i_op = &btrfs_dir_inode_operations;
6949 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006950
Eric Paris2a7dba32011-02-01 11:05:39 -05006951 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006952 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006953 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006954
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006955 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006956 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006957 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006958 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006959
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006960 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6961 dentry->d_name.name,
6962 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006963 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006964 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006965
Al Viro1e2e5472018-05-04 08:23:01 -04006966 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006967
6968out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006969 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006970 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006971 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006972 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006973 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006974 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006975 return err;
6976}
6977
Chris Masonc8b97812008-10-29 14:49:59 -04006978static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006979 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006980 size_t pg_offset, u64 extent_offset,
6981 struct btrfs_file_extent_item *item)
6982{
6983 int ret;
6984 struct extent_buffer *leaf = path->nodes[0];
6985 char *tmp;
6986 size_t max_size;
6987 unsigned long inline_size;
6988 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006989 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006990
6991 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006992 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006993 max_size = btrfs_file_extent_ram_bytes(leaf, item);
Josef Bacik437bd072021-10-21 14:58:33 -04006994 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
Chris Masonc8b97812008-10-29 14:49:59 -04006995 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006996 if (!tmp)
6997 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006998 ptr = btrfs_file_extent_inline_start(item);
6999
7000 read_extent_buffer(leaf, tmp, ptr, inline_size);
7001
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007002 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08007003 ret = btrfs_decompress(compress_type, tmp, page,
7004 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05007005
7006 /*
7007 * decompression code contains a memset to fill in any space between the end
7008 * of the uncompressed data and the end of max_size in case the decompressed
7009 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7010 * the end of an inline extent and the beginning of the next block, so we
7011 * cover that region here.
7012 */
7013
Ira Weinyd048b9c2021-05-04 18:40:07 -07007014 if (max_size + pg_offset < PAGE_SIZE)
7015 memzero_page(page, pg_offset + max_size,
7016 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04007017 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007018 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007019}
7020
Omar Sandoval39b07b52019-12-02 17:34:23 -08007021/**
7022 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
7023 * @inode: file to search in
7024 * @page: page to read extent data into if the extent is inline
7025 * @pg_offset: offset into @page to copy to
7026 * @start: file offset
7027 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04007028 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08007029 * This returns the first &struct extent_map which overlaps with the given
7030 * range, reading it from the B-tree and caching it if necessary. Note that
7031 * there may be more extents which overlap the given range after the returned
7032 * extent_map.
7033 *
7034 * If @page is not NULL and the extent is inline, this also reads the extent
7035 * data directly into the page and marks the extent up to date in the io_tree.
7036 *
7037 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04007038 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007039struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08007040 struct page *page, size_t pg_offset,
7041 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04007042{
David Sterba3ffbd682018-06-29 10:56:42 +02007043 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007044 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007045 u64 extent_start = 0;
7046 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007047 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07007048 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04007049 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007050 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007051 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007052 struct extent_buffer *leaf;
7053 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007054 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007055 struct extent_map_tree *em_tree = &inode->extent_tree;
7056 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007057
Chris Mason890871b2009-09-02 16:24:52 -04007058 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007059 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007060 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007061
Chris Masona52d9a82007-08-27 16:49:44 -04007062 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007063 if (em->start > start || em->start + em->len <= start)
7064 free_extent_map(em);
7065 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007066 free_extent_map(em);
7067 else
7068 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007069 }
David Sterba172ddd62011-04-21 00:48:27 +02007070 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007071 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007072 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05007073 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007074 }
Chris Masond1310b22008-01-24 16:13:08 -05007075 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007076 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007077 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007078 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007079
Liu Bobee6ec82018-08-17 05:05:28 +08007080 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04007081 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007082 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08007083 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04007084 }
7085
Liu Bobee6ec82018-08-17 05:05:28 +08007086 /* Chances are we'll be called again, so go ahead and do readahead */
7087 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04007088
7089 /*
7090 * The same explanation in load_free_space_cache applies here as well,
7091 * we only read when we're loading the free space cache, and at that
7092 * point the commit_root has everything we need.
7093 */
7094 if (btrfs_is_free_space_inode(inode)) {
7095 path->search_commit_root = 1;
7096 path->skip_locking = 1;
7097 }
Josef Bacik51899412020-08-20 11:46:01 -04007098
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007099 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007100 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007101 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007102 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007103 if (path->slots[0] == 0)
7104 goto not_found;
7105 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007106 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007107 }
7108
Chris Mason5f39d392007-10-15 16:14:19 -04007109 leaf = path->nodes[0];
7110 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007111 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007112 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007113 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007114 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007115 /*
7116 * If we backup past the first extent we want to move forward
7117 * and see if there is an extent in front of us, otherwise we'll
7118 * say there is a hole for our whole search range which can
7119 * cause problems.
7120 */
7121 extent_end = start;
7122 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007123 }
7124
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007125 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007126 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00007127 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007128 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7129 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007130 /* Only regular file could have regular/prealloc extent */
7131 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007132 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007133 btrfs_crit(fs_info,
7134 "regular/prealloc extent found for non-regular inode %llu",
7135 btrfs_ino(inode));
7136 goto out;
7137 }
Liu Bo09ed2f12017-03-10 11:09:48 -08007138 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7139 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007140 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08007141 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7142 path->slots[0],
7143 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007144 }
Josef Bacik25a50342013-10-14 12:08:38 -04007145next:
Yan Zheng9036c102008-10-30 14:19:41 -04007146 if (start >= extent_end) {
7147 path->slots[0]++;
7148 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7149 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007150 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007151 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007152 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007153 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007154
Yan Zheng9036c102008-10-30 14:19:41 -04007155 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007156 }
Yan Zheng9036c102008-10-30 14:19:41 -04007157 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7158 if (found_key.objectid != objectid ||
7159 found_key.type != BTRFS_EXTENT_DATA_KEY)
7160 goto not_found;
7161 if (start + len <= found_key.offset)
7162 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007163 if (start > found_key.offset)
7164 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007165
7166 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007167 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007168 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007169 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007170 em->block_start = EXTENT_MAP_HOLE;
7171 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007172 }
7173
Omar Sandoval39b07b52019-12-02 17:34:23 -08007174 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007175
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007176 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7177 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007178 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007179 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007180 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007181 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007182 size_t size;
7183 size_t extent_offset;
7184 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007185
Omar Sandoval39b07b52019-12-02 17:34:23 -08007186 if (!page)
Yan689f9342007-10-29 11:41:07 -04007187 goto out;
Yan689f9342007-10-29 11:41:07 -04007188
Qu Wenruoe41ca582018-06-06 15:41:49 +08007189 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007190 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007191 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7192 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007193 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007195 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007196 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007197 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007198
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007199 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007200 if (btrfs_file_extent_compression(leaf, item) !=
7201 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007202 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007203 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007204 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007205 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007206 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007207 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007208 read_extent_buffer(leaf, map + pg_offset, ptr,
7209 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007210 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007211 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007212 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007213 copy_size);
7214 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007215 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007216 }
Chris Mason179e29e2007-11-01 11:28:41 -04007217 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007218 }
Chris Masond1310b22008-01-24 16:13:08 -05007219 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007220 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007221 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007222 }
7223not_found:
7224 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007225 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007226 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007227 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007228insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007229 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007230 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007231 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007232 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007233 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7234 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007235 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007236 goto out;
7237 }
Chris Masond1310b22008-01-24 16:13:08 -05007238
Chris Mason890871b2009-09-02 16:24:52 -04007239 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007240 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007241 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007242out:
Liu Boc6414282018-08-23 07:36:17 +08007243 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007244
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007245 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007246
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007247 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007248 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007249 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007250 }
7251 return em;
7252}
7253
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007254struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007255 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007256{
7257 struct extent_map *em;
7258 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007259 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007260 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007261 u64 delalloc_len;
7262 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007263 int err = 0;
7264
Omar Sandoval39b07b52019-12-02 17:34:23 -08007265 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007266 if (IS_ERR(em))
7267 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007268 /*
7269 * If our em maps to:
7270 * - a hole or
7271 * - a pre-alloc extent,
7272 * there might actually be delalloc bytes behind it.
7273 */
7274 if (em->block_start != EXTENT_MAP_HOLE &&
7275 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7276 return em;
7277 else
7278 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007279
7280 /* check to see if we've wrapped (len == -1 or similar) */
7281 end = start + len;
7282 if (end < start)
7283 end = (u64)-1;
7284 else
7285 end -= 1;
7286
7287 em = NULL;
7288
7289 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007290 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007291 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007292 delalloc_end = delalloc_start + delalloc_len;
7293 if (delalloc_end < delalloc_start)
7294 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007295
7296 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007297 * We didn't find anything useful, return the original results from
7298 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007299 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007300 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007301 em = hole_em;
7302 hole_em = NULL;
7303 goto out;
7304 }
7305
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007306 /*
7307 * Adjust the delalloc_start to make sure it doesn't go backwards from
7308 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007309 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007310 delalloc_start = max(start, delalloc_start);
7311 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007312
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007313 if (delalloc_len > 0) {
7314 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007315 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007316 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007317
David Sterba172ddd62011-04-21 00:48:27 +02007318 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007319 if (!em) {
7320 err = -ENOMEM;
7321 goto out;
7322 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007323
7324 ASSERT(hole_em);
7325 /*
7326 * When btrfs_get_extent can't find anything it returns one
7327 * huge hole
7328 *
7329 * Make sure what it found really fits our range, and adjust to
7330 * make sure it is based on the start from the caller
7331 */
7332 if (hole_end <= start || hole_em->start > end) {
7333 free_extent_map(hole_em);
7334 hole_em = NULL;
7335 } else {
7336 hole_start = max(hole_em->start, start);
7337 hole_len = hole_end - hole_start;
7338 }
7339
7340 if (hole_em && delalloc_start > hole_start) {
7341 /*
7342 * Our hole starts before our delalloc, so we have to
7343 * return just the parts of the hole that go until the
7344 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007345 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007346 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007347 em->start = hole_start;
7348 em->orig_start = hole_start;
7349 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007350 * Don't adjust block start at all, it is fixed at
7351 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007352 */
7353 em->block_start = hole_em->block_start;
7354 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007355 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7356 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007357 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007358 /*
7359 * Hole is out of passed range or it starts after
7360 * delalloc range
7361 */
7362 em->start = delalloc_start;
7363 em->len = delalloc_len;
7364 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007365 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007366 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007367 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007368 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007369 return hole_em;
7370 }
7371out:
7372
7373 free_extent_map(hole_em);
7374 if (err) {
7375 free_extent_map(em);
7376 return ERR_PTR(err);
7377 }
7378 return em;
7379}
7380
Nikolay Borisov64f54182020-06-03 08:55:31 +03007381static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007382 const u64 start,
7383 const u64 len,
7384 const u64 orig_start,
7385 const u64 block_start,
7386 const u64 block_len,
7387 const u64 orig_block_len,
7388 const u64 ram_bytes,
7389 const int type)
7390{
7391 struct extent_map *em = NULL;
7392 int ret;
7393
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007394 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007395 em = create_io_em(inode, start, len, orig_start, block_start,
7396 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007397 BTRFS_COMPRESS_NONE, /* compress_type */
7398 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007399 if (IS_ERR(em))
7400 goto out;
7401 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007402 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7403 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007404 if (ret) {
7405 if (em) {
7406 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007407 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007408 }
7409 em = ERR_PTR(ret);
7410 }
7411 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007412
7413 return em;
7414}
7415
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007416static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417 u64 start, u64 len)
7418{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007419 struct btrfs_root *root = inode->root;
7420 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007421 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007422 struct btrfs_key ins;
7423 u64 alloc_hint;
7424 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007426 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007427 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007428 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007429 if (ret)
7430 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007431
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007432 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007433 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007434 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007435 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007436 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007437 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7438 1);
Josef Bacik00361582013-08-14 14:02:47 -04007439
Josef Bacik4b46fce2010-05-23 11:00:55 -04007440 return em;
7441}
7442
Anand Jainf4639632021-02-10 21:25:16 -08007443static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007444{
7445 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007446 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007447
7448 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7449 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007450 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007451 if (block_group)
7452 btrfs_put_block_group(block_group);
7453 return readonly;
7454}
7455
Chris Mason46bfbb52010-05-26 11:04:10 -04007456/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007457 * Check if we can do nocow write into the range [@offset, @offset + @len)
7458 *
7459 * @offset: File offset
7460 * @len: The length to write, will be updated to the nocow writeable
7461 * range
7462 * @orig_start: (optional) Return the original file offset of the file extent
7463 * @orig_len: (optional) Return the original on-disk length of the file extent
7464 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007465 * @strict: if true, omit optimizations that might force us into unnecessary
7466 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007467 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007468 * Return:
7469 * >0 and update @len if we can do nocow write
7470 * 0 if we can't do nocow write
7471 * <0 if error happened
7472 *
7473 * NOTE: This only checks the file extents, caller is responsible to wait for
7474 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007475 */
Josef Bacik00361582013-08-14 14:02:47 -04007476noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007477 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007478 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007479{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007481 struct btrfs_path *path;
7482 int ret;
7483 struct extent_buffer *leaf;
7484 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007485 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007486 struct btrfs_file_extent_item *fi;
7487 struct btrfs_key key;
7488 u64 disk_bytenr;
7489 u64 backref_offset;
7490 u64 extent_end;
7491 u64 num_bytes;
7492 int slot;
7493 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007494 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007495
Chris Mason46bfbb52010-05-26 11:04:10 -04007496 path = btrfs_alloc_path();
7497 if (!path)
7498 return -ENOMEM;
7499
David Sterbaf85b7372017-01-20 14:54:07 +01007500 ret = btrfs_lookup_file_extent(NULL, root, path,
7501 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007502 if (ret < 0)
7503 goto out;
7504
7505 slot = path->slots[0];
7506 if (ret == 1) {
7507 if (slot == 0) {
7508 /* can't find the item, must cow */
7509 ret = 0;
7510 goto out;
7511 }
7512 slot--;
7513 }
7514 ret = 0;
7515 leaf = path->nodes[0];
7516 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007517 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007518 key.type != BTRFS_EXTENT_DATA_KEY) {
7519 /* not our file or wrong item type, must cow */
7520 goto out;
7521 }
7522
7523 if (key.offset > offset) {
7524 /* Wrong offset, must cow */
7525 goto out;
7526 }
7527
7528 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7529 found_type = btrfs_file_extent_type(leaf, fi);
7530 if (found_type != BTRFS_FILE_EXTENT_REG &&
7531 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7532 /* not a regular extent, must cow */
7533 goto out;
7534 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007535
7536 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7537 goto out;
7538
Miao Xiee77751a2013-12-27 21:11:50 +08007539 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7540 if (extent_end <= offset)
7541 goto out;
7542
Chris Mason46bfbb52010-05-26 11:04:10 -04007543 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007544 if (disk_bytenr == 0)
7545 goto out;
7546
7547 if (btrfs_file_extent_compression(leaf, fi) ||
7548 btrfs_file_extent_encryption(leaf, fi) ||
7549 btrfs_file_extent_other_encoding(leaf, fi))
7550 goto out;
7551
Ethan Lien78d42952018-05-17 14:58:29 +08007552 /*
7553 * Do the same check as in btrfs_cross_ref_exist but without the
7554 * unnecessary search.
7555 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007556 if (!strict &&
7557 (btrfs_file_extent_generation(leaf, fi) <=
7558 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007559 goto out;
7560
Chris Mason46bfbb52010-05-26 11:04:10 -04007561 backref_offset = btrfs_file_extent_offset(leaf, fi);
7562
Josef Bacik7ee9e442013-06-21 16:37:03 -04007563 if (orig_start) {
7564 *orig_start = key.offset - backref_offset;
7565 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7566 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7567 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007569 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007570 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007571
7572 num_bytes = min(offset + *len, extent_end) - offset;
7573 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7574 u64 range_end;
7575
Jeff Mahoneyda170662016-06-15 09:22:56 -04007576 range_end = round_up(offset + num_bytes,
7577 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007578 ret = test_range_bit(io_tree, offset, range_end,
7579 EXTENT_DELALLOC, 0, NULL);
7580 if (ret) {
7581 ret = -EAGAIN;
7582 goto out;
7583 }
7584 }
7585
Josef Bacik1bda19e2013-10-18 12:10:36 -04007586 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007587
7588 /*
7589 * look for other files referencing this extent, if we
7590 * find any we must cow
7591 */
Josef Bacik00361582013-08-14 14:02:47 -04007592
Liu Boe4c3b2d2017-01-30 12:25:28 -08007593 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007594 key.offset - backref_offset, disk_bytenr,
7595 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007596 if (ret) {
7597 ret = 0;
7598 goto out;
7599 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007600
7601 /*
7602 * adjust disk_bytenr and num_bytes to cover just the bytes
7603 * in this extent we are about to write. If there
7604 * are any csums in that range we have to cow in order
7605 * to keep the csums correct
7606 */
7607 disk_bytenr += backref_offset;
7608 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007609 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7610 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007611 /*
7612 * all of the above have passed, it is safe to overwrite this extent
7613 * without cow
7614 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007615 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007616 ret = 1;
7617out:
7618 btrfs_free_path(path);
7619 return ret;
7620}
7621
Josef Bacikeb838e72012-07-31 16:28:48 -04007622static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007623 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007624{
7625 struct btrfs_ordered_extent *ordered;
7626 int ret = 0;
7627
7628 while (1) {
7629 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007630 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007631 /*
7632 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007633 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007634 * extents in this range.
7635 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007636 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007637 lockend - lockstart + 1);
7638
7639 /*
7640 * We need to make sure there are no buffered pages in this
7641 * range either, we could have raced between the invalidate in
7642 * generic_file_direct_write and locking the extent. The
7643 * invalidate needs to happen so that reads after a write do not
7644 * get stale data.
7645 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007646 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007647 (!writing || !filemap_range_has_page(inode->i_mapping,
7648 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007649 break;
7650
7651 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007652 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007653
7654 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007655 /*
7656 * If we are doing a DIO read and the ordered extent we
7657 * found is for a buffered write, we can not wait for it
7658 * to complete and retry, because if we do so we can
7659 * deadlock with concurrent buffered writes on page
7660 * locks. This happens only if our DIO read covers more
7661 * than one extent map, if at this point has already
7662 * created an ordered extent for a previous extent map
7663 * and locked its range in the inode's io tree, and a
7664 * concurrent write against that previous extent map's
7665 * range and this range started (we unlock the ranges
7666 * in the io tree only when the bios complete and
7667 * buffered writes always lock pages before attempting
7668 * to lock range in the io tree).
7669 */
7670 if (writing ||
7671 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007672 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007673 else
7674 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 btrfs_put_ordered_extent(ordered);
7676 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007678 * We could trigger writeback for this range (and wait
7679 * for it to complete) and then invalidate the pages for
7680 * this range (through invalidate_inode_pages2_range()),
7681 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007682 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007683 * triggered a readahead) on a page lock due to an
7684 * ordered dio extent we created before but did not have
7685 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007686 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007687 * ordered extent to complete while holding a lock on
7688 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007689 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007690 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007691 }
7692
Filipe Mananaade77022016-02-18 14:28:55 +00007693 if (ret)
7694 break;
7695
Josef Bacikeb838e72012-07-31 16:28:48 -04007696 cond_resched();
7697 }
7698
7699 return ret;
7700}
7701
Liu Bo6f9994d2017-01-31 07:50:22 -08007702/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007703static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7704 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007705 u64 block_len, u64 orig_block_len,
7706 u64 ram_bytes, int compress_type,
7707 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007708{
7709 struct extent_map_tree *em_tree;
7710 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007711 int ret;
7712
Liu Bo6f9994d2017-01-31 07:50:22 -08007713 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7714 type == BTRFS_ORDERED_COMPRESSED ||
7715 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007716 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007717
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007718 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007719 em = alloc_extent_map();
7720 if (!em)
7721 return ERR_PTR(-ENOMEM);
7722
7723 em->start = start;
7724 em->orig_start = orig_start;
7725 em->len = len;
7726 em->block_len = block_len;
7727 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007728 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007729 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007730 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007731 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007732 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007733 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007734 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007735 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7736 em->compress_type = compress_type;
7737 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007738
7739 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007740 btrfs_drop_extent_cache(inode, em->start,
7741 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007743 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007744 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007745 /*
7746 * The caller has taken lock_extent(), who could race with us
7747 * to add em?
7748 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007749 } while (ret == -EEXIST);
7750
7751 if (ret) {
7752 free_extent_map(em);
7753 return ERR_PTR(ret);
7754 }
7755
Liu Bo6f9994d2017-01-31 07:50:22 -08007756 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007757 return em;
7758}
7759
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007760
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007761static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007762 struct inode *inode,
7763 struct btrfs_dio_data *dio_data,
7764 u64 start, u64 len)
7765{
7766 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7767 struct extent_map *em = *map;
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007768 int type;
7769 u64 block_start, orig_start, orig_block_len, ram_bytes;
7770 bool can_nocow = false;
7771 bool space_reserved = false;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007772 int ret = 0;
7773
7774 /*
7775 * We don't allocate a new extent in the following cases
7776 *
7777 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7778 * existing extent.
7779 * 2) The extent is marked as PREALLOC. We're good to go here and can
7780 * just use the extent.
7781 *
7782 */
7783 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7784 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7785 em->block_start != EXTENT_MAP_HOLE)) {
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007786 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7787 type = BTRFS_ORDERED_PREALLOC;
7788 else
7789 type = BTRFS_ORDERED_NOCOW;
7790 len = min(len, em->len - (start - em->start));
7791 block_start = em->block_start + (start - em->start);
7792
7793 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007794 &orig_block_len, &ram_bytes, false) == 1 &&
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007795 btrfs_inc_nocow_writers(fs_info, block_start))
7796 can_nocow = true;
7797 }
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007798
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007799 if (can_nocow) {
7800 struct extent_map *em2;
7801
7802 /* We can NOCOW, so only need to reserve metadata space. */
7803 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
7804 if (ret < 0) {
7805 /* Our caller expects us to free the input extent map. */
7806 free_extent_map(em);
7807 *map = NULL;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007808 btrfs_dec_nocow_writers(fs_info, block_start);
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007809 goto out;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007810 }
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007811 space_reserved = true;
7812
7813 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
7814 orig_start, block_start,
7815 len, orig_block_len,
7816 ram_bytes, type);
7817 btrfs_dec_nocow_writers(fs_info, block_start);
7818 if (type == BTRFS_ORDERED_PREALLOC) {
7819 free_extent_map(em);
7820 *map = em = em2;
7821 }
7822
7823 if (IS_ERR(em2)) {
7824 ret = PTR_ERR(em2);
7825 goto out;
7826 }
7827 } else {
7828 const u64 prev_len = len;
7829
7830 /* Our caller expects us to free the input extent map. */
7831 free_extent_map(em);
7832 *map = NULL;
7833
7834 /* We have to COW, so need to reserve metadata and data space. */
7835 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7836 &dio_data->data_reserved,
7837 start, len);
7838 if (ret < 0)
7839 goto out;
7840 space_reserved = true;
7841
7842 em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
7843 if (IS_ERR(em)) {
7844 ret = PTR_ERR(em);
7845 goto out;
7846 }
7847 *map = em;
7848 len = min(len, em->len - (start - em->start));
7849 if (len < prev_len)
7850 btrfs_delalloc_release_space(BTRFS_I(inode),
7851 dio_data->data_reserved,
7852 start + len, prev_len - len,
7853 true);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007854 }
7855
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007856 /*
7857 * We have created our ordered extent, so we can now release our reservation
7858 * for an outstanding extent.
7859 */
7860 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007861
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007862 /*
7863 * Need to update the i_size under the extent lock so buffered
7864 * readers will get the updated i_size when we unlock.
7865 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007866 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007867 i_size_write(inode, start + len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007868out:
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007869 if (ret && space_reserved) {
7870 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
7871 if (can_nocow) {
7872 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
7873 } else {
7874 btrfs_delalloc_release_space(BTRFS_I(inode),
7875 dio_data->data_reserved,
7876 start, len, true);
7877 extent_changeset_free(dio_data->data_reserved);
7878 dio_data->data_reserved = NULL;
7879 }
7880 }
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007881 return ret;
7882}
7883
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007884static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7885 loff_t length, unsigned int flags, struct iomap *iomap,
7886 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007887{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307891 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007892 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007893 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007894 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007895 u64 len = length;
7896 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007897
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007898 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007899 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007900
Josef Bacikc3298612012-08-03 16:49:19 -04007901 lockstart = start;
7902 lockend = start + len - 1;
7903
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007904 /*
7905 * The generic stuff only does filemap_write_and_wait_range, which
7906 * isn't enough if we've written compressed pages to this area, so we
7907 * need to flush the dirty pages again to make absolutely sure that any
7908 * outstanding dirty pages are on disk.
7909 */
7910 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7911 &BTRFS_I(inode)->runtime_flags)) {
7912 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7913 start + length - 1);
7914 if (ret)
7915 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007916 }
7917
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007918 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7919 if (!dio_data)
7920 return -ENOMEM;
7921
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007922 iomap->private = dio_data;
7923
7924
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 /*
7926 * If this errors out it's because we couldn't invalidate pagecache for
7927 * this range and we need to fallback to buffered.
7928 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007929 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007930 ret = -ENOTBLK;
7931 goto err;
7932 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007933
Omar Sandoval39b07b52019-12-02 17:34:23 -08007934 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007935 if (IS_ERR(em)) {
7936 ret = PTR_ERR(em);
7937 goto unlock_err;
7938 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007939
7940 /*
7941 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7942 * io. INLINE is special, and we could probably kludge it in here, but
7943 * it's still buffered so for safety lets just fall back to the generic
7944 * buffered path.
7945 *
7946 * For COMPRESSED we _have_ to read the entire extent in so we can
7947 * decompress it, so there will be buffering required no matter what we
7948 * do, so go ahead and fallback to buffered.
7949 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007950 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007951 * to buffered IO. Don't blame me, this is the price we pay for using
7952 * the generic code.
7953 */
7954 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7955 em->block_start == EXTENT_MAP_INLINE) {
7956 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007957 ret = -ENOTBLK;
7958 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007959 }
7960
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007961 len = min(len, em->len - (start - em->start));
7962 if (write) {
7963 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7964 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007965 if (ret < 0)
7966 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007967 unlock_extents = true;
7968 /* Recalc len in case the new em is smaller than requested */
7969 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007970 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007971 /*
7972 * We need to unlock only the end area that we aren't using.
7973 * The rest is going to be unlocked by the endio routine.
7974 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007975 lockstart = start + len;
7976 if (lockstart < lockend)
7977 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007978 }
7979
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007980 if (unlock_extents)
7981 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7982 lockstart, lockend, &cached_state);
7983 else
7984 free_extent_state(cached_state);
7985
7986 /*
7987 * Translate extent map information to iomap.
7988 * We trim the extents (and move the addr) even though iomap code does
7989 * that, since we have locked only the parts we are performing I/O in.
7990 */
7991 if ((em->block_start == EXTENT_MAP_HOLE) ||
7992 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7993 iomap->addr = IOMAP_NULL_ADDR;
7994 iomap->type = IOMAP_HOLE;
7995 } else {
7996 iomap->addr = em->block_start + (start - em->start);
7997 iomap->type = IOMAP_MAPPED;
7998 }
7999 iomap->offset = start;
Anand Jaind24fa5c2021-08-24 13:05:19 +08008000 iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008001 iomap->length = len;
8002
Johannes Thumshirne380adf2021-05-19 00:40:27 +09008003 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09008004 iomap->flags |= IOMAP_F_ZONE_APPEND;
8005
Josef Bacik4b46fce2010-05-23 11:00:55 -04008006 free_extent_map(em);
8007
8008 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04008009
8010unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07008011 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
8012 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00008013err:
Filipe Mananaf0bfa762021-10-28 16:03:41 +01008014 kfree(dio_data);
8015
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008016 return ret;
8017}
8018
8019static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
8020 ssize_t written, unsigned int flags, struct iomap *iomap)
8021{
8022 int ret = 0;
8023 struct btrfs_dio_data *dio_data = iomap->private;
8024 size_t submitted = dio_data->submitted;
8025 const bool write = !!(flags & IOMAP_WRITE);
8026
8027 if (!write && (iomap->type == IOMAP_HOLE)) {
8028 /* If reading from a hole, unlock and return */
8029 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
8030 goto out;
8031 }
8032
8033 if (submitted < length) {
8034 pos += submitted;
8035 length -= submitted;
8036 if (write)
8037 __endio_write_update_ordered(BTRFS_I(inode), pos,
8038 length, false);
8039 else
8040 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
8041 pos + length - 1);
8042 ret = -ENOTBLK;
8043 }
8044
Filipe Mananaf0bfa762021-10-28 16:03:41 +01008045 if (write)
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008046 extent_changeset_free(dio_data->data_reserved);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008047out:
8048 kfree(dio_data);
8049 iomap->private = NULL;
8050
Josef Bacikeb838e72012-07-31 16:28:48 -04008051 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008052}
8053
Omar Sandoval769b4f22020-04-16 14:46:22 -07008054static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8055{
8056 /*
8057 * This implies a barrier so that stores to dio_bio->bi_status before
8058 * this and loads of dio_bio->bi_status after this are fully ordered.
8059 */
8060 if (!refcount_dec_and_test(&dip->refs))
8061 return;
8062
Naohiro Aotacfe94442021-02-04 19:21:59 +09008063 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008064 __endio_write_update_ordered(BTRFS_I(dip->inode),
Qu Wenruo47926ab2021-10-08 15:29:59 +08008065 dip->file_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07008066 dip->bytes,
8067 !dip->dio_bio->bi_status);
8068 } else {
8069 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
Qu Wenruo47926ab2021-10-08 15:29:59 +08008070 dip->file_offset,
8071 dip->file_offset + dip->bytes - 1);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008072 }
8073
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008074 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008075 kfree(dip);
8076}
8077
Omar Sandoval77d5d682020-04-16 14:46:25 -07008078static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8079 int mirror_num,
8080 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08008081{
Omar Sandoval77d5d682020-04-16 14:46:25 -07008082 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008083 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008084 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008085
Mike Christie37226b22016-06-05 14:31:52 -05008086 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008087
Omar Sandoval5c047a62020-04-16 14:46:24 -07008088 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08008089 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02008090 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008091
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008092 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07008093 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07008094 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008095 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08008096 return ret;
8097}
8098
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008099static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008100 struct btrfs_bio *bbio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008101 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08008102{
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008103 struct inode *inode = dip->inode;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008104 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8105 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04008106 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008107 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8108 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07008109 struct bio_vec bvec;
8110 struct bvec_iter iter;
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008111 const u64 orig_file_offset = dip->file_offset;
8112 u64 start = orig_file_offset;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008113 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008114 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008115
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008116 __bio_for_each_segment(bvec, &bbio->bio, iter, bbio->iter) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008117 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118
Liu Bo17347ce2017-05-15 15:33:27 -07008119 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8120 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008121 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07008122 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008123 if (uptodate &&
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008124 (!csum || !check_data_csum(inode, bbio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07008125 bio_offset, bvec.bv_page,
8126 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008127 clean_io_failure(fs_info, failure_tree, io_tree,
8128 start, bvec.bv_page,
8129 btrfs_ino(BTRFS_I(inode)),
8130 pgoff);
8131 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08008132 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008133
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008134 ASSERT((start - orig_file_offset) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08008135 ret = btrfs_repair_one_sector(inode,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008136 &bbio->bio,
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008137 start - orig_file_offset,
Qu Wenruo150e4b02021-05-03 10:08:55 +08008138 bvec.bv_page, pgoff,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008139 start, bbio->mirror_num,
Qu Wenruo150e4b02021-05-03 10:08:55 +08008140 submit_dio_repair_bio);
8141 if (ret)
8142 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008143 }
8144 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008145 ASSERT(bio_offset + sectorsize > bio_offset);
8146 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308147 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308148 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008149 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008150 return err;
8151}
8152
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008153static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08008154 const u64 offset, const u64 bytes,
8155 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008156{
Qu Wenruoe65f1522021-04-01 15:15:06 +08008157 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8158 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00008159}
8160
Qu Wenruo8896a082020-10-21 14:24:53 +08008161static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008162 struct bio *bio,
8163 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008164{
Qu Wenruo1941b642020-12-02 14:47:57 +08008165 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008166}
8167
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008168static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008169{
8170 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008171 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008172
Miao Xie8b110e32014-09-12 18:44:03 +08008173 if (err)
8174 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008175 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008176 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008177 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008178 bio->bi_iter.bi_size, err);
8179
Qu Wenruof4f39fc2021-10-08 15:30:00 +08008180 if (bio_op(bio) == REQ_OP_READ)
8181 err = btrfs_check_read_dio_bio(dip, btrfs_bio(bio), !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008182
Omar Sandoval769b4f22020-04-16 14:46:22 -07008183 if (err)
8184 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008185
Qu Wenruo47926ab2021-10-08 15:29:59 +08008186 btrfs_record_physical_zoned(dip->inode, dip->file_offset, bio);
Naohiro Aota544d24f2021-02-04 19:22:06 +09008187
Miao Xiee65e1532010-11-22 03:04:43 +00008188 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008189 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008190}
8191
David Sterbad0ee3932018-03-08 14:35:48 +01008192static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8193 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008194{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008195 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008196 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008197 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008198 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008199
Liu Bo4c274bc2017-11-01 17:19:27 -06008200 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008201 if (async_submit)
8202 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8203
Josef Bacik5fd02042012-05-02 14:00:54 -04008204 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008205 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008206 if (ret)
8207 goto err;
8208 }
Miao Xiee65e1532010-11-22 03:04:43 +00008209
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008210 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008211 goto map;
8212
8213 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008214 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008215 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008216 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008217 } else if (write) {
8218 /*
8219 * If we aren't doing async submit, calculate the csum of the
8220 * bio now.
8221 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008222 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008223 if (ret)
8224 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008225 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008226 u64 csum_offset;
8227
Qu Wenruo47926ab2021-10-08 15:29:59 +08008228 csum_offset = file_offset - dip->file_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008229 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008230 csum_offset *= fs_info->csum_size;
Qu Wenruoc3a3b192021-09-15 15:17:18 +08008231 btrfs_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008232 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008233map:
Chris Mason08635ba2019-07-10 12:28:14 -07008234 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008235err:
Miao Xiee65e1532010-11-22 03:04:43 +00008236 return ret;
8237}
8238
Omar Sandovalc36cac22020-04-16 14:46:13 -07008239/*
8240 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8241 * or ordered extents whether or not we submit any bios.
8242 */
8243static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8244 struct inode *inode,
8245 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008246{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008247 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008248 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8249 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008250 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008251
Omar Sandoval85879572020-04-16 14:46:21 -07008252 dip_size = sizeof(*dip);
8253 if (!write && csum) {
8254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008255 size_t nblocks;
8256
David Sterba265fdfa2020-07-01 21:19:09 +02008257 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008258 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008259 }
8260
8261 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008262 if (!dip)
8263 return NULL;
8264
Omar Sandovalc36cac22020-04-16 14:46:13 -07008265 dip->inode = inode;
Qu Wenruo47926ab2021-10-08 15:29:59 +08008266 dip->file_offset = file_offset;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008267 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008268 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008269 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008270 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008271 return dip;
8272}
8273
Christoph Hellwig3e087732021-10-12 13:12:24 +02008274static void btrfs_submit_direct(const struct iomap_iter *iter,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008275 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008276{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07008277 struct inode *inode = iter->inode;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008278 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008280 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8281 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008282 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008283 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008284 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008285 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008286 u64 submit_len;
Naohiro Aota42b5d732021-07-21 21:43:34 +09008287 u64 clone_offset = 0;
8288 u64 clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008289 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308290 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008291 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008292 struct btrfs_io_geometry geom;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07008293 struct btrfs_dio_data *dio_data = iter->iomap.private;
Michal Rostecki42034312021-01-27 14:57:27 +01008294 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008295
Omar Sandovalc36cac22020-04-16 14:46:13 -07008296 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8297 if (!dip) {
8298 if (!write) {
8299 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8300 file_offset + dio_bio->bi_iter.bi_size - 1);
8301 }
8302 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008303 bio_endio(dio_bio);
Christoph Hellwig3e087732021-10-12 13:12:24 +02008304 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04008305 }
8306
Josef Bacik334c16d2020-10-16 11:29:14 -04008307 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008308 /*
8309 * Load the csums up front to reduce csum tree searches and
8310 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008311 *
8312 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008313 */
Qu Wenruo62751932020-12-02 14:48:06 +08008314 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008315 if (status != BLK_STS_OK)
8316 goto out_err;
8317 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008318
Omar Sandoval769b4f22020-04-16 14:46:22 -07008319 start_sector = dio_bio->bi_iter.bi_sector;
8320 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008321
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008322 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008323 logical = start_sector << 9;
8324 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8325 if (IS_ERR(em)) {
8326 status = errno_to_blk_status(PTR_ERR(em));
8327 em = NULL;
8328 goto out_err_em;
8329 }
8330 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008331 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008332 if (ret) {
8333 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008334 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008335 }
Omar Sandoval769b4f22020-04-16 14:46:22 -07008336
Naohiro Aota42b5d732021-07-21 21:43:34 +09008337 clone_len = min(submit_len, geom.len);
8338 ASSERT(clone_len <= UINT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008339
Liu Bo725130b2017-05-16 09:51:39 -07008340 /*
8341 * This will never fail as it's passing GPF_NOFS and
8342 * the allocation is backed by btrfs_bioset.
8343 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008344 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008345 bio->bi_private = dip;
8346 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008347
Naohiro Aota544d24f2021-02-04 19:22:06 +09008348 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8349 status = extract_ordered_extent(BTRFS_I(inode), bio,
8350 file_offset);
8351 if (status) {
8352 bio_put(bio);
8353 goto out_err;
8354 }
8355 }
8356
Liu Bo725130b2017-05-16 09:51:39 -07008357 ASSERT(submit_len >= clone_len);
8358 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008359
Liu Bo725130b2017-05-16 09:51:39 -07008360 /*
8361 * Increase the count before we submit the bio so we know
8362 * the end IO handler won't happen before we increase the
8363 * count. Otherwise, the dip might get freed before we're
8364 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008365 *
8366 * We transfer the initial reference to the last bio, so we
8367 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008368 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008369 if (submit_len > 0) {
8370 refcount_inc(&dip->refs);
8371 /*
8372 * If we are submitting more than one bio, submit them
8373 * all asynchronously. The exception is RAID 5 or 6, as
8374 * asynchronous checksums make it difficult to collect
8375 * full stripe writes.
8376 */
8377 if (!raid56)
8378 async_submit = 1;
8379 }
Miao Xiee65e1532010-11-22 03:04:43 +00008380
David Sterbad0ee3932018-03-08 14:35:48 +01008381 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008382 async_submit);
8383 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008384 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008385 if (submit_len > 0)
8386 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008387 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008388 }
Liu Bo725130b2017-05-16 09:51:39 -07008389
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008390 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008391 clone_offset += clone_len;
8392 start_sector += clone_len >> 9;
8393 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008394
8395 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008396 } while (submit_len > 0);
Christoph Hellwig3e087732021-10-12 13:12:24 +02008397 return;
Miao Xiee65e1532010-11-22 03:04:43 +00008398
Michal Rostecki42034312021-01-27 14:57:27 +01008399out_err_em:
8400 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008401out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008402 dip->dio_bio->bi_status = status;
8403 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008404}
8405
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008406const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008407 .iomap_begin = btrfs_dio_iomap_begin,
8408 .iomap_end = btrfs_dio_iomap_end,
8409};
8410
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008411const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008412 .submit_io = btrfs_submit_direct,
8413};
8414
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008415static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008416 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008417{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008418 int ret;
8419
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008420 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008421 if (ret)
8422 return ret;
8423
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008424 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008425}
8426
Chris Mason9ebefb182007-06-15 13:50:00 -04008427int btrfs_readpage(struct file *file, struct page *page)
8428{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008429 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8430 u64 start = page_offset(page);
8431 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008432 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008433 int ret;
8434
Nikolay Borisov0f208812020-09-14 14:39:16 +03008435 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8436
Qu Wenruo390ed292021-04-14 16:42:15 +08008437 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8438 if (bio_ctrl.bio)
8439 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008440 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008441}
Chris Mason1832a6d2007-12-21 16:27:21 -05008442
Chris Mason39279cc2007-06-12 06:35:45 -04008443static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8444{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008445 struct inode *inode = page->mapping->host;
8446 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008447
8448 if (current->flags & PF_MEMALLOC) {
8449 redirty_page_for_writepage(wbc, page);
8450 unlock_page(page);
8451 return 0;
8452 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008453
8454 /*
8455 * If we are under memory pressure we will call this directly from the
8456 * VM, we need to make sure we have the inode referenced for the ordered
8457 * extent. If not just return like we didn't do anything.
8458 */
8459 if (!igrab(inode)) {
8460 redirty_page_for_writepage(wbc, page);
8461 return AOP_WRITEPAGE_ACTIVATE;
8462 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008463 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008464 btrfs_add_delayed_iput(inode);
8465 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008466}
Chris Mason39279cc2007-06-12 06:35:45 -04008467
Eric Sandeen48a3b632013-04-25 20:41:01 +00008468static int btrfs_writepages(struct address_space *mapping,
8469 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008470{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008471 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008472}
8473
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008474static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008475{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008476 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008477}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008478
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008479/*
8480 * For releasepage() and invalidatepage() we have a race window where
8481 * end_page_writeback() is called but the subpage spinlock is not yet released.
8482 * If we continue to release/invalidate the page, we could cause use-after-free
8483 * for subpage spinlock. So this function is to spin and wait for subpage
8484 * spinlock.
8485 */
8486static void wait_subpage_spinlock(struct page *page)
8487{
8488 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8489 struct btrfs_subpage *subpage;
8490
8491 if (fs_info->sectorsize == PAGE_SIZE)
8492 return;
8493
8494 ASSERT(PagePrivate(page) && page->private);
8495 subpage = (struct btrfs_subpage *)page->private;
8496
8497 /*
8498 * This may look insane as we just acquire the spinlock and release it,
8499 * without doing anything. But we just want to make sure no one is
8500 * still holding the subpage spinlock.
8501 * And since the page is not dirty nor writeback, and we have page
8502 * locked, the only possible way to hold a spinlock is from the endio
8503 * function to clear page writeback.
8504 *
8505 * Here we just acquire the spinlock so that all existing callers
8506 * should exit and we're safe to release/invalidate the page.
8507 */
8508 spin_lock_irq(&subpage->lock);
8509 spin_unlock_irq(&subpage->lock);
8510}
8511
Chris Masone6dcd2d2008-07-17 12:53:50 -04008512static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008513{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008514 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008515
8516 if (ret == 1) {
8517 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008518 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008519 }
Chris Masona52d9a82007-08-27 16:49:44 -04008520 return ret;
8521}
Chris Mason39279cc2007-06-12 06:35:45 -04008522
Chris Masone6dcd2d2008-07-17 12:53:50 -04008523static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8524{
Chris Mason98509cf2008-09-11 15:51:43 -04008525 if (PageWriteback(page) || PageDirty(page))
8526 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008527 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008528}
8529
Roman Gushchinf8e66082020-03-04 16:57:35 -08008530#ifdef CONFIG_MIGRATION
8531static int btrfs_migratepage(struct address_space *mapping,
8532 struct page *newpage, struct page *page,
8533 enum migrate_mode mode)
8534{
8535 int ret;
8536
8537 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8538 if (ret != MIGRATEPAGE_SUCCESS)
8539 return ret;
8540
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008541 if (page_has_private(page))
8542 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008543
Qu Wenruof57ad932021-04-07 19:22:13 +08008544 if (PageOrdered(page)) {
8545 ClearPageOrdered(page);
8546 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008547 }
8548
8549 if (mode != MIGRATE_SYNC_NO_COPY)
8550 migrate_page_copy(newpage, page);
8551 else
8552 migrate_page_states(newpage, page);
8553 return MIGRATEPAGE_SUCCESS;
8554}
8555#endif
8556
Lukas Czernerd47992f2013-05-21 23:17:23 -04008557static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8558 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008559{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008560 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008561 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008562 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008563 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008564 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008565 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008566 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008567 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008568
Chris Mason8b62b722009-09-02 16:53:46 -04008569 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008570 * We have page locked so no new ordered extent can be created on this
8571 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008572 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008573 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008574 * Furthermore, endio function won't skip page which has Ordered
8575 * (Private2) already cleared, so it's possible for endio and
8576 * invalidatepage to do the same ordered extent accounting twice
8577 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008578 *
8579 * So here we wait for any submitted bios to finish, so that we won't
8580 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008581 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008582 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008583 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008584
Qu Wenruobcd77452021-05-31 16:50:55 +08008585 /*
8586 * For subpage case, we have call sites like
8587 * btrfs_punch_hole_lock_range() which passes range not aligned to
8588 * sectorsize.
8589 * If the range doesn't cover the full page, we don't need to and
8590 * shouldn't clear page extent mapped, as page->private can still
8591 * record subpage dirty bits for other part of the range.
8592 *
8593 * For cases that can invalidate the full even the range doesn't
8594 * cover the full page, like invalidating the last page, we're
8595 * still safe to wait for ordered extent to finish.
8596 */
8597 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008598 btrfs_releasepage(page, GFP_NOFS);
8599 return;
8600 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008601
8602 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008603 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008604
Qu Wenruo3b835842021-04-06 19:54:53 +08008605 cur = page_start;
8606 while (cur < page_end) {
8607 struct btrfs_ordered_extent *ordered;
8608 bool delete_states;
8609 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008610 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008611
8612 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8613 page_end + 1 - cur);
8614 if (!ordered) {
8615 range_end = page_end;
8616 /*
8617 * No ordered extent covering this range, we are safe
8618 * to delete all extent states in the range.
8619 */
8620 delete_states = true;
8621 goto next;
8622 }
8623 if (ordered->file_offset > cur) {
8624 /*
8625 * There is a range between [cur, oe->file_offset) not
8626 * covered by any ordered extent.
8627 * We are safe to delete all extent states, and handle
8628 * the ordered extent in the next iteration.
8629 */
8630 range_end = ordered->file_offset - 1;
8631 delete_states = true;
8632 goto next;
8633 }
8634
8635 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8636 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008637 ASSERT(range_end + 1 - cur < U32_MAX);
8638 range_len = range_end + 1 - cur;
8639 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008640 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008641 * If Ordered (Private2) is cleared, it means endio has
8642 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008643 * We can't delete the extent states as
8644 * btrfs_finish_ordered_io() may still use some of them.
8645 */
8646 delete_states = false;
8647 goto next;
8648 }
Qu Wenruob945a462021-05-31 16:50:46 +08008649 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008650
Chris Masoneb84ae02008-07-17 13:53:27 -04008651 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008652 * IO on this page will never be started, so we need to account
8653 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8654 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008655 *
8656 * This will also unlock the range for incoming
8657 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008658 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008659 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008660 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008661 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008662 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008663 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008664
8665 spin_lock_irq(&inode->ordered_tree.lock);
8666 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8667 ordered->truncated_len = min(ordered->truncated_len,
8668 cur - ordered->file_offset);
8669 spin_unlock_irq(&inode->ordered_tree.lock);
8670
8671 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008672 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008673 btrfs_finish_ordered_io(ordered);
8674 /*
8675 * The ordered extent has finished, now we're again
8676 * safe to delete all extent states of the range.
8677 */
8678 delete_states = true;
8679 } else {
8680 /*
8681 * btrfs_finish_ordered_io() will get executed by endio
8682 * of other pages, thus we can't delete extent states
8683 * anymore
8684 */
8685 delete_states = false;
8686 }
8687next:
8688 if (ordered)
8689 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008690 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008691 * Qgroup reserved space handler
8692 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008693 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008694 * 1) Already written to disk or bio already finished
8695 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8696 * Qgroup will be handled by its qgroup_record then.
8697 * btrfs_qgroup_free_data() call will do nothing here.
8698 *
8699 * 2) Not written to disk yet
8700 * Then btrfs_qgroup_free_data() call will clear the
8701 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8702 * reserved data space.
8703 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008704 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008705 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008706 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008707 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008708 EXTENT_DELALLOC | EXTENT_UPTODATE |
8709 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008710 delete_states, &cached_state);
8711 }
8712 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008713 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008714 /*
8715 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008716 * should not have Ordered (Private2) anymore, or the above iteration
8717 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008718 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008719 ASSERT(!PageOrdered(page));
Qu Wenruoe4f94342021-09-27 15:21:49 +08008720 btrfs_page_clear_checked(fs_info, page, page_offset(page), PAGE_SIZE);
Qu Wenruo3b835842021-04-06 19:54:53 +08008721 if (!inode_evicting)
8722 __btrfs_releasepage(page, GFP_NOFS);
Qu Wenruo32443de2021-01-26 16:34:00 +08008723 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008724}
8725
Chris Mason9ebefb182007-06-15 13:50:00 -04008726/*
8727 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8728 * called from a page fault handler when a page is first dirtied. Hence we must
8729 * be careful to check for EOF conditions here. We set the page up correctly
8730 * for a written page which means we get ENOSPC checking when writing into
8731 * holes and correct delalloc and unwritten extent mapping on filesystems that
8732 * support these features.
8733 *
8734 * We are not allowed to take the i_mutex here so we have to play games to
8735 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008736 * truncate_setsize() writes the inode size before removing pages, once we have
8737 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008738 * beyond EOF, then the page is guaranteed safe against truncation until we
8739 * unlock the page.
8740 */
Souptick Joardera528a242018-06-06 19:54:44 +05308741vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008742{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008743 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008744 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8747 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008748 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008749 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008751 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308752 vm_fault_t ret;
8753 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008754 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308755 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008756 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008757 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308758 u64 end;
8759
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008760 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008761
Jan Karab2b5ef52012-06-12 16:20:45 +02008762 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008763 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008764 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308765 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008766
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308767 /*
8768 * Reserving delalloc space after obtaining the page lock can lead to
8769 * deadlock. For example, if a dirty page is locked by this function
8770 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8771 * dirty page write out, then the btrfs_writepage() function could
8772 * end up waiting indefinitely to get a lock on the page currently
8773 * being processed by btrfs_page_mkwrite() function.
8774 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008775 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8776 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308777 if (!ret2) {
8778 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008779 reserved = 1;
8780 }
Souptick Joardera528a242018-06-06 19:54:44 +05308781 if (ret2) {
8782 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008783 if (reserved)
8784 goto out;
8785 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008786 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008787
Nick Piggin56a76f82009-03-31 15:23:23 -07008788 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008790 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008791 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008792 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008793
Chris Mason9ebefb182007-06-15 13:50:00 -04008794 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008795 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008796 /* page got truncated out from underneath us */
8797 goto out_unlock;
8798 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008799 wait_on_page_writeback(page);
8800
David Sterbaff13db42015-12-03 14:30:40 +01008801 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008802 ret2 = set_page_extent_mapped(page);
8803 if (ret2 < 0) {
8804 ret = vmf_error(ret2);
8805 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8806 goto out_unlock;
8807 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008808
Chris Masoneb84ae02008-07-17 13:53:27 -04008809 /*
8810 * we can't set the delalloc bits if there are pending ordered
8811 * extents. Drop our locks and wait for them to finish
8812 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008813 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8814 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008816 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008817 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008818 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008819 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008820 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 btrfs_put_ordered_extent(ordered);
8822 goto again;
8823 }
8824
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008825 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008826 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008827 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008828 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308829 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008830 btrfs_delalloc_release_space(BTRFS_I(inode),
8831 data_reserved, page_start,
8832 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308833 }
8834 }
8835
Josef Bacikfbf19082009-10-01 17:10:23 -04008836 /*
Liu Bo54160342016-12-13 12:15:19 -08008837 * page_mkwrite gets called when the page is firstly dirtied after it's
8838 * faulted in, but write(2) could also dirty a page and set delalloc
8839 * bits, thus in this case for space account reason, we still need to
8840 * clear any delalloc bits within this page range since we have to
8841 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008842 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308843 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008844 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8845 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008846
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008847 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008848 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308849 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008850 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008851 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008852 ret = VM_FAULT_SIGBUS;
8853 goto out_unlock;
8854 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008855
8856 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008857 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008858 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008859 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008860 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008861
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008862 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008863 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008864 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008865 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08008866 btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008867 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8868 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008869
Filipe Mananabc0939f2021-02-23 12:08:48 +00008870 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008871
David Sterbae43bbe52017-12-12 21:43:52 +01008872 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008873 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008874
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008875 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8876 sb_end_pagefault(inode->i_sb);
8877 extent_changeset_free(data_reserved);
8878 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008879
8880out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008881 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008882 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008883out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008884 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008885 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008886 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008887out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008888 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008889 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008890 return ret;
8891}
8892
Filipe Manana213e8c52018-02-06 20:40:31 +00008893static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008894{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008895 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008896 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008897 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008898 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008899 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008900 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008901 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008902 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008903
Filipe Manana213e8c52018-02-06 20:40:31 +00008904 if (!skip_writeback) {
8905 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8906 (u64)-1);
8907 if (ret)
8908 return ret;
8909 }
Chris Mason39279cc2007-06-12 06:35:45 -04008910
Josef Bacikfcb80c22011-05-03 10:40:22 -04008911 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008912 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8913 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008914 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008915 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008916 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008917 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008918 * be free'd up by the truncate operation, but also have some slack
8919 * space reserved in case it uses space during the truncate (thank you
8920 * very much snapshotting).
8921 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008922 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008923 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008924 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008925 * doesn't end up using space reserved for updating the inode. We also
8926 * need to be able to stop the transaction and start a new one, which
8927 * means we need to be able to update the inode several times, and we
8928 * have no idea of knowing how many times that will be, so we can't just
8929 * reserve 1 item for the entirety of the operation, so that has to be
8930 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008931 *
8932 * So that leaves us with
8933 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008934 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008935 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008936 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008937 * updating the inode.
8938 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008939 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008940 if (!rsv)
8941 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008942 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008943 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008944
Josef Bacik907cbce2011-08-08 13:46:15 -04008945 /*
Josef Bacik07127182011-08-19 10:29:59 -04008946 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008947 * 1 for updating the inode.
8948 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008949 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008950 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008951 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008952 goto out;
8953 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008954
Josef Bacik907cbce2011-08-08 13:46:15 -04008955 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008956 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008957 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008958 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008959
Josef Bacikca7e70f2012-08-27 17:48:15 -04008960 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008961
Yan, Zheng80825102009-11-12 09:35:36 +00008962 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008963 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008964 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008965 BTRFS_EXTENT_DATA_KEY,
8966 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008967 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008968 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008969 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008970
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008971 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008972 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008973 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008974
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008975 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008976 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008977
8978 trans = btrfs_start_transaction(root, 2);
8979 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008980 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008981 trans = NULL;
8982 break;
8983 }
8984
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008985 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008986 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008987 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008988 BUG_ON(ret); /* shouldn't happen */
8989 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008990 }
8991
Josef Bacikddfae632017-10-19 14:16:02 -04008992 /*
8993 * We can't call btrfs_truncate_block inside a trans handle as we could
8994 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8995 * we've truncated everything except the last little bit, and can do
8996 * btrfs_truncate_block and then update the disk_i_size.
8997 */
8998 if (ret == NEED_TRUNCATE_BLOCK) {
8999 btrfs_end_transaction(trans);
9000 btrfs_btree_balance_dirty(fs_info);
9001
Nikolay Borisov217f42e2020-11-02 16:49:03 +02009002 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04009003 if (ret)
9004 goto out;
9005 trans = btrfs_start_transaction(root, 1);
9006 if (IS_ERR(trans)) {
9007 ret = PTR_ERR(trans);
9008 goto out;
9009 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02009010 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04009011 }
9012
Chris Mason917c16b2011-11-08 14:49:59 -05009013 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009014 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009015
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009016 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009017 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009018 if (ret2 && !ret)
9019 ret = ret2;
9020
9021 ret2 = btrfs_end_transaction(trans);
9022 if (ret2 && !ret)
9023 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009024 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009025 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009026out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009027 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01009028 /*
9029 * So if we truncate and then write and fsync we normally would just
9030 * write the extents that changed, which is a problem if we need to
9031 * first truncate that entire inode. So set this flag so we write out
9032 * all of the extents in the inode to the sync log so we're completely
9033 * safe.
9034 *
9035 * If no extents were dropped or trimmed we don't need to force the next
9036 * fsync to truncate all the inode's items from the log and re-log them
9037 * all. This means the truncate operation did not change the file size,
9038 * or changed it to a smaller size but there was only an implicit hole
9039 * between the old i_size and the new i_size, and there were no prealloc
9040 * extents beyond i_size to drop.
9041 */
9042 if (extents_found > 0)
9043 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009044
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009045 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009046}
9047
Sven Wegener3b963622008-06-09 21:57:42 -04009048/*
Chris Masond352ac62008-09-29 15:18:18 -04009049 * create a new subvolume directory/inode (helper for the ioctl).
9050 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009051int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009052 struct btrfs_root *new_root,
Christian Brauner4d4340c2021-07-27 12:48:52 +02009053 struct btrfs_root *parent_root,
9054 struct user_namespace *mnt_userns)
Chris Mason39279cc2007-06-12 06:35:45 -04009055{
Chris Mason39279cc2007-06-12 06:35:45 -04009056 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009057 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009058 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02009059 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04009060
Nikolay Borisov23125102020-12-07 17:32:36 +02009061 err = btrfs_get_free_objectid(new_root, &ino);
9062 if (err < 0)
9063 return err;
9064
Christian Brauner4d4340c2021-07-27 12:48:52 +02009065 inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02009066 ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009067 S_IFDIR | (~current_umask() & S_IRWXUGO),
9068 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009069 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009070 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009071 inode->i_op = &btrfs_dir_inode_operations;
9072 inode->i_fop = &btrfs_dir_file_operations;
9073
Miklos Szeredibfe86842011-10-28 14:13:29 +02009074 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009075 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009076 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009077
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009078 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9079 if (err)
9080 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009081 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009082 new_root->root_key.objectid, err);
9083
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009084 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009085
Yan, Zheng76dda932009-09-21 16:00:26 -04009086 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009087 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009088}
9089
Chris Mason39279cc2007-06-12 06:35:45 -04009090struct inode *btrfs_alloc_inode(struct super_block *sb)
9091{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009092 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009093 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009094 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009095
David Sterba712e36c2017-10-31 17:08:27 +01009096 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009097 if (!ei)
9098 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009099
9100 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009101 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009102 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009103 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009104 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009105 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009106 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009107 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009108 ei->disk_i_size = 0;
9109 ei->flags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -07009110 ei->ro_flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009111 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009112 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009113 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009114 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01009115 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009116 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009117
Josef Bacik9e0baf62011-07-15 15:16:44 +00009118 spin_lock_init(&ei->lock);
9119 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009120 if (sb->s_magic != BTRFS_TEST_MAGIC)
9121 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9122 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009123 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009124 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009125 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009126
Miao Xie16cdcec2011-04-22 18:12:22 +08009127 ei->delayed_node = NULL;
9128
chandan r9cc97d62012-07-04 12:48:07 +05309129 ei->i_otime.tv_sec = 0;
9130 ei->i_otime.tv_nsec = 0;
9131
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009132 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009133 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009134 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9135 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9136 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05009137 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9138 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01009139 ei->io_tree.track_uptodate = true;
9140 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009141 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009142 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009143 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009144 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009145 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009146 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05009147 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009148
9149 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009150}
9151
Josef Bacikaaedb552013-10-11 14:44:09 -04009152#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9153void btrfs_test_destroy_inode(struct inode *inode)
9154{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009155 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009156 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9157}
9158#endif
9159
Al Viro26602ca2019-04-10 15:14:41 -04009160void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009161{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009162 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9163}
9164
Nikolay Borisov633cc812020-09-18 12:15:49 +03009165void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009166{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009167 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03009168 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9169 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009170
Nikolay Borisov633cc812020-09-18 12:15:49 +03009171 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9172 WARN_ON(vfs_inode->i_data.nrpages);
9173 WARN_ON(inode->block_rsv.reserved);
9174 WARN_ON(inode->block_rsv.size);
9175 WARN_ON(inode->outstanding_extents);
Filipe Mananadc287222021-09-16 11:32:14 +01009176 if (!S_ISDIR(vfs_inode->i_mode)) {
9177 WARN_ON(inode->delalloc_bytes);
9178 WARN_ON(inode->new_delalloc_bytes);
9179 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009180 WARN_ON(inode->csum_bytes);
9181 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009182
Chris Mason5a3f23d2009-03-31 13:27:11 -04009183 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009184 * This can happen where we create an inode, but somebody else also
9185 * created the same inode and we need to destroy the one we already
9186 * created.
9187 */
9188 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009189 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009190
Chris Masond3977122009-01-05 21:25:51 -05009191 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009192 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009193 if (!ordered)
9194 break;
9195 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009196 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009197 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08009198 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03009199 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009200 btrfs_put_ordered_extent(ordered);
9201 btrfs_put_ordered_extent(ordered);
9202 }
9203 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009204 btrfs_qgroup_check_reserved_leak(inode);
9205 inode_tree_del(inode);
9206 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9207 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9208 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04009209}
9210
Al Viro45321ac2010-06-07 13:43:19 -04009211int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009212{
9213 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009214
Naohiro Aota6379ef92013-06-06 09:56:34 +00009215 if (root == NULL)
9216 return 1;
9217
Liu Bofa6ac872013-02-20 14:10:23 +00009218 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009219 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009220 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009221 else
Al Viro45321ac2010-06-07 13:43:19 -04009222 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009223}
9224
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009225static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009226{
9227 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9228
9229 inode_init_once(&ei->vfs_inode);
9230}
9231
David Sterbae67c7182018-02-19 17:24:18 +01009232void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009233{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009234 /*
9235 * Make sure all delayed rcu free inodes are flushed before we
9236 * destroy cache.
9237 */
9238 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009239 kmem_cache_destroy(btrfs_inode_cachep);
9240 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009241 kmem_cache_destroy(btrfs_path_cachep);
9242 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009243 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009244}
9245
Liu Bof5c29bd2017-11-02 17:21:50 -06009246int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009247{
David Sterba837e1972012-09-07 03:00:48 -06009248 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009249 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009250 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9251 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009252 if (!btrfs_inode_cachep)
9253 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009254
David Sterba837e1972012-09-07 03:00:48 -06009255 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009256 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009257 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009258 if (!btrfs_trans_handle_cachep)
9259 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009260
David Sterba837e1972012-09-07 03:00:48 -06009261 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009262 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009263 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009264 if (!btrfs_path_cachep)
9265 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009266
David Sterba837e1972012-09-07 03:00:48 -06009267 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009268 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009269 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009270 if (!btrfs_free_space_cachep)
9271 goto fail;
9272
Christophe Leroy3acd4852019-08-21 15:05:55 +00009273 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9274 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009275 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009276 if (!btrfs_free_space_bitmap_cachep)
9277 goto fail;
9278
Chris Mason39279cc2007-06-12 06:35:45 -04009279 return 0;
9280fail:
9281 btrfs_destroy_cachep();
9282 return -ENOMEM;
9283}
9284
Christian Brauner549c7292021-01-21 14:19:43 +01009285static int btrfs_getattr(struct user_namespace *mnt_userns,
9286 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009287 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009288{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009289 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009290 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009291 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009292 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009293 u32 bi_flags = BTRFS_I(inode)->flags;
Boris Burkov14605402021-06-30 13:01:49 -07009294 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
Yonghong Song04a87e32017-05-12 15:07:43 -07009295
9296 stat->result_mask |= STATX_BTIME;
9297 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9298 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9299 if (bi_flags & BTRFS_INODE_APPEND)
9300 stat->attributes |= STATX_ATTR_APPEND;
9301 if (bi_flags & BTRFS_INODE_COMPRESS)
9302 stat->attributes |= STATX_ATTR_COMPRESSED;
9303 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9304 stat->attributes |= STATX_ATTR_IMMUTABLE;
9305 if (bi_flags & BTRFS_INODE_NODUMP)
9306 stat->attributes |= STATX_ATTR_NODUMP;
Boris Burkov14605402021-06-30 13:01:49 -07009307 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9308 stat->attributes |= STATX_ATTR_VERITY;
Yonghong Song04a87e32017-05-12 15:07:43 -07009309
9310 stat->attributes_mask |= (STATX_ATTR_APPEND |
9311 STATX_ATTR_COMPRESSED |
9312 STATX_ATTR_IMMUTABLE |
9313 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009314
Christian Braunerc020d2e2021-07-27 12:48:43 +02009315 generic_fillattr(mnt_userns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009316 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009317
9318 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009319 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009320 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009321 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009322 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009323 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009324 return 0;
9325}
9326
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009327static int btrfs_rename_exchange(struct inode *old_dir,
9328 struct dentry *old_dentry,
9329 struct inode *new_dir,
9330 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009332 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009333 struct btrfs_trans_handle *trans;
9334 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009335 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009336 struct inode *new_inode = new_dentry->d_inode;
9337 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009338 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009339 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9340 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341 u64 old_idx = 0;
9342 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009343 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009344 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009345 bool root_log_pinned = false;
9346 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009347 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009348
NeilBrown3f79f6f2021-08-06 14:26:24 +10009349 /*
9350 * For non-subvolumes allow exchange only within one subvolume, in the
9351 * same inode namespace. Two subvolumes (represented as directory) can
9352 * be exchanged as they're a logical link and have a fixed inode number.
9353 */
9354 if (root != dest &&
9355 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9356 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009357 return -EXDEV;
9358
9359 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009360 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9361 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009362 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009363
9364 /*
9365 * We want to reserve the absolute worst case amount of items. So if
9366 * both inodes are subvols and we need to unlink them then that would
9367 * require 4 item modifications, but if they are both normal inodes it
9368 * would require 5 item modifications, so we'll assume their normal
9369 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9370 * should cover the worst case number of items we'll modify.
9371 */
9372 trans = btrfs_start_transaction(root, 12);
9373 if (IS_ERR(trans)) {
9374 ret = PTR_ERR(trans);
9375 goto out_notrans;
9376 }
9377
Josef Bacik00aa8e82021-03-12 15:25:02 -05009378 if (dest != root) {
9379 ret = btrfs_record_root_in_trans(trans, dest);
9380 if (ret)
9381 goto out_fail;
9382 }
Josef Bacik3e174092019-11-15 15:43:06 -05009383
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009384 /*
9385 * We need to find a free sequence number both in the source and
9386 * in the destination directory for the exchange.
9387 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009388 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009389 if (ret)
9390 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009391 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009392 if (ret)
9393 goto out_fail;
9394
9395 BTRFS_I(old_inode)->dir_index = 0ULL;
9396 BTRFS_I(new_inode)->dir_index = 0ULL;
9397
9398 /* Reference for the source. */
9399 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9400 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009401 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009402 } else {
9403 ret = btrfs_insert_inode_ref(trans, dest,
9404 new_dentry->d_name.name,
9405 new_dentry->d_name.len,
9406 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009407 btrfs_ino(BTRFS_I(new_dir)),
9408 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009409 if (ret)
9410 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009411 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009412 }
9413
9414 /* And now for the dest. */
9415 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9416 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009417 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009418 } else {
9419 ret = btrfs_insert_inode_ref(trans, root,
9420 old_dentry->d_name.name,
9421 old_dentry->d_name.len,
9422 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009423 btrfs_ino(BTRFS_I(old_dir)),
9424 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009425 if (ret) {
9426 if (need_abort)
9427 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009428 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009429 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009430 }
9431
9432 /* Update inode version and ctime/mtime. */
9433 inode_inc_iversion(old_dir);
9434 inode_inc_iversion(new_dir);
9435 inode_inc_iversion(old_inode);
9436 inode_inc_iversion(new_inode);
9437 old_dir->i_ctime = old_dir->i_mtime = ctime;
9438 new_dir->i_ctime = new_dir->i_mtime = ctime;
9439 old_inode->i_ctime = ctime;
9440 new_inode->i_ctime = ctime;
9441
9442 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009443 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9444 BTRFS_I(old_inode), 1);
9445 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9446 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 }
9448
Filipe Mananabd54f382021-07-27 11:24:45 +01009449 /*
9450 * Now pin the logs of the roots. We do it to ensure that no other task
9451 * can sync the logs while we are in progress with the rename, because
9452 * that could result in an inconsistency in case any of the inodes that
9453 * are part of this rename operation were logged before.
9454 *
9455 * We pin the logs even if at this precise moment none of the inodes was
9456 * logged before. This is because right after we checked for that, some
9457 * other task fsyncing some other inode not involved with this rename
9458 * operation could log that one of our inodes exists.
9459 *
9460 * We don't need to pin the logs before the above calls to
9461 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9462 */
9463 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9464 btrfs_pin_log_trans(root);
9465 root_log_pinned = true;
9466 }
9467 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9468 btrfs_pin_log_trans(dest);
9469 dest_log_pinned = true;
9470 }
9471
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009472 /* src is a subvolume */
9473 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009474 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009475 } else { /* src is an inode */
Filipe Manana4467af82021-10-25 17:31:50 +01009476 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009477 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009478 old_dentry->d_name.name,
9479 old_dentry->d_name.len);
9480 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009481 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009482 }
9483 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009484 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 goto out_fail;
9486 }
9487
9488 /* dest is a subvolume */
9489 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009490 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 } else { /* dest is an inode */
Filipe Manana4467af82021-10-25 17:31:50 +01009492 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009493 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009494 new_dentry->d_name.name,
9495 new_dentry->d_name.len);
9496 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009497 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 }
9499 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009500 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 goto out_fail;
9502 }
9503
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009504 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 new_dentry->d_name.name,
9506 new_dentry->d_name.len, 0, old_idx);
9507 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009508 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 goto out_fail;
9510 }
9511
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009512 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 old_dentry->d_name.name,
9514 old_dentry->d_name.len, 0, new_idx);
9515 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009516 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 goto out_fail;
9518 }
9519
9520 if (old_inode->i_nlink == 1)
9521 BTRFS_I(old_inode)->dir_index = old_idx;
9522 if (new_inode->i_nlink == 1)
9523 BTRFS_I(new_inode)->dir_index = new_idx;
9524
Filipe Manana86e8aa02016-05-05 02:02:27 +01009525 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009526 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9527 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009529 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009531 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009532 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9533 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009535 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536 }
9537out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009538 /*
9539 * If we have pinned a log and an error happened, we unpin tasks
9540 * trying to sync the log and force them to fallback to a transaction
9541 * commit if the log currently contains any of the inodes involved in
9542 * this rename operation (to ensure we do not persist a log with an
9543 * inconsistent state for any of these inodes or leading to any
9544 * inconsistencies when replayed). If the transaction was aborted, the
9545 * abortion reason is propagated to userspace when attempting to commit
9546 * the transaction. If the log does not contain any of these inodes, we
9547 * allow the tasks to sync it.
9548 */
9549 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009550 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9551 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9552 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana1c167b82021-07-29 15:28:34 +01009553 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
David Sterba90787762019-03-20 13:28:05 +01009554 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009555
9556 if (root_log_pinned) {
9557 btrfs_end_log_trans(root);
9558 root_log_pinned = false;
9559 }
9560 if (dest_log_pinned) {
9561 btrfs_end_log_trans(dest);
9562 dest_log_pinned = false;
9563 }
9564 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009565 ret2 = btrfs_end_transaction(trans);
9566 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009568 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9569 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009570 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571
9572 return ret;
9573}
9574
9575static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9576 struct btrfs_root *root,
Christian Braunerca072742021-07-27 12:48:42 +02009577 struct user_namespace *mnt_userns,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009578 struct inode *dir,
9579 struct dentry *dentry)
9580{
9581 int ret;
9582 struct inode *inode;
9583 u64 objectid;
9584 u64 index;
9585
Nikolay Borisov543068a2020-12-07 17:32:33 +02009586 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 if (ret)
9588 return ret;
9589
Christian Braunerca072742021-07-27 12:48:42 +02009590 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 dentry->d_name.name,
9592 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009593 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594 objectid,
9595 S_IFCHR | WHITEOUT_MODE,
9596 &index);
9597
9598 if (IS_ERR(inode)) {
9599 ret = PTR_ERR(inode);
9600 return ret;
9601 }
9602
9603 inode->i_op = &btrfs_special_inode_operations;
9604 init_special_inode(inode, inode->i_mode,
9605 WHITEOUT_DEV);
9606
9607 ret = btrfs_init_inode_security(trans, inode, dir,
9608 &dentry->d_name);
9609 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009610 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009612 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9613 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009615 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009617 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009618out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009620 if (ret)
9621 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 iput(inode);
9623
Filipe Mananac9901612016-05-05 01:41:57 +01009624 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625}
9626
Christian Braunerca072742021-07-27 12:48:42 +02009627static int btrfs_rename(struct user_namespace *mnt_userns,
9628 struct inode *old_dir, struct dentry *old_dentry,
9629 struct inode *new_dir, struct dentry *new_dentry,
9630 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009632 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009633 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009634 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009635 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9636 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009637 struct inode *new_inode = d_inode(new_dentry);
9638 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009639 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009640 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009641 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009642 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009643 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009644
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009645 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009646 return -EPERM;
9647
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009648 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009649 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009650 return -EXDEV;
9651
Li Zefan33345d012011-04-20 10:31:50 +08009652 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009653 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009654 return -ENOTEMPTY;
9655
Chris Mason39279cc2007-06-12 06:35:45 -04009656 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009657 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009658 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009659
9660
9661 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009662 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009663 new_dentry->d_name.name,
9664 new_dentry->d_name.len);
9665
9666 if (ret) {
9667 if (ret == -EEXIST) {
9668 /* we shouldn't get
9669 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309670 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009671 return ret;
9672 }
9673 } else {
9674 /* maybe -EOVERFLOW */
9675 return ret;
9676 }
9677 }
9678 ret = 0;
9679
Chris Mason5a3f23d2009-03-31 13:27:11 -04009680 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009681 * we're using rename to replace one file with another. Start IO on it
9682 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009683 */
Chris Mason8d875f92014-08-12 10:47:42 -07009684 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009685 filemap_flush(old_inode->i_mapping);
9686
Yan, Zheng76dda932009-09-21 16:00:26 -04009687 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009688 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009689 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009690 /*
9691 * We want to reserve the absolute worst case amount of items. So if
9692 * both inodes are subvols and we need to unlink them then that would
9693 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009695 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9696 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009697 * If our rename has the whiteout flag, we need more 5 units for the
9698 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9699 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009700 */
Filipe Manana5062af32016-05-05 10:26:26 +01009701 trans_num_items = 11;
9702 if (flags & RENAME_WHITEOUT)
9703 trans_num_items += 5;
9704 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009705 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 ret = PTR_ERR(trans);
9707 goto out_notrans;
9708 }
Chris Mason5f39d392007-10-15 16:14:19 -04009709
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009710 if (dest != root) {
9711 ret = btrfs_record_root_in_trans(trans, dest);
9712 if (ret)
9713 goto out_fail;
9714 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009715
Nikolay Borisov877574e2017-02-20 13:50:33 +02009716 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009717 if (ret)
9718 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009719
Miao Xie67de1172013-12-26 13:07:06 +08009720 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009721 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009722 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009723 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009724 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009725 ret = btrfs_insert_inode_ref(trans, dest,
9726 new_dentry->d_name.name,
9727 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009728 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009729 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009730 if (ret)
9731 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009732 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009733
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009734 inode_inc_iversion(old_dir);
9735 inode_inc_iversion(new_dir);
9736 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009737 old_dir->i_ctime = old_dir->i_mtime =
9738 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009739 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009740
Chris Mason12fcfd22009-03-24 10:24:20 -04009741 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009742 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9743 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009744
Li Zefan33345d012011-04-20 10:31:50 +08009745 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009746 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009747 } else {
Filipe Mananabd54f382021-07-27 11:24:45 +01009748 /*
9749 * Now pin the log. We do it to ensure that no other task can
9750 * sync the log while we are in progress with the rename, as
9751 * that could result in an inconsistency in case any of the
9752 * inodes that are part of this rename operation were logged
9753 * before.
9754 *
9755 * We pin the log even if at this precise moment none of the
9756 * inodes was logged before. This is because right after we
9757 * checked for that, some other task fsyncing some other inode
9758 * not involved with this rename operation could log that one of
9759 * our inodes exists.
9760 *
9761 * We don't need to pin the logs before the above call to
9762 * btrfs_insert_inode_ref(), since that does not need to change
9763 * a log.
9764 */
9765 btrfs_pin_log_trans(root);
9766 log_pinned = true;
Filipe Manana4467af82021-10-25 17:31:50 +01009767 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009768 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009769 old_dentry->d_name.name,
9770 old_dentry->d_name.len);
9771 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009772 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009773 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009774 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009775 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009776 goto out_fail;
9777 }
Chris Mason39279cc2007-06-12 06:35:45 -04009778
9779 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009780 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009781 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009782 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009783 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009784 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009785 BUG_ON(new_inode->i_nlink == 0);
9786 } else {
Filipe Manana4467af82021-10-25 17:31:50 +01009787 ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009788 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009789 new_dentry->d_name.name,
9790 new_dentry->d_name.len);
9791 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009792 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009793 ret = btrfs_orphan_add(trans,
9794 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009797 goto out_fail;
9798 }
Chris Mason39279cc2007-06-12 06:35:45 -04009799 }
Josef Bacikaec74772008-07-24 12:12:38 -04009800
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009801 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009803 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009805 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009806 goto out_fail;
9807 }
Chris Mason39279cc2007-06-12 06:35:45 -04009808
Miao Xie67de1172013-12-26 13:07:06 +08009809 if (old_inode->i_nlink == 1)
9810 BTRFS_I(old_inode)->dir_index = index;
9811
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009812 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009813 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9814 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009815 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009816 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818
9819 if (flags & RENAME_WHITEOUT) {
Christian Braunerca072742021-07-27 12:48:42 +02009820 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9821 old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009822
9823 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009824 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 goto out_fail;
9826 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009827 }
Chris Mason39279cc2007-06-12 06:35:45 -04009828out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009829 /*
9830 * If we have pinned the log and an error happened, we unpin tasks
9831 * trying to sync the log and force them to fallback to a transaction
9832 * commit if the log currently contains any of the inodes involved in
9833 * this rename operation (to ensure we do not persist a log with an
9834 * inconsistent state for any of these inodes or leading to any
9835 * inconsistencies when replayed). If the transaction was aborted, the
9836 * abortion reason is propagated to userspace when attempting to commit
9837 * the transaction. If the log does not contain any of these inodes, we
9838 * allow the tasks to sync it.
9839 */
9840 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009841 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9842 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9843 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009844 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009845 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009846 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009847
9848 btrfs_end_log_trans(root);
9849 log_pinned = false;
9850 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009851 ret2 = btrfs_end_transaction(trans);
9852 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009853out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009854 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009855 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009856
Chris Mason39279cc2007-06-12 06:35:45 -04009857 return ret;
9858}
9859
Christian Brauner549c7292021-01-21 14:19:43 +01009860static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9861 struct dentry *old_dentry, struct inode *new_dir,
9862 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009863{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009864 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009865 return -EINVAL;
9866
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009867 if (flags & RENAME_EXCHANGE)
9868 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9869 new_dentry);
9870
Christian Braunerca072742021-07-27 12:48:42 +02009871 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9872 new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009873}
9874
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009875struct btrfs_delalloc_work {
9876 struct inode *inode;
9877 struct completion completion;
9878 struct list_head list;
9879 struct btrfs_work work;
9880};
9881
Miao Xie8ccf6f192012-10-25 09:28:04 +00009882static void btrfs_run_delalloc_work(struct btrfs_work *work)
9883{
9884 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009885 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009886
9887 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9888 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009889 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009890 filemap_flush(inode->i_mapping);
9891 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9892 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009893 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009894
Nikolay Borisov076da912018-04-23 10:54:16 +03009895 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009896 complete(&delalloc_work->completion);
9897}
9898
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009899static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009900{
9901 struct btrfs_delalloc_work *work;
9902
David Sterba100d5702015-12-08 14:39:32 +01009903 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009904 if (!work)
9905 return NULL;
9906
9907 init_completion(&work->completion);
9908 INIT_LIST_HEAD(&work->list);
9909 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009910 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009911
9912 return work;
9913}
9914
Chris Masond352ac62008-09-29 15:18:18 -04009915/*
9916 * some fairly slow code that needs optimization. This walks the list
9917 * of all the inodes with pending delalloc and forces them to disk.
9918 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009919static int start_delalloc_inodes(struct btrfs_root *root,
9920 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009921 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009922{
Chris Masonea8c2812008-08-04 23:17:27 -04009923 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009924 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009925 struct btrfs_delalloc_work *work, *next;
9926 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009927 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009928 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009929 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009930
Miao Xie8ccf6f192012-10-25 09:28:04 +00009931 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009932 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009933
Miao Xie573bfb72014-03-06 13:55:03 +08009934 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009935 spin_lock(&root->delalloc_lock);
9936 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009937 while (!list_empty(&splice)) {
9938 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009939 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009940
Miao Xieeb73c1b2013-05-15 07:48:22 +00009941 list_move_tail(&binode->delalloc_inodes,
9942 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009943
9944 if (in_reclaim_context &&
9945 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9946 continue;
9947
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009948 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009949 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009950 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009951 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009952 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009953 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009954
Ethan Lien3cd24c62018-11-01 14:49:03 +08009955 if (snapshot)
9956 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9957 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009958 if (full_flush) {
9959 work = btrfs_alloc_delalloc_work(inode);
9960 if (!work) {
9961 iput(inode);
9962 ret = -ENOMEM;
9963 goto out;
9964 }
9965 list_add_tail(&work->list, &works);
9966 btrfs_queue_work(root->fs_info->flush_workers,
9967 &work->work);
9968 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009969 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009970 btrfs_add_delayed_iput(inode);
9971 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009972 goto out;
9973 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009974 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009975 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009976 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009977 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009978
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009979out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009980 list_for_each_entry_safe(work, next, &works, list) {
9981 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009982 wait_for_completion(&work->completion);
9983 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009984 }
9985
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009986 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009987 spin_lock(&root->delalloc_lock);
9988 list_splice_tail(&splice, &root->delalloc_inodes);
9989 spin_unlock(&root->delalloc_lock);
9990 }
Miao Xie573bfb72014-03-06 13:55:03 +08009991 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009992 return ret;
9993}
9994
Filipe Mananaf9baa502021-04-22 12:08:05 +01009995int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009996{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009997 struct writeback_control wbc = {
9998 .nr_to_write = LONG_MAX,
9999 .sync_mode = WB_SYNC_NONE,
10000 .range_start = 0,
10001 .range_end = LLONG_MAX,
10002 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010003 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010004
Josef Bacik84961532021-10-05 16:35:25 -040010005 if (BTRFS_FS_ERROR(fs_info))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010006 return -EROFS;
10007
Filipe Mananaf9baa502021-04-22 12:08:05 +010010008 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009}
10010
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010011int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +000010012 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010013{
Josef Bacike076ab2a2021-01-07 17:08:30 -050010014 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010015 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -050010016 .sync_mode = WB_SYNC_NONE,
10017 .range_start = 0,
10018 .range_end = LLONG_MAX,
10019 };
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 struct btrfs_root *root;
10021 struct list_head splice;
10022 int ret;
10023
Josef Bacik84961532021-10-05 16:35:25 -040010024 if (BTRFS_FS_ERROR(fs_info))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025 return -EROFS;
10026
10027 INIT_LIST_HEAD(&splice);
10028
Miao Xie573bfb72014-03-06 13:55:03 +080010029 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010030 spin_lock(&fs_info->delalloc_root_lock);
10031 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +020010032 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -050010033 /*
10034 * Reset nr_to_write here so we know that we're doing a full
10035 * flush.
10036 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010037 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -050010038 wbc.nr_to_write = LONG_MAX;
10039
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 root = list_first_entry(&splice, struct btrfs_root,
10041 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -050010042 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010043 BUG_ON(!root);
10044 list_move_tail(&root->delalloc_root,
10045 &fs_info->delalloc_roots);
10046 spin_unlock(&fs_info->delalloc_root_lock);
10047
Josef Bacike076ab2a2021-01-07 17:08:30 -050010048 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -050010049 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -050010050 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010051 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 spin_lock(&fs_info->delalloc_root_lock);
10053 }
10054 spin_unlock(&fs_info->delalloc_root_lock);
10055
Miao Xie6c255e62014-03-06 13:55:01 +080010056 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010057out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010058 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010059 spin_lock(&fs_info->delalloc_root_lock);
10060 list_splice_tail(&splice, &fs_info->delalloc_roots);
10061 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010062 }
Miao Xie573bfb72014-03-06 13:55:03 +080010063 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010064 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010065}
10066
Christian Brauner549c7292021-01-21 14:19:43 +010010067static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
10068 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -040010069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010070 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010071 struct btrfs_trans_handle *trans;
10072 struct btrfs_root *root = BTRFS_I(dir)->root;
10073 struct btrfs_path *path;
10074 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010075 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010076 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010077 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010078 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010079 int name_len;
10080 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010081 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010082 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010083 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010084
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010085 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010086 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010087 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010088
Josef Bacik9ed74f22009-09-11 16:12:44 -040010089 /*
10090 * 2 items for inode item and ref
10091 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010092 * 1 item for updating parent inode item
10093 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010094 * 1 item for xattr if selinux is on
10095 */
Filipe Manana9269d122015-12-31 18:16:29 +000010096 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010097 if (IS_ERR(trans))
10098 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010099
Nikolay Borisov543068a2020-12-07 17:32:33 +020010100 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +080010101 if (err)
10102 goto out_unlock;
10103
Christian Brauner5a052102021-07-27 12:48:47 +020010104 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +020010105 dentry->d_name.name, dentry->d_name.len,
10106 btrfs_ino(BTRFS_I(dir)), objectid,
10107 S_IFLNK | S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010108 if (IS_ERR(inode)) {
10109 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010110 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010111 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010112 }
Chris Mason39279cc2007-06-12 06:35:45 -040010113
Casey Schauflerad19db72011-12-15 10:09:07 -050010114 /*
10115 * If the active LSM wants to access the inode during
10116 * d_instantiate it needs these. Smack checks to see
10117 * if the filesystem supports xattrs by looking at the
10118 * ops vector.
10119 */
10120 inode->i_fop = &btrfs_file_operations;
10121 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010122 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010123
10124 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10125 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010126 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010127
Chris Mason39279cc2007-06-12 06:35:45 -040010128 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010129 if (!path) {
10130 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010131 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010132 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010133 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010134 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010135 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010136 datasize = btrfs_file_extent_calc_inline_size(name_len);
10137 err = btrfs_insert_empty_item(trans, root, path, &key,
10138 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010139 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010140 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010141 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010142 }
Chris Mason5f39d392007-10-15 16:14:19 -040010143 leaf = path->nodes[0];
10144 ei = btrfs_item_ptr(leaf, path->slots[0],
10145 struct btrfs_file_extent_item);
10146 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10147 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010148 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010149 btrfs_set_file_extent_encryption(leaf, ei, 0);
10150 btrfs_set_file_extent_compression(leaf, ei, 0);
10151 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10152 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10153
Chris Mason39279cc2007-06-12 06:35:45 -040010154 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010155 write_extent_buffer(leaf, symname, ptr, name_len);
10156 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010157 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010158
Chris Mason39279cc2007-06-12 06:35:45 -040010159 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010160 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010161 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010162 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010163 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +000010164 /*
10165 * Last step, add directory indexes for our symlink inode. This is the
10166 * last step to avoid extra cleanup of these indexes if an error happens
10167 * elsewhere above.
10168 */
10169 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010170 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10171 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010172 if (err)
10173 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010174
Al Viro1e2e5472018-05-04 08:23:01 -040010175 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010176
10177out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010178 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010179 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010180 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010181 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010182 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010183 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010184 return err;
10185}
Chris Mason16432982008-04-10 10:23:21 -040010186
Filipe Manana8fccebf2020-09-08 11:27:20 +010010187static struct btrfs_trans_handle *insert_prealloc_file_extent(
10188 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010189 struct btrfs_inode *inode,
10190 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +080010191 u64 file_offset)
10192{
10193 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +010010194 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010195 struct btrfs_trans_handle *trans = trans_in;
10196 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010197 u64 start = ins->objectid;
10198 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010199 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +080010200 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010201
10202 memset(&stack_fi, 0, sizeof(stack_fi));
10203
10204 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10205 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10206 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10207 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10208 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10209 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10210 /* Encryption and other encoding is reserved and all 0 */
10211
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010212 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10213 if (qgroup_released < 0)
10214 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010215
10216 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010217 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +000010218 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010219 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010220 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010221 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010222 return trans;
10223 }
10224
10225 extent_info.disk_offset = start;
10226 extent_info.disk_len = len;
10227 extent_info.data_offset = 0;
10228 extent_info.data_len = len;
10229 extent_info.file_offset = file_offset;
10230 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010231 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010232 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010233 extent_info.insertions = 0;
10234
10235 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010236 if (!path) {
10237 ret = -ENOMEM;
10238 goto free_qgroup;
10239 }
Filipe Manana8fccebf2020-09-08 11:27:20 +010010240
Nikolay Borisovbfc78472021-02-17 15:12:47 +020010241 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +010010242 file_offset + len - 1, &extent_info,
10243 &trans);
10244 btrfs_free_path(path);
10245 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010246 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010247 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010248
10249free_qgroup:
10250 /*
10251 * We have released qgroup data range at the beginning of the function,
10252 * and normally qgroup_released bytes will be freed when committing
10253 * transaction.
10254 * But if we error out early, we have to free what we have released
10255 * or we leak qgroup data reservation.
10256 */
10257 btrfs_qgroup_free_refroot(inode->root->fs_info,
10258 inode->root->root_key.objectid, qgroup_released,
10259 BTRFS_QGROUP_RSV_DATA);
10260 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +080010261}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010262
Josef Bacik0af3d002010-06-21 14:48:16 -040010263static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10264 u64 start, u64 num_bytes, u64 min_size,
10265 loff_t actual_len, u64 *alloc_hint,
10266 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010269 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10270 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010271 struct btrfs_root *root = BTRFS_I(inode)->root;
10272 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010273 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010274 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010275 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010276 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010277 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010279 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010280 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010281
Josef Bacik0af3d002010-06-21 14:48:16 -040010282 if (trans)
10283 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010285 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010286 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010287 /*
10288 * If we are severely fragmented we could end up with really
10289 * small allocations, so if the allocator is returning small
10290 * chunks lets make its job easier by only searching for those
10291 * sized chunks.
10292 */
10293 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010294 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10295 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010296 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010297 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010298
10299 /*
10300 * We've reserved this space, and thus converted it from
10301 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10302 * from here on out we will only need to clear our reservation
10303 * for the remaining unreserved area, so advance our
10304 * clear_offset by our extent size.
10305 */
10306 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010307
Josef Bacik0b670dc2015-09-23 17:11:16 -040010308 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010309 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10310 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010311 /*
10312 * Now that we inserted the prealloc extent we can finally
10313 * decrement the number of reservations in the block group.
10314 * If we did it before, we could race with relocation and have
10315 * relocation miss the reserved extent, making it fail later.
10316 */
10317 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010318 if (IS_ERR(trans)) {
10319 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010320 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010321 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010322 break;
10323 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010324
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010325 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010326 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010327
Josef Bacik5dc562c2012-08-17 13:14:17 -040010328 em = alloc_extent_map();
10329 if (!em) {
10330 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10331 &BTRFS_I(inode)->runtime_flags);
10332 goto next;
10333 }
10334
10335 em->start = cur_offset;
10336 em->orig_start = cur_offset;
10337 em->len = ins.offset;
10338 em->block_start = ins.objectid;
10339 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010340 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010341 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010342 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10343 em->generation = trans->transid;
10344
10345 while (1) {
10346 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010347 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010348 write_unlock(&em_tree->lock);
10349 if (ret != -EEXIST)
10350 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010351 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010352 cur_offset + ins.offset - 1,
10353 0);
10354 }
10355 free_extent_map(em);
10356next:
Yan Zhengd899e052008-10-30 14:25:28 -040010357 num_bytes -= ins.offset;
10358 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010359 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010360
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010361 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010362 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010363 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010364 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010365 (actual_len > inode->i_size) &&
10366 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010367 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010368 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010369 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010370 i_size = cur_offset;
10371 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010372 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010373 }
10374
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010375 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010376
10377 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010378 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010379 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010380 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010381 break;
10382 }
Yan Zhengd899e052008-10-30 14:25:28 -040010383
Filipe Manana8fccebf2020-09-08 11:27:20 +010010384 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010385 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010386 trans = NULL;
10387 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010388 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010389 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010390 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010391 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010392 return ret;
10393}
10394
Josef Bacik0af3d002010-06-21 14:48:16 -040010395int btrfs_prealloc_file_range(struct inode *inode, int mode,
10396 u64 start, u64 num_bytes, u64 min_size,
10397 loff_t actual_len, u64 *alloc_hint)
10398{
10399 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10400 min_size, actual_len, alloc_hint,
10401 NULL);
10402}
10403
10404int btrfs_prealloc_file_range_trans(struct inode *inode,
10405 struct btrfs_trans_handle *trans, int mode,
10406 u64 start, u64 num_bytes, u64 min_size,
10407 loff_t actual_len, u64 *alloc_hint)
10408{
10409 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10410 min_size, actual_len, alloc_hint, trans);
10411}
10412
Chris Masone6dcd2d2008-07-17 12:53:50 -040010413static int btrfs_set_page_dirty(struct page *page)
10414{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010415 return __set_page_dirty_nobuffers(page);
10416}
10417
Christian Brauner549c7292021-01-21 14:19:43 +010010418static int btrfs_permission(struct user_namespace *mnt_userns,
10419 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010420{
Li Zefanb83cc962010-12-20 16:04:08 +080010421 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010422 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010423
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010424 if (mask & MAY_WRITE &&
10425 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10426 if (btrfs_root_readonly(root))
10427 return -EROFS;
10428 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10429 return -EACCES;
10430 }
Christian Brauner3bc71ba2021-07-27 12:48:50 +020010431 return generic_permission(mnt_userns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010432}
Chris Mason39279cc2007-06-12 06:35:45 -040010433
Christian Brauner549c7292021-01-21 14:19:43 +010010434static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10435 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010436{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010437 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010438 struct btrfs_trans_handle *trans;
10439 struct btrfs_root *root = BTRFS_I(dir)->root;
10440 struct inode *inode = NULL;
10441 u64 objectid;
10442 u64 index;
10443 int ret = 0;
10444
10445 /*
10446 * 5 units required for adding orphan entry
10447 */
10448 trans = btrfs_start_transaction(root, 5);
10449 if (IS_ERR(trans))
10450 return PTR_ERR(trans);
10451
Nikolay Borisov543068a2020-12-07 17:32:33 +020010452 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010453 if (ret)
10454 goto out;
10455
Christian Brauner98b6ab52021-07-27 12:48:48 +020010456 inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010457 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010458 if (IS_ERR(inode)) {
10459 ret = PTR_ERR(inode);
10460 inode = NULL;
10461 goto out;
10462 }
10463
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 inode->i_fop = &btrfs_file_operations;
10465 inode->i_op = &btrfs_file_inode_operations;
10466
10467 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468
Chris Masonb0d5d102014-09-08 13:08:51 -070010469 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10470 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010471 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010472
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010473 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010474 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010475 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010476 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010477 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010478 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010479
Filipe Manana5762b5c2014-08-01 00:10:32 +010010480 /*
10481 * We set number of links to 0 in btrfs_new_inode(), and here we set
10482 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10483 * through:
10484 *
10485 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10486 */
10487 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010488 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010489 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010490 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010491out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010492 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010493 if (ret && inode)
10494 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010495 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010496 return ret;
10497}
10498
Qu Wenruod2a91062021-05-31 16:50:49 +080010499void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010500{
Qu Wenruod2a91062021-05-31 16:50:49 +080010501 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010502 unsigned long index = start >> PAGE_SHIFT;
10503 unsigned long end_index = end >> PAGE_SHIFT;
10504 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010505 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010506
Qu Wenruod2a91062021-05-31 16:50:49 +080010507 ASSERT(end + 1 - start <= U32_MAX);
10508 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010509 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010510 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010511 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010512
10513 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010514 put_page(page);
10515 index++;
10516 }
10517}
10518
Omar Sandovaled46ff32016-11-03 10:28:14 -070010519#ifdef CONFIG_SWAP
10520/*
10521 * Add an entry indicating a block group or device which is pinned by a
10522 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10523 * negative errno on failure.
10524 */
10525static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10526 bool is_block_group)
10527{
10528 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10529 struct btrfs_swapfile_pin *sp, *entry;
10530 struct rb_node **p;
10531 struct rb_node *parent = NULL;
10532
10533 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10534 if (!sp)
10535 return -ENOMEM;
10536 sp->ptr = ptr;
10537 sp->inode = inode;
10538 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010539 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010540
10541 spin_lock(&fs_info->swapfile_pins_lock);
10542 p = &fs_info->swapfile_pins.rb_node;
10543 while (*p) {
10544 parent = *p;
10545 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10546 if (sp->ptr < entry->ptr ||
10547 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10548 p = &(*p)->rb_left;
10549 } else if (sp->ptr > entry->ptr ||
10550 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10551 p = &(*p)->rb_right;
10552 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010553 if (is_block_group)
10554 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010555 spin_unlock(&fs_info->swapfile_pins_lock);
10556 kfree(sp);
10557 return 1;
10558 }
10559 }
10560 rb_link_node(&sp->node, parent, p);
10561 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10562 spin_unlock(&fs_info->swapfile_pins_lock);
10563 return 0;
10564}
10565
10566/* Free all of the entries pinned by this swapfile. */
10567static void btrfs_free_swapfile_pins(struct inode *inode)
10568{
10569 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10570 struct btrfs_swapfile_pin *sp;
10571 struct rb_node *node, *next;
10572
10573 spin_lock(&fs_info->swapfile_pins_lock);
10574 node = rb_first(&fs_info->swapfile_pins);
10575 while (node) {
10576 next = rb_next(node);
10577 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10578 if (sp->inode == inode) {
10579 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010580 if (sp->is_block_group) {
10581 btrfs_dec_block_group_swap_extents(sp->ptr,
10582 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010583 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010584 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010585 kfree(sp);
10586 }
10587 node = next;
10588 }
10589 spin_unlock(&fs_info->swapfile_pins_lock);
10590}
10591
10592struct btrfs_swap_info {
10593 u64 start;
10594 u64 block_start;
10595 u64 block_len;
10596 u64 lowest_ppage;
10597 u64 highest_ppage;
10598 unsigned long nr_pages;
10599 int nr_extents;
10600};
10601
10602static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10603 struct btrfs_swap_info *bsi)
10604{
10605 unsigned long nr_pages;
10606 u64 first_ppage, first_ppage_reported, next_ppage;
10607 int ret;
10608
10609 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10610 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10611 PAGE_SIZE) >> PAGE_SHIFT;
10612
10613 if (first_ppage >= next_ppage)
10614 return 0;
10615 nr_pages = next_ppage - first_ppage;
10616
10617 first_ppage_reported = first_ppage;
10618 if (bsi->start == 0)
10619 first_ppage_reported++;
10620 if (bsi->lowest_ppage > first_ppage_reported)
10621 bsi->lowest_ppage = first_ppage_reported;
10622 if (bsi->highest_ppage < (next_ppage - 1))
10623 bsi->highest_ppage = next_ppage - 1;
10624
10625 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10626 if (ret < 0)
10627 return ret;
10628 bsi->nr_extents += ret;
10629 bsi->nr_pages += nr_pages;
10630 return 0;
10631}
10632
10633static void btrfs_swap_deactivate(struct file *file)
10634{
10635 struct inode *inode = file_inode(file);
10636
10637 btrfs_free_swapfile_pins(inode);
10638 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10639}
10640
10641static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10642 sector_t *span)
10643{
10644 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010645 struct btrfs_root *root = BTRFS_I(inode)->root;
10646 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10648 struct extent_state *cached_state = NULL;
10649 struct extent_map *em = NULL;
10650 struct btrfs_device *device = NULL;
10651 struct btrfs_swap_info bsi = {
10652 .lowest_ppage = (sector_t)-1ULL,
10653 };
10654 int ret = 0;
10655 u64 isize;
10656 u64 start;
10657
10658 /*
10659 * If the swap file was just created, make sure delalloc is done. If the
10660 * file changes again after this, the user is doing something stupid and
10661 * we don't really care.
10662 */
10663 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10664 if (ret)
10665 return ret;
10666
10667 /*
10668 * The inode is locked, so these flags won't change after we check them.
10669 */
10670 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10671 btrfs_warn(fs_info, "swapfile must not be compressed");
10672 return -EINVAL;
10673 }
10674 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10675 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10676 return -EINVAL;
10677 }
10678 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10679 btrfs_warn(fs_info, "swapfile must not be checksummed");
10680 return -EINVAL;
10681 }
10682
10683 /*
10684 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010685 * under us. The exclop protection makes sure they aren't running/won't
10686 * run concurrently while we are mapping the swap extents, and
10687 * fs_info->swapfile_pins prevents them from running while the swap
10688 * file is active and moving the extents. Note that this also prevents
10689 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010690 * really worth the trouble to allow it.
10691 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010692 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010693 btrfs_warn(fs_info,
10694 "cannot activate swapfile while exclusive operation is running");
10695 return -EBUSY;
10696 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010697
10698 /*
10699 * Prevent snapshot creation while we are activating the swap file.
10700 * We do not want to race with snapshot creation. If snapshot creation
10701 * already started before we bumped nr_swapfiles from 0 to 1 and
10702 * completes before the first write into the swap file after it is
10703 * activated, than that write would fallback to COW.
10704 */
10705 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10706 btrfs_exclop_finish(fs_info);
10707 btrfs_warn(fs_info,
10708 "cannot activate swapfile because snapshot creation is in progress");
10709 return -EINVAL;
10710 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010711 /*
10712 * Snapshots can create extents which require COW even if NODATACOW is
10713 * set. We use this counter to prevent snapshots. We must increment it
10714 * before walking the extents because we don't want a concurrent
10715 * snapshot to run after we've already checked the extents.
10716 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010717 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010718
10719 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10720
10721 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10722 start = 0;
10723 while (start < isize) {
10724 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010725 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010726 u64 len = isize - start;
10727
Omar Sandoval39b07b52019-12-02 17:34:23 -080010728 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010729 if (IS_ERR(em)) {
10730 ret = PTR_ERR(em);
10731 goto out;
10732 }
10733
10734 if (em->block_start == EXTENT_MAP_HOLE) {
10735 btrfs_warn(fs_info, "swapfile must not have holes");
10736 ret = -EINVAL;
10737 goto out;
10738 }
10739 if (em->block_start == EXTENT_MAP_INLINE) {
10740 /*
10741 * It's unlikely we'll ever actually find ourselves
10742 * here, as a file small enough to fit inline won't be
10743 * big enough to store more than the swap header, but in
10744 * case something changes in the future, let's catch it
10745 * here rather than later.
10746 */
10747 btrfs_warn(fs_info, "swapfile must not be inline");
10748 ret = -EINVAL;
10749 goto out;
10750 }
10751 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10752 btrfs_warn(fs_info, "swapfile must not be compressed");
10753 ret = -EINVAL;
10754 goto out;
10755 }
10756
10757 logical_block_start = em->block_start + (start - em->start);
10758 len = min(len, em->len - (start - em->start));
10759 free_extent_map(em);
10760 em = NULL;
10761
Boris Burkova84d5d42020-08-18 11:00:05 -070010762 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010763 if (ret < 0) {
10764 goto out;
10765 } else if (ret) {
10766 ret = 0;
10767 } else {
10768 btrfs_warn(fs_info,
10769 "swapfile must not be copy-on-write");
10770 ret = -EINVAL;
10771 goto out;
10772 }
10773
10774 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10775 if (IS_ERR(em)) {
10776 ret = PTR_ERR(em);
10777 goto out;
10778 }
10779
10780 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10781 btrfs_warn(fs_info,
10782 "swapfile must have single data profile");
10783 ret = -EINVAL;
10784 goto out;
10785 }
10786
10787 if (device == NULL) {
10788 device = em->map_lookup->stripes[0].dev;
10789 ret = btrfs_add_swapfile_pin(inode, device, false);
10790 if (ret == 1)
10791 ret = 0;
10792 else if (ret)
10793 goto out;
10794 } else if (device != em->map_lookup->stripes[0].dev) {
10795 btrfs_warn(fs_info, "swapfile must be on one device");
10796 ret = -EINVAL;
10797 goto out;
10798 }
10799
10800 physical_block_start = (em->map_lookup->stripes[0].physical +
10801 (logical_block_start - em->start));
10802 len = min(len, em->len - (logical_block_start - em->start));
10803 free_extent_map(em);
10804 em = NULL;
10805
10806 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10807 if (!bg) {
10808 btrfs_warn(fs_info,
10809 "could not find block group containing swapfile");
10810 ret = -EINVAL;
10811 goto out;
10812 }
10813
Filipe Manana195a49e2021-02-05 12:55:37 +000010814 if (!btrfs_inc_block_group_swap_extents(bg)) {
10815 btrfs_warn(fs_info,
10816 "block group for swapfile at %llu is read-only%s",
10817 bg->start,
10818 atomic_read(&fs_info->scrubs_running) ?
10819 " (scrub running)" : "");
10820 btrfs_put_block_group(bg);
10821 ret = -EINVAL;
10822 goto out;
10823 }
10824
Omar Sandovaled46ff32016-11-03 10:28:14 -070010825 ret = btrfs_add_swapfile_pin(inode, bg, true);
10826 if (ret) {
10827 btrfs_put_block_group(bg);
10828 if (ret == 1)
10829 ret = 0;
10830 else
10831 goto out;
10832 }
10833
10834 if (bsi.block_len &&
10835 bsi.block_start + bsi.block_len == physical_block_start) {
10836 bsi.block_len += len;
10837 } else {
10838 if (bsi.block_len) {
10839 ret = btrfs_add_swap_extent(sis, &bsi);
10840 if (ret)
10841 goto out;
10842 }
10843 bsi.start = start;
10844 bsi.block_start = physical_block_start;
10845 bsi.block_len = len;
10846 }
10847
10848 start += len;
10849 }
10850
10851 if (bsi.block_len)
10852 ret = btrfs_add_swap_extent(sis, &bsi);
10853
10854out:
10855 if (!IS_ERR_OR_NULL(em))
10856 free_extent_map(em);
10857
10858 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10859
10860 if (ret)
10861 btrfs_swap_deactivate(file);
10862
Filipe Mananadd0734f2021-02-05 12:55:38 +000010863 btrfs_drew_write_unlock(&root->snapshot_lock);
10864
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010865 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010866
10867 if (ret)
10868 return ret;
10869
10870 if (device)
10871 sis->bdev = device->bdev;
10872 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10873 sis->max = bsi.nr_pages;
10874 sis->pages = bsi.nr_pages - 1;
10875 sis->highest_bit = bsi.nr_pages - 1;
10876 return bsi.nr_extents;
10877}
10878#else
10879static void btrfs_swap_deactivate(struct file *file)
10880{
10881}
10882
10883static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10884 sector_t *span)
10885{
10886 return -EOPNOTSUPP;
10887}
10888#endif
10889
Filipe Manana2766ff62020-11-04 11:07:34 +000010890/*
10891 * Update the number of bytes used in the VFS' inode. When we replace extents in
10892 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10893 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10894 * always get a correct value.
10895 */
10896void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10897 const u64 add_bytes,
10898 const u64 del_bytes)
10899{
10900 if (add_bytes == del_bytes)
10901 return;
10902
10903 spin_lock(&inode->lock);
10904 if (del_bytes > 0)
10905 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10906 if (add_bytes > 0)
10907 inode_add_bytes(&inode->vfs_inode, add_bytes);
10908 spin_unlock(&inode->lock);
10909}
10910
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010911static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010912 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010913 .lookup = btrfs_lookup,
10914 .create = btrfs_create,
10915 .unlink = btrfs_unlink,
10916 .link = btrfs_link,
10917 .mkdir = btrfs_mkdir,
10918 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010919 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010920 .symlink = btrfs_symlink,
10921 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010922 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010923 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010924 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010925 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010926 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010927 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010928 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010929 .fileattr_get = btrfs_fileattr_get,
10930 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010931};
Yan, Zheng76dda932009-09-21 16:00:26 -040010932
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010933static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010934 .llseek = generic_file_llseek,
10935 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010936 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010937 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010938 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010939#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010940 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010941#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010942 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010943 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010944};
10945
Chris Mason35054392009-01-21 13:11:13 -050010946/*
10947 * btrfs doesn't support the bmap operation because swapfiles
10948 * use bmap to make a mapping of extents in the file. They assume
10949 * these extents won't change over the life of the file and they
10950 * use the bmap result to do IO directly to the drive.
10951 *
10952 * the btrfs bmap call would return logical addresses that aren't
10953 * suitable for IO and they also will change frequently as COW
10954 * operations happen. So, swapfile + btrfs == corruption.
10955 *
10956 * For now we're avoiding this by dropping bmap.
10957 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010958static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010959 .readpage = btrfs_readpage,
10960 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010961 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010962 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010963 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010964 .invalidatepage = btrfs_invalidatepage,
10965 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010966#ifdef CONFIG_MIGRATION
10967 .migratepage = btrfs_migratepage,
10968#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010969 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010970 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010971 .swap_activate = btrfs_swap_activate,
10972 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010973};
10974
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010975static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010976 .getattr = btrfs_getattr,
10977 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010978 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010979 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010980 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010981 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010982 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010983 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010984 .fileattr_get = btrfs_fileattr_get,
10985 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010986};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010987static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010988 .getattr = btrfs_getattr,
10989 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010990 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010991 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010992 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010993 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010994 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010995};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010996static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010997 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010998 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010999 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011000 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011001 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011002 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011003};
Yan, Zheng76dda932009-09-21 16:00:26 -040011004
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011005const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011006 .d_delete = btrfs_dentry_delete,
11007};