blob: 3b2403b6127ff948615802450e1b2e6b9f4a68cc [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"
Josef Bacik26c2c452021-12-03 17:18:03 -050057#include "inode-item.h"
Chris Mason39279cc2007-06-12 06:35:45 -040058
59struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020060 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040061 struct btrfs_root *root;
62};
63
Filipe Mananaf28a4922015-12-08 19:23:20 +000064struct btrfs_dio_data {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050065 ssize_t submitted;
66 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000067};
68
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070071static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070074static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
77struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000080struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
Eric Sandeen3972f262013-01-12 02:57:22 +000082static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000083static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040084static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030085static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050086 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030087 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030088 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030089static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080091 u64 block_len, u64 orig_block_len,
92 u64 ram_bytes, int compress_type,
93 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030095static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080096 const u64 offset, const u64 bytes,
97 const bool uptodate);
98
99/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500100 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
101 *
102 * ilock_flags can have the following bit set:
103 *
104 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
105 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
106 * return -EAGAIN
Josef Bacik8318ba72021-02-10 17:14:33 -0500107 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500108 */
109int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
110{
111 if (ilock_flags & BTRFS_ILOCK_SHARED) {
112 if (ilock_flags & BTRFS_ILOCK_TRY) {
113 if (!inode_trylock_shared(inode))
114 return -EAGAIN;
115 else
116 return 0;
117 }
118 inode_lock_shared(inode);
119 } else {
120 if (ilock_flags & BTRFS_ILOCK_TRY) {
121 if (!inode_trylock(inode))
122 return -EAGAIN;
123 else
124 return 0;
125 }
126 inode_lock(inode);
127 }
Josef Bacik8318ba72021-02-10 17:14:33 -0500128 if (ilock_flags & BTRFS_ILOCK_MMAP)
129 down_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500130 return 0;
131}
132
133/*
134 * btrfs_inode_unlock - unock inode i_rwsem
135 *
136 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
137 * to decide whether the lock acquired is shared or exclusive.
138 */
139void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
140{
Josef Bacik8318ba72021-02-10 17:14:33 -0500141 if (ilock_flags & BTRFS_ILOCK_MMAP)
142 up_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500143 if (ilock_flags & BTRFS_ILOCK_SHARED)
144 inode_unlock_shared(inode);
145 else
146 inode_unlock(inode);
147}
148
149/*
Qu Wenruo524272602017-03-08 10:25:52 +0800150 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100151 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800152 *
153 * NOTE: caller must ensure that when an error happens, it can not call
154 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
155 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
156 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200157 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800158 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300159static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200160 struct page *locked_page,
161 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800162{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900163 unsigned long index = offset >> PAGE_SHIFT;
164 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200165 u64 page_start = page_offset(locked_page);
166 u64 page_end = page_start + PAGE_SIZE - 1;
167
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900168 struct page *page;
169
170 while (index <= end_index) {
Qu Wenruo968f2562021-05-18 15:09:41 +0800171 /*
172 * For locked page, we will call end_extent_writepage() on it
173 * in run_delalloc_range() for the error handling. That
174 * end_extent_writepage() function will call
175 * btrfs_mark_ordered_io_finished() to clear page Ordered and
176 * run the ordered extent accounting.
177 *
178 * Here we can't just clear the Ordered bit, or
179 * btrfs_mark_ordered_io_finished() would skip the accounting
180 * for the page range, and the ordered extent will never finish.
181 */
182 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
183 index++;
184 continue;
185 }
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300186 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900187 index++;
188 if (!page)
189 continue;
Qu Wenruo968f2562021-05-18 15:09:41 +0800190
191 /*
192 * Here we just clear all Ordered bits for every page in the
193 * range, then __endio_write_update_ordered() will handle
194 * the ordered extent accounting for the range.
195 */
Qu Wenruob945a462021-05-31 16:50:46 +0800196 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
197 offset, bytes);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900198 put_page(page);
199 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200200
Qu Wenruob945a462021-05-31 16:50:46 +0800201 /* The locked page covers the full range, nothing needs to be done */
202 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
203 return;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200204 /*
205 * In case this page belongs to the delalloc range being instantiated
206 * then skip it, since the first page of a range is going to be
207 * properly cleaned up by the caller of run_delalloc_range
208 */
209 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
Qu Wenruob945a462021-05-31 16:50:46 +0800210 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
211 offset = page_offset(locked_page) + PAGE_SIZE;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200212 }
213
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300214 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800215}
216
Eric Sandeen48a3b632013-04-25 20:41:01 +0000217static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400218
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000219static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500220 struct inode *inode, struct inode *dir,
221 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500222{
223 int err;
224
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000225 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500226 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500227 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500228 return err;
229}
230
Chris Masond352ac62008-09-29 15:18:18 -0400231/*
Chris Masonc8b97812008-10-29 14:49:59 -0400232 * this does all the hard work for inserting an inline extent into
233 * the btree. The caller should have done a btrfs_drop_extents so that
234 * no overlapping inline items exist in the btree
235 */
Chris Mason40f76582014-05-21 13:35:51 -0700236static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000237 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400238 struct btrfs_root *root, struct inode *inode,
239 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000240 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400241 struct page **compressed_pages)
242{
Chris Masonc8b97812008-10-29 14:49:59 -0400243 struct extent_buffer *leaf;
244 struct page *page = NULL;
245 char *kaddr;
246 unsigned long ptr;
247 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400248 int ret;
249 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400250 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400251
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800252 ASSERT((compressed_size > 0 && compressed_pages) ||
253 (compressed_size == 0 && !compressed_pages));
254
Li Zefanfe3f5662011-03-28 08:30:38 +0000255 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400256 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000258 if (!extent_inserted) {
259 struct btrfs_key key;
260 size_t datasize;
261
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200262 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000263 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200264 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000265
266 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000267 ret = btrfs_insert_empty_item(trans, root, path, &key,
268 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200269 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000270 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400271 }
272 leaf = path->nodes[0];
273 ei = btrfs_item_ptr(leaf, path->slots[0],
274 struct btrfs_file_extent_item);
275 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
276 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
277 btrfs_set_file_extent_encryption(leaf, ei, 0);
278 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
279 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
280 ptr = btrfs_file_extent_inline_start(ei);
281
Li Zefan261507a02010-12-17 14:21:50 +0800282 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400283 struct page *cpage;
284 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500285 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400286 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500287 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300288 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400289
David Sterba3a60f652021-10-27 10:39:03 +0200290 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 write_extent_buffer(leaf, kaddr, ptr, cur_size);
David Sterba3a60f652021-10-27 10:39:03 +0200292 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400293
294 i++;
295 ptr += cur_size;
296 compressed_size -= cur_size;
297 }
298 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800299 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400300 } else {
301 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300302 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400303 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800304 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100305 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400306 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800307 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300308 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400309 }
310 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400312
Yan, Zhengc2167752009-11-12 09:34:21 +0000313 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500314 * We align size to sectorsize for inline extents just for simplicity
315 * sake.
316 */
317 size = ALIGN(size, root->fs_info->sectorsize);
318 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
319 if (ret)
320 goto fail;
321
322 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000323 * we're an inline extent, so nobody can
324 * extend the file past i_size without locking
325 * a page we already have locked.
326 *
327 * We must do any isize and inode updates
328 * before we unlock the pages. Otherwise we
329 * could end up racing with unlink.
330 */
Chris Masonc8b97812008-10-29 14:49:59 -0400331 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400332fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200333 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400334}
335
336
337/*
338 * conditionally insert an inline extent into the file. This
339 * does the checks required to make sure the data is small enough
340 * to fit as an inline extent.
341 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300342static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400343 u64 end, size_t compressed_size,
344 int compress_type,
345 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400346{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000347 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300348 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400349 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400350 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300351 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400352 u64 actual_end = min(end + 1, isize);
353 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400354 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400355 u64 data_len = inline_len;
356 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000357 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400358
359 if (compressed_size)
360 data_len = compressed_size;
361
362 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400363 actual_end > fs_info->sectorsize ||
364 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400365 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400366 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400367 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400368 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400369 return 1;
370 }
371
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000372 path = btrfs_alloc_path();
373 if (!path)
374 return -ENOMEM;
375
Josef Bacik00361582013-08-14 14:02:47 -0400376 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000377 if (IS_ERR(trans)) {
378 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400379 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000380 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300381 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400382
Filipe Manana5893dfb2020-11-04 11:07:32 +0000383 drop_args.path = path;
384 drop_args.start = start;
385 drop_args.end = aligned_end;
386 drop_args.drop_cache = true;
387 drop_args.replace_extent = true;
388
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000389 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000390 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000391 compressed_size);
392 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000393 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000394 inline_len);
395
Filipe Manana5893dfb2020-11-04 11:07:32 +0000396 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400397 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400398 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400399 goto out;
400 }
Chris Masonc8b97812008-10-29 14:49:59 -0400401
402 if (isize > actual_end)
403 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000404 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300405 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400406 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000407 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400408 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400409 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400410 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400411 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400412 ret = 1;
413 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100414 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400415
Filipe Manana2766ff62020-11-04 11:07:34 +0000416 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200417 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000418 if (ret && ret != -ENOSPC) {
419 btrfs_abort_transaction(trans, ret);
420 goto out;
421 } else if (ret == -ENOSPC) {
422 ret = 1;
423 goto out;
424 }
425
Nikolay Borisova0349402020-06-03 08:55:12 +0300426 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400427out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800428 /*
429 * Don't forget to free the reserved space, as for inlined extent
430 * it won't count as data extent, free them directly here.
431 * And at reserve time, it's always aligned to page size, so
432 * just free one page here.
433 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300434 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000435 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400436 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400437 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400438}
439
Chris Mason771ed682008-11-06 22:02:51 -0500440struct async_extent {
441 u64 start;
442 u64 ram_size;
443 u64 compressed_size;
444 struct page **pages;
445 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800446 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500447 struct list_head list;
448};
449
Nikolay Borisov97db1202019-03-12 17:20:24 +0200450struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500451 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500452 struct page *locked_page;
453 u64 start;
454 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600455 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500456 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700457 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500458 struct btrfs_work work;
Qu Wenruo9e895a82021-09-27 15:21:45 +0800459 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -0500460};
461
Nikolay Borisov97db1202019-03-12 17:20:24 +0200462struct async_cow {
Nikolay Borisov97db1202019-03-12 17:20:24 +0200463 atomic_t num_chunks;
464 struct async_chunk chunks[];
465};
466
467static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500468 u64 start, u64 ram_size,
469 u64 compressed_size,
470 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800471 unsigned long nr_pages,
472 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500473{
474 struct async_extent *async_extent;
475
476 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100477 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500478 async_extent->start = start;
479 async_extent->ram_size = ram_size;
480 async_extent->compressed_size = compressed_size;
481 async_extent->pages = pages;
482 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800483 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500484 list_add_tail(&async_extent->list, &cow->extents);
485 return 0;
486}
487
Qu Wenruo42c16da2019-07-01 05:12:46 +0000488/*
489 * Check if the inode has flags compatible with compression
490 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300491static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000492{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300493 if (inode->flags & BTRFS_INODE_NODATACOW ||
494 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000495 return false;
496 return true;
497}
498
499/*
500 * Check if the inode needs to be submitted to compression, based on mount
501 * options, defragmentation, properties or heuristics.
502 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300503static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
504 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800505{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300506 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800507
Nikolay Borisov808a1292020-06-03 08:55:27 +0300508 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000509 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
510 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300511 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000512 return 0;
513 }
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800514 /*
515 * Special check for subpage.
516 *
517 * We lock the full page then run each delalloc range in the page, thus
518 * for the following case, we will hit some subpage specific corner case:
519 *
520 * 0 32K 64K
521 * | |///////| |///////|
522 * \- A \- B
523 *
524 * In above case, both range A and range B will try to unlock the full
525 * page [0, 64K), causing the one finished later will have page
526 * unlocked already, triggering various page lock requirement BUG_ON()s.
527 *
528 * So here we add an artificial limit that subpage compression can only
529 * if the range is fully page aligned.
530 *
531 * In theory we only need to ensure the first page is fully covered, but
532 * the tailing partial page will be locked until the full compression
533 * finishes, delaying the write of other range.
534 *
535 * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range
536 * first to prevent any submitted async extent to unlock the full page.
537 * By this, we can ensure for subpage case that only the last async_cow
538 * will unlock the full page.
539 */
540 if (fs_info->sectorsize < PAGE_SIZE) {
541 if (!IS_ALIGNED(start, PAGE_SIZE) ||
542 !IS_ALIGNED(end + 1, PAGE_SIZE))
543 return 0;
544 }
545
Wang Shilongf79707b2014-07-17 11:44:09 +0800546 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400547 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800548 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200549 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300550 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200551 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800552 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300553 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800554 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400555 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300556 inode->flags & BTRFS_INODE_COMPRESS ||
557 inode->prop_compress)
558 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800559 return 0;
560}
561
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200562static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800563 u64 start, u64 end, u64 num_bytes, u64 small_write)
564{
565 /* If this is a small write inside eof, kick off a defrag */
566 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200567 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800568 btrfs_add_inode_defrag(NULL, inode);
569}
570
Chris Masonc8b97812008-10-29 14:49:59 -0400571/*
Chris Mason771ed682008-11-06 22:02:51 -0500572 * we create compressed extents in two phases. The first
573 * phase compresses a range of pages that have already been
574 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400575 *
Chris Mason771ed682008-11-06 22:02:51 -0500576 * This is done inside an ordered work queue, and the compression
577 * is spread across many cpus. The actual IO submission is step
578 * two, and the ordered work queue takes care of making sure that
579 * happens in the same order things were put onto the queue by
580 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 *
Chris Mason771ed682008-11-06 22:02:51 -0500582 * If this code finds it can't get good compression, it puts an
583 * entry onto the work queue to write the uncompressed bytes. This
584 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300585 * are written in the same order that the flusher thread sent them
586 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400587 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300588static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400589{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200590 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400591 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400592 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200593 u64 start = async_chunk->start;
594 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400595 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400596 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400597 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400598 struct page **pages = NULL;
599 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400600 unsigned long total_compressed = 0;
601 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400602 int i;
603 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400604 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300605 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400606 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400607
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200608 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
609 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400610
Josef Bacikd98da492019-10-11 09:03:54 -0400611 /*
612 * We need to save i_size before now because it could change in between
613 * us evaluating the size and assigning it. This is because we lock and
614 * unlock the page in truncate and fallocate, and then modify the i_size
615 * later on.
616 *
617 * The barriers are to emulate READ_ONCE, remove that once i_size_read
618 * does that for us.
619 */
620 barrier();
621 i_size = i_size_read(inode);
622 barrier();
623 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400624again:
625 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300626 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100627 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
628 nr_pages = min_t(unsigned long, nr_pages,
629 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400630
Chris Masonf03d9301f2009-02-04 09:31:06 -0500631 /*
632 * we don't want to send crud past the end of i_size through
633 * compression, that's just a waste of CPU time. So, if the
634 * end of the file is before the start of our current
635 * requested range of bytes, we bail out to the uncompressed
636 * cleanup code that can deal with all of this.
637 *
638 * It isn't really the fastest way to fix things, but this is a
639 * very uncommon corner.
640 */
641 if (actual_end <= start)
642 goto cleanup_and_bail_uncompressed;
643
Chris Masonc8b97812008-10-29 14:49:59 -0400644 total_compressed = actual_end - start;
645
Shilong Wang4bcbb332014-10-07 18:44:35 -0400646 /*
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800647 * Skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400648 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400649 */
650 if (total_compressed <= blocksize &&
651 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
652 goto cleanup_and_bail_uncompressed;
653
Qu Wenruo0cf9b242021-09-27 15:22:08 +0800654 /*
655 * For subpage case, we require full page alignment for the sector
656 * aligned range.
657 * Thus we must also check against @actual_end, not just @end.
658 */
659 if (blocksize < PAGE_SIZE) {
660 if (!IS_ALIGNED(start, PAGE_SIZE) ||
661 !IS_ALIGNED(round_up(actual_end, blocksize), PAGE_SIZE))
662 goto cleanup_and_bail_uncompressed;
663 }
664
David Sterba069eac72017-02-14 19:36:54 +0100665 total_compressed = min_t(unsigned long, total_compressed,
666 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400667 total_in = 0;
668 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400669
Chris Mason771ed682008-11-06 22:02:51 -0500670 /*
671 * we do compression for mount -o compress and when the
672 * inode has not been flagged as nocompress. This flag can
673 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400674 */
Qu Wenruo4e965572021-08-25 13:41:42 +0800675 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400676 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100677 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800678 if (!pages) {
679 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100680 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800681 goto cont;
682 }
Chris Mason179e29e2007-11-01 11:28:41 -0400683
David Sterbaeec63c62017-07-17 19:41:31 +0200684 if (BTRFS_I(inode)->defrag_compress)
685 compress_type = BTRFS_I(inode)->defrag_compress;
686 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200687 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800688
Chris Mason4adaa612013-03-26 13:07:00 -0400689 /*
690 * we need to call clear_page_dirty_for_io on each
691 * page in the range. Otherwise applications with the file
692 * mmap'd can wander in and change the page contents while
693 * we are compressing them.
694 *
695 * If the compression fails for any reason, we set the pages
696 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300697 *
698 * Note that the remaining part is redirtied, the start pointer
699 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400700 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300701 if (!redirty) {
702 extent_range_clear_dirty_for_io(inode, start, end);
703 redirty = 1;
704 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200705
706 /* Compression level is applied here and only here */
707 ret = btrfs_compress_pages(
708 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800709 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100710 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100711 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800712 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100713 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400714
715 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100716 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100717 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400718
719 /* zero the tail end of the last page, we might be
720 * sending it down to disk
721 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700722 if (offset)
723 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400724 will_compress = 1;
725 }
726 }
Li Zefan560f7d72011-09-08 10:22:01 +0800727cont:
Qu Wenruo73672532021-07-26 14:34:59 +0800728 /*
729 * Check cow_file_range() for why we don't even try to create inline
730 * extent for subpage case.
731 */
732 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400733 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300734 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400735 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500736 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400737 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300738 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
739 0, BTRFS_COMPRESS_NONE,
740 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400741 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500742 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300743 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000744 total_compressed,
745 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400746 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400748 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400749 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
750 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100751 unsigned long page_error_op;
752
Filipe Mananae6eb4312014-10-10 10:45:12 +0100753 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400754
Chris Mason771ed682008-11-06 22:02:51 -0500755 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100756 * inline extent creation worked or returned error,
757 * we don't need to create any more async work items.
758 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400759 *
760 * We use DO_ACCOUNTING here because we need the
761 * delalloc_release_metadata to be done _after_ we drop
762 * our outstanding extent for clearing delalloc for this
763 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500764 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300765 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
766 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300767 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800768 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800769 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100770 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400771 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300772
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800773 /*
774 * Ensure we only free the compressed pages if we have
775 * them allocated, as we can still reach here with
776 * inode_need_compress() == false.
777 */
778 if (pages) {
779 for (i = 0; i < nr_pages; i++) {
780 WARN_ON(pages[i]->mapping);
781 put_page(pages[i]);
782 }
783 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300784 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300785 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400786 }
787 }
788
789 if (will_compress) {
790 /*
791 * we aren't doing an inline extent round the compressed size
792 * up to a block size boundary so the allocator does sane
793 * things
794 */
Qu Wenruofda28322013-02-26 08:10:22 +0000795 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400796
797 /*
798 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300799 * win, compare the page count read with the blocks on disk,
800 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400801 */
Qu Wenruo4c162772021-09-27 15:21:59 +0800802 total_in = round_up(total_in, fs_info->sectorsize);
Timofey Titovets170607e2017-06-06 14:41:15 +0300803 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300804 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700805
806 /*
807 * The async work queues will take care of doing actual
808 * allocation on disk for these compressed pages, and
809 * will submit them to the elevator.
810 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200811 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100812 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700813 compress_type);
814
Timofey Titovets11708622017-10-03 18:06:01 +0300815 if (start + total_in < end) {
816 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700817 pages = NULL;
818 cond_resched();
819 goto again;
820 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300821 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400822 }
823 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700824 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400825 /*
826 * the compression code ran but failed to make things smaller,
827 * free any pages it allocated and our page pointer array
828 */
David Sterba4d3a8002017-02-14 19:04:07 +0100829 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400830 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300831 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400832 }
833 kfree(pages);
834 pages = NULL;
835 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100836 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400837
838 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400839 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200840 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500841 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500842 }
Chris Masonc8b97812008-10-29 14:49:59 -0400843 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500844cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700845 /*
846 * No compression, but we still need to write the pages in the file
847 * we've been given so far. redirty the locked page if it corresponds
848 * to our extent and set things up for the async work queue to run
849 * cow_file_range to do the normal delalloc dance.
850 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700851 if (async_chunk->locked_page &&
852 (page_offset(async_chunk->locked_page) >= start &&
853 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200854 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700855 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700856 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700857
858 if (redirty)
859 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200860 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700861 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300862 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500863
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300864 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500865}
Chris Mason771ed682008-11-06 22:02:51 -0500866
Filipe Manana40ae8372014-10-06 22:14:24 +0100867static void free_async_extent_pages(struct async_extent *async_extent)
868{
869 int i;
870
871 if (!async_extent->pages)
872 return;
873
874 for (i = 0; i < async_extent->nr_pages; i++) {
875 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300876 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100877 }
878 kfree(async_extent->pages);
879 async_extent->nr_pages = 0;
880 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500881}
882
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800883static int submit_uncompressed_range(struct btrfs_inode *inode,
884 struct async_extent *async_extent,
885 struct page *locked_page)
Chris Mason771ed682008-11-06 22:02:51 -0500886{
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800887 u64 start = async_extent->start;
888 u64 end = async_extent->start + async_extent->ram_size - 1;
889 unsigned long nr_written = 0;
890 int page_started = 0;
891 int ret;
Chris Mason771ed682008-11-06 22:02:51 -0500892
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800893 /*
894 * Call cow_file_range() to run the delalloc range directly, since we
895 * won't go to NOCOW or async path again.
896 *
897 * Also we call cow_file_range() with @unlock_page == 0, so that we
898 * can directly submit them without interruption.
899 */
900 ret = cow_file_range(inode, locked_page, start, end, &page_started,
901 &nr_written, 0);
902 /* Inline extent inserted, page gets unlocked and everything is done */
903 if (page_started) {
904 ret = 0;
905 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500906 }
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800907 if (ret < 0) {
908 if (locked_page)
909 unlock_page(locked_page);
910 goto out;
911 }
912
913 ret = extent_write_locked_range(&inode->vfs_inode, start, end);
914 /* All pages will be unlocked, including @locked_page */
915out:
916 kfree(async_extent);
917 return ret;
918}
919
Qu Wenruob4ccace2021-09-27 15:21:57 +0800920static int submit_one_async_extent(struct btrfs_inode *inode,
921 struct async_chunk *async_chunk,
922 struct async_extent *async_extent,
923 u64 *alloc_hint)
Chris Mason771ed682008-11-06 22:02:51 -0500924{
Qu Wenruob4ccace2021-09-27 15:21:57 +0800925 struct extent_io_tree *io_tree = &inode->io_tree;
926 struct btrfs_root *root = inode->root;
927 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500928 struct btrfs_key ins;
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800929 struct page *locked_page = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500930 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500931 int ret = 0;
Qu Wenruob4ccace2021-09-27 15:21:57 +0800932 u64 start = async_extent->start;
933 u64 end = async_extent->start + async_extent->ram_size - 1;
Chris Mason771ed682008-11-06 22:02:51 -0500934
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800935 /*
936 * If async_chunk->locked_page is in the async_extent range, we need to
937 * handle it.
938 */
939 if (async_chunk->locked_page) {
940 u64 locked_page_start = page_offset(async_chunk->locked_page);
941 u64 locked_page_end = locked_page_start + PAGE_SIZE - 1;
942
943 if (!(start >= locked_page_end || end <= locked_page_start))
944 locked_page = async_chunk->locked_page;
945 }
Qu Wenruob4ccace2021-09-27 15:21:57 +0800946 lock_extent(io_tree, start, end);
Chris Mason771ed682008-11-06 22:02:51 -0500947
Qu Wenruo2b83a0e2021-09-27 15:22:03 +0800948 /* We have fall back to uncompressed write */
949 if (!async_extent->pages)
950 return submit_uncompressed_range(inode, async_extent, locked_page);
Qu Wenruob4ccace2021-09-27 15:21:57 +0800951
952 ret = btrfs_reserve_extent(root, async_extent->ram_size,
953 async_extent->compressed_size,
954 async_extent->compressed_size,
955 0, *alloc_hint, &ins, 1, 1);
956 if (ret) {
957 free_async_extent_pages(async_extent);
958 /*
959 * Here we used to try again by going back to non-compressed
960 * path for ENOSPC. But we can't reserve space even for
961 * compressed size, how could it work for uncompressed size
962 * which requires larger size? So here we directly go error
963 * path.
964 */
965 goto out_free;
966 }
967
968 /* Here we're doing allocation and writeback of the compressed pages */
969 em = create_io_em(inode, start,
970 async_extent->ram_size, /* len */
971 start, /* orig_start */
972 ins.objectid, /* block_start */
973 ins.offset, /* block_len */
974 ins.offset, /* orig_block_len */
975 async_extent->ram_size, /* ram_bytes */
976 async_extent->compress_type,
977 BTRFS_ORDERED_COMPRESSED);
978 if (IS_ERR(em)) {
979 ret = PTR_ERR(em);
980 goto out_free_reserve;
981 }
982 free_extent_map(em);
983
984 ret = btrfs_add_ordered_extent_compress(inode, start, /* file_offset */
985 ins.objectid, /* disk_bytenr */
986 async_extent->ram_size, /* num_bytes */
987 ins.offset, /* disk_num_bytes */
988 async_extent->compress_type);
989 if (ret) {
990 btrfs_drop_extent_cache(inode, start, end, 0);
991 goto out_free_reserve;
992 }
993 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
994
995 /* Clear dirty, set writeback and unlock the pages. */
996 extent_clear_unlock_delalloc(inode, start, end,
997 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
998 PAGE_UNLOCK | PAGE_START_WRITEBACK);
999 if (btrfs_submit_compressed_write(inode, start, /* file_offset */
1000 async_extent->ram_size, /* num_bytes */
1001 ins.objectid, /* disk_bytenr */
1002 ins.offset, /* compressed_len */
1003 async_extent->pages, /* compressed_pages */
1004 async_extent->nr_pages,
1005 async_chunk->write_flags,
1006 async_chunk->blkcg_css)) {
1007 const u64 start = async_extent->start;
1008 const u64 end = start + async_extent->ram_size - 1;
1009
1010 btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0);
1011
1012 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
1013 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
1014 free_async_extent_pages(async_extent);
1015 }
1016 *alloc_hint = ins.objectid + ins.offset;
1017 kfree(async_extent);
1018 return ret;
1019
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001020out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001021 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001022 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001023out_free:
Qu Wenruob4ccace2021-09-27 15:21:57 +08001024 extent_clear_unlock_delalloc(inode, start, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001025 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001026 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -04001027 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001028 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1029 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +01001030 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 kfree(async_extent);
Qu Wenruob4ccace2021-09-27 15:21:57 +08001032 return ret;
1033}
1034
1035/*
1036 * Phase two of compressed writeback. This is the ordered portion of the code,
1037 * which only gets called in the order the work was queued. We walk all the
1038 * async extents created by compress_file_range and send them down to the disk.
1039 */
1040static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
1041{
1042 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
1043 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1044 struct async_extent *async_extent;
1045 u64 alloc_hint = 0;
1046 int ret = 0;
1047
1048 while (!list_empty(&async_chunk->extents)) {
1049 u64 extent_start;
1050 u64 ram_size;
1051
1052 async_extent = list_entry(async_chunk->extents.next,
1053 struct async_extent, list);
1054 list_del(&async_extent->list);
1055 extent_start = async_extent->start;
1056 ram_size = async_extent->ram_size;
1057
1058 ret = submit_one_async_extent(inode, async_chunk, async_extent,
1059 &alloc_hint);
1060 btrfs_debug(fs_info,
1061"async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1062 inode->root->root_key.objectid,
1063 btrfs_ino(inode), extent_start, ram_size, ret);
1064 }
Chris Mason771ed682008-11-06 22:02:51 -05001065}
1066
Nikolay Borisov43c69842020-06-03 08:55:02 +03001067static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04001068 u64 num_bytes)
1069{
Nikolay Borisov43c69842020-06-03 08:55:02 +03001070 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -04001071 struct extent_map *em;
1072 u64 alloc_hint = 0;
1073
1074 read_lock(&em_tree->lock);
1075 em = search_extent_mapping(em_tree, start, num_bytes);
1076 if (em) {
1077 /*
1078 * if block start isn't an actual block number then find the
1079 * first block in this inode and use that as a hint. If that
1080 * block is also bogus then just don't worry about it.
1081 */
1082 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1083 free_extent_map(em);
1084 em = search_extent_mapping(em_tree, 0, 0);
1085 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1086 alloc_hint = em->block_start;
1087 if (em)
1088 free_extent_map(em);
1089 } else {
1090 alloc_hint = em->block_start;
1091 free_extent_map(em);
1092 }
1093 }
1094 read_unlock(&em_tree->lock);
1095
1096 return alloc_hint;
1097}
1098
Chris Mason771ed682008-11-06 22:02:51 -05001099/*
1100 * when extent_io.c finds a delayed allocation range in the file,
1101 * the call backs end up in this code. The basic idea is to
1102 * allocate extents on disk for the range, and create ordered data structs
1103 * in ram to track those extents.
1104 *
1105 * locked_page is the page that writepage had locked already. We use
1106 * it to make sure we don't do extra locks or unlocks.
1107 *
1108 * *page_started is set to one if we unlock locked_page and do everything
1109 * required to start IO on it. It may be clean and already done with
1110 * IO when we return.
1111 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001112static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001113 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001114 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001115 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001116{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001117 struct btrfs_root *root = inode->root;
1118 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001119 u64 alloc_hint = 0;
1120 u64 num_bytes;
1121 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001122 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001123 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001124 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001125 struct btrfs_key ins;
1126 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001127 unsigned clear_bits;
1128 unsigned long page_ops;
1129 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001130 int ret = 0;
1131
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001132 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001133 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001134 ret = -EINVAL;
1135 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001136 }
Chris Mason771ed682008-11-06 22:02:51 -05001137
Qu Wenruofda28322013-02-26 08:10:22 +00001138 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001139 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001140 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001141
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001142 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001143
Qu Wenruo73672532021-07-26 14:34:59 +08001144 /*
1145 * Due to the page size limit, for subpage we can only trigger the
1146 * writeback for the dirty sectors of page, that means data writeback
1147 * is doing more writeback than what we want.
1148 *
1149 * This is especially unexpected for some call sites like fallocate,
1150 * where we only increase i_size after everything is done.
1151 * This means we can trigger inline extent even if we didn't want to.
1152 * So here we skip inline extent creation completely.
1153 */
1154 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Mason771ed682008-11-06 22:02:51 -05001155 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001156 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001157 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001158 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001159 /*
1160 * We use DO_ACCOUNTING here because we need the
1161 * delalloc_release_metadata to be run _after_ we drop
1162 * our outstanding extent for clearing delalloc for this
1163 * range.
1164 */
Qu Wenruo4750af32021-05-31 16:50:48 +08001165 extent_clear_unlock_delalloc(inode, start, end,
1166 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001167 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001168 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1169 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001170 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001171 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001172 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001173 *page_started = 1;
Qu Wenruo4750af32021-05-31 16:50:48 +08001174 /*
1175 * locked_page is locked by the caller of
1176 * writepage_delalloc(), not locked by
1177 * __process_pages_contig().
1178 *
1179 * We can't let __process_pages_contig() to unlock it,
1180 * as it doesn't have any subpage::writers recorded.
1181 *
1182 * Here we manually unlock the page, since the caller
1183 * can't use page_started to determine if it's an
1184 * inline extent or a compressed extent.
1185 */
1186 unlock_page(locked_page);
Chris Mason771ed682008-11-06 22:02:51 -05001187 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001189 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001190 }
1191 }
Chris Masonc8b97812008-10-29 14:49:59 -04001192
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001193 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1194 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001195
Filipe Manana432cd2a2020-06-08 13:32:55 +01001196 /*
1197 * Relocation relies on the relocated extents to have exactly the same
1198 * size as the original extents. Normally writeback for relocation data
1199 * extents follows a NOCOW path because relocation preallocates the
1200 * extents. However, due to an operation such as scrub turning a block
1201 * group to RO mode, it may fallback to COW mode, so we must make sure
1202 * an extent allocated during COW has exactly the requested size and can
1203 * not be split into smaller extents, otherwise relocation breaks and
1204 * fails during the stage where it updates the bytenr of file extent
1205 * items.
1206 */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001207 if (btrfs_is_data_reloc_root(root))
Filipe Manana432cd2a2020-06-08 13:32:55 +01001208 min_alloc_size = num_bytes;
1209 else
1210 min_alloc_size = fs_info->sectorsize;
1211
Anand Jain3752d222018-02-15 12:29:38 +08001212 while (num_bytes > 0) {
1213 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001214 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001215 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001216 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001217 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001219 cur_alloc_size = ins.offset;
1220 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001221
Chris Mason771ed682008-11-06 22:02:51 -05001222 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001223 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001224 start, /* orig_start */
1225 ins.objectid, /* block_start */
1226 ins.offset, /* block_len */
1227 ins.offset, /* orig_block_len */
1228 ram_size, /* ram_bytes */
1229 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001230 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001231 if (IS_ERR(em)) {
1232 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001233 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001234 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001235 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001236
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001237 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001238 ram_size, cur_alloc_size,
1239 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001240 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001241 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001242
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001243 if (btrfs_is_data_reloc_root(root)) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001244 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001245 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001246 /*
1247 * Only drop cache here, and process as normal.
1248 *
1249 * We must not allow extent_clear_unlock_delalloc()
1250 * at out_unlock label to free meta of this ordered
1251 * extent, as its meta should be freed by
1252 * btrfs_finish_ordered_io().
1253 *
1254 * So we must continue until @start is increased to
1255 * skip current ordered extent.
1256 */
Josef Bacik00361582013-08-14 14:02:47 -04001257 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001258 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001259 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001260 }
1261
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001262 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001263
Qu Wenruof57ad932021-04-07 19:22:13 +08001264 /*
1265 * We're not doing compressed IO, don't unlock the first page
1266 * (which the caller expects to stay locked), don't clear any
1267 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001268 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001269 * Do set the Ordered (Private2) bit so we know this page was
1270 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001271 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001272 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001273 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001274
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001275 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001276 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001278 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001279 if (num_bytes < cur_alloc_size)
1280 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001281 else
Anand Jain3752d222018-02-15 12:29:38 +08001282 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001283 alloc_hint = ins.objectid + ins.offset;
1284 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001285 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001286
1287 /*
1288 * btrfs_reloc_clone_csums() error, since start is increased
1289 * extent_clear_unlock_delalloc() at out_unlock label won't
1290 * free metadata of current ordered extent, we're OK to exit.
1291 */
1292 if (ret)
1293 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001294 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001296 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001297
Filipe Mananad9f85962014-08-25 10:43:00 +01001298out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001299 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001300out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001301 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001302 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001304 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1305 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001306 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001307 /*
1308 * If we reserved an extent for our delalloc range (or a subrange) and
1309 * failed to create the respective ordered extent, then it means that
1310 * when we reserved the extent we decremented the extent's size from
1311 * the data space_info's bytes_may_use counter and incremented the
1312 * space_info's bytes_reserved counter by the same amount. We must make
1313 * sure extent_clear_unlock_delalloc() does not try to decrement again
1314 * the data space_info's bytes_may_use counter, therefore we do not pass
1315 * it the flag EXTENT_CLEAR_DATA_RESV.
1316 */
1317 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001318 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001319 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001320 locked_page,
1321 clear_bits,
1322 page_ops);
1323 start += cur_alloc_size;
1324 if (start >= end)
1325 goto out;
1326 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001327 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001328 clear_bits | EXTENT_CLEAR_DATA_RESV,
1329 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001331}
Chris Masonc8b97812008-10-29 14:49:59 -04001332
Chris Mason771ed682008-11-06 22:02:51 -05001333/*
1334 * work queue call back to started compression on a file and pages
1335 */
1336static noinline void async_cow_start(struct btrfs_work *work)
1337{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001338 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001339 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001340
Nikolay Borisovb5326272019-03-12 17:20:25 +02001341 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001342
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001343 compressed_extents = compress_file_range(async_chunk);
1344 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001345 btrfs_add_delayed_iput(async_chunk->inode);
1346 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001347 }
Chris Mason771ed682008-11-06 22:02:51 -05001348}
1349
1350/*
1351 * work queue call back to submit previously compressed pages
1352 */
1353static noinline void async_cow_submit(struct btrfs_work *work)
1354{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001355 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1356 work);
1357 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001358 unsigned long nr_pages;
1359
Nikolay Borisovb5326272019-03-12 17:20:25 +02001360 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001361 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001362
Nikolay Borisov4546d172019-01-03 10:50:03 +02001363 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001364 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001365 * in which case we don't have anything to submit, yet we need to
1366 * always adjust ->async_delalloc_pages as its paired with the init
1367 * happening in cow_file_range_async
1368 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001369 if (async_chunk->inode)
1370 submit_compressed_extents(async_chunk);
Josef Bacikac981412021-07-14 14:47:17 -04001371
1372 /* atomic_sub_return implies a barrier */
1373 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1374 5 * SZ_1M)
1375 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001376}
Chris Masonc8b97812008-10-29 14:49:59 -04001377
Chris Mason771ed682008-11-06 22:02:51 -05001378static noinline void async_cow_free(struct btrfs_work *work)
1379{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001380 struct async_chunk *async_chunk;
Qu Wenruo9e895a82021-09-27 15:21:45 +08001381 struct async_cow *async_cow;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001382
Nikolay Borisovb5326272019-03-12 17:20:25 +02001383 async_chunk = container_of(work, struct async_chunk, work);
1384 if (async_chunk->inode)
1385 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001386 if (async_chunk->blkcg_css)
1387 css_put(async_chunk->blkcg_css);
Qu Wenruo9e895a82021-09-27 15:21:45 +08001388
1389 async_cow = async_chunk->async_cow;
1390 if (atomic_dec_and_test(&async_cow->num_chunks))
1391 kvfree(async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001392}
1393
Nikolay Borisov751b6432020-06-03 08:55:22 +03001394static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001395 struct writeback_control *wbc,
1396 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001397 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001398 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001399{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001400 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001401 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001402 struct async_cow *ctx;
1403 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001404 unsigned long nr_pages;
1405 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001406 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1407 int i;
1408 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001409 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001410 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001411
Nikolay Borisov751b6432020-06-03 08:55:22 +03001412 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001413
Nikolay Borisov751b6432020-06-03 08:55:22 +03001414 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001415 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1416 num_chunks = 1;
1417 should_compress = false;
1418 } else {
1419 should_compress = true;
1420 }
1421
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001422 nofs_flag = memalloc_nofs_save();
1423 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1424 memalloc_nofs_restore(nofs_flag);
1425
Nikolay Borisov97db1202019-03-12 17:20:24 +02001426 if (!ctx) {
1427 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1428 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1429 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001430 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1431 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001432
Nikolay Borisov751b6432020-06-03 08:55:22 +03001433 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1434 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001435 return -ENOMEM;
1436 }
1437
1438 async_chunk = ctx->chunks;
1439 atomic_set(&ctx->num_chunks, num_chunks);
1440
1441 for (i = 0; i < num_chunks; i++) {
1442 if (should_compress)
1443 cur_end = min(end, start + SZ_512K - 1);
1444 else
1445 cur_end = end;
1446
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001447 /*
1448 * igrab is called higher up in the call chain, take only the
1449 * lightweight reference for the callback lifetime
1450 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001451 ihold(&inode->vfs_inode);
Qu Wenruo9e895a82021-09-27 15:21:45 +08001452 async_chunk[i].async_cow = ctx;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001453 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001454 async_chunk[i].start = start;
1455 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001456 async_chunk[i].write_flags = write_flags;
1457 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001458
Chris Mason1d53c9e2019-07-10 12:28:16 -07001459 /*
1460 * The locked_page comes all the way from writepage and its
1461 * the original page we were actually given. As we spread
1462 * this large delalloc region across multiple async_chunk
1463 * structs, only the first struct needs a pointer to locked_page
1464 *
1465 * This way we don't need racey decisions about who is supposed
1466 * to unlock it.
1467 */
1468 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001469 /*
1470 * Depending on the compressibility, the pages might or
1471 * might not go through async. We want all of them to
1472 * be accounted against wbc once. Let's do it here
1473 * before the paths diverge. wbc accounting is used
1474 * only for foreign writeback detection and doesn't
1475 * need full accuracy. Just account the whole thing
1476 * against the first page.
1477 */
1478 wbc_account_cgroup_owner(wbc, locked_page,
1479 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001480 async_chunk[i].locked_page = locked_page;
1481 locked_page = NULL;
1482 } else {
1483 async_chunk[i].locked_page = NULL;
1484 }
1485
Chris Masonec39f762019-07-10 12:28:17 -07001486 if (blkcg_css != blkcg_root_css) {
1487 css_get(blkcg_css);
1488 async_chunk[i].blkcg_css = blkcg_css;
1489 } else {
1490 async_chunk[i].blkcg_css = NULL;
1491 }
1492
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001493 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1494 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001495
Nikolay Borisov97db1202019-03-12 17:20:24 +02001496 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001498
Nikolay Borisov97db1202019-03-12 17:20:24 +02001499 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001500
Chris Mason771ed682008-11-06 22:02:51 -05001501 *nr_written += nr_pages;
1502 start = cur_end + 1;
1503 }
1504 *page_started = 1;
1505 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001506}
1507
Naohiro Aota42c01102021-02-04 19:22:07 +09001508static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1509 struct page *locked_page, u64 start,
1510 u64 end, int *page_started,
1511 unsigned long *nr_written)
1512{
1513 int ret;
1514
1515 ret = cow_file_range(inode, locked_page, start, end, page_started,
1516 nr_written, 0);
1517 if (ret)
1518 return ret;
1519
1520 if (*page_started)
1521 return 0;
1522
1523 __set_page_dirty_nobuffers(locked_page);
1524 account_page_redirty(locked_page);
Qu Wenruo2bd0fc92021-09-27 15:21:58 +08001525 extent_write_locked_range(&inode->vfs_inode, start, end);
Naohiro Aota42c01102021-02-04 19:22:07 +09001526 *page_started = 1;
1527
1528 return 0;
1529}
1530
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001531static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001532 u64 bytenr, u64 num_bytes)
1533{
Josef Bacikfc28b252021-11-05 16:45:48 -04001534 struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr);
Yan Zheng17d217f2008-12-12 10:03:38 -05001535 struct btrfs_ordered_sum *sums;
Josef Bacikfc28b252021-11-05 16:45:48 -04001536 int ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001537 LIST_HEAD(list);
1538
Josef Bacikfc28b252021-11-05 16:45:48 -04001539 ret = btrfs_lookup_csums_range(csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001540 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001541 if (ret == 0 && list_empty(&list))
1542 return 0;
1543
1544 while (!list_empty(&list)) {
1545 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1546 list_del(&sums->list);
1547 kfree(sums);
1548 }
Liu Bo58113752018-01-31 17:09:13 -07001549 if (ret < 0)
1550 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001551 return 1;
1552}
1553
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001554static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001555 const u64 start, const u64 end,
1556 int *page_started, unsigned long *nr_written)
1557{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001558 const bool is_space_ino = btrfs_is_free_space_inode(inode);
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001559 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001560 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001561 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001562 u64 range_start = start;
1563 u64 count;
1564
1565 /*
1566 * If EXTENT_NORESERVE is set it means that when the buffered write was
1567 * made we had not enough available data space and therefore we did not
1568 * reserve data space for it, since we though we could do NOCOW for the
1569 * respective file range (either there is prealloc extent or the inode
1570 * has the NOCOW bit set).
1571 *
1572 * However when we need to fallback to COW mode (because for example the
1573 * block group for the corresponding extent was turned to RO mode by a
1574 * scrub or relocation) we need to do the following:
1575 *
1576 * 1) We increment the bytes_may_use counter of the data space info.
1577 * If COW succeeds, it allocates a new data extent and after doing
1578 * that it decrements the space info's bytes_may_use counter and
1579 * increments its bytes_reserved counter by the same amount (we do
1580 * this at btrfs_add_reserved_bytes()). So we need to increment the
1581 * bytes_may_use counter to compensate (when space is reserved at
1582 * buffered write time, the bytes_may_use counter is incremented);
1583 *
1584 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1585 * that if the COW path fails for any reason, it decrements (through
1586 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1587 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001588 *
1589 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001590 * space cache inode or an inode of the data relocation tree, we must
1591 * also increment bytes_may_use of the data space_info for the same
1592 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001593 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001594 * group that contains that extent to RO mode and therefore force COW
1595 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001596 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001597 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001598 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001599 if (count > 0 || is_space_ino || is_reloc_ino) {
1600 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001601 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001602 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1603
Filipe Manana6bd335b2020-06-08 13:33:05 +01001604 if (is_space_ino || is_reloc_ino)
1605 bytes = range_bytes;
1606
Filipe Manana467dc472020-05-27 11:16:07 +01001607 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001608 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001609 spin_unlock(&sinfo->lock);
1610
Filipe Manana2166e5e2020-05-27 11:16:19 +01001611 if (count > 0)
1612 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1613 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001614 }
1615
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001616 return cow_file_range(inode, locked_page, start, end, page_started,
1617 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001618}
1619
Chris Masond352ac62008-09-29 15:18:18 -04001620/*
1621 * when nowcow writeback call back. This checks for snapshots or COW copies
1622 * of the extents that exist in the file, and COWs the file as required.
1623 *
1624 * If no cow copies or snapshots exist, we write directly to the existing
1625 * blocks on disk
1626 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001627static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001628 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001629 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001630 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001631 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001632{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001633 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1634 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001635 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001636 u64 cow_start = (u64)-1;
1637 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001638 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001639 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001640 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1641 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001642 bool nocow = false;
1643 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001644 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001645
1646 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001647 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001648 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001649 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001650 EXTENT_DO_ACCOUNTING |
1651 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001652 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001653 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001654 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001655 }
Li Zefan82d59022011-04-20 10:33:24 +08001656
Yan Zheng80ff3852008-10-30 14:20:02 -04001657 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001658 struct btrfs_key found_key;
1659 struct btrfs_file_extent_item *fi;
1660 struct extent_buffer *leaf;
1661 u64 extent_end;
1662 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001663 u64 num_bytes = 0;
1664 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001665 u64 ram_bytes;
1666 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001667
1668 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001669
Liu Boe4c3b2d2017-01-30 12:25:28 -08001670 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001671 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001672 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001673 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001674
1675 /*
1676 * If there is no extent for our range when doing the initial
1677 * search, then go back to the previous slot as it will be the
1678 * one containing the search offset
1679 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001680 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1681 leaf = path->nodes[0];
1682 btrfs_item_key_to_cpu(leaf, &found_key,
1683 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001684 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001685 found_key.type == BTRFS_EXTENT_DATA_KEY)
1686 path->slots[0]--;
1687 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001688 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001689next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001690 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001691 leaf = path->nodes[0];
1692 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1693 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001694 if (ret < 0) {
1695 if (cow_start != (u64)-1)
1696 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001697 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001698 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001699 if (ret > 0)
1700 break;
1701 leaf = path->nodes[0];
1702 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001703
Yan Zheng80ff3852008-10-30 14:20:02 -04001704 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001705
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001706 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001707 if (found_key.objectid > ino)
1708 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001709 /*
1710 * Keep searching until we find an EXTENT_ITEM or there are no
1711 * more extents for this inode
1712 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001713 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1714 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1715 path->slots[0]++;
1716 goto next_slot;
1717 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001718
1719 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001720 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001721 found_key.offset > end)
1722 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001723
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001724 /*
1725 * If the found extent starts after requested offset, then
1726 * adjust extent_end to be right before this extent begins
1727 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001728 if (found_key.offset > cur_offset) {
1729 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001730 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001731 goto out_check;
1732 }
Chris Masonc31f8832008-01-08 15:46:31 -05001733
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001734 /*
1735 * Found extent which begins before our range and potentially
1736 * intersect it
1737 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001738 fi = btrfs_item_ptr(leaf, path->slots[0],
1739 struct btrfs_file_extent_item);
1740 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001741
Josef Bacikcc95bef2013-04-04 14:31:27 -04001742 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001743 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1744 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001745 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001746 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001747 extent_end = found_key.offset +
1748 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001749 disk_num_bytes =
1750 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001751 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001752 * If the extent we got ends before our current offset,
1753 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001754 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001755 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001756 path->slots[0]++;
1757 goto next_slot;
1758 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001759 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001760 if (disk_bytenr == 0)
1761 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001762 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001763 if (btrfs_file_extent_compression(leaf, fi) ||
1764 btrfs_file_extent_encryption(leaf, fi) ||
1765 btrfs_file_extent_other_encoding(leaf, fi))
1766 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001767 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001768 * If extent is created before the last volume's snapshot
1769 * this implies the extent is shared, hence we can't do
1770 * nocow. This is the same check as in
1771 * btrfs_cross_ref_exist but without calling
1772 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001773 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001774 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001775 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001776 btrfs_root_last_snapshot(&root->root_item))
1777 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001778 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1779 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001780
1781 /*
1782 * The following checks can be expensive, as they need to
1783 * take other locks and do btree or rbtree searches, so
1784 * release the path to avoid blocking other tasks for too
1785 * long.
1786 */
1787 btrfs_release_path(path);
1788
Liu Bo58113752018-01-31 17:09:13 -07001789 ret = btrfs_cross_ref_exist(root, ino,
1790 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001791 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001792 if (ret) {
1793 /*
1794 * ret could be -EIO if the above fails to read
1795 * metadata.
1796 */
1797 if (ret < 0) {
1798 if (cow_start != (u64)-1)
1799 cur_offset = cow_start;
1800 goto error;
1801 }
1802
Nikolay Borisov3e024842019-08-21 10:42:03 +03001803 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001804 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001805 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001807 disk_bytenr += cur_offset - found_key.offset;
1808 num_bytes = min(end + 1, extent_end) - cur_offset;
1809 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001810 * If there are pending snapshots for this root, we
1811 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001812 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001813 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001814 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001815 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001816 * force cow if csum exists in the range.
1817 * this ensure that csum for a given extent are
1818 * either valid or do not exist.
1819 */
Liu Bo58113752018-01-31 17:09:13 -07001820 ret = csum_exist_in_range(fs_info, disk_bytenr,
1821 num_bytes);
1822 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001823 /*
1824 * ret could be -EIO if the above fails to read
1825 * metadata.
1826 */
1827 if (ret < 0) {
1828 if (cow_start != (u64)-1)
1829 cur_offset = cow_start;
1830 goto error;
1831 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001832 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001833 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001834 }
Filipe Manana20903032021-02-05 12:55:36 +00001835 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001836 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001837 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001838 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001839 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001840 extent_end = found_key.offset + ram_bytes;
1841 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001842 /* Skip extents outside of our requested range */
1843 if (extent_end <= start) {
1844 path->slots[0]++;
1845 goto next_slot;
1846 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001847 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001848 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001849 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001850 }
1851out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001852 /*
1853 * If nocow is false then record the beginning of the range
1854 * that needs to be COWed
1855 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001856 if (!nocow) {
1857 if (cow_start == (u64)-1)
1858 cow_start = cur_offset;
1859 cur_offset = extent_end;
1860 if (cur_offset > end)
1861 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001862 if (!path->nodes[0])
1863 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001864 path->slots[0]++;
1865 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001866 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001867
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001868 /*
1869 * COW range from cow_start to found_key.offset - 1. As the key
1870 * will contain the beginning of the first extent that can be
1871 * NOCOW, following one which needs to be COW'ed
1872 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001873 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001874 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001875 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001876 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001877 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001878 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001879 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001880 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001881
Yan Zhengd899e052008-10-30 14:25:28 -04001882 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001883 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001884 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001885
Nikolay Borisov968322c2020-06-03 08:55:21 +03001886 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001887 orig_start,
1888 disk_bytenr, /* block_start */
1889 num_bytes, /* block_len */
1890 disk_num_bytes, /* orig_block_len */
1891 ram_bytes, BTRFS_COMPRESS_NONE,
1892 BTRFS_ORDERED_PREALLOC);
1893 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001894 ret = PTR_ERR(em);
1895 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001896 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001897 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001898 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001899 disk_bytenr, num_bytes,
1900 num_bytes,
1901 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001902 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001903 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001904 cur_offset + num_bytes - 1,
1905 0);
1906 goto error;
1907 }
Yan Zhengd899e052008-10-30 14:25:28 -04001908 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001909 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001910 disk_bytenr, num_bytes,
1911 num_bytes,
1912 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001913 if (ret)
1914 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001915 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001916
Filipe Mananaf78c4362016-05-09 13:15:41 +01001917 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001918 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001919 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001920
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001921 if (btrfs_is_data_reloc_root(root))
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001922 /*
1923 * Error handled later, as we must prevent
1924 * extent_clear_unlock_delalloc() in error handler
1925 * from freeing metadata of created ordered extent.
1926 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001927 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001928 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001929
Nikolay Borisov968322c2020-06-03 08:55:21 +03001930 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001931 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001932 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001933 EXTENT_DELALLOC |
1934 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001935 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001936
Yan Zheng80ff3852008-10-30 14:20:02 -04001937 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001938
1939 /*
1940 * btrfs_reloc_clone_csums() error, now we're OK to call error
1941 * handler, as metadata for created ordered extent will only
1942 * be freed by btrfs_finish_ordered_io().
1943 */
1944 if (ret)
1945 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001946 if (cur_offset > end)
1947 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001948 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001949 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001950
Robbie Ko506481b2018-10-30 18:04:04 +08001951 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001952 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001953
Yan Zheng80ff3852008-10-30 14:20:02 -04001954 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001955 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001956 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1957 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001958 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001959 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001960 }
1961
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001962error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001963 if (nocow)
1964 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1965
Josef Bacik17ca04a2012-05-31 15:58:55 -04001966 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001967 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001968 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001969 EXTENT_DELALLOC | EXTENT_DEFRAG |
1970 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001971 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001972 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001973 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001974 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001975}
1976
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001977static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001978{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001979 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1980 if (inode->defrag_bytes &&
1981 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1982 0, NULL))
1983 return false;
1984 return true;
1985 }
1986 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001987}
1988
Chris Masond352ac62008-09-29 15:18:18 -04001989/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001990 * Function to process delayed allocation (create CoW) for ranges which are
1991 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001992 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001993int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001994 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1995 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001996{
Chris Masonbe20aa92007-12-17 20:14:01 -05001997 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001998 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001999
Qu Wenruo2749f7e2021-09-27 15:22:07 +08002000 /*
2001 * The range must cover part of the @locked_page, or the returned
2002 * @page_started can confuse the caller.
2003 */
2004 ASSERT(!(end <= page_offset(locked_page) ||
2005 start >= page_offset(locked_page) + PAGE_SIZE));
2006
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06002007 if (should_nocow(inode, start, end)) {
Johannes Thumshirn2adada82021-09-09 01:19:29 +09002008 /*
2009 * Normally on a zoned device we're only doing COW writes, but
2010 * in case of relocation on a zoned filesystem we have taken
2011 * precaution, that we're only writing sequentially. It's safe
2012 * to use run_delalloc_nocow() here, like for regular
2013 * preallocated inodes.
2014 */
2015 ASSERT(!zoned ||
2016 (zoned && btrfs_is_data_reloc_root(inode->root)));
Nikolay Borisov98456b92020-06-03 08:55:29 +03002017 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06002018 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03002019 } else if (!inode_can_compress(inode) ||
2020 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09002021 if (zoned)
2022 ret = run_delalloc_zoned(inode, locked_page, start, end,
2023 page_started, nr_written);
2024 else
2025 ret = cow_file_range(inode, locked_page, start, end,
2026 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04002027 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03002028 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
2029 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01002030 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04002031 }
Qu Wenruo7361b4a2021-07-28 14:05:05 +08002032 ASSERT(ret <= 0);
Qu Wenruo524272602017-03-08 10:25:52 +08002033 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03002034 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02002035 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04002036 return ret;
2037}
2038
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02002039void btrfs_split_delalloc_extent(struct inode *inode,
2040 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002041{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002042 u64 size;
2043
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002044 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04002045 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002046 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002047
Josef Bacikdcab6a32015-02-11 15:08:59 -05002048 size = orig->end - orig->start + 1;
2049 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01002050 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002051 u64 new_size;
2052
2053 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002054 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04002055 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002056 */
2057 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01002058 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04002059 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01002060 num_extents += count_max_extents(new_size);
2061 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002062 return;
2063 }
2064
Josef Bacik9e0baf62011-07-15 15:16:44 +00002065 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002066 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002067 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002068}
2069
2070/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002071 * Handle merged delayed allocation extents so we can keep track of new extents
2072 * that are just merged onto old extents, such as when we are doing sequential
2073 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04002074 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02002075void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2076 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002077{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002078 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01002079 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002080
Josef Bacik9ed74f22009-09-11 16:12:44 -04002081 /* not delalloc, ignore it */
2082 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002083 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002084
Josef Bacik8461a3d2015-03-13 15:12:08 -04002085 if (new->start > other->start)
2086 new_size = new->end - other->start + 1;
2087 else
2088 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002089
2090 /* we're not bigger than the max, unreserve the space and go */
2091 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2092 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002093 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002094 spin_unlock(&BTRFS_I(inode)->lock);
2095 return;
2096 }
2097
2098 /*
Josef Bacikba117212015-03-13 15:01:24 -04002099 * We have to add up either side to figure out how many extents were
2100 * accounted for before we merged into one big extent. If the number of
2101 * extents we accounted for is <= the amount we need for the new range
2102 * then we can return, otherwise drop. Think of it like this
2103 *
2104 * [ 4k][MAX_SIZE]
2105 *
2106 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2107 * need 2 outstanding extents, on one side we have 1 and the other side
2108 * we have 1 so they are == and we can return. But in this case
2109 *
2110 * [MAX_SIZE+4k][MAX_SIZE+4k]
2111 *
2112 * Each range on their own accounts for 2 extents, but merged together
2113 * they are only 3 extents worth of accounting, so we need to drop in
2114 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002115 */
Josef Bacikba117212015-03-13 15:01:24 -04002116 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002117 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002118 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002119 num_extents += count_max_extents(old_size);
2120 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002121 return;
2122
Josef Bacik9e0baf62011-07-15 15:16:44 +00002123 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002124 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002125 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002126}
2127
Miao Xieeb73c1b2013-05-15 07:48:22 +00002128static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2129 struct inode *inode)
2130{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002131 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2132
Miao Xieeb73c1b2013-05-15 07:48:22 +00002133 spin_lock(&root->delalloc_lock);
2134 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2135 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2136 &root->delalloc_inodes);
2137 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2138 &BTRFS_I(inode)->runtime_flags);
2139 root->nr_delalloc_inodes++;
2140 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002141 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002142 BUG_ON(!list_empty(&root->delalloc_root));
2143 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 &fs_info->delalloc_roots);
2145 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002146 }
2147 }
2148 spin_unlock(&root->delalloc_lock);
2149}
2150
Nikolay Borisov2b877332018-04-27 12:21:51 +03002151
2152void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2153 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002154{
David Sterba3ffbd682018-06-29 10:56:42 +02002155 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002156
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002157 if (!list_empty(&inode->delalloc_inodes)) {
2158 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002159 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002160 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002161 root->nr_delalloc_inodes--;
2162 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002163 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002164 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002165 BUG_ON(list_empty(&root->delalloc_root));
2166 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002167 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002168 }
2169 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002170}
2171
2172static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2173 struct btrfs_inode *inode)
2174{
2175 spin_lock(&root->delalloc_lock);
2176 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002177 spin_unlock(&root->delalloc_lock);
2178}
2179
Chris Masond352ac62008-09-29 15:18:18 -04002180/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002181 * Properly track delayed allocation bytes in the inode and to maintain the
2182 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002183 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002184void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2185 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002186{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2188
Wang Shilong47059d92014-07-03 18:22:07 +08002189 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2190 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002191 /*
2192 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002193 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002194 * bit, which is only set or cleared with irqs on
2195 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002196 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002197 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002198 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002199 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002200 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002201
Josef Bacik8b62f872017-10-19 14:15:55 -04002202 spin_lock(&BTRFS_I(inode)->lock);
2203 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2204 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002205
Josef Bacik6a3891c2015-03-16 17:38:52 -04002206 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002207 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002208 return;
2209
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002210 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2211 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002212 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002213 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002214 if (*bits & EXTENT_DEFRAG)
2215 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002216 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002217 &BTRFS_I(inode)->runtime_flags))
2218 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002219 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002220 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002221
2222 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2223 (*bits & EXTENT_DELALLOC_NEW)) {
2224 spin_lock(&BTRFS_I(inode)->lock);
2225 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2226 state->start;
2227 spin_unlock(&BTRFS_I(inode)->lock);
2228 }
Chris Mason291d6732008-01-29 15:55:23 -05002229}
2230
Chris Masond352ac62008-09-29 15:18:18 -04002231/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002232 * Once a range is no longer delalloc this function ensures that proper
2233 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002234 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002235void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2236 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002237{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002238 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2239 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002240 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002241 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002242
Filipe Manana4a4b9642017-07-27 19:52:55 +01002243 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2244 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002245 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002246 spin_unlock(&inode->lock);
2247 }
Wang Shilong47059d92014-07-03 18:22:07 +08002248
Chris Mason75eff682008-12-15 15:54:40 -05002249 /*
2250 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002251 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002252 * bit, which is only set or cleared with irqs on
2253 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002254 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002255 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002256 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002257
Josef Bacik8b62f872017-10-19 14:15:55 -04002258 spin_lock(&inode->lock);
2259 btrfs_mod_outstanding_extents(inode, -num_extents);
2260 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002261
Josef Bacikb6d08f02013-09-27 14:57:43 -04002262 /*
2263 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002264 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002265 * error.
2266 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002267 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002268 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002269 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002270
Josef Bacik6a3891c2015-03-16 17:38:52 -04002271 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002272 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002273 return;
2274
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002275 if (!btrfs_is_data_reloc_root(root) &&
Filipe Mananaa315e682017-03-06 23:04:20 +00002276 do_list && !(state->state & EXTENT_NORESERVE) &&
2277 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002278 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002279
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002280 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2281 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002282 spin_lock(&inode->lock);
2283 inode->delalloc_bytes -= len;
2284 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002285 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002286 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002287 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002288 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002289 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002290
2291 if ((state->state & EXTENT_DELALLOC_NEW) &&
2292 (*bits & EXTENT_DELALLOC_NEW)) {
2293 spin_lock(&inode->lock);
2294 ASSERT(inode->new_delalloc_bytes >= len);
2295 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002296 if (*bits & EXTENT_ADD_INODE_BYTES)
2297 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002298 spin_unlock(&inode->lock);
2299 }
Chris Mason291d6732008-01-29 15:55:23 -05002300}
2301
Chris Masond352ac62008-09-29 15:18:18 -04002302/*
Chris Masond352ac62008-09-29 15:18:18 -04002303 * in order to insert checksums into the metadata in large chunks,
2304 * we wait until bio submission time. All the pages in the bio are
2305 * checksummed and sums are attached onto the ordered extent record.
2306 *
2307 * At IO completion time the cums attached on the ordered extent record
2308 * are inserted into the btree
2309 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002310static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002311 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002312{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002313 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002314}
Chris Masone0156402008-04-16 11:15:20 -04002315
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002316/*
2317 * Split an extent_map at [start, start + len]
2318 *
2319 * This function is intended to be used only for extract_ordered_extent().
2320 */
2321static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2322 u64 pre, u64 post)
2323{
2324 struct extent_map_tree *em_tree = &inode->extent_tree;
2325 struct extent_map *em;
2326 struct extent_map *split_pre = NULL;
2327 struct extent_map *split_mid = NULL;
2328 struct extent_map *split_post = NULL;
2329 int ret = 0;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002330 unsigned long flags;
2331
2332 /* Sanity check */
2333 if (pre == 0 && post == 0)
2334 return 0;
2335
2336 split_pre = alloc_extent_map();
2337 if (pre)
2338 split_mid = alloc_extent_map();
2339 if (post)
2340 split_post = alloc_extent_map();
2341 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2342 ret = -ENOMEM;
2343 goto out;
2344 }
2345
2346 ASSERT(pre + post < len);
2347
2348 lock_extent(&inode->io_tree, start, start + len - 1);
2349 write_lock(&em_tree->lock);
2350 em = lookup_extent_mapping(em_tree, start, len);
2351 if (!em) {
2352 ret = -EIO;
2353 goto out_unlock;
2354 }
2355
2356 ASSERT(em->len == len);
2357 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2358 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
Naohiro Aota63fb5872021-08-09 09:29:18 +09002359 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2360 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2361 ASSERT(!list_empty(&em->list));
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002362
2363 flags = em->flags;
2364 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002365
2366 /* First, replace the em with a new extent_map starting from * em->start */
2367 split_pre->start = em->start;
2368 split_pre->len = (pre ? pre : em->len - post);
2369 split_pre->orig_start = split_pre->start;
2370 split_pre->block_start = em->block_start;
2371 split_pre->block_len = split_pre->len;
2372 split_pre->orig_block_len = split_pre->block_len;
2373 split_pre->ram_bytes = split_pre->len;
2374 split_pre->flags = flags;
2375 split_pre->compress_type = em->compress_type;
2376 split_pre->generation = em->generation;
2377
Naohiro Aota63fb5872021-08-09 09:29:18 +09002378 replace_extent_mapping(em_tree, em, split_pre, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002379
2380 /*
2381 * Now we only have an extent_map at:
2382 * [em->start, em->start + pre] if pre != 0
2383 * [em->start, em->start + em->len - post] if pre == 0
2384 */
2385
2386 if (pre) {
2387 /* Insert the middle extent_map */
2388 split_mid->start = em->start + pre;
2389 split_mid->len = em->len - pre - post;
2390 split_mid->orig_start = split_mid->start;
2391 split_mid->block_start = em->block_start + pre;
2392 split_mid->block_len = split_mid->len;
2393 split_mid->orig_block_len = split_mid->block_len;
2394 split_mid->ram_bytes = split_mid->len;
2395 split_mid->flags = flags;
2396 split_mid->compress_type = em->compress_type;
2397 split_mid->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002398 add_extent_mapping(em_tree, split_mid, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002399 }
2400
2401 if (post) {
2402 split_post->start = em->start + em->len - post;
2403 split_post->len = post;
2404 split_post->orig_start = split_post->start;
2405 split_post->block_start = em->block_start + em->len - post;
2406 split_post->block_len = split_post->len;
2407 split_post->orig_block_len = split_post->block_len;
2408 split_post->ram_bytes = split_post->len;
2409 split_post->flags = flags;
2410 split_post->compress_type = em->compress_type;
2411 split_post->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002412 add_extent_mapping(em_tree, split_post, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002413 }
2414
2415 /* Once for us */
2416 free_extent_map(em);
2417 /* Once for the tree */
2418 free_extent_map(em);
2419
2420out_unlock:
2421 write_unlock(&em_tree->lock);
2422 unlock_extent(&inode->io_tree, start, start + len - 1);
2423out:
2424 free_extent_map(split_pre);
2425 free_extent_map(split_mid);
2426 free_extent_map(split_post);
2427
2428 return ret;
2429}
2430
Naohiro Aotad22002f2021-02-04 19:22:00 +09002431static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2432 struct bio *bio, loff_t file_offset)
2433{
2434 struct btrfs_ordered_extent *ordered;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002435 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002436 u64 file_len;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002437 u64 len = bio->bi_iter.bi_size;
2438 u64 end = start + len;
2439 u64 ordered_end;
2440 u64 pre, post;
2441 int ret = 0;
2442
2443 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2444 if (WARN_ON_ONCE(!ordered))
2445 return BLK_STS_IOERR;
2446
2447 /* No need to split */
2448 if (ordered->disk_num_bytes == len)
2449 goto out;
2450
2451 /* We cannot split once end_bio'd ordered extent */
2452 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2453 ret = -EINVAL;
2454 goto out;
2455 }
2456
2457 /* We cannot split a compressed ordered extent */
2458 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2459 ret = -EINVAL;
2460 goto out;
2461 }
2462
2463 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2464 /* bio must be in one ordered extent */
2465 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2466 ret = -EINVAL;
2467 goto out;
2468 }
2469
2470 /* Checksum list should be empty */
2471 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2472 ret = -EINVAL;
2473 goto out;
2474 }
2475
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002476 file_len = ordered->num_bytes;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002477 pre = start - ordered->disk_bytenr;
2478 post = ordered_end - end;
2479
2480 ret = btrfs_split_ordered_extent(ordered, pre, post);
2481 if (ret)
2482 goto out;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002483 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
Naohiro Aotad22002f2021-02-04 19:22:00 +09002484
2485out:
Naohiro Aotad22002f2021-02-04 19:22:00 +09002486 btrfs_put_ordered_extent(ordered);
2487
2488 return errno_to_blk_status(ret);
2489}
2490
Chris Mason4a69a412008-11-06 22:03:00 -05002491/*
Chris Masoncad321a2008-12-17 14:51:42 -05002492 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002493 * on write, or reading the csums from the tree before a read.
2494 *
2495 * Rules about async/sync submit,
2496 * a) read: sync submit
2497 *
2498 * b) write without checksum: sync submit
2499 *
2500 * c) write with checksum:
2501 * c-1) if bio is issued by fsync: sync submit
2502 * (sync_writers != 0)
2503 *
2504 * c-2) if root is reloc root: sync submit
2505 * (only in case of buffered IO)
2506 *
2507 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002508 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002509blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2510 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002511
Chris Mason44b8bd72008-04-16 11:14:51 -04002512{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002513 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002514 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302515 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002516 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002517 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002518 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002519
Josef Bacik42437a62020-10-16 11:29:18 -04002520 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
Josef Bacik056c8312021-11-05 16:45:47 -04002521 test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
Chris Masoncad321a2008-12-17 14:51:42 -05002522
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002523 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302524 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002525
Naohiro Aotad22002f2021-02-04 19:22:00 +09002526 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2527 struct page *page = bio_first_bvec_all(bio)->bv_page;
2528 loff_t file_offset = page_offset(page);
2529
2530 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2531 if (ret)
2532 goto out;
2533 }
2534
Naohiro Aotacfe94442021-02-04 19:21:59 +09002535 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002536 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002537 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002538 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002539
Chris Masond20f7042008-12-08 16:58:54 -05002540 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002541 ret = btrfs_submit_compressed_read(inode, bio,
2542 mirror_num,
2543 bio_flags);
2544 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002545 } else {
2546 /*
2547 * Lookup bio sums does extra checks around whether we
2548 * need to csum or not, which is why we ignore skip_sum
2549 * here.
2550 */
Qu Wenruo62751932020-12-02 14:48:06 +08002551 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002552 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002553 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002554 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002555 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002556 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002557 /* csum items have already been cloned */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002558 if (btrfs_is_data_reloc_root(root))
Yan Zheng17d217f2008-12-12 10:03:38 -05002559 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002560 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002561 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2562 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002563 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002564 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002565 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002566 if (ret)
2567 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002568 }
2569
Chris Mason0b86a832008-03-24 15:01:56 -04002570mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002571 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002572
2573out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002574 if (ret) {
2575 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002576 bio_endio(bio);
2577 }
Stefan Behrens61891922012-11-05 18:51:52 +01002578 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002579}
Chris Mason6885f302008-02-20 16:11:05 -05002580
Chris Masond352ac62008-09-29 15:18:18 -04002581/*
2582 * given a list of ordered sums record them in the inode. This happens
2583 * at IO completion time based on sums calculated at bio submission time.
2584 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002585static int add_pending_csums(struct btrfs_trans_handle *trans,
2586 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002587{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588 struct btrfs_ordered_sum *sum;
Josef Bacikfc28b252021-11-05 16:45:48 -04002589 struct btrfs_root *csum_root = NULL;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002590 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002591
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002592 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002593 trans->adding_csums = true;
Josef Bacikfc28b252021-11-05 16:45:48 -04002594 if (!csum_root)
2595 csum_root = btrfs_csum_root(trans->fs_info,
2596 sum->bytenr);
2597 ret = btrfs_csum_file_blocks(trans, csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002598 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002599 if (ret)
2600 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002601 }
2602 return 0;
2603}
2604
Filipe Mananac3347302020-11-04 11:07:31 +00002605static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2606 const u64 start,
2607 const u64 len,
2608 struct extent_state **cached_state)
2609{
2610 u64 search_start = start;
2611 const u64 end = start + len - 1;
2612
2613 while (search_start < end) {
2614 const u64 search_len = end - search_start + 1;
2615 struct extent_map *em;
2616 u64 em_len;
2617 int ret = 0;
2618
2619 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2620 if (IS_ERR(em))
2621 return PTR_ERR(em);
2622
2623 if (em->block_start != EXTENT_MAP_HOLE)
2624 goto next;
2625
2626 em_len = em->len;
2627 if (em->start < search_start)
2628 em_len -= search_start - em->start;
2629 if (em_len > search_len)
2630 em_len = search_len;
2631
2632 ret = set_extent_bit(&inode->io_tree, search_start,
2633 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002634 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2635 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002636next:
2637 search_start = extent_map_end(em);
2638 free_extent_map(em);
2639 if (ret)
2640 return ret;
2641 }
2642 return 0;
2643}
2644
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002645int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002646 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002647 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002648{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002649 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002650
2651 if (start >= i_size_read(&inode->vfs_inode) &&
2652 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2653 /*
2654 * There can't be any extents following eof in this case so just
2655 * set the delalloc new bit for the range directly.
2656 */
2657 extra_bits |= EXTENT_DELALLOC_NEW;
2658 } else {
2659 int ret;
2660
2661 ret = btrfs_find_new_delalloc_bytes(inode, start,
2662 end + 1 - start,
2663 cached_state);
2664 if (ret)
2665 return ret;
2666 }
2667
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002668 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2669 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002670}
2671
Chris Masond352ac62008-09-29 15:18:18 -04002672/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002673struct btrfs_writepage_fixup {
2674 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002675 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002676 struct btrfs_work work;
2677};
2678
Christoph Hellwigb2950862008-12-02 09:54:17 -05002679static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002680{
2681 struct btrfs_writepage_fixup *fixup;
2682 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002683 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002684 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002685 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002686 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002687 u64 page_start;
2688 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002689 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002690 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002691
2692 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2693 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002694 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002695 page_start = page_offset(page);
2696 page_end = page_offset(page) + PAGE_SIZE - 1;
2697
2698 /*
2699 * This is similar to page_mkwrite, we need to reserve the space before
2700 * we take the page lock.
2701 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002702 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2703 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002704again:
Chris Mason247e7432008-07-17 12:53:51 -04002705 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002706
Chris Mason25f3c502020-01-21 11:51:42 -05002707 /*
2708 * Before we queued this fixup, we took a reference on the page.
2709 * page->mapping may go NULL, but it shouldn't be moved to a different
2710 * address space.
2711 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002712 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2713 /*
2714 * Unfortunately this is a little tricky, either
2715 *
2716 * 1) We got here and our page had already been dealt with and
2717 * we reserved our space, thus ret == 0, so we need to just
2718 * drop our space reservation and bail. This can happen the
2719 * first time we come into the fixup worker, or could happen
2720 * while waiting for the ordered extent.
2721 * 2) Our page was already dealt with, but we happened to get an
2722 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2723 * this case we obviously don't have anything to release, but
2724 * because the page was already dealt with we don't want to
2725 * mark the page with an error, so make sure we're resetting
2726 * ret to 0. This is why we have this check _before_ the ret
2727 * check, because we do not want to have a surprise ENOSPC
2728 * when the page was already properly dealt with.
2729 */
2730 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002731 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2732 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002733 page_start, PAGE_SIZE,
2734 true);
2735 }
2736 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002737 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002738 }
Chris Mason25f3c502020-01-21 11:51:42 -05002739
2740 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002741 * We can't mess with the page state unless it is locked, so now that
2742 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002743 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002744 if (ret)
2745 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002746
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002747 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002748
2749 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002750 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002751 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002752
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002753 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002754 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002755 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2756 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002757 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002758 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002759 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002760 goto again;
2761 }
Chris Mason247e7432008-07-17 12:53:51 -04002762
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002763 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002764 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002765 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002766 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002767
Chris Mason25f3c502020-01-21 11:51:42 -05002768 /*
2769 * Everything went as planned, we're now the owner of a dirty page with
2770 * delayed allocation bits set and space reserved for our COW
2771 * destination.
2772 *
2773 * The page was dirty when we started, nothing should have cleaned it.
2774 */
2775 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002776 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002777out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002778 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002779 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002780 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2781 PAGE_SIZE, true);
2782 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002783 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002784out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002785 if (ret) {
2786 /*
2787 * We hit ENOSPC or other errors. Update the mapping and page
2788 * to reflect the errors and clean the page.
2789 */
2790 mapping_set_error(page->mapping, ret);
2791 end_extent_writepage(page, ret, page_start, page_end);
2792 clear_page_dirty_for_io(page);
2793 SetPageError(page);
2794 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08002795 btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002796 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002797 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002798 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002799 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002800 /*
2801 * As a precaution, do a delayed iput in case it would be the last iput
2802 * that could need flushing space. Recursing back to fixup worker would
2803 * deadlock.
2804 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002805 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002806}
2807
2808/*
2809 * There are a few paths in the higher layers of the kernel that directly
2810 * set the page dirty bit without asking the filesystem if it is a
2811 * good idea. This causes problems because we want to make sure COW
2812 * properly happens and the data=ordered rules are followed.
2813 *
Chris Masonc8b97812008-10-29 14:49:59 -04002814 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002815 * hasn't been properly setup for IO. We kick off an async process
2816 * to fix it up. The async helper will wait for ordered extents, set
2817 * the delalloc bit and make it safe to write the page.
2818 */
Qu Wenruoa129ffb2021-07-27 13:41:32 +08002819int btrfs_writepage_cow_fixup(struct page *page)
Chris Mason247e7432008-07-17 12:53:51 -04002820{
2821 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002822 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002823 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002824
Qu Wenruof57ad932021-04-07 19:22:13 +08002825 /* This page has ordered extent covering it already */
2826 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002827 return 0;
2828
Chris Mason25f3c502020-01-21 11:51:42 -05002829 /*
2830 * PageChecked is set below when we create a fixup worker for this page,
2831 * don't try to create another one if we're already PageChecked()
2832 *
2833 * The extent_io writepage code will redirty the page if we send back
2834 * EAGAIN.
2835 */
Chris Mason247e7432008-07-17 12:53:51 -04002836 if (PageChecked(page))
2837 return -EAGAIN;
2838
2839 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2840 if (!fixup)
2841 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002842
Josef Bacikf4b13632020-01-21 14:34:52 -05002843 /*
2844 * We are already holding a reference to this inode from
2845 * write_cache_pages. We need to hold it because the space reservation
2846 * takes place outside of the page lock, and we can't trust
2847 * page->mapping outside of the page lock.
2848 */
2849 ihold(inode);
Qu Wenruoe4f94342021-09-27 15:21:49 +08002850 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002851 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002852 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002853 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002854 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002855 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002856
2857 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002858}
2859
Yan Zhengd899e052008-10-30 14:25:28 -04002860static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002861 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002862 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002863 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002864 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002865{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002866 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002867 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002868 struct btrfs_path *path;
2869 struct extent_buffer *leaf;
2870 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002871 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2872 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2873 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2874 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002875 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002876 int ret;
2877
2878 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002879 if (!path)
2880 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002881
Chris Masona1ed8352009-09-11 12:27:37 -04002882 /*
2883 * we may be replacing one extent in the tree with another.
2884 * The new extent is pinned in the extent map, and we don't want
2885 * to drop it from the cache until it is completely in the btree.
2886 *
2887 * So, tell btrfs_drop_extents to leave this extent in the cache.
2888 * the caller is expected to unpin it and allow it to be merged
2889 * with the others.
2890 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002891 drop_args.path = path;
2892 drop_args.start = file_pos;
2893 drop_args.end = file_pos + num_bytes;
2894 drop_args.replace_extent = true;
2895 drop_args.extent_item_size = sizeof(*stack_fi);
2896 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 if (ret)
2898 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002899
Filipe Manana5893dfb2020-11-04 11:07:32 +00002900 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002901 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002902 ins.offset = file_pos;
2903 ins.type = BTRFS_EXTENT_DATA_KEY;
2904
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002905 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002906 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002907 if (ret)
2908 goto out;
2909 }
Yan Zhengd899e052008-10-30 14:25:28 -04002910 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002911 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2912 write_extent_buffer(leaf, stack_fi,
2913 btrfs_item_ptr_offset(leaf, path->slots[0]),
2914 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002915
Yan Zhengd899e052008-10-30 14:25:28 -04002916 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002917 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002918
Filipe Manana2766ff62020-11-04 11:07:34 +00002919 /*
2920 * If we dropped an inline extent here, we know the range where it is
2921 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
David Sterba1a9fd412021-05-21 17:42:23 +02002922 * number of bytes only for that range containing the inline extent.
Filipe Manana2766ff62020-11-04 11:07:34 +00002923 * The remaining of the range will be processed when clearning the
2924 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2925 */
2926 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2927 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2928
2929 inline_size = drop_args.bytes_found - inline_size;
2930 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2931 drop_args.bytes_found -= inline_size;
2932 num_bytes -= sectorsize;
2933 }
2934
2935 if (update_inode_bytes)
2936 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002937
2938 ins.objectid = disk_bytenr;
2939 ins.offset = disk_num_bytes;
2940 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002941
Nikolay Borisovc553f942020-06-03 08:55:19 +03002942 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002943 if (ret)
2944 goto out;
2945
Nikolay Borisovc553f942020-06-03 08:55:19 +03002946 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002947 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002948out:
Yan Zhengd899e052008-10-30 14:25:28 -04002949 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002950
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002951 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002952}
2953
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002954static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002955 u64 start, u64 len)
2956{
David Sterba32da53862019-10-29 19:20:18 +01002957 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002958
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002959 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002960 ASSERT(cache);
2961
2962 spin_lock(&cache->lock);
2963 cache->delalloc_bytes -= len;
2964 spin_unlock(&cache->lock);
2965
2966 btrfs_put_block_group(cache);
2967}
2968
Qu Wenruo203f44c52020-06-10 09:04:40 +08002969static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002970 struct btrfs_ordered_extent *oe)
2971{
2972 struct btrfs_file_extent_item stack_fi;
2973 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002974 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002975
2976 memset(&stack_fi, 0, sizeof(stack_fi));
2977 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2978 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2979 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2980 oe->disk_num_bytes);
2981 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2982 logical_len = oe->truncated_len;
2983 else
2984 logical_len = oe->num_bytes;
2985 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2986 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2987 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2988 /* Encryption and other encoding is reserved and all 0 */
2989
Filipe Manana2766ff62020-11-04 11:07:34 +00002990 /*
2991 * For delalloc, when completing an ordered extent we update the inode's
2992 * bytes when clearing the range in the inode's io tree, so pass false
2993 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2994 * except if the ordered extent was truncated.
2995 */
2996 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2997 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2998
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002999 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
3000 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00003001 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08003002}
3003
3004/*
3005 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04003006 * an ordered extent if the range of bytes in the file it covers are
3007 * fully written.
3008 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003009static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003010{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003011 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3012 struct btrfs_root *root = inode->root;
3013 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003014 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003015 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003016 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003017 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08003018 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003020 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03003021 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003022 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003023 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00003024 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003025
Omar Sandovalbffe6332019-12-02 17:34:19 -08003026 start = ordered_extent->file_offset;
3027 end = start + ordered_extent->num_bytes - 1;
3028
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003029 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3030 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3031 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00003032 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003033
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003034 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003035
Josef Bacik5fd02042012-05-02 14:00:54 -04003036 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3037 ret = -EIO;
3038 goto out;
3039 }
3040
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003041 /* A valid bdev implies a write on a sequential zone */
3042 if (ordered_extent->bdev) {
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003043 btrfs_rewrite_logical_zoned(ordered_extent);
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003044 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3045 ordered_extent->disk_num_bytes);
3046 }
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003047
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003048 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08003049
Josef Bacik77cef2e2013-08-29 13:57:21 -04003050 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3051 truncated = true;
3052 logical_len = ordered_extent->truncated_len;
3053 /* Truncated the entire extent, don't bother adding */
3054 if (!logical_len)
3055 goto out;
3056 }
3057
Yan, Zhengc2167752009-11-12 09:34:21 +00003058 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003060
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003061 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003062 if (freespace_inode)
3063 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003064 else
3065 trans = btrfs_join_transaction(root);
3066 if (IS_ERR(trans)) {
3067 ret = PTR_ERR(trans);
3068 trans = NULL;
3069 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003070 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003071 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02003072 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003073 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003074 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003075 goto out;
3076 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003077
Filipe Manana2766ff62020-11-04 11:07:34 +00003078 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003079 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003080
Nikolay Borisov8d510122019-10-08 20:43:06 +03003081 if (freespace_inode)
3082 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003083 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003084 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 if (IS_ERR(trans)) {
3086 ret = PTR_ERR(trans);
3087 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003088 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003089 }
Chris Masona79b7d42014-05-22 16:18:52 -07003090
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003091 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003092
Chris Masonc8b97812008-10-29 14:49:59 -04003093 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003094 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003095 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003096 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003097 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04003098 ordered_extent->file_offset,
3099 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003100 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003101 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003102 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03003103 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04003104 if (!ret) {
3105 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003106 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003107 ordered_extent->disk_bytenr,
3108 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04003109 }
Yan Zhengd899e052008-10-30 14:25:28 -04003110 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003111 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003112 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003113 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003114 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003115 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003116 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003117
Nikolay Borisov510f85e2020-09-18 12:15:52 +03003118 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02003119 if (ret) {
3120 btrfs_abort_transaction(trans, ret);
3121 goto out;
3122 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123
Filipe Manana2766ff62020-11-04 11:07:34 +00003124 /*
3125 * If this is a new delalloc range, clear its new delalloc flag to
3126 * update the inode's number of bytes. This needs to be done first
3127 * before updating the inode item.
3128 */
3129 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3130 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003131 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00003132 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3133 0, 0, &cached_state);
3134
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003135 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003136 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003137 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003138 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003139 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003140 }
3141 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003142out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003143 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003144 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003145 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003146
Miao Xiea698d0752012-09-20 01:51:59 -06003147 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003148 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003149
Josef Bacik77cef2e2013-08-29 13:57:21 -04003150 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003151 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003152
Josef Bacikd61bec02021-05-19 09:38:27 -04003153 /*
3154 * If we failed to finish this ordered extent for any reason we
3155 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3156 * extent, and mark the inode with the error if it wasn't
3157 * already set. Any error during writeback would have already
3158 * set the mapping error, so we need to set it if we're the ones
3159 * marking this ordered extent as failed.
3160 */
3161 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3162 &ordered_extent->flags))
3163 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3164
Josef Bacik77cef2e2013-08-29 13:57:21 -04003165 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003166 unwritten_start += logical_len;
3167 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003168
3169 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003170 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003171
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003172 /*
3173 * If the ordered extent had an IOERR or something else went
3174 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003175 * back to the allocator. We only free the extent in the
3176 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003177 *
3178 * If we made it past insert_reserved_file_extent before we
3179 * errored out then we don't need to do this as the accounting
3180 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003181 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003182 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003183 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003184 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003185 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3186 /*
3187 * Discard the range before returning it back to the
3188 * free space pool
3189 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003190 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003191 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003192 ordered_extent->disk_bytenr,
3193 ordered_extent->disk_num_bytes,
3194 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003195 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003196 ordered_extent->disk_bytenr,
3197 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003198 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003199 }
3200
Josef Bacik5fd02042012-05-02 14:00:54 -04003201 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003202 * This needs to be done to make sure anybody waiting knows we are done
3203 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003204 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003205 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003206
Chris Masone6dcd2d2008-07-17 12:53:50 -04003207 /* once for us */
3208 btrfs_put_ordered_extent(ordered_extent);
3209 /* once for the tree */
3210 btrfs_put_ordered_extent(ordered_extent);
3211
Josef Bacik5fd02042012-05-02 14:00:54 -04003212 return ret;
3213}
3214
3215static void finish_ordered_fn(struct btrfs_work *work)
3216{
3217 struct btrfs_ordered_extent *ordered_extent;
3218 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3219 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003220}
3221
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003222void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3223 struct page *page, u64 start,
David Sterba25c12522021-07-26 14:15:08 +02003224 u64 end, bool uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003225{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003226 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003227
Qu Wenruoe65f1522021-04-01 15:15:06 +08003228 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3229 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003230}
3231
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003232/*
3233 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003234 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003235 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003236 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003237 * @page: page where is the data to be verified
3238 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003239 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003240 *
3241 * The length of such check is always one sector size.
3242 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003243static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003244 u32 bio_offset, struct page *page, u32 pgoff,
3245 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003246{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3248 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003250 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003251 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003252 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003253 u8 *csum_expected;
3254 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003255
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003256 ASSERT(pgoff + len <= PAGE_SIZE);
3257
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003258 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003259 csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003260
3261 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003262 shash->tfm = fs_info->csum_shash;
3263
Eric Biggersfd080012020-04-30 23:51:59 -07003264 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003265
3266 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003267 goto zeroit;
3268
3269 kunmap_atomic(kaddr);
3270 return 0;
3271zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003272 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003273 bbio->mirror_num);
3274 if (bbio->device)
3275 btrfs_dev_stat_inc_and_print(bbio->device,
Nikolay Borisov814723e2020-07-02 15:23:32 +03003276 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003277 memset(kaddr + pgoff, 1, len);
3278 flush_dcache_page(page);
3279 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003280 return -EIO;
3281}
3282
Chris Masond352ac62008-09-29 15:18:18 -04003283/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003284 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003285 * if there's a match, we allow the bio to finish. If not, the code in
3286 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003287 *
3288 * @bio_offset: offset to the beginning of the bio (in bytes)
3289 * @start: file offset of the range start
3290 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003291 *
3292 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3293 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003294 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003295unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio,
3296 u32 bio_offset, struct page *page,
3297 u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003298{
Chris Mason07157aa2007-08-30 08:50:51 -04003299 struct inode *inode = page->mapping->host;
Qu Wenruoe4f94342021-09-27 15:21:49 +08003300 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masond1310b22008-01-24 16:13:08 -05003301 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003302 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003303 const u32 sectorsize = root->fs_info->sectorsize;
3304 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003305 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003306
Qu Wenruoe4f94342021-09-27 15:21:49 +08003307 if (btrfs_page_test_checked(fs_info, page, start, end + 1 - start)) {
3308 btrfs_page_clear_checked(fs_info, page, start, end + 1 - start);
Miao Xiedc380ae2014-09-12 18:43:55 +08003309 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003310 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003311
Qu Wenruo3670e642021-07-26 14:34:51 +08003312 /*
Qu Wenruoe4f94342021-09-27 15:21:49 +08003313 * This only happens for NODATASUM or compressed read.
3314 * Normally this should be covered by above check for compressed read
3315 * or the next check for NODATASUM. Just do a quicker exit here.
Qu Wenruo3670e642021-07-26 14:34:51 +08003316 */
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003317 if (bbio->csum == NULL)
Qu Wenruo3670e642021-07-26 14:34:51 +08003318 return 0;
3319
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003320 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003321 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003322
Josef Bacik056c8312021-11-05 16:45:47 -04003323 if (unlikely(test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)))
Josef Bacik42437a62020-10-16 11:29:18 -04003324 return 0;
3325
Qu Wenruof44cf412020-12-02 14:47:59 +08003326 ASSERT(page_offset(page) <= start &&
3327 end <= page_offset(page) + PAGE_SIZE - 1);
3328 for (pg_off = offset_in_page(start);
3329 pg_off < offset_in_page(end);
3330 pg_off += sectorsize, bio_offset += sectorsize) {
Qu Wenruoe3c62322021-07-26 14:35:04 +08003331 u64 file_offset = pg_off + page_offset(page);
Qu Wenruof44cf412020-12-02 14:47:59 +08003332 int ret;
3333
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09003334 if (btrfs_is_data_reloc_root(root) &&
Qu Wenruoe3c62322021-07-26 14:35:04 +08003335 test_range_bit(io_tree, file_offset,
3336 file_offset + sectorsize - 1,
3337 EXTENT_NODATASUM, 1, NULL)) {
3338 /* Skip the range without csum for data reloc inode */
3339 clear_extent_bits(io_tree, file_offset,
3340 file_offset + sectorsize - 1,
3341 EXTENT_NODATASUM);
3342 continue;
3343 }
Qu Wenruoc3a3b192021-09-15 15:17:18 +08003344 ret = check_data_csum(inode, bbio, bio_offset, page, pg_off,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003345 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003346 if (ret < 0) {
3347 const int nr_bit = (pg_off - offset_in_page(start)) >>
3348 root->fs_info->sectorsize_bits;
3349
3350 result |= (1U << nr_bit);
3351 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003352 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003353 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003354}
Chris Masonb888db22007-08-27 16:49:44 -04003355
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003356/*
3357 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3358 *
3359 * @inode: The inode we want to perform iput on
3360 *
3361 * This function uses the generic vfs_inode::i_count to track whether we should
3362 * just decrement it (in case it's > 1) or if this is the last iput then link
3363 * the inode to the delayed iput machinery. Delayed iputs are processed at
3364 * transaction commit time/superblock commit/cleaner kthread.
3365 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003366void btrfs_add_delayed_iput(struct inode *inode)
3367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003369 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003370
3371 if (atomic_add_unless(&inode->i_count, -1, 1))
3372 return;
3373
Josef Bacik034f7842018-12-03 11:06:52 -05003374 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003375 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003376 ASSERT(list_empty(&binode->delayed_iput));
3377 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003378 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003379 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3380 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003381}
3382
Josef Bacik63611e72019-06-18 10:59:18 -04003383static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3384 struct btrfs_inode *inode)
3385{
3386 list_del_init(&inode->delayed_iput);
3387 spin_unlock(&fs_info->delayed_iput_lock);
3388 iput(&inode->vfs_inode);
3389 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3390 wake_up(&fs_info->delayed_iputs_wait);
3391 spin_lock(&fs_info->delayed_iput_lock);
3392}
3393
3394static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3395 struct btrfs_inode *inode)
3396{
3397 if (!list_empty(&inode->delayed_iput)) {
3398 spin_lock(&fs_info->delayed_iput_lock);
3399 if (!list_empty(&inode->delayed_iput))
3400 run_delayed_iput_locked(fs_info, inode);
3401 spin_unlock(&fs_info->delayed_iput_lock);
3402 }
3403}
3404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003405void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003406{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003407
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003408 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003409 while (!list_empty(&fs_info->delayed_iputs)) {
3410 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003411
David Sterba8089fe62015-11-19 14:15:51 +01003412 inode = list_first_entry(&fs_info->delayed_iputs,
3413 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003414 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003415 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003416 }
David Sterba8089fe62015-11-19 14:15:51 +01003417 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003418}
3419
Josef Bacik034f7842018-12-03 11:06:52 -05003420/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003421 * Wait for flushing all delayed iputs
3422 *
3423 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003424 *
3425 * This will wait on any delayed iputs that are currently running with KILLABLE
3426 * set. Once they are all done running we will return, unless we are killed in
3427 * which case we return EINTR. This helps in user operations like fallocate etc
3428 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003429 *
3430 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003431 */
3432int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3433{
3434 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3435 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3436 if (ret)
3437 return -EINTR;
3438 return 0;
3439}
3440
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003442 * This creates an orphan entry for the given inode in case something goes wrong
3443 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003444 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003445int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003446 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003447{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003448 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003449
Omar Sandoval27919062018-05-11 13:13:37 -07003450 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3451 if (ret && ret != -EEXIST) {
3452 btrfs_abort_transaction(trans, ret);
3453 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 }
3455
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003457}
3458
3459/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003460 * We have done the delete so we can go ahead and remove the orphan item for
3461 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003462 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003463static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003464 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003465{
Omar Sandoval27919062018-05-11 13:13:37 -07003466 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003467}
3468
3469/*
3470 * this cleans up any orphans that may be left on the list from the last use
3471 * of this root.
3472 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003474{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003475 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003476 struct btrfs_path *path;
3477 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003478 struct btrfs_key key, found_key;
3479 struct btrfs_trans_handle *trans;
3480 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003481 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003482 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003483
Josef Bacik54230012021-11-09 10:12:06 -05003484 if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003486
3487 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (!path) {
3489 ret = -ENOMEM;
3490 goto out;
3491 }
David Sterbae4058b52015-11-27 16:31:35 +01003492 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003493
3494 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003495 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003496 key.offset = (u64)-1;
3497
Josef Bacik7b128762008-07-24 12:17:14 -04003498 while (1) {
3499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 if (ret < 0)
3501 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003502
3503 /*
3504 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003505 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003506 * find the key and see if we have stuff that matches
3507 */
3508 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003510 if (path->slots[0] == 0)
3511 break;
3512 path->slots[0]--;
3513 }
3514
3515 /* pull out the item */
3516 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3518
3519 /* make sure the item matches what we want */
3520 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3521 break;
David Sterba962a2982014-06-04 18:41:45 +02003522 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003523 break;
3524
3525 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003526 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003527
3528 /*
3529 * this is where we are basically btrfs_lookup, without the
3530 * crossing root thing. we store the inode number in the
3531 * offset of the orphan item.
3532 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003533
3534 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 btrfs_err(fs_info,
3536 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003537 ret = -EINVAL;
3538 goto out;
3539 }
3540
3541 last_objectid = found_key.offset;
3542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003543 found_key.objectid = found_key.offset;
3544 found_key.type = BTRFS_INODE_ITEM_KEY;
3545 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003546 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303547 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003548 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003549 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003550
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003551 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003552 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003553 int is_dead_root = 0;
3554
3555 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003556 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003557 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003558 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003559 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003560 * We need to distinguish those two, as the orphan item
3561 * for a root must not get deleted before the deletion
3562 * of the snapshot/subvolume's tree completes.
3563 *
3564 * btrfs_find_orphan_roots() ran before us, which has
3565 * found all deleted roots and loaded them into
3566 * fs_info->fs_roots_radix. So here we can find if an
3567 * orphan item corresponds to a deleted root by looking
3568 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003569 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003570
3571 spin_lock(&fs_info->fs_roots_radix_lock);
3572 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3573 (unsigned long)found_key.objectid);
3574 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3575 is_dead_root = 1;
3576 spin_unlock(&fs_info->fs_roots_radix_lock);
3577
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003578 if (is_dead_root) {
3579 /* prevent this orphan from being found again */
3580 key.offset = found_key.objectid - 1;
3581 continue;
3582 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003583
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003584 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003585
Josef Bacika8c9e572011-09-21 16:55:59 -04003586 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003587 * If we have an inode with links, there are a couple of
Boris Burkov70524252021-06-30 13:01:50 -07003588 * possibilities:
3589 *
3590 * 1. We were halfway through creating fsverity metadata for the
3591 * file. In that case, the orphan item represents incomplete
3592 * fsverity metadata which must be cleaned up with
3593 * btrfs_drop_verity_items and deleting the orphan item.
3594
3595 * 2. Old kernels (before v3.12) used to create an
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003596 * orphan item for truncate indicating that there were possibly
3597 * extent items past i_size that needed to be deleted. In v3.12,
3598 * truncate was changed to update i_size in sync with the extent
3599 * items, but the (useless) orphan item was still created. Since
3600 * v4.18, we don't create the orphan item for truncate at all.
3601 *
3602 * So, this item could mean that we need to do a truncate, but
3603 * only if this filesystem was last used on a pre-v3.12 kernel
3604 * and was not cleanly unmounted. The odds of that are quite
3605 * slim, and it's a pain to do the truncate now, so just delete
3606 * the orphan item.
3607 *
3608 * It's also possible that this orphan item was supposed to be
3609 * deleted but wasn't. The inode number may have been reused,
3610 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003611 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003612 if (ret == -ENOENT || inode->i_nlink) {
Boris Burkov70524252021-06-30 13:01:50 -07003613 if (!ret) {
3614 ret = btrfs_drop_verity_items(BTRFS_I(inode));
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003615 iput(inode);
Boris Burkov70524252021-06-30 13:01:50 -07003616 if (ret)
3617 goto out;
3618 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003619 trans = btrfs_start_transaction(root, 1);
3620 if (IS_ERR(trans)) {
3621 ret = PTR_ERR(trans);
3622 goto out;
3623 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 btrfs_debug(fs_info, "auto deleting %Lu",
3625 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003626 ret = btrfs_del_orphan_item(trans, root,
3627 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003628 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003629 if (ret)
3630 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003631 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003632 }
Josef Bacik7b128762008-07-24 12:17:14 -04003633
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003634 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003635
3636 /* this will do delete_inode and everything for us */
3637 iput(inode);
3638 }
Miao Xie3254c872011-11-10 20:45:05 -05003639 /* release the path since we're done with it */
3640 btrfs_release_path(path);
3641
Omar Sandovala575cee2018-05-11 13:13:38 -07003642 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003643 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003644 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003645 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003646 }
Josef Bacik7b128762008-07-24 12:17:14 -04003647
3648 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003649 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003650
3651out:
3652 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003653 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003654 btrfs_free_path(path);
3655 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003656}
3657
Chris Masond352ac62008-09-29 15:18:18 -04003658/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003659 * very simple check to peek ahead in the leaf looking for xattrs. If we
3660 * don't find any xattrs, we know there can't be any acls.
3661 *
3662 * slot is the slot the inode is in, objectid is the objectid of the inode
3663 */
3664static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003665 int slot, u64 objectid,
3666 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003667{
3668 u32 nritems = btrfs_header_nritems(leaf);
3669 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003670 static u64 xattr_access = 0;
3671 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003672 int scanned = 0;
3673
Josef Bacikf23b5a52013-06-19 10:16:26 -04003674 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003675 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3676 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3677 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3678 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003679 }
3680
Chris Mason46a53cc2009-04-27 11:47:50 -04003681 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003682 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003683 while (slot < nritems) {
3684 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3685
3686 /* we found a different objectid, there must not be acls */
3687 if (found_key.objectid != objectid)
3688 return 0;
3689
3690 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003691 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 if (*first_xattr_slot == -1)
3693 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003694 if (found_key.offset == xattr_access ||
3695 found_key.offset == xattr_default)
3696 return 1;
3697 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003698
3699 /*
3700 * we found a key greater than an xattr key, there can't
3701 * be any acls later on
3702 */
3703 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3704 return 0;
3705
3706 slot++;
3707 scanned++;
3708
3709 /*
3710 * it goes inode, inode backrefs, xattrs, extents,
3711 * so if there are a ton of hard links to an inode there can
3712 * be a lot of backrefs. Don't waste time searching too hard,
3713 * this is just an optimization
3714 */
3715 if (scanned >= 8)
3716 break;
3717 }
3718 /* we hit the end of the leaf before we found an xattr or
3719 * something larger than an xattr. We have to assume the inode
3720 * has acls
3721 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003722 if (*first_xattr_slot == -1)
3723 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003724 return 1;
3725}
3726
3727/*
Chris Masond352ac62008-09-29 15:18:18 -04003728 * read an inode from the btree into the in-memory inode
3729 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003730static int btrfs_read_locked_inode(struct inode *inode,
3731 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003734 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003735 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 struct btrfs_inode_item *inode_item;
3737 struct btrfs_root *root = BTRFS_I(inode)->root;
3738 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003739 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003740 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003741 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003742 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003743 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003744 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003745
3746 ret = btrfs_fill_inode(inode, &rdev);
3747 if (!ret)
3748 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003749
Filipe Manana4222ea72018-10-24 10:13:03 +01003750 if (!path) {
3751 path = btrfs_alloc_path();
3752 if (!path)
3753 return -ENOMEM;
3754 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003755
Chris Mason39279cc2007-06-12 06:35:45 -04003756 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003759 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003760 if (path != in_path)
3761 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003762 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003763 }
Chris Mason39279cc2007-06-12 06:35:45 -04003764
Chris Mason5f39d392007-10-15 16:14:19 -04003765 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003766
3767 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003768 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003769
Chris Mason5f39d392007-10-15 16:14:19 -04003770 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3771 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003772 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003773 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003774 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3775 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003776 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003777 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3778 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003779
David Sterbaa937b972014-12-12 17:39:12 +01003780 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3781 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
David Sterbaa937b972014-12-12 17:39:12 +01003783 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3784 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003785
David Sterbaa937b972014-12-12 17:39:12 +01003786 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3787 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
chandan r9cc97d62012-07-04 12:48:07 +05303789 BTRFS_I(inode)->i_otime.tv_sec =
3790 btrfs_timespec_sec(leaf, &inode_item->otime);
3791 BTRFS_I(inode)->i_otime.tv_nsec =
3792 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003794 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003795 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003796 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3797
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003798 inode_set_iversion_queried(inode,
3799 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003800 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003801 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003802 rdev = btrfs_inode_rdev(leaf, inode_item);
3803
Josef Bacikaec74772008-07-24 12:12:38 -04003804 BTRFS_I(inode)->index_cnt = (u64)-1;
Boris Burkov77eea052021-06-30 13:01:48 -07003805 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3806 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
Miao Xie67de1172013-12-26 13:07:06 +08003807
3808cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003809 /*
3810 * If we were modified in the current generation and evicted from memory
3811 * and then re-read we need to do a full sync since we don't have any
3812 * idea about which extents were modified before we were evicted from
3813 * cache.
3814 *
3815 * This is required for both inode re-read from disk and delayed inode
3816 * in delayed_nodes_tree.
3817 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003818 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003819 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3820 &BTRFS_I(inode)->runtime_flags);
3821
Filipe Mananabde6c242015-07-24 00:00:19 +01003822 /*
3823 * We don't persist the id of the transaction where an unlink operation
3824 * against the inode was last made. So here we assume the inode might
3825 * have been evicted, and therefore the exact value of last_unlink_trans
3826 * lost, and set it to last_trans to avoid metadata inconsistencies
3827 * between the inode and its parent if the inode is fsync'ed and the log
3828 * replayed. For example, in the scenario:
3829 *
3830 * touch mydir/foo
3831 * ln mydir/foo mydir/bar
3832 * sync
3833 * unlink mydir/bar
3834 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3835 * xfs_io -c fsync mydir/foo
3836 * <power failure>
3837 * mount fs, triggers fsync log replay
3838 *
3839 * We must make sure that when we fsync our inode foo we also log its
3840 * parent inode, otherwise after log replay the parent still has the
3841 * dentry with the "bar" name but our inode foo has a link count of 1
3842 * and doesn't have an inode ref with the name "bar" anymore.
3843 *
3844 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003845 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003846 * transaction commits on fsync if our inode is a directory, or if our
3847 * inode is not a directory, logging its parent unnecessarily.
3848 */
3849 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3850
Filipe Manana3ebac172020-07-15 12:30:43 +01003851 /*
3852 * Same logic as for last_unlink_trans. We don't persist the generation
3853 * of the last transaction where this inode was used for a reflink
3854 * operation, so after eviction and reloading the inode we must be
3855 * pessimistic and assume the last transaction that modified the inode.
3856 */
3857 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3858
Miao Xie67de1172013-12-26 13:07:06 +08003859 path->slots[0]++;
3860 if (inode->i_nlink != 1 ||
3861 path->slots[0] >= btrfs_header_nritems(leaf))
3862 goto cache_acl;
3863
3864 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003865 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003866 goto cache_acl;
3867
3868 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3869 if (location.type == BTRFS_INODE_REF_KEY) {
3870 struct btrfs_inode_ref *ref;
3871
3872 ref = (struct btrfs_inode_ref *)ptr;
3873 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3874 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3875 struct btrfs_inode_extref *extref;
3876
3877 extref = (struct btrfs_inode_extref *)ptr;
3878 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3879 extref);
3880 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003881cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003882 /*
3883 * try to precache a NULL acl entry for files that don't have
3884 * any xattrs or acls
3885 */
Li Zefan33345d012011-04-20 10:31:50 +08003886 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003887 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003888 if (first_xattr_slot != -1) {
3889 path->slots[0] = first_xattr_slot;
3890 ret = btrfs_load_inode_props(inode, path);
3891 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003892 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003893 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003894 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003895 root->root_key.objectid, ret);
3896 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003897 if (path != in_path)
3898 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003899
Al Viro72c04902009-06-24 16:58:48 -04003900 if (!maybe_acls)
3901 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003902
Chris Mason39279cc2007-06-12 06:35:45 -04003903 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003904 case S_IFREG:
3905 inode->i_mapping->a_ops = &btrfs_aops;
3906 inode->i_fop = &btrfs_file_operations;
3907 inode->i_op = &btrfs_file_inode_operations;
3908 break;
3909 case S_IFDIR:
3910 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003911 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 break;
3913 case S_IFLNK:
3914 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003915 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003916 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003917 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003918 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003919 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003920 init_special_inode(inode, inode->i_mode, rdev);
3921 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003922 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003923
David Sterba7b6a2212018-03-26 18:40:21 +02003924 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003925 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003926}
3927
Chris Masond352ac62008-09-29 15:18:18 -04003928/*
3929 * given a leaf and an inode, copy the inode fields into the leaf
3930 */
Chris Masone02119d2008-09-05 16:13:11 -04003931static void fill_inode_item(struct btrfs_trans_handle *trans,
3932 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003933 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003934 struct inode *inode)
3935{
Liu Bo51fab692012-12-27 09:01:21 +00003936 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07003937 u64 flags;
Chris Mason5f39d392007-10-15 16:14:19 -04003938
David Sterbac82f8232019-08-09 17:48:21 +02003939 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003940
David Sterbacc4c13d2020-04-29 02:15:56 +02003941 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3942 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3943 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3944 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3945 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003946
David Sterbacc4c13d2020-04-29 02:15:56 +02003947 btrfs_set_token_timespec_sec(&token, &item->atime,
3948 inode->i_atime.tv_sec);
3949 btrfs_set_token_timespec_nsec(&token, &item->atime,
3950 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003951
David Sterbacc4c13d2020-04-29 02:15:56 +02003952 btrfs_set_token_timespec_sec(&token, &item->mtime,
3953 inode->i_mtime.tv_sec);
3954 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3955 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003956
David Sterbacc4c13d2020-04-29 02:15:56 +02003957 btrfs_set_token_timespec_sec(&token, &item->ctime,
3958 inode->i_ctime.tv_sec);
3959 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3960 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003961
David Sterbacc4c13d2020-04-29 02:15:56 +02003962 btrfs_set_token_timespec_sec(&token, &item->otime,
3963 BTRFS_I(inode)->i_otime.tv_sec);
3964 btrfs_set_token_timespec_nsec(&token, &item->otime,
3965 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303966
David Sterbacc4c13d2020-04-29 02:15:56 +02003967 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3968 btrfs_set_token_inode_generation(&token, item,
3969 BTRFS_I(inode)->generation);
3970 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3971 btrfs_set_token_inode_transid(&token, item, trans->transid);
3972 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07003973 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3974 BTRFS_I(inode)->ro_flags);
3975 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02003976 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003977}
3978
Chris Masond352ac62008-09-29 15:18:18 -04003979/*
3980 * copy everything in the in-memory inode into the btree.
3981 */
Chris Mason21151332011-11-10 20:39:08 -05003982static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003983 struct btrfs_root *root,
3984 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003985{
3986 struct btrfs_inode_item *inode_item;
3987 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003989 int ret;
3990
3991 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003992 if (!path)
3993 return -ENOMEM;
3994
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003995 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003996 if (ret) {
3997 if (ret > 0)
3998 ret = -ENOENT;
3999 goto failed;
4000 }
4001
Chris Mason5f39d392007-10-15 16:14:19 -04004002 leaf = path->nodes[0];
4003 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004004 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004005
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02004006 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004007 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02004008 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004009 ret = 0;
4010failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004011 btrfs_free_path(path);
4012 return ret;
4013}
4014
Chris Masond352ac62008-09-29 15:18:18 -04004015/*
Chris Mason21151332011-11-10 20:39:08 -05004016 * copy everything in the in-memory inode into the btree.
4017 */
4018noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004019 struct btrfs_root *root,
4020 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004021{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004022 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004023 int ret;
4024
4025 /*
4026 * If the inode is a free space inode, we can deadlock during commit
4027 * if we put it into the delayed code.
4028 *
4029 * The data relocation inode should also be directly updated
4030 * without delay
4031 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004032 if (!btrfs_is_free_space_inode(inode)
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09004033 && !btrfs_is_data_reloc_root(root)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004034 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004035 btrfs_update_root_times(trans, root);
4036
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004037 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004038 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004039 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05004040 return ret;
4041 }
4042
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004043 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004044}
4045
Nikolay Borisov729f7962020-11-02 16:49:06 +02004046int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4047 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004048{
4049 int ret;
4050
Nikolay Borisov729f7962020-11-02 16:49:06 +02004051 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004052 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02004053 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004054 return ret;
4055}
4056
4057/*
Chris Masond352ac62008-09-29 15:18:18 -04004058 * unlink helper that gets used here in inode.c and in the tree logging
4059 * recovery code. It remove a link in a directory with a given name, and
4060 * also drops the back refs in the inode to the directory
4061 */
Al Viro92986792011-03-04 17:14:37 +00004062static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 struct btrfs_inode *dir,
4064 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004065 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004066{
Filipe Manana4467af82021-10-25 17:31:50 +01004067 struct btrfs_root *root = dir->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004068 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004070 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004072 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004073 u64 ino = btrfs_ino(inode);
4074 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004075
4076 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004077 if (!path) {
4078 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004079 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004080 }
4081
Li Zefan33345d012011-04-20 10:31:50 +08004082 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004083 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004084 if (IS_ERR_OR_NULL(di)) {
4085 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004086 goto err;
4087 }
Chris Mason39279cc2007-06-12 06:35:45 -04004088 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004089 if (ret)
4090 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004091 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004092
Miao Xie67de1172013-12-26 13:07:06 +08004093 /*
4094 * If we don't have dir index, we have to get it by looking up
4095 * the inode ref, since we get the inode ref, remove it directly,
4096 * it is unnecessary to do delayed deletion.
4097 *
4098 * But if we have dir index, needn't search inode ref to get it.
4099 * Since the inode ref is close to the inode item, it is better
4100 * that we delay to delete it, and just do this deletion when
4101 * we update the inode item.
4102 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004103 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004104 ret = btrfs_delayed_delete_inode_ref(inode);
4105 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004106 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004107 goto skip_backref;
4108 }
4109 }
4110
Li Zefan33345d012011-04-20 10:31:50 +08004111 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4112 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004113 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004114 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004115 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004116 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004118 goto err;
4119 }
Miao Xie67de1172013-12-26 13:07:06 +08004120skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004121 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004124 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 }
Chris Mason39279cc2007-06-12 06:35:45 -04004126
Josef Bacik9a35fc92021-10-05 16:35:24 -04004127 btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4128 dir_ino);
4129 btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir, index);
Josef Bacik63611e72019-06-18 10:59:18 -04004130
4131 /*
4132 * If we have a pending delayed iput we could end up with the final iput
4133 * being run in btrfs-cleaner context. If we have enough of these built
4134 * up we can end up burning a lot of time in btrfs-cleaner without any
4135 * way to throttle the unlinks. Since we're currently holding a ref on
4136 * the inode we can run the delayed iput here without any issues as the
4137 * final iput won't be done until after we drop the ref we're currently
4138 * holding.
4139 */
4140 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004141err:
4142 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004143 if (ret)
4144 goto out;
4145
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004146 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004147 inode_inc_iversion(&inode->vfs_inode);
4148 inode_inc_iversion(&dir->vfs_inode);
4149 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4150 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004151 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004152out:
Chris Mason39279cc2007-06-12 06:35:45 -04004153 return ret;
4154}
4155
Al Viro92986792011-03-04 17:14:37 +00004156int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004158 const char *name, int name_len)
4159{
4160 int ret;
Filipe Manana4467af82021-10-25 17:31:50 +01004161 ret = __btrfs_unlink_inode(trans, dir, inode, name, name_len);
Al Viro92986792011-03-04 17:14:37 +00004162 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004163 drop_nlink(&inode->vfs_inode);
Filipe Manana4467af82021-10-25 17:31:50 +01004164 ret = btrfs_update_inode(trans, inode->root, inode);
Al Viro92986792011-03-04 17:14:37 +00004165 }
4166 return ret;
4167}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168
4169/*
4170 * helper to start transaction for unlink and rmdir.
4171 *
Josef Bacikd52be812013-05-29 14:54:47 -04004172 * unlink and rmdir are special in btrfs, they do not always free space, so
4173 * if we cannot make our reservations the normal way try and see if there is
4174 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4175 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004176 */
Josef Bacikd52be812013-05-29 14:54:47 -04004177static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004180
Josef Bacike70bea52011-10-11 14:18:24 -04004181 /*
4182 * 1 for the possible orphan item
4183 * 1 for the dir item
4184 * 1 for the dir index
4185 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004186 * 1 for the inode
4187 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004188 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189}
4190
Chris Mason39279cc2007-06-12 06:35:45 -04004191static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4192{
Chris Mason39279cc2007-06-12 06:35:45 -04004193 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004194 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 int ret;
4196
Josef Bacikd52be812013-05-29 14:54:47 -04004197 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004198 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004199 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004200
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004201 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4202 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004203
Filipe Manana4467af82021-10-25 17:31:50 +01004204 ret = btrfs_unlink_inode(trans, BTRFS_I(dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004205 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4206 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004207 if (ret)
4208 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004209
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004211 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004212 if (ret)
4213 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004214 }
Josef Bacik7b128762008-07-24 12:17:14 -04004215
Tsutomu Itohb5324022011-07-19 07:27:20 +00004216out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004217 btrfs_end_transaction(trans);
Filipe Manana4467af82021-10-25 17:31:50 +01004218 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 return ret;
4220}
4221
Misono Tomohirof60a2362018-04-18 11:34:52 +09004222static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004223 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004225 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004226 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 struct btrfs_path *path;
4228 struct extent_buffer *leaf;
4229 struct btrfs_dir_item *di;
4230 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004231 const char *name = dentry->d_name.name;
4232 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233 u64 index;
4234 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004235 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004236 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237
Josef Bacik045d3962019-12-18 17:20:27 -05004238 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4239 objectid = inode->root->root_key.objectid;
4240 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4241 objectid = inode->location.objectid;
4242 } else {
4243 WARN_ON(1);
4244 return -EINVAL;
4245 }
4246
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247 path = btrfs_alloc_path();
4248 if (!path)
4249 return -ENOMEM;
4250
Li Zefan33345d012011-04-20 10:31:50 +08004251 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004254 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 goto out;
4256 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257
4258 leaf = path->nodes[0];
4259 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4260 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4261 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004263 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 goto out;
4265 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004266 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004267
Josef Bacikd49d3282019-12-18 17:20:28 -05004268 /*
4269 * This is a placeholder inode for a subvolume we didn't have a
4270 * reference to at the time of the snapshot creation. In the meantime
4271 * we could have renamed the real subvol link into our snapshot, so
David Sterba1a9fd412021-05-21 17:42:23 +02004272 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
Josef Bacikd49d3282019-12-18 17:20:28 -05004273 * Instead simply lookup the dir_index_item for this entry so we can
4274 * remove it. Otherwise we know we have a ref to the root and we can
4275 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4276 */
4277 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004278 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 if (IS_ERR_OR_NULL(di)) {
4281 if (!di)
4282 ret = -ENOENT;
4283 else
4284 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004285 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 goto out;
4287 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288
4289 leaf = path->nodes[0];
4290 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004291 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004292 btrfs_release_path(path);
4293 } else {
4294 ret = btrfs_del_root_ref(trans, objectid,
4295 root->root_key.objectid, dir_ino,
4296 &index, name, name_len);
4297 if (ret) {
4298 btrfs_abort_transaction(trans, ret);
4299 goto out;
4300 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004301 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004302
Lu Fengqi9add2942018-08-01 11:32:26 +08004303 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004305 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004306 goto out;
4307 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004308
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004309 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004310 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004311 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004312 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004313 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004314 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004315out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004316 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004318}
4319
Misono Tomohiroec42f162018-04-18 11:34:13 +09004320/*
4321 * Helper to check if the subvolume references other subvolumes or if it's
4322 * default.
4323 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004324static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004325{
4326 struct btrfs_fs_info *fs_info = root->fs_info;
4327 struct btrfs_path *path;
4328 struct btrfs_dir_item *di;
4329 struct btrfs_key key;
4330 u64 dir_id;
4331 int ret;
4332
4333 path = btrfs_alloc_path();
4334 if (!path)
4335 return -ENOMEM;
4336
4337 /* Make sure this root isn't set as the default subvol */
4338 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4339 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4340 dir_id, "default", 7, 0);
4341 if (di && !IS_ERR(di)) {
4342 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4343 if (key.objectid == root->root_key.objectid) {
4344 ret = -EPERM;
4345 btrfs_err(fs_info,
4346 "deleting default subvolume %llu is not allowed",
4347 key.objectid);
4348 goto out;
4349 }
4350 btrfs_release_path(path);
4351 }
4352
4353 key.objectid = root->root_key.objectid;
4354 key.type = BTRFS_ROOT_REF_KEY;
4355 key.offset = (u64)-1;
4356
4357 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4358 if (ret < 0)
4359 goto out;
4360 BUG_ON(ret == 0);
4361
4362 ret = 0;
4363 if (path->slots[0] > 0) {
4364 path->slots[0]--;
4365 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4366 if (key.objectid == root->root_key.objectid &&
4367 key.type == BTRFS_ROOT_REF_KEY)
4368 ret = -ENOTEMPTY;
4369 }
4370out:
4371 btrfs_free_path(path);
4372 return ret;
4373}
4374
Nikolay Borisov20a68002018-04-27 14:36:24 +03004375/* Delete all dentries for inodes belonging to the root */
4376static void btrfs_prune_dentries(struct btrfs_root *root)
4377{
4378 struct btrfs_fs_info *fs_info = root->fs_info;
4379 struct rb_node *node;
4380 struct rb_node *prev;
4381 struct btrfs_inode *entry;
4382 struct inode *inode;
4383 u64 objectid = 0;
4384
Josef Bacik84961532021-10-05 16:35:25 -04004385 if (!BTRFS_FS_ERROR(fs_info))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004386 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4387
4388 spin_lock(&root->inode_lock);
4389again:
4390 node = root->inode_tree.rb_node;
4391 prev = NULL;
4392 while (node) {
4393 prev = node;
4394 entry = rb_entry(node, struct btrfs_inode, rb_node);
4395
David Sterba37508512018-06-29 10:56:40 +02004396 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004397 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004398 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004399 node = node->rb_right;
4400 else
4401 break;
4402 }
4403 if (!node) {
4404 while (prev) {
4405 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004406 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004407 node = prev;
4408 break;
4409 }
4410 prev = rb_next(prev);
4411 }
4412 }
4413 while (node) {
4414 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004415 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004416 inode = igrab(&entry->vfs_inode);
4417 if (inode) {
4418 spin_unlock(&root->inode_lock);
4419 if (atomic_read(&inode->i_count) > 1)
4420 d_prune_aliases(inode);
4421 /*
4422 * btrfs_drop_inode will have it removed from the inode
4423 * cache when its usage count hits zero.
4424 */
4425 iput(inode);
4426 cond_resched();
4427 spin_lock(&root->inode_lock);
4428 goto again;
4429 }
4430
4431 if (cond_resched_lock(&root->inode_lock))
4432 goto again;
4433
4434 node = rb_next(node);
4435 }
4436 spin_unlock(&root->inode_lock);
4437}
4438
Misono Tomohirof60a2362018-04-18 11:34:52 +09004439int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4440{
4441 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4442 struct btrfs_root *root = BTRFS_I(dir)->root;
4443 struct inode *inode = d_inode(dentry);
4444 struct btrfs_root *dest = BTRFS_I(inode)->root;
4445 struct btrfs_trans_handle *trans;
4446 struct btrfs_block_rsv block_rsv;
4447 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004448 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004449
4450 /*
4451 * Don't allow to delete a subvolume with send in progress. This is
4452 * inside the inode lock so the error handling that has to drop the bit
4453 * again is not run concurrently.
4454 */
4455 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004456 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004457 spin_unlock(&dest->root_item_lock);
4458 btrfs_warn(fs_info,
4459 "attempt to delete subvolume %llu during send",
4460 dest->root_key.objectid);
4461 return -EPERM;
4462 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004463 root_flags = btrfs_root_flags(&dest->root_item);
4464 btrfs_set_root_flags(&dest->root_item,
4465 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4466 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004467
4468 down_write(&fs_info->subvol_sem);
4469
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004470 ret = may_destroy_subvol(dest);
4471 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004472 goto out_up_write;
4473
4474 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4475 /*
4476 * One for dir inode,
4477 * two for dir entries,
4478 * two for root ref/backref.
4479 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004480 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4481 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004482 goto out_up_write;
4483
4484 trans = btrfs_start_transaction(root, 0);
4485 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004486 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004487 goto out_release;
4488 }
4489 trans->block_rsv = &block_rsv;
4490 trans->bytes_reserved = block_rsv.size;
4491
4492 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4493
Josef Bacik045d3962019-12-18 17:20:27 -05004494 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004495 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004496 btrfs_abort_transaction(trans, ret);
4497 goto out_end_trans;
4498 }
4499
Josef Bacik2731f512021-03-12 15:25:04 -05004500 ret = btrfs_record_root_in_trans(trans, dest);
4501 if (ret) {
4502 btrfs_abort_transaction(trans, ret);
4503 goto out_end_trans;
4504 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004505
4506 memset(&dest->root_item.drop_progress, 0,
4507 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004508 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004509 btrfs_set_root_refs(&dest->root_item, 0);
4510
4511 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4512 ret = btrfs_insert_orphan_item(trans,
4513 fs_info->tree_root,
4514 dest->root_key.objectid);
4515 if (ret) {
4516 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004517 goto out_end_trans;
4518 }
4519 }
4520
Lu Fengqid1957792018-05-29 15:01:54 +08004521 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004522 BTRFS_UUID_KEY_SUBVOL,
4523 dest->root_key.objectid);
4524 if (ret && ret != -ENOENT) {
4525 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004526 goto out_end_trans;
4527 }
4528 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004529 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004530 dest->root_item.received_uuid,
4531 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4532 dest->root_key.objectid);
4533 if (ret && ret != -ENOENT) {
4534 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004535 goto out_end_trans;
4536 }
4537 }
4538
Qu Wenruo082b6c92020-06-16 10:17:37 +08004539 free_anon_bdev(dest->anon_dev);
4540 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004541out_end_trans:
4542 trans->block_rsv = NULL;
4543 trans->bytes_reserved = 0;
4544 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004545 inode->i_flags |= S_DEAD;
4546out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004547 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004548out_up_write:
4549 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004550 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004551 spin_lock(&dest->root_item_lock);
4552 root_flags = btrfs_root_flags(&dest->root_item);
4553 btrfs_set_root_flags(&dest->root_item,
4554 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4555 spin_unlock(&dest->root_item_lock);
4556 } else {
4557 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004558 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004559 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004560 }
4561
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004562 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004563}
4564
Chris Mason39279cc2007-06-12 06:35:45 -04004565static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4566{
David Howells2b0143b2015-03-17 22:25:59 +00004567 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004568 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004569 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004570 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004571
David Sterbab3ae2442012-09-13 16:04:34 -06004572 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004573 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004574 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004575 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004576
Josef Bacikd52be812013-05-29 14:54:47 -04004577 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004578 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004579 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004580
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004581 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004582 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004583 goto out;
4584 }
4585
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004586 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004587 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004588 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004589
Filipe Manana44f714d2016-06-06 16:11:13 +01004590 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4591
Chris Mason39279cc2007-06-12 06:35:45 -04004592 /* now the directory is empty */
Filipe Manana4467af82021-10-25 17:31:50 +01004593 err = btrfs_unlink_inode(trans, BTRFS_I(dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004594 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4595 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004596 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004597 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004598 /*
4599 * Propagate the last_unlink_trans value of the deleted dir to
4600 * its parent directory. This is to prevent an unrecoverable
4601 * log tree in the case we do something like this:
4602 * 1) create dir foo
4603 * 2) create snapshot under dir foo
4604 * 3) delete the snapshot
4605 * 4) rmdir foo
4606 * 5) mkdir foo
4607 * 6) fsync foo or some file inside foo
4608 */
4609 if (last_unlink_trans >= trans->transid)
4610 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4611 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004612out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004613 btrfs_end_transaction(trans);
Filipe Manana4467af82021-10-25 17:31:50 +01004614 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004615
Chris Mason39279cc2007-06-12 06:35:45 -04004616 return err;
4617}
4618
Josef Bacikddfae632017-10-19 14:16:02 -04004619/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304620 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004621 * @inode - inode that we're zeroing
4622 * @from - the offset to start zeroing
4623 * @len - the length to zero, 0 to zero the entire range respective to the
4624 * offset
4625 * @front - zero up to the offset instead of from the offset on
4626 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304627 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004628 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004629 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004630int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4631 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004632{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004633 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4634 struct address_space *mapping = inode->vfs_inode.i_mapping;
4635 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004636 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004637 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004638 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004639 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004640 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004641 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304642 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004643 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004644 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004645 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004646 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304647 u64 block_start;
4648 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004649
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004650 if (IS_ALIGNED(offset, blocksize) &&
4651 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004652 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304653
Josef Bacik8b62f872017-10-19 14:15:55 -04004654 block_start = round_down(from, blocksize);
4655 block_end = block_start + blocksize - 1;
4656
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004657 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4658 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004659 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004660 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004661 /* For nocow case, no need to reserve data space */
4662 only_release_metadata = true;
4663 } else {
4664 goto out;
4665 }
4666 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004667 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004668 if (ret < 0) {
4669 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004670 btrfs_free_reserved_data_space(inode, data_reserved,
4671 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004672 goto out;
4673 }
Chris Mason211c17f2008-05-15 09:13:45 -04004674again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004675 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004676 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004677 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4678 blocksize, true);
4679 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004680 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004681 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004682 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004683 ret = set_page_extent_mapped(page);
4684 if (ret < 0)
4685 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686
Chris Masona52d9a82007-08-27 16:49:44 -04004687 if (!PageUptodate(page)) {
4688 ret = btrfs_readpage(NULL, page);
4689 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004690 if (page->mapping != mapping) {
4691 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004692 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004693 goto again;
4694 }
Chris Masona52d9a82007-08-27 16:49:44 -04004695 if (!PageUptodate(page)) {
4696 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004697 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004698 }
4699 }
Chris Mason211c17f2008-05-15 09:13:45 -04004700 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004703
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004704 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004705 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004707 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004708 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004709 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004710 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004711 btrfs_put_ordered_extent(ordered);
4712 goto again;
4713 }
4714
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004715 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004716 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4717 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004718
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004719 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004720 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004721 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004723 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004724 goto out_unlock;
4725 }
4726
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004728 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304729 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04004730 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07004731 memzero_page(page, (block_start - page_offset(page)),
4732 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004733 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07004734 memzero_page(page, (block_start - page_offset(page)) + offset,
4735 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004736 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08004738 btrfs_page_clear_checked(fs_info, page, block_start,
4739 block_end + 1 - block_start);
Qu Wenruo6c9ac8b2021-05-31 16:50:51 +08004740 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
David Sterbae43bbe52017-12-12 21:43:52 +01004741 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004742
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004743 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004744 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004745 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004746
Chris Mason89642222008-07-24 09:41:53 -04004747out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004748 if (ret) {
4749 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004750 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004751 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004752 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004753 block_start, blocksize, true);
4754 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004755 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004756 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004757 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004758out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004759 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004760 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004761 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004762 return ret;
4763}
4764
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004765static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004766 u64 offset, u64 len)
4767{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004768 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004769 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004770 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004771 int ret;
4772
4773 /*
Filipe Mananacceaa892021-07-20 16:03:40 +01004774 * If NO_HOLES is enabled, we don't need to do anything.
4775 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
4776 * or btrfs_update_inode() will be called, which guarantee that the next
4777 * fsync will know this inode was changed and needs to be logged.
Josef Bacik16e75492013-10-22 12:18:51 -04004778 */
Filipe Mananacceaa892021-07-20 16:03:40 +01004779 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04004780 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04004781
4782 /*
4783 * 1 - for the one we're dropping
4784 * 1 - for the one we're adding
4785 * 1 - for updating the inode.
4786 */
4787 trans = btrfs_start_transaction(root, 3);
4788 if (IS_ERR(trans))
4789 return PTR_ERR(trans);
4790
Filipe Manana5893dfb2020-11-04 11:07:32 +00004791 drop_args.start = offset;
4792 drop_args.end = offset + len;
4793 drop_args.drop_cache = true;
4794
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004795 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004796 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004797 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004798 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004799 return ret;
4800 }
4801
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004802 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004803 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00004804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004805 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00004806 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004807 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4808 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00004809 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004810 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004811 return ret;
4812}
4813
Josef Bacik695a0d02011-03-04 15:46:53 -05004814/*
4815 * This function puts in dummy file extents for the area we're creating a hole
4816 * for. So if we are truncating this file to a larger size we need to insert
4817 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4818 * the range between oldsize and size
4819 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004820int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004821{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004822 struct btrfs_root *root = inode->root;
4823 struct btrfs_fs_info *fs_info = root->fs_info;
4824 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004825 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004826 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004827 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004828 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4829 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004830 u64 last_byte;
4831 u64 cur_offset;
4832 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004833 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004834
Josef Bacika71754f2013-06-17 17:14:39 -04004835 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 * If our size started in the middle of a block we need to zero out the
4837 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004838 * expose stale data.
4839 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004840 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004841 if (err)
4842 return err;
4843
Yan Zheng9036c102008-10-30 14:19:41 -04004844 if (size <= hole_start)
4845 return 0;
4846
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004847 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4848 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004849 cur_offset = hole_start;
4850 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004851 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004852 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004853 if (IS_ERR(em)) {
4854 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004855 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004856 break;
4857 }
Yan Zheng9036c102008-10-30 14:19:41 -04004858 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004859 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004860 hole_size = last_byte - cur_offset;
4861
Yan, Zheng80825102009-11-12 09:35:36 +00004862 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004863 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004864
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004865 err = maybe_insert_hole(root, inode, cur_offset,
4866 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04004867 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004868 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004869
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004870 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004871 cur_offset, hole_size);
4872 if (err)
4873 break;
4874
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004875 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004876 cur_offset + hole_size - 1, 0);
4877 hole_em = alloc_extent_map();
4878 if (!hole_em) {
4879 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004880 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004881 goto next;
4882 }
4883 hole_em->start = cur_offset;
4884 hole_em->len = hole_size;
4885 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004886
Josef Bacik5dc562c2012-08-17 13:14:17 -04004887 hole_em->block_start = EXTENT_MAP_HOLE;
4888 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004889 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004890 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004891 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004893
4894 while (1) {
4895 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004896 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004897 write_unlock(&em_tree->lock);
4898 if (err != -EEXIST)
4899 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004900 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004901 cur_offset +
4902 hole_size - 1, 0);
4903 }
4904 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004905 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004906 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004907 cur_offset, hole_size);
4908 if (err)
4909 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004910 }
Josef Bacik16e75492013-10-22 12:18:51 -04004911next:
Yan Zheng9036c102008-10-30 14:19:41 -04004912 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004913 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004914 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004915 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004916 break;
4917 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004918 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004919 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004920 return err;
4921}
4922
Eric Sandeen3972f262013-01-12 02:57:22 +00004923static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004924{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004925 struct btrfs_root *root = BTRFS_I(inode)->root;
4926 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004927 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004928 loff_t newsize = attr->ia_size;
4929 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004930 int ret;
4931
Eric Sandeen3972f262013-01-12 02:57:22 +00004932 /*
4933 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4934 * special case where we need to update the times despite not having
4935 * these flags set. For all other operations the VFS set these flags
4936 * explicitly if it wants a timestamp update.
4937 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004938 if (newsize != oldsize) {
4939 inode_inc_iversion(inode);
4940 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4941 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004942 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004943 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004944
Josef Bacika41ad392011-01-31 15:30:16 -05004945 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004947 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004948 * This is to ensure the snapshot captures a fully consistent
4949 * state of this file - if the snapshot captures this expanding
4950 * truncation, it must capture all writes that happened before
4951 * this truncation.
4952 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004953 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004954 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004955 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004956 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004957 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004958 }
Yan, Zheng80825102009-11-12 09:35:36 +00004959
Miao Xief4a2f4c2011-12-14 20:12:01 -05004960 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004961 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004962 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004963 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004964 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004965
4966 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02004967 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304968 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004969 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004970 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004971 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004972 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09004973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4974
4975 if (btrfs_is_zoned(fs_info)) {
4976 ret = btrfs_wait_ordered_range(inode,
4977 ALIGN(newsize, fs_info->sectorsize),
4978 (u64)-1);
4979 if (ret)
4980 return ret;
4981 }
Yan, Zheng80825102009-11-12 09:35:36 +00004982
Josef Bacika41ad392011-01-31 15:30:16 -05004983 /*
4984 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004985 * zero. Make sure any new writes to the file get on disk
4986 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05004987 */
4988 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004989 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04004990 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004991
Josef Bacika41ad392011-01-31 15:30:16 -05004992 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004993
Miao Xie2e60a512013-02-08 07:01:08 +00004994 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00004995
Filipe Manana213e8c52018-02-06 20:40:31 +00004996 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004997 if (ret && inode->i_nlink) {
4998 int err;
4999
5000 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005001 * Truncate failed, so fix up the in-memory size. We
5002 * adjusted disk_i_size down as we removed extents, so
5003 * wait for disk_i_size to be stable and then update the
5004 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005005 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005006 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005007 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005008 return err;
5009 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005010 }
Yan, Zheng80825102009-11-12 09:35:36 +00005011 }
5012
Josef Bacika41ad392011-01-31 15:30:16 -05005013 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005014}
5015
Christian Brauner549c7292021-01-21 14:19:43 +01005016static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5017 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005018{
David Howells2b0143b2015-03-17 22:25:59 +00005019 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005020 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005021 int err;
5022
Li Zefanb83cc962010-12-20 16:04:08 +08005023 if (btrfs_root_readonly(root))
5024 return -EROFS;
5025
Christian Braunerd4d09462021-07-27 12:48:49 +02005026 err = setattr_prepare(mnt_userns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 if (err)
5028 return err;
5029
Chris Mason5a3f23d2009-03-31 13:27:11 -04005030 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005031 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005032 if (err)
5033 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005034 }
Yan Zheng9036c102008-10-30 14:19:41 -04005035
Christoph Hellwig10257742010-06-04 11:30:02 +02005036 if (attr->ia_valid) {
Christian Braunerd4d09462021-07-27 12:48:49 +02005037 setattr_copy(mnt_userns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005038 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005039 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005040
Josef Bacik22c44fe2011-11-30 10:45:38 -05005041 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunerd4d09462021-07-27 12:48:49 +02005042 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005043 }
5044
Chris Mason39279cc2007-06-12 06:35:45 -04005045 return err;
5046}
Chris Mason61295eb2008-01-14 16:24:38 -05005047
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005048/*
5049 * While truncating the inode pages during eviction, we get the VFS calling
5050 * btrfs_invalidatepage() against each page of the inode. This is slow because
5051 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5052 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5053 * extent_state structures over and over, wasting lots of time.
5054 *
5055 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5056 * those expensive operations on a per page basis and do only the ordered io
5057 * finishing, while we release here the extent_map and extent_state structures,
5058 * without the excessive merging and splitting.
5059 */
5060static void evict_inode_truncate_pages(struct inode *inode)
5061{
5062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5063 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5064 struct rb_node *node;
5065
5066 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005067 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005068
5069 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005070 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005071 struct extent_map *em;
5072
Liu Bo07e1ce02018-08-23 03:51:52 +08005073 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005074 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005075 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5076 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005077 remove_extent_mapping(map_tree, em);
5078 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005079 if (need_resched()) {
5080 write_unlock(&map_tree->lock);
5081 cond_resched();
5082 write_lock(&map_tree->lock);
5083 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005084 }
5085 write_unlock(&map_tree->lock);
5086
Filipe Manana6ca07092015-05-26 00:55:42 +01005087 /*
5088 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005089 * We can have ongoing bios started by readahead that have
5090 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005091 * still in progress (unlocked the pages in the bio but did not yet
5092 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005093 * ranges can still be locked and eviction started because before
5094 * submitting those bios, which are executed by a separate task (work
5095 * queue kthread), inode references (inode->i_count) were not taken
5096 * (which would be dropped in the end io callback of each bio).
5097 * Therefore here we effectively end up waiting for those bios and
5098 * anyone else holding locked ranges without having bumped the inode's
5099 * reference count - if we don't do it, when they access the inode's
5100 * io_tree to unlock a range it may be too late, leading to an
5101 * use-after-free issue.
5102 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005103 spin_lock(&io_tree->lock);
5104 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5105 struct extent_state *state;
5106 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005107 u64 start;
5108 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005109 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005110
5111 node = rb_first(&io_tree->state);
5112 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005113 start = state->start;
5114 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005115 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005116 spin_unlock(&io_tree->lock);
5117
David Sterbaff13db42015-12-03 14:30:40 +01005118 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005119
5120 /*
5121 * If still has DELALLOC flag, the extent didn't reach disk,
5122 * and its reserved space won't be freed by delayed_ref.
5123 * So we need to free its reserved space here.
5124 * (Refer to comment in btrfs_invalidatepage, case 2)
5125 *
5126 * Note, end is the bytenr of last byte, so we need + 1 here.
5127 */
Filipe Manana421f0922018-10-12 13:02:48 +01005128 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005129 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5130 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005131
Filipe Manana6ca07092015-05-26 00:55:42 +01005132 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005133 EXTENT_LOCKED | EXTENT_DELALLOC |
5134 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5135 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005136
Filipe Manana7064dd52014-08-08 02:47:05 +01005137 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005138 spin_lock(&io_tree->lock);
5139 }
5140 spin_unlock(&io_tree->lock);
5141}
5142
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005143static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005144 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005145{
5146 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikd3984c92019-08-01 18:19:37 -04005147 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005148 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005149 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005150
Josef Bacikd3984c92019-08-01 18:19:37 -04005151 /*
5152 * Eviction should be taking place at some place safe because of our
5153 * delayed iputs. However the normal flushing code will run delayed
5154 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5155 *
5156 * We reserve the delayed_refs_extra here again because we can't use
5157 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5158 * above. We reserve our extra bit here because we generate a ton of
5159 * delayed refs activity by truncating.
5160 *
Josef Bacikee6adbf2021-11-09 10:12:04 -05005161 * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can,
5162 * if we fail to make this reservation we can re-try without the
5163 * delayed_refs_extra so we can make some forward progress.
Josef Bacikd3984c92019-08-01 18:19:37 -04005164 */
Josef Bacik92705012021-11-09 10:12:07 -05005165 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra,
Josef Bacikd3984c92019-08-01 18:19:37 -04005166 BTRFS_RESERVE_FLUSH_EVICT);
5167 if (ret) {
Josef Bacik92705012021-11-09 10:12:07 -05005168 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size,
Josef Bacikee6adbf2021-11-09 10:12:04 -05005169 BTRFS_RESERVE_FLUSH_EVICT);
5170 if (ret) {
Josef Bacikd3984c92019-08-01 18:19:37 -04005171 btrfs_warn(fs_info,
5172 "could not allocate space for delete; will truncate on mount");
5173 return ERR_PTR(-ENOSPC);
5174 }
5175 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005176 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005177
5178 trans = btrfs_join_transaction(root);
5179 if (IS_ERR(trans))
5180 return trans;
5181
5182 if (delayed_refs_extra) {
5183 trans->block_rsv = &fs_info->trans_block_rsv;
5184 trans->bytes_reserved = delayed_refs_extra;
5185 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5186 delayed_refs_extra, 1);
5187 }
5188 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005189}
5190
Al Virobd555972010-06-07 11:35:40 -04005191void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005194 struct btrfs_trans_handle *trans;
5195 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005196 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005197 int ret;
5198
liubo1abe9b82011-03-24 11:18:59 +00005199 trace_btrfs_inode_evict(inode);
5200
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005201 if (!root) {
Boris Burkov14605402021-06-30 13:01:49 -07005202 fsverity_cleanup_inode(inode);
Liu Boe8f1bc12018-01-25 11:02:53 -07005203 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005204 return;
5205 }
5206
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005207 evict_inode_truncate_pages(inode);
5208
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005209 if (inode->i_nlink &&
5210 ((btrfs_root_refs(&root->root_item) != 0 &&
5211 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005212 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005213 goto no_delete;
5214
Omar Sandoval27919062018-05-11 13:13:37 -07005215 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005216 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005217
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005218 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005219
Omar Sandoval7b40b692018-05-11 13:13:33 -07005220 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005221 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005222
Yan, Zheng76dda932009-09-21 16:00:26 -04005223 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005224 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5225 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005226 goto no_delete;
5227 }
5228
Josef Bacik2adc75d2021-12-03 17:18:07 -05005229 /*
5230 * This makes sure the inode item in tree is uptodate and the space for
5231 * the inode update is released.
5232 */
Nikolay Borisovaa790212017-01-10 20:35:40 +02005233 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005234 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005235 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005236
Josef Bacik2adc75d2021-12-03 17:18:07 -05005237 /*
5238 * This drops any pending insert or delete operations we have for this
5239 * inode. We could have a delayed dir index deletion queued up, but
5240 * we're removing the inode completely so that'll be taken care of in
5241 * the truncate.
5242 */
5243 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
5244
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005245 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005246 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005247 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005248 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005249 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005250
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005251 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005252
Yan, Zheng80825102009-11-12 09:35:36 +00005253 while (1) {
Josef Bacikd9ac19c2021-12-03 17:18:09 -05005254 struct btrfs_truncate_control control = {
Josef Bacik71d18b52021-12-03 17:18:15 -05005255 .inode = BTRFS_I(inode),
Josef Bacik487e81d2021-12-03 17:18:14 -05005256 .ino = btrfs_ino(BTRFS_I(inode)),
Josef Bacikd9ac19c2021-12-03 17:18:09 -05005257 .new_size = 0,
5258 .min_type = 0,
5259 };
5260
Josef Bacikad80cf52018-09-28 07:18:19 -04005261 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005262 if (IS_ERR(trans))
5263 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005264
5265 trans->block_rsv = rsv;
5266
Josef Bacik71d18b52021-12-03 17:18:15 -05005267 ret = btrfs_truncate_inode_items(trans, root, &control);
Omar Sandoval27919062018-05-11 13:13:37 -07005268 trans->block_rsv = &fs_info->trans_block_rsv;
5269 btrfs_end_transaction(trans);
5270 btrfs_btree_balance_dirty(fs_info);
5271 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5272 goto free_rsv;
5273 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005274 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005275 }
5276
Josef Bacik4ef31a42013-08-13 14:10:08 -04005277 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005278 * Errors here aren't a big deal, it just means we leave orphan items in
5279 * the tree. They will be cleaned up on the next mount. If the inode
5280 * number gets reused, cleanup deletes the orphan item without doing
5281 * anything, and unlink reuses the existing orphan item.
5282 *
5283 * If it turns out that we are dropping too many of these, we might want
5284 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005285 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005286 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005287 if (!IS_ERR(trans)) {
5288 trans->block_rsv = rsv;
5289 btrfs_orphan_del(trans, BTRFS_I(inode));
5290 trans->block_rsv = &fs_info->trans_block_rsv;
5291 btrfs_end_transaction(trans);
5292 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005293
Omar Sandoval27919062018-05-11 13:13:37 -07005294free_rsv:
5295 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005296no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005297 /*
5298 * If we didn't successfully delete, the orphan item will still be in
5299 * the tree and we'll retry on the next mount. Again, we might also want
5300 * to retry these periodically in the future.
5301 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005302 btrfs_remove_delayed_node(BTRFS_I(inode));
Boris Burkov14605402021-06-30 13:01:49 -07005303 fsverity_cleanup_inode(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005304 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005305}
5306
5307/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005308 * Return the key found in the dir entry in the location pointer, fill @type
5309 * with BTRFS_FT_*, and return 0.
5310 *
Su Yue005d6712018-03-05 17:13:37 +08005311 * If no dir entries were found, returns -ENOENT.
5312 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005313 */
5314static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005315 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005316{
5317 const char *name = dentry->d_name.name;
5318 int namelen = dentry->d_name.len;
5319 struct btrfs_dir_item *di;
5320 struct btrfs_path *path;
5321 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005322 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005323
5324 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005325 if (!path)
5326 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005327
David Sterbaf85b7372017-01-20 14:54:07 +01005328 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5329 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005330 if (IS_ERR_OR_NULL(di)) {
5331 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005332 goto out;
5333 }
Chris Masond3977122009-01-05 21:25:51 -05005334
Chris Mason5f39d392007-10-15 16:14:19 -04005335 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005336 if (location->type != BTRFS_INODE_ITEM_KEY &&
5337 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005338 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005339 btrfs_warn(root->fs_info,
5340"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5341 __func__, name, btrfs_ino(BTRFS_I(dir)),
5342 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005343 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005344 if (!ret)
5345 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005346out:
Chris Mason39279cc2007-06-12 06:35:45 -04005347 btrfs_free_path(path);
5348 return ret;
5349}
5350
5351/*
5352 * when we hit a tree root in a directory, the btrfs part of the inode
5353 * needs to be changed to reflect the root directory of the tree root. This
5354 * is kind of like crossing a mount point.
5355 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005357 struct inode *dir,
5358 struct dentry *dentry,
5359 struct btrfs_key *location,
5360 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005361{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005362 struct btrfs_path *path;
5363 struct btrfs_root *new_root;
5364 struct btrfs_root_ref *ref;
5365 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005366 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 int ret;
5368 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005369
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005370 path = btrfs_alloc_path();
5371 if (!path) {
5372 err = -ENOMEM;
5373 goto out;
5374 }
Chris Mason39279cc2007-06-12 06:35:45 -04005375
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005377 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5378 key.type = BTRFS_ROOT_REF_KEY;
5379 key.offset = location->objectid;
5380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005382 if (ret) {
5383 if (ret < 0)
5384 err = ret;
5385 goto out;
5386 }
Chris Mason39279cc2007-06-12 06:35:45 -04005387
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005388 leaf = path->nodes[0];
5389 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005390 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5392 goto out;
5393
5394 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5395 (unsigned long)(ref + 1),
5396 dentry->d_name.len);
5397 if (ret)
5398 goto out;
5399
David Sterbab3b4aa72011-04-21 01:20:15 +02005400 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401
David Sterba56e93572020-05-15 19:35:55 +02005402 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403 if (IS_ERR(new_root)) {
5404 err = PTR_ERR(new_root);
5405 goto out;
5406 }
5407
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005408 *sub_root = new_root;
5409 location->objectid = btrfs_root_dirid(&new_root->root_item);
5410 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005411 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005412 err = 0;
5413out:
5414 btrfs_free_path(path);
5415 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005416}
5417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418static void inode_tree_add(struct inode *inode)
5419{
5420 struct btrfs_root *root = BTRFS_I(inode)->root;
5421 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005422 struct rb_node **p;
5423 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005424 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005425 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005426
Al Viro1d3382cb2010-10-23 15:19:20 -04005427 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005428 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005429 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005430 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005431 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005432 while (*p) {
5433 parent = *p;
5434 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5435
David Sterba37508512018-06-29 10:56:40 +02005436 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005437 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005438 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005439 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440 else {
5441 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005442 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005443 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005444 RB_CLEAR_NODE(parent);
5445 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005446 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 }
5448 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005449 rb_link_node(new, parent, p);
5450 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 spin_unlock(&root->inode_lock);
5452}
5453
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005454static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005456 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005457 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005459 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005460 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5461 rb_erase(&inode->rb_node, &root->inode_tree);
5462 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005463 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005464 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005465 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005466
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005467 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005468 spin_lock(&root->inode_lock);
5469 empty = RB_EMPTY_ROOT(&root->inode_tree);
5470 spin_unlock(&root->inode_lock);
5471 if (empty)
5472 btrfs_add_dead_root(root);
5473 }
5474}
5475
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476
Chris Masone02119d2008-09-05 16:13:11 -04005477static int btrfs_init_locked_inode(struct inode *inode, void *p)
5478{
5479 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005480
5481 inode->i_ino = args->ino;
5482 BTRFS_I(inode)->location.objectid = args->ino;
5483 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5484 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005485 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5486 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005487 return 0;
5488}
5489
5490static int btrfs_find_actor(struct inode *inode, void *opaque)
5491{
5492 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005493
5494 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005495 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005496}
5497
David Sterba0202e832020-05-15 19:35:59 +02005498static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005499 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005500{
5501 struct inode *inode;
5502 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005503 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005504
David Sterba0202e832020-05-15 19:35:59 +02005505 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005506 args.root = root;
5507
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005508 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005509 btrfs_init_locked_inode,
5510 (void *)&args);
5511 return inode;
5512}
5513
David Sterba4c66e0d2019-10-03 19:09:35 +02005514/*
David Sterba0202e832020-05-15 19:35:59 +02005515 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005516 * Path can be preallocated to prevent recursing back to iget through
5517 * allocator. NULL is also valid but may require an additional allocation
5518 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305519 */
David Sterba0202e832020-05-15 19:35:59 +02005520struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005521 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305522{
5523 struct inode *inode;
5524
David Sterba0202e832020-05-15 19:35:59 +02005525 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305526 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305528
5529 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005530 int ret;
5531
Filipe Manana4222ea72018-10-24 10:13:03 +01005532 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005533 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005534 inode_tree_add(inode);
5535 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005536 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005537 iget_failed(inode);
5538 /*
5539 * ret > 0 can come from btrfs_search_slot called by
5540 * btrfs_read_locked_inode, this means the inode item
5541 * was not found.
5542 */
5543 if (ret > 0)
5544 ret = -ENOENT;
5545 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005546 }
5547 }
5548
Balaji Rao1a54ef82008-07-21 02:01:04 +05305549 return inode;
5550}
5551
David Sterba0202e832020-05-15 19:35:59 +02005552struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005553{
David Sterba0202e832020-05-15 19:35:59 +02005554 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005555}
5556
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557static struct inode *new_simple_dir(struct super_block *s,
5558 struct btrfs_key *key,
5559 struct btrfs_root *root)
5560{
5561 struct inode *inode = new_inode(s);
5562
5563 if (!inode)
5564 return ERR_PTR(-ENOMEM);
5565
Josef Bacik5c8fd992020-02-14 16:11:43 -05005566 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005567 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005568 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569
5570 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005571 /*
5572 * We only need lookup, the rest is read-only and there's no inode
5573 * associated with the dentry
5574 */
5575 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005576 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005577 inode->i_fop = &simple_dir_operations;
5578 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005579 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305580 inode->i_atime = inode->i_mtime;
5581 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005582 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583
5584 return inode;
5585}
5586
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005587static inline u8 btrfs_inode_type(struct inode *inode)
5588{
5589 /*
5590 * Compile-time asserts that generic FT_* types still match
5591 * BTRFS_FT_* types
5592 */
5593 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5594 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5595 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5596 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5597 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5598 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5599 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5600 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5601
5602 return fs_umode_to_ftype(inode->i_mode);
5603}
5604
Chris Mason3de45862008-11-17 21:02:50 -05005605struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005606{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005607 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005608 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005609 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 struct btrfs_root *sub_root = root;
5611 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005612 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005613 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005614
5615 if (dentry->d_name.len > BTRFS_NAME_LEN)
5616 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005618 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005619 if (ret < 0)
5620 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005621
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005623 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005624 if (IS_ERR(inode))
5625 return inode;
5626
5627 /* Do extra check against inode mode with di_type */
5628 if (btrfs_inode_type(inode) != di_type) {
5629 btrfs_crit(fs_info,
5630"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5631 inode->i_mode, btrfs_inode_type(inode),
5632 di_type);
5633 iput(inode);
5634 return ERR_PTR(-EUCLEAN);
5635 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005637 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005639 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005640 &location, &sub_root);
5641 if (ret < 0) {
5642 if (ret != -ENOENT)
5643 inode = ERR_PTR(ret);
5644 else
5645 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5646 } else {
David Sterba0202e832020-05-15 19:35:59 +02005647 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005648 }
Josef Bacik87270022020-01-24 09:32:31 -05005649 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005650 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005651
Julia Lawall34d19ba2011-01-24 19:55:19 +00005652 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005653 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005654 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005655 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005656 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005657 if (ret) {
5658 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005659 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005660 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005661 }
5662
Chris Mason3de45862008-11-17 21:02:50 -05005663 return inode;
5664}
5665
Nick Pigginfe15ce42011-01-07 17:49:23 +11005666static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005667{
5668 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005669 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005670
Li Zefan848cce02012-02-21 17:04:28 +08005671 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005672 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005673
Li Zefan848cce02012-02-21 17:04:28 +08005674 if (inode) {
5675 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005676 if (btrfs_root_refs(&root->root_item) == 0)
5677 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005678
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005679 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005680 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005681 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005682 return 0;
5683}
5684
Chris Mason3de45862008-11-17 21:02:50 -05005685static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005686 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005687{
Al Viro3837d202018-10-10 16:38:27 -04005688 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005689
Al Viro3837d202018-10-10 16:38:27 -04005690 if (inode == ERR_PTR(-ENOENT))
5691 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005692 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005693}
5694
Josef Bacik23b5ec72017-07-24 15:14:25 -04005695/*
5696 * All this infrastructure exists because dir_emit can fault, and we are holding
5697 * the tree lock when doing readdir. For now just allocate a buffer and copy
5698 * our information into that, and then dir_emit from the buffer. This is
5699 * similar to what NFS does, only we don't keep the buffer around in pagecache
5700 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5701 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5702 * tree lock.
5703 */
5704static int btrfs_opendir(struct inode *inode, struct file *file)
5705{
5706 struct btrfs_file_private *private;
5707
5708 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5709 if (!private)
5710 return -ENOMEM;
5711 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5712 if (!private->filldir_buf) {
5713 kfree(private);
5714 return -ENOMEM;
5715 }
5716 file->private_data = private;
5717 return 0;
5718}
5719
5720struct dir_entry {
5721 u64 ino;
5722 u64 offset;
5723 unsigned type;
5724 int name_len;
5725};
5726
5727static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5728{
5729 while (entries--) {
5730 struct dir_entry *entry = addr;
5731 char *name = (char *)(entry + 1);
5732
David Sterba92d32172018-04-16 21:10:14 +02005733 ctx->pos = get_unaligned(&entry->offset);
5734 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5735 get_unaligned(&entry->ino),
5736 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005737 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005738 addr += sizeof(struct dir_entry) +
5739 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005740 ctx->pos++;
5741 }
5742 return 0;
5743}
5744
Al Viro9cdda8d2013-05-22 16:48:09 -04005745static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005746{
Al Viro9cdda8d2013-05-22 16:48:09 -04005747 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005748 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005749 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005750 struct btrfs_dir_item *di;
5751 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005752 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005753 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005754 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005755 struct list_head ins_list;
5756 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005757 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005758 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005760 char *name_ptr;
5761 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005762 int entries = 0;
5763 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005764 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005765 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005766
Al Viro9cdda8d2013-05-22 16:48:09 -04005767 if (!dir_emit_dots(file, ctx))
5768 return 0;
5769
David Woodhouse49593bf2008-08-17 17:08:36 +01005770 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005771 if (!path)
5772 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005773
Josef Bacik23b5ec72017-07-24 15:14:25 -04005774 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005775 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005776
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005777 INIT_LIST_HEAD(&ins_list);
5778 INIT_LIST_HEAD(&del_list);
5779 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005780
Josef Bacik23b5ec72017-07-24 15:14:25 -04005781again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005782 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005783 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005784 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005785
Chris Mason39279cc2007-06-12 06:35:45 -04005786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5787 if (ret < 0)
5788 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005789
5790 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005791 struct dir_entry *entry;
5792
Chris Mason5f39d392007-10-15 16:14:19 -04005793 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005794 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005795 if (slot >= btrfs_header_nritems(leaf)) {
5796 ret = btrfs_next_leaf(root, path);
5797 if (ret < 0)
5798 goto err;
5799 else if (ret > 0)
5800 break;
5801 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005802 }
Chris Mason3de45862008-11-17 21:02:50 -05005803
Chris Mason5f39d392007-10-15 16:14:19 -04005804 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5805
5806 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005807 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005808 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005809 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005810 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005811 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005812 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005813 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005815 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005816 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5817 PAGE_SIZE) {
5818 btrfs_release_path(path);
5819 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5820 if (ret)
5821 goto nopos;
5822 addr = private->filldir_buf;
5823 entries = 0;
5824 total_len = 0;
5825 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005826 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005827
5828 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005829 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005830 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005831 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5832 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005833 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005834 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005835 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005836 put_unaligned(location.objectid, &entry->ino);
5837 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005838 entries++;
5839 addr += sizeof(struct dir_entry) + name_len;
5840 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005841next:
5842 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005843 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005844 btrfs_release_path(path);
5845
5846 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5847 if (ret)
5848 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005849
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005850 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005851 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005852 goto nopos;
5853
Zach Browndb62efb2013-07-11 16:19:42 -07005854 /*
5855 * Stop new entries from being returned after we return the last
5856 * entry.
5857 *
5858 * New directory entries are assigned a strictly increasing
5859 * offset. This means that new entries created during readdir
5860 * are *guaranteed* to be seen in the future by that readdir.
5861 * This has broken buggy programs which operate on names as
5862 * they're returned by readdir. Until we re-use freed offsets
5863 * we have this hack to stop new entries from being returned
5864 * under the assumption that they'll never reach this huge
5865 * offset.
5866 *
5867 * This is being careful not to overflow 32bit loff_t unless the
5868 * last entry requires it because doing so has broken 32bit apps
5869 * in the past.
5870 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005871 if (ctx->pos >= INT_MAX)
5872 ctx->pos = LLONG_MAX;
5873 else
5874 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005875nopos:
5876 ret = 0;
5877err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005878 if (put)
5879 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005880 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005881 return ret;
5882}
5883
Chris Mason39279cc2007-06-12 06:35:45 -04005884/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005885 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005886 * inode changes. But, it is most likely to find the inode in cache.
5887 * FIXME, needs more benchmarking...there are no reasons other than performance
5888 * to keep or drop this code.
5889 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005890static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005891{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 struct btrfs_root *root = BTRFS_I(inode)->root;
5894 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005895 int ret;
5896
Josef Bacik72ac3c02012-05-23 14:13:11 -04005897 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005898 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005899
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005900 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005901 if (IS_ERR(trans))
5902 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005903
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005904 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02005905 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04005906 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005907 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005908 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005909 if (IS_ERR(trans))
5910 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005911
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005912 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005913 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005914 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005915 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005916 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005917
5918 return ret;
5919}
5920
5921/*
5922 * This is a copy of file_update_time. We need this so we can return error on
5923 * ENOSPC for updating the inode in the case of file write and mmap writes.
5924 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005925static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005926 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005927{
Alexander Block2bc5565282012-06-15 09:49:33 +02005928 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005929 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005930
5931 if (btrfs_root_readonly(root))
5932 return -EROFS;
5933
Josef Bacike41f9412012-03-26 09:46:47 -04005934 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005935 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005936 if (flags & S_CTIME)
5937 inode->i_ctime = *now;
5938 if (flags & S_MTIME)
5939 inode->i_mtime = *now;
5940 if (flags & S_ATIME)
5941 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005942 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005943}
5944
Chris Masond352ac62008-09-29 15:18:18 -04005945/*
5946 * find the highest existing sequence number in a directory
5947 * and then set the in-memory index_cnt variable to reflect
5948 * free sequence numbers
5949 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005950static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005951{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005952 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005953 struct btrfs_key key, found_key;
5954 struct btrfs_path *path;
5955 struct extent_buffer *leaf;
5956 int ret;
5957
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005958 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005959 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005960 key.offset = (u64)-1;
5961
5962 path = btrfs_alloc_path();
5963 if (!path)
5964 return -ENOMEM;
5965
5966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5967 if (ret < 0)
5968 goto out;
5969 /* FIXME: we should be able to handle this */
5970 if (ret == 0)
5971 goto out;
5972 ret = 0;
5973
5974 /*
5975 * MAGIC NUMBER EXPLANATION:
5976 * since we search a directory based on f_pos we have to start at 2
5977 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5978 * else has to start at 2
5979 */
5980 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005981 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005982 goto out;
5983 }
5984
5985 path->slots[0]--;
5986
5987 leaf = path->nodes[0];
5988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5989
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005990 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005991 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005992 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005993 goto out;
5994 }
5995
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005996 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005997out:
5998 btrfs_free_path(path);
5999 return ret;
6000}
6001
Chris Masond352ac62008-09-29 15:18:18 -04006002/*
6003 * helper to find a free sequence number in a given directory. This current
6004 * code is very simple, later versions will do smarter things in the btree
6005 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006006int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006007{
6008 int ret = 0;
6009
Nikolay Borisov877574e2017-02-20 13:50:33 +02006010 if (dir->index_cnt == (u64)-1) {
6011 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006012 if (ret) {
6013 ret = btrfs_set_inode_index_count(dir);
6014 if (ret)
6015 return ret;
6016 }
Josef Bacikaec74772008-07-24 12:12:38 -04006017 }
6018
Nikolay Borisov877574e2017-02-20 13:50:33 +02006019 *index = dir->index_cnt;
6020 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006021
6022 return ret;
6023}
6024
Chris Masonb0d5d102014-09-08 13:08:51 -07006025static int btrfs_insert_inode_locked(struct inode *inode)
6026{
6027 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006028
6029 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006030 args.root = BTRFS_I(inode)->root;
6031
6032 return insert_inode_locked4(inode,
6033 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6034 btrfs_find_actor, &args);
6035}
6036
Anand Jain19aee8d2017-07-18 17:37:05 +08006037/*
6038 * Inherit flags from the parent inode.
6039 *
6040 * Currently only the compression flags and the cow flags are inherited.
6041 */
6042static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6043{
6044 unsigned int flags;
6045
6046 if (!dir)
6047 return;
6048
6049 flags = BTRFS_I(dir)->flags;
6050
6051 if (flags & BTRFS_INODE_NOCOMPRESS) {
6052 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6053 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6054 } else if (flags & BTRFS_INODE_COMPRESS) {
6055 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6056 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6057 }
6058
6059 if (flags & BTRFS_INODE_NODATACOW) {
6060 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6061 if (S_ISREG(inode->i_mode))
6062 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6063 }
6064
David Sterba7b6a2212018-03-26 18:40:21 +02006065 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006066}
6067
Chris Mason39279cc2007-06-12 06:35:45 -04006068static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6069 struct btrfs_root *root,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006070 struct user_namespace *mnt_userns,
Josef Bacikaec74772008-07-24 12:12:38 -04006071 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006072 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006073 u64 ref_objectid, u64 objectid,
6074 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006075{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006076 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006077 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006078 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006079 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006080 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006081 struct btrfs_inode_ref *ref;
6082 struct btrfs_key key[2];
6083 u32 sizes[2];
Filipe Mananab7ef5f32021-09-24 12:28:13 +01006084 struct btrfs_item_batch batch;
Chris Mason9c583092008-01-29 15:15:18 -05006085 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006086 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006087 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006088
Chris Mason5f39d392007-10-15 16:14:19 -04006089 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006090 if (!path)
6091 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006092
Josef Bacik11a19a92019-09-09 10:12:04 -04006093 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006094 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006095 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006096 if (!inode) {
6097 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006098 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006099 }
Chris Mason39279cc2007-06-12 06:35:45 -04006100
Li Zefan581bb052011-04-20 10:06:11 +08006101 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006102 * O_TMPFILE, set link count to 0, so that after this point,
6103 * we fill in an inode item with the correct link count.
6104 */
6105 if (!name)
6106 set_nlink(inode, 0);
6107
6108 /*
Li Zefan581bb052011-04-20 10:06:11 +08006109 * we have to initialize this early, so we can reclaim the inode
6110 * number if we fail afterwards in this function.
6111 */
6112 inode->i_ino = objectid;
6113
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006114 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006115 trace_btrfs_inode_request(dir);
6116
Nikolay Borisov877574e2017-02-20 13:50:33 +02006117 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006118 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006119 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006120 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006121 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006122 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006123 } else if (dir) {
6124 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006125 }
6126 /*
6127 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006128 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006129 * number
6130 */
6131 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006132 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006133 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006134 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006135 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006136
Josef Bacik5dc562c2012-08-17 13:14:17 -04006137 /*
6138 * We could have gotten an inode number from somebody who was fsynced
6139 * and then removed in this same transaction, so let's just set full
6140 * sync since it will be a full sync anyway and this will blow away the
6141 * old info in the log.
6142 */
6143 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6144
Chris Mason9c583092008-01-29 15:15:18 -05006145 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006146 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006147 key[0].offset = 0;
6148
Chris Mason9c583092008-01-29 15:15:18 -05006149 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006150
6151 if (name) {
6152 /*
6153 * Start new inodes with an inode_ref. This is slightly more
6154 * efficient for small numbers of hard links since they will
6155 * be packed into one item. Extended refs will kick in if we
6156 * add more hard links than can fit in the ref item.
6157 */
6158 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006159 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006160 key[1].offset = ref_objectid;
6161
6162 sizes[1] = name_len + sizeof(*ref);
6163 }
Chris Mason9c583092008-01-29 15:15:18 -05006164
Chris Masonb0d5d102014-09-08 13:08:51 -07006165 location = &BTRFS_I(inode)->location;
6166 location->objectid = objectid;
6167 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006168 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006169
6170 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006171 if (ret < 0) {
6172 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006173 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006174 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006175
Filipe Mananab7ef5f32021-09-24 12:28:13 +01006176 batch.keys = &key[0];
6177 batch.data_sizes = &sizes[0];
6178 batch.total_data_size = sizes[0] + (name ? sizes[1] : 0);
6179 batch.nr = name ? 2 : 1;
6180 ret = btrfs_insert_empty_items(trans, root, path, &batch);
Chris Mason9c583092008-01-29 15:15:18 -05006181 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006182 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006183
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006184 inode_init_owner(mnt_userns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006185 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306186
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006187 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306188 inode->i_atime = inode->i_mtime;
6189 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006190 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306191
Chris Mason5f39d392007-10-15 16:14:19 -04006192 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6193 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006194 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006195 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006196 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006197
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006198 if (name) {
6199 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6200 struct btrfs_inode_ref);
6201 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6202 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6203 ptr = (unsigned long)(ref + 1);
6204 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6205 }
Chris Mason9c583092008-01-29 15:15:18 -05006206
Chris Mason5f39d392007-10-15 16:14:19 -04006207 btrfs_mark_buffer_dirty(path->nodes[0]);
6208 btrfs_free_path(path);
6209
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006210 btrfs_inherit_iflags(inode, dir);
6211
Al Viro569254b2011-07-24 17:08:40 -04006212 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006213 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006214 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006215 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006216 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6217 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006218 }
6219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006220 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006221
6222 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006223 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006224
Alexander Block8ea05e32012-07-25 17:35:53 +02006225 btrfs_update_root_times(trans, root);
6226
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006227 ret = btrfs_inode_inherit_props(trans, inode, dir);
6228 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006229 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006230 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006231 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006232
Chris Mason39279cc2007-06-12 06:35:45 -04006233 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006234
6235fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006236 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006237fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006238 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006239 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006240 btrfs_free_path(path);
6241 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006242}
6243
Chris Masond352ac62008-09-29 15:18:18 -04006244/*
6245 * utility function to add 'inode' into 'parent_inode' with
6246 * a give name and a given sequence number.
6247 * if 'add_backref' is true, also insert a backref from the
6248 * inode to the parent directory.
6249 */
Chris Masone02119d2008-09-05 16:13:11 -04006250int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006251 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006252 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006253{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006254 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006255 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006256 struct btrfs_root *root = parent_inode->root;
6257 u64 ino = btrfs_ino(inode);
6258 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006259
Li Zefan33345d012011-04-20 10:31:50 +08006260 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006261 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006262 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006263 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006264 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006265 key.offset = 0;
6266 }
Chris Mason39279cc2007-06-12 06:35:45 -04006267
Li Zefan33345d012011-04-20 10:31:50 +08006268 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006269 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006270 root->root_key.objectid, parent_ino,
6271 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006272 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006273 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6274 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006275 }
6276
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006277 /* Nothing to clean up yet */
6278 if (ret)
6279 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006280
Lu Fengqi684572d2018-08-04 21:10:57 +08006281 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006282 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006283 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284 goto fail_dir_item;
6285 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006290 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006291 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006292 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006293 /*
6294 * If we are replaying a log tree, we do not want to update the mtime
6295 * and ctime of the parent directory with the current time, since the
6296 * log replay procedure is responsible for setting them to their correct
6297 * values (the ones it had when the fsync was done).
6298 */
6299 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6300 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6301
6302 parent_inode->vfs_inode.i_mtime = now;
6303 parent_inode->vfs_inode.i_ctime = now;
6304 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006305 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006306 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006307 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006308 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006309
6310fail_dir_item:
6311 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6312 u64 local_index;
6313 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006314 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006315 root->root_key.objectid, parent_ino,
6316 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006317 if (err)
6318 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006319 } else if (add_backref) {
6320 u64 local_index;
6321 int err;
6322
6323 err = btrfs_del_inode_ref(trans, root, name, name_len,
6324 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006325 if (err)
6326 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006327 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006328
6329 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006330 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006331}
6332
6333static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006334 struct btrfs_inode *dir, struct dentry *dentry,
6335 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006336{
Josef Bacika1b075d2010-11-19 20:36:11 +00006337 int err = btrfs_add_link(trans, dir, inode,
6338 dentry->d_name.name, dentry->d_name.len,
6339 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006340 if (err > 0)
6341 err = -EEXIST;
6342 return err;
6343}
6344
Christian Brauner549c7292021-01-21 14:19:43 +01006345static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6346 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006347{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006348 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006349 struct btrfs_trans_handle *trans;
6350 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006351 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006352 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006353 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006354 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006355
Josef Bacik9ed74f22009-09-11 16:12:44 -04006356 /*
6357 * 2 for inode item and ref
6358 * 2 for dir items
6359 * 1 for xattr if selinux is on
6360 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006361 trans = btrfs_start_transaction(root, 5);
6362 if (IS_ERR(trans))
6363 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006364
Nikolay Borisov543068a2020-12-07 17:32:33 +02006365 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006366 if (err)
6367 goto out_unlock;
6368
Christian Brauner72105272021-07-27 12:48:44 +02006369 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006370 dentry->d_name.name, dentry->d_name.len,
6371 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006372 if (IS_ERR(inode)) {
6373 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006374 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006375 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006376 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006377
Casey Schauflerad19db72011-12-15 10:09:07 -05006378 /*
6379 * If the active LSM wants to access the inode during
6380 * d_instantiate it needs these. Smack checks to see
6381 * if the filesystem supports xattrs by looking at the
6382 * ops vector.
6383 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006384 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 init_special_inode(inode, inode->i_mode, rdev);
6386
6387 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006388 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006389 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006390
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006391 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6392 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006393 if (err)
6394 goto out_unlock;
6395
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006396 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006397 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006398
Josef Bacik618e21d2007-07-11 10:18:17 -04006399out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006400 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006402 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006403 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006404 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006405 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006406 return err;
6407}
6408
Christian Brauner549c7292021-01-21 14:19:43 +01006409static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6410 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006411{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006412 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006413 struct btrfs_trans_handle *trans;
6414 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006415 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006417 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006418 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Josef Bacik9ed74f22009-09-11 16:12:44 -04006420 /*
6421 * 2 for inode item and ref
6422 * 2 for dir items
6423 * 1 for xattr if selinux is on
6424 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006425 trans = btrfs_start_transaction(root, 5);
6426 if (IS_ERR(trans))
6427 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006428
Nikolay Borisov543068a2020-12-07 17:32:33 +02006429 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006430 if (err)
6431 goto out_unlock;
6432
Christian Braunere93ca492021-07-27 12:48:45 +02006433 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006434 dentry->d_name.name, dentry->d_name.len,
6435 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006436 if (IS_ERR(inode)) {
6437 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006438 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006439 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006440 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006441 /*
6442 * If the active LSM wants to access the inode during
6443 * d_instantiate it needs these. Smack checks to see
6444 * if the filesystem supports xattrs by looking at the
6445 * ops vector.
6446 */
6447 inode->i_fop = &btrfs_file_operations;
6448 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006449 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006450
6451 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6452 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006453 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006454
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006455 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006456 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006457 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006458
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006459 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6460 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006462 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463
Al Viro1e2e5472018-05-04 08:23:01 -04006464 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006465
Chris Mason39279cc2007-06-12 06:35:45 -04006466out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006467 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006468 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006469 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006470 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006471 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006472 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 return err;
6474}
6475
6476static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6477 struct dentry *dentry)
6478{
Filipe Manana271dba452016-01-05 16:24:05 +00006479 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006480 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006481 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006483 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 int err;
6485 int drop_inode = 0;
6486
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006487 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006488 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006489 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006490
Mark Fashehf1863732012-08-08 11:32:27 -07006491 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006492 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006493
Nikolay Borisov877574e2017-02-20 13:50:33 +02006494 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006495 if (err)
6496 goto fail;
6497
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006499 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006501 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006502 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006503 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006504 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 if (IS_ERR(trans)) {
6506 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006507 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006508 goto fail;
6509 }
Chris Mason5f39d392007-10-15 16:14:19 -04006510
Miao Xie67de1172013-12-26 13:07:06 +08006511 /* There are several dir indexes for this inode, clear the cache. */
6512 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006513 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006514 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006515 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006516 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006517 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006518
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006519 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6520 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006521
Yan, Zhenga5719522009-09-24 09:17:31 -04006522 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006523 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006524 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006525 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006526
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006527 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006528 if (err)
6529 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006530 if (inode->i_nlink == 1) {
6531 /*
6532 * If new hard link count is 1, it's a file created
6533 * with open(2) O_TMPFILE flag.
6534 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006535 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006536 if (err)
6537 goto fail;
6538 }
Al Viro08c422c2011-12-23 07:58:13 -05006539 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006540 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006541 }
Chris Mason39279cc2007-06-12 06:35:45 -04006542
Chris Mason1832a6d2007-12-21 16:27:21 -05006543fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006544 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006545 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006546 if (drop_inode) {
6547 inode_dec_link_count(inode);
6548 iput(inode);
6549 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006550 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006551 return err;
6552}
6553
Christian Brauner549c7292021-01-21 14:19:43 +01006554static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6555 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006556{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006558 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006559 struct btrfs_trans_handle *trans;
6560 struct btrfs_root *root = BTRFS_I(dir)->root;
6561 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006562 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006563 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006564
Josef Bacik9ed74f22009-09-11 16:12:44 -04006565 /*
6566 * 2 items for inode and ref
6567 * 2 items for dir items
6568 * 1 for xattr if selinux is on
6569 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 trans = btrfs_start_transaction(root, 5);
6571 if (IS_ERR(trans))
6572 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006573
Nikolay Borisov543068a2020-12-07 17:32:33 +02006574 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006575 if (err)
6576 goto out_fail;
6577
Christian Braunerb0b3e442021-07-27 12:48:46 +02006578 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006579 dentry->d_name.name, dentry->d_name.len,
6580 btrfs_ino(BTRFS_I(dir)), objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01006581 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006582 if (IS_ERR(inode)) {
6583 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006584 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006585 goto out_fail;
6586 }
Chris Mason5f39d392007-10-15 16:14:19 -04006587
Chris Masonb0d5d102014-09-08 13:08:51 -07006588 /* these must be set before we unlock the inode */
6589 inode->i_op = &btrfs_dir_inode_operations;
6590 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006591
Eric Paris2a7dba32011-02-01 11:05:39 -05006592 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006593 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006594 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006595
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006596 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006597 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006598 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006599 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006600
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006601 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6602 dentry->d_name.name,
6603 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006604 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006605 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006606
Al Viro1e2e5472018-05-04 08:23:01 -04006607 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006608
6609out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006610 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006611 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006612 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006613 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006614 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 return err;
6617}
6618
Chris Masonc8b97812008-10-29 14:49:59 -04006619static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006620 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006621 size_t pg_offset, u64 extent_offset,
6622 struct btrfs_file_extent_item *item)
6623{
6624 int ret;
6625 struct extent_buffer *leaf = path->nodes[0];
6626 char *tmp;
6627 size_t max_size;
6628 unsigned long inline_size;
6629 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006630 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006631
6632 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006633 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006634 max_size = btrfs_file_extent_ram_bytes(leaf, item);
Josef Bacik437bd072021-10-21 14:58:33 -04006635 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
Chris Masonc8b97812008-10-29 14:49:59 -04006636 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006637 if (!tmp)
6638 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006639 ptr = btrfs_file_extent_inline_start(item);
6640
6641 read_extent_buffer(leaf, tmp, ptr, inline_size);
6642
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006643 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006644 ret = btrfs_decompress(compress_type, tmp, page,
6645 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006646
6647 /*
6648 * decompression code contains a memset to fill in any space between the end
6649 * of the uncompressed data and the end of max_size in case the decompressed
6650 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6651 * the end of an inline extent and the beginning of the next block, so we
6652 * cover that region here.
6653 */
6654
Ira Weinyd048b9c2021-05-04 18:40:07 -07006655 if (max_size + pg_offset < PAGE_SIZE)
6656 memzero_page(page, pg_offset + max_size,
6657 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006658 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006659 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006660}
6661
Omar Sandoval39b07b52019-12-02 17:34:23 -08006662/**
6663 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6664 * @inode: file to search in
6665 * @page: page to read extent data into if the extent is inline
6666 * @pg_offset: offset into @page to copy to
6667 * @start: file offset
6668 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006669 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006670 * This returns the first &struct extent_map which overlaps with the given
6671 * range, reading it from the B-tree and caching it if necessary. Note that
6672 * there may be more extents which overlap the given range after the returned
6673 * extent_map.
6674 *
6675 * If @page is not NULL and the extent is inline, this also reads the extent
6676 * data directly into the page and marks the extent up to date in the io_tree.
6677 *
6678 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006679 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006680struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006681 struct page *page, size_t pg_offset,
6682 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006683{
David Sterba3ffbd682018-06-29 10:56:42 +02006684 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006685 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006686 u64 extent_start = 0;
6687 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006688 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006689 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006690 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006691 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006692 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006693 struct extent_buffer *leaf;
6694 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006695 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006696 struct extent_map_tree *em_tree = &inode->extent_tree;
6697 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006698
Chris Mason890871b2009-09-02 16:24:52 -04006699 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006700 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006701 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006702
Chris Masona52d9a82007-08-27 16:49:44 -04006703 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006704 if (em->start > start || em->start + em->len <= start)
6705 free_extent_map(em);
6706 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006707 free_extent_map(em);
6708 else
6709 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006710 }
David Sterba172ddd62011-04-21 00:48:27 +02006711 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006712 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006713 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006714 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006715 }
Chris Masond1310b22008-01-24 16:13:08 -05006716 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006717 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006718 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006719 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006720
Liu Bobee6ec82018-08-17 05:05:28 +08006721 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006722 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006723 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006724 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006725 }
6726
Liu Bobee6ec82018-08-17 05:05:28 +08006727 /* Chances are we'll be called again, so go ahead and do readahead */
6728 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006729
6730 /*
6731 * The same explanation in load_free_space_cache applies here as well,
6732 * we only read when we're loading the free space cache, and at that
6733 * point the commit_root has everything we need.
6734 */
6735 if (btrfs_is_free_space_inode(inode)) {
6736 path->search_commit_root = 1;
6737 path->skip_locking = 1;
6738 }
Josef Bacik51899412020-08-20 11:46:01 -04006739
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006740 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006741 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006742 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006743 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006744 if (path->slots[0] == 0)
6745 goto not_found;
6746 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006747 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006748 }
6749
Chris Mason5f39d392007-10-15 16:14:19 -04006750 leaf = path->nodes[0];
6751 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006752 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006753 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006754 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006755 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006756 /*
6757 * If we backup past the first extent we want to move forward
6758 * and see if there is an extent in front of us, otherwise we'll
6759 * say there is a hole for our whole search range which can
6760 * cause problems.
6761 */
6762 extent_end = start;
6763 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006764 }
6765
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006766 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006767 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006768 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006769 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6770 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006771 /* Only regular file could have regular/prealloc extent */
6772 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006773 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006774 btrfs_crit(fs_info,
6775 "regular/prealloc extent found for non-regular inode %llu",
6776 btrfs_ino(inode));
6777 goto out;
6778 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006779 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6780 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006781 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006782 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6783 path->slots[0],
6784 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006785 }
Josef Bacik25a50342013-10-14 12:08:38 -04006786next:
Yan Zheng9036c102008-10-30 14:19:41 -04006787 if (start >= extent_end) {
6788 path->slots[0]++;
6789 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6790 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006791 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006792 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006793 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006794 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006795
Yan Zheng9036c102008-10-30 14:19:41 -04006796 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006797 }
Yan Zheng9036c102008-10-30 14:19:41 -04006798 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6799 if (found_key.objectid != objectid ||
6800 found_key.type != BTRFS_EXTENT_DATA_KEY)
6801 goto not_found;
6802 if (start + len <= found_key.offset)
6803 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006804 if (start > found_key.offset)
6805 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006806
6807 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006808 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006809 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006810 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006811 em->block_start = EXTENT_MAP_HOLE;
6812 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006813 }
6814
Omar Sandoval39b07b52019-12-02 17:34:23 -08006815 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006816
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006817 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6818 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006819 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006820 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006821 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006822 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006823 size_t size;
6824 size_t extent_offset;
6825 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006826
Omar Sandoval39b07b52019-12-02 17:34:23 -08006827 if (!page)
Yan689f9342007-10-29 11:41:07 -04006828 goto out;
Yan689f9342007-10-29 11:41:07 -04006829
Qu Wenruoe41ca582018-06-06 15:41:49 +08006830 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006831 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006832 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6833 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006834 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006835 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006836 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006837 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006838 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006839
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006840 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006841 if (btrfs_file_extent_compression(leaf, item) !=
6842 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006843 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006844 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006845 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006846 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006847 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08006848 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006849 read_extent_buffer(leaf, map + pg_offset, ptr,
6850 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006851 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006852 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006853 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006854 copy_size);
6855 }
Ira Weiny58c1a352021-02-16 18:48:23 -08006856 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04006857 }
Chris Mason179e29e2007-11-01 11:28:41 -04006858 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006859 }
Chris Masond1310b22008-01-24 16:13:08 -05006860 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006861 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006862 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006863 }
6864not_found:
6865 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006866 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006867 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006868 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006869insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006870 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006871 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006872 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006873 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006874 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6875 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006876 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006877 goto out;
6878 }
Chris Masond1310b22008-01-24 16:13:08 -05006879
Chris Mason890871b2009-09-02 16:24:52 -04006880 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006881 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006882 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006883out:
Liu Boc6414282018-08-23 07:36:17 +08006884 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006885
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006886 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006887
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006888 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006889 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006890 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006891 }
6892 return em;
6893}
6894
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006895struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006896 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006897{
6898 struct extent_map *em;
6899 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006900 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006901 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006902 u64 delalloc_len;
6903 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006904 int err = 0;
6905
Omar Sandoval39b07b52019-12-02 17:34:23 -08006906 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006907 if (IS_ERR(em))
6908 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006909 /*
6910 * If our em maps to:
6911 * - a hole or
6912 * - a pre-alloc extent,
6913 * there might actually be delalloc bytes behind it.
6914 */
6915 if (em->block_start != EXTENT_MAP_HOLE &&
6916 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6917 return em;
6918 else
6919 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006920
6921 /* check to see if we've wrapped (len == -1 or similar) */
6922 end = start + len;
6923 if (end < start)
6924 end = (u64)-1;
6925 else
6926 end -= 1;
6927
6928 em = NULL;
6929
6930 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006931 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006932 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006933 delalloc_end = delalloc_start + delalloc_len;
6934 if (delalloc_end < delalloc_start)
6935 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006936
6937 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006938 * We didn't find anything useful, return the original results from
6939 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006940 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006941 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006942 em = hole_em;
6943 hole_em = NULL;
6944 goto out;
6945 }
6946
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006947 /*
6948 * Adjust the delalloc_start to make sure it doesn't go backwards from
6949 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006950 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006951 delalloc_start = max(start, delalloc_start);
6952 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006953
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006954 if (delalloc_len > 0) {
6955 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006956 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006957 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006958
David Sterba172ddd62011-04-21 00:48:27 +02006959 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006960 if (!em) {
6961 err = -ENOMEM;
6962 goto out;
6963 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006964
6965 ASSERT(hole_em);
6966 /*
6967 * When btrfs_get_extent can't find anything it returns one
6968 * huge hole
6969 *
6970 * Make sure what it found really fits our range, and adjust to
6971 * make sure it is based on the start from the caller
6972 */
6973 if (hole_end <= start || hole_em->start > end) {
6974 free_extent_map(hole_em);
6975 hole_em = NULL;
6976 } else {
6977 hole_start = max(hole_em->start, start);
6978 hole_len = hole_end - hole_start;
6979 }
6980
6981 if (hole_em && delalloc_start > hole_start) {
6982 /*
6983 * Our hole starts before our delalloc, so we have to
6984 * return just the parts of the hole that go until the
6985 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006986 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006987 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006988 em->start = hole_start;
6989 em->orig_start = hole_start;
6990 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006991 * Don't adjust block start at all, it is fixed at
6992 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006993 */
6994 em->block_start = hole_em->block_start;
6995 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006996 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6997 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006998 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006999 /*
7000 * Hole is out of passed range or it starts after
7001 * delalloc range
7002 */
7003 em->start = delalloc_start;
7004 em->len = delalloc_len;
7005 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007006 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007007 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007008 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007009 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007010 return hole_em;
7011 }
7012out:
7013
7014 free_extent_map(hole_em);
7015 if (err) {
7016 free_extent_map(em);
7017 return ERR_PTR(err);
7018 }
7019 return em;
7020}
7021
Nikolay Borisov64f54182020-06-03 08:55:31 +03007022static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007023 const u64 start,
7024 const u64 len,
7025 const u64 orig_start,
7026 const u64 block_start,
7027 const u64 block_len,
7028 const u64 orig_block_len,
7029 const u64 ram_bytes,
7030 const int type)
7031{
7032 struct extent_map *em = NULL;
7033 int ret;
7034
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007035 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007036 em = create_io_em(inode, start, len, orig_start, block_start,
7037 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007038 BTRFS_COMPRESS_NONE, /* compress_type */
7039 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007040 if (IS_ERR(em))
7041 goto out;
7042 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007043 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7044 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007045 if (ret) {
7046 if (em) {
7047 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007048 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007049 }
7050 em = ERR_PTR(ret);
7051 }
7052 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007053
7054 return em;
7055}
7056
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007057static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007058 u64 start, u64 len)
7059{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007060 struct btrfs_root *root = inode->root;
7061 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007062 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007063 struct btrfs_key ins;
7064 u64 alloc_hint;
7065 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007066
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007067 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007068 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007069 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007070 if (ret)
7071 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007072
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007073 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007074 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007075 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007076 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007077 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007078 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7079 1);
Josef Bacik00361582013-08-14 14:02:47 -04007080
Josef Bacik4b46fce2010-05-23 11:00:55 -04007081 return em;
7082}
7083
Anand Jainf4639632021-02-10 21:25:16 -08007084static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007085{
7086 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007087 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007088
7089 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7090 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007091 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007092 if (block_group)
7093 btrfs_put_block_group(block_group);
7094 return readonly;
7095}
7096
Chris Mason46bfbb52010-05-26 11:04:10 -04007097/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007098 * Check if we can do nocow write into the range [@offset, @offset + @len)
7099 *
7100 * @offset: File offset
7101 * @len: The length to write, will be updated to the nocow writeable
7102 * range
7103 * @orig_start: (optional) Return the original file offset of the file extent
7104 * @orig_len: (optional) Return the original on-disk length of the file extent
7105 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007106 * @strict: if true, omit optimizations that might force us into unnecessary
7107 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007108 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007109 * Return:
7110 * >0 and update @len if we can do nocow write
7111 * 0 if we can't do nocow write
7112 * <0 if error happened
7113 *
7114 * NOTE: This only checks the file extents, caller is responsible to wait for
7115 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007116 */
Josef Bacik00361582013-08-14 14:02:47 -04007117noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007118 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007119 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007120{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007122 struct btrfs_path *path;
7123 int ret;
7124 struct extent_buffer *leaf;
7125 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007126 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007127 struct btrfs_file_extent_item *fi;
7128 struct btrfs_key key;
7129 u64 disk_bytenr;
7130 u64 backref_offset;
7131 u64 extent_end;
7132 u64 num_bytes;
7133 int slot;
7134 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007135 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007136
Chris Mason46bfbb52010-05-26 11:04:10 -04007137 path = btrfs_alloc_path();
7138 if (!path)
7139 return -ENOMEM;
7140
David Sterbaf85b7372017-01-20 14:54:07 +01007141 ret = btrfs_lookup_file_extent(NULL, root, path,
7142 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007143 if (ret < 0)
7144 goto out;
7145
7146 slot = path->slots[0];
7147 if (ret == 1) {
7148 if (slot == 0) {
7149 /* can't find the item, must cow */
7150 ret = 0;
7151 goto out;
7152 }
7153 slot--;
7154 }
7155 ret = 0;
7156 leaf = path->nodes[0];
7157 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007158 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007159 key.type != BTRFS_EXTENT_DATA_KEY) {
7160 /* not our file or wrong item type, must cow */
7161 goto out;
7162 }
7163
7164 if (key.offset > offset) {
7165 /* Wrong offset, must cow */
7166 goto out;
7167 }
7168
7169 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7170 found_type = btrfs_file_extent_type(leaf, fi);
7171 if (found_type != BTRFS_FILE_EXTENT_REG &&
7172 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7173 /* not a regular extent, must cow */
7174 goto out;
7175 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007176
7177 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7178 goto out;
7179
Miao Xiee77751a2013-12-27 21:11:50 +08007180 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7181 if (extent_end <= offset)
7182 goto out;
7183
Chris Mason46bfbb52010-05-26 11:04:10 -04007184 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007185 if (disk_bytenr == 0)
7186 goto out;
7187
7188 if (btrfs_file_extent_compression(leaf, fi) ||
7189 btrfs_file_extent_encryption(leaf, fi) ||
7190 btrfs_file_extent_other_encoding(leaf, fi))
7191 goto out;
7192
Ethan Lien78d42952018-05-17 14:58:29 +08007193 /*
7194 * Do the same check as in btrfs_cross_ref_exist but without the
7195 * unnecessary search.
7196 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007197 if (!strict &&
7198 (btrfs_file_extent_generation(leaf, fi) <=
7199 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007200 goto out;
7201
Chris Mason46bfbb52010-05-26 11:04:10 -04007202 backref_offset = btrfs_file_extent_offset(leaf, fi);
7203
Josef Bacik7ee9e442013-06-21 16:37:03 -04007204 if (orig_start) {
7205 *orig_start = key.offset - backref_offset;
7206 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7207 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7208 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007209
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007210 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007211 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007212
7213 num_bytes = min(offset + *len, extent_end) - offset;
7214 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7215 u64 range_end;
7216
Jeff Mahoneyda170662016-06-15 09:22:56 -04007217 range_end = round_up(offset + num_bytes,
7218 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007219 ret = test_range_bit(io_tree, offset, range_end,
7220 EXTENT_DELALLOC, 0, NULL);
7221 if (ret) {
7222 ret = -EAGAIN;
7223 goto out;
7224 }
7225 }
7226
Josef Bacik1bda19e2013-10-18 12:10:36 -04007227 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007228
7229 /*
7230 * look for other files referencing this extent, if we
7231 * find any we must cow
7232 */
Josef Bacik00361582013-08-14 14:02:47 -04007233
Liu Boe4c3b2d2017-01-30 12:25:28 -08007234 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007235 key.offset - backref_offset, disk_bytenr,
7236 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007237 if (ret) {
7238 ret = 0;
7239 goto out;
7240 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007241
7242 /*
7243 * adjust disk_bytenr and num_bytes to cover just the bytes
7244 * in this extent we are about to write. If there
7245 * are any csums in that range we have to cow in order
7246 * to keep the csums correct
7247 */
7248 disk_bytenr += backref_offset;
7249 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007250 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7251 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007252 /*
7253 * all of the above have passed, it is safe to overwrite this extent
7254 * without cow
7255 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007256 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007257 ret = 1;
7258out:
7259 btrfs_free_path(path);
7260 return ret;
7261}
7262
Josef Bacikeb838e72012-07-31 16:28:48 -04007263static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007264 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007265{
7266 struct btrfs_ordered_extent *ordered;
7267 int ret = 0;
7268
7269 while (1) {
7270 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007271 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007272 /*
7273 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007274 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007275 * extents in this range.
7276 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007277 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007278 lockend - lockstart + 1);
7279
7280 /*
7281 * We need to make sure there are no buffered pages in this
7282 * range either, we could have raced between the invalidate in
7283 * generic_file_direct_write and locking the extent. The
7284 * invalidate needs to happen so that reads after a write do not
7285 * get stale data.
7286 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007287 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007288 (!writing || !filemap_range_has_page(inode->i_mapping,
7289 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007290 break;
7291
7292 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007293 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007294
7295 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007296 /*
7297 * If we are doing a DIO read and the ordered extent we
7298 * found is for a buffered write, we can not wait for it
7299 * to complete and retry, because if we do so we can
7300 * deadlock with concurrent buffered writes on page
7301 * locks. This happens only if our DIO read covers more
7302 * than one extent map, if at this point has already
7303 * created an ordered extent for a previous extent map
7304 * and locked its range in the inode's io tree, and a
7305 * concurrent write against that previous extent map's
7306 * range and this range started (we unlock the ranges
7307 * in the io tree only when the bios complete and
7308 * buffered writes always lock pages before attempting
7309 * to lock range in the io tree).
7310 */
7311 if (writing ||
7312 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007313 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007314 else
7315 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007316 btrfs_put_ordered_extent(ordered);
7317 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007318 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007319 * We could trigger writeback for this range (and wait
7320 * for it to complete) and then invalidate the pages for
7321 * this range (through invalidate_inode_pages2_range()),
7322 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007323 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007324 * triggered a readahead) on a page lock due to an
7325 * ordered dio extent we created before but did not have
7326 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007327 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007328 * ordered extent to complete while holding a lock on
7329 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007330 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007331 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007332 }
7333
Filipe Mananaade77022016-02-18 14:28:55 +00007334 if (ret)
7335 break;
7336
Josef Bacikeb838e72012-07-31 16:28:48 -04007337 cond_resched();
7338 }
7339
7340 return ret;
7341}
7342
Liu Bo6f9994d2017-01-31 07:50:22 -08007343/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007344static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7345 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007346 u64 block_len, u64 orig_block_len,
7347 u64 ram_bytes, int compress_type,
7348 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007349{
7350 struct extent_map_tree *em_tree;
7351 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007352 int ret;
7353
Liu Bo6f9994d2017-01-31 07:50:22 -08007354 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7355 type == BTRFS_ORDERED_COMPRESSED ||
7356 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007357 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007358
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007359 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007360 em = alloc_extent_map();
7361 if (!em)
7362 return ERR_PTR(-ENOMEM);
7363
7364 em->start = start;
7365 em->orig_start = orig_start;
7366 em->len = len;
7367 em->block_len = block_len;
7368 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007369 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007370 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007371 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007372 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007373 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007374 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007375 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007376 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7377 em->compress_type = compress_type;
7378 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007379
7380 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007381 btrfs_drop_extent_cache(inode, em->start,
7382 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007383 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007384 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007385 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007386 /*
7387 * The caller has taken lock_extent(), who could race with us
7388 * to add em?
7389 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007390 } while (ret == -EEXIST);
7391
7392 if (ret) {
7393 free_extent_map(em);
7394 return ERR_PTR(ret);
7395 }
7396
Liu Bo6f9994d2017-01-31 07:50:22 -08007397 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007398 return em;
7399}
7400
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007401
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007402static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007403 struct inode *inode,
7404 struct btrfs_dio_data *dio_data,
7405 u64 start, u64 len)
7406{
7407 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7408 struct extent_map *em = *map;
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007409 int type;
7410 u64 block_start, orig_start, orig_block_len, ram_bytes;
7411 bool can_nocow = false;
7412 bool space_reserved = false;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007413 int ret = 0;
7414
7415 /*
7416 * We don't allocate a new extent in the following cases
7417 *
7418 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7419 * existing extent.
7420 * 2) The extent is marked as PREALLOC. We're good to go here and can
7421 * just use the extent.
7422 *
7423 */
7424 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7425 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7426 em->block_start != EXTENT_MAP_HOLE)) {
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007427 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7428 type = BTRFS_ORDERED_PREALLOC;
7429 else
7430 type = BTRFS_ORDERED_NOCOW;
7431 len = min(len, em->len - (start - em->start));
7432 block_start = em->block_start + (start - em->start);
7433
7434 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007435 &orig_block_len, &ram_bytes, false) == 1 &&
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007436 btrfs_inc_nocow_writers(fs_info, block_start))
7437 can_nocow = true;
7438 }
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007439
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007440 if (can_nocow) {
7441 struct extent_map *em2;
7442
7443 /* We can NOCOW, so only need to reserve metadata space. */
7444 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
7445 if (ret < 0) {
7446 /* Our caller expects us to free the input extent map. */
7447 free_extent_map(em);
7448 *map = NULL;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007449 btrfs_dec_nocow_writers(fs_info, block_start);
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007450 goto out;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007451 }
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007452 space_reserved = true;
7453
7454 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
7455 orig_start, block_start,
7456 len, orig_block_len,
7457 ram_bytes, type);
7458 btrfs_dec_nocow_writers(fs_info, block_start);
7459 if (type == BTRFS_ORDERED_PREALLOC) {
7460 free_extent_map(em);
7461 *map = em = em2;
7462 }
7463
7464 if (IS_ERR(em2)) {
7465 ret = PTR_ERR(em2);
7466 goto out;
7467 }
7468 } else {
7469 const u64 prev_len = len;
7470
7471 /* Our caller expects us to free the input extent map. */
7472 free_extent_map(em);
7473 *map = NULL;
7474
7475 /* We have to COW, so need to reserve metadata and data space. */
7476 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7477 &dio_data->data_reserved,
7478 start, len);
7479 if (ret < 0)
7480 goto out;
7481 space_reserved = true;
7482
7483 em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
7484 if (IS_ERR(em)) {
7485 ret = PTR_ERR(em);
7486 goto out;
7487 }
7488 *map = em;
7489 len = min(len, em->len - (start - em->start));
7490 if (len < prev_len)
7491 btrfs_delalloc_release_space(BTRFS_I(inode),
7492 dio_data->data_reserved,
7493 start + len, prev_len - len,
7494 true);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007495 }
7496
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007497 /*
7498 * We have created our ordered extent, so we can now release our reservation
7499 * for an outstanding extent.
7500 */
7501 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007502
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007503 /*
7504 * Need to update the i_size under the extent lock so buffered
7505 * readers will get the updated i_size when we unlock.
7506 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007507 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007508 i_size_write(inode, start + len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007509out:
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007510 if (ret && space_reserved) {
7511 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
7512 if (can_nocow) {
7513 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
7514 } else {
7515 btrfs_delalloc_release_space(BTRFS_I(inode),
7516 dio_data->data_reserved,
7517 start, len, true);
7518 extent_changeset_free(dio_data->data_reserved);
7519 dio_data->data_reserved = NULL;
7520 }
7521 }
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007522 return ret;
7523}
7524
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007525static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7526 loff_t length, unsigned int flags, struct iomap *iomap,
7527 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007528{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007529 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007530 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007531 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307532 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007534 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007535 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007536 u64 len = length;
7537 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007538
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007539 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007540 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007541
Josef Bacikc3298612012-08-03 16:49:19 -04007542 lockstart = start;
7543 lockend = start + len - 1;
7544
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007545 /*
7546 * The generic stuff only does filemap_write_and_wait_range, which
7547 * isn't enough if we've written compressed pages to this area, so we
7548 * need to flush the dirty pages again to make absolutely sure that any
7549 * outstanding dirty pages are on disk.
7550 */
7551 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7552 &BTRFS_I(inode)->runtime_flags)) {
7553 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7554 start + length - 1);
7555 if (ret)
7556 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007557 }
7558
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007559 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7560 if (!dio_data)
7561 return -ENOMEM;
7562
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007563 iomap->private = dio_data;
7564
7565
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 /*
7567 * If this errors out it's because we couldn't invalidate pagecache for
7568 * this range and we need to fallback to buffered.
7569 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007570 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007571 ret = -ENOTBLK;
7572 goto err;
7573 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007574
Omar Sandoval39b07b52019-12-02 17:34:23 -08007575 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007576 if (IS_ERR(em)) {
7577 ret = PTR_ERR(em);
7578 goto unlock_err;
7579 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580
7581 /*
7582 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7583 * io. INLINE is special, and we could probably kludge it in here, but
7584 * it's still buffered so for safety lets just fall back to the generic
7585 * buffered path.
7586 *
7587 * For COMPRESSED we _have_ to read the entire extent in so we can
7588 * decompress it, so there will be buffering required no matter what we
7589 * do, so go ahead and fallback to buffered.
7590 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007591 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007592 * to buffered IO. Don't blame me, this is the price we pay for using
7593 * the generic code.
7594 */
7595 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7596 em->block_start == EXTENT_MAP_INLINE) {
7597 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007598 ret = -ENOTBLK;
7599 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600 }
7601
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007602 len = min(len, em->len - (start - em->start));
7603 if (write) {
7604 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7605 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007606 if (ret < 0)
7607 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007608 unlock_extents = true;
7609 /* Recalc len in case the new em is smaller than requested */
7610 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007611 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007612 /*
7613 * We need to unlock only the end area that we aren't using.
7614 * The rest is going to be unlocked by the endio routine.
7615 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007616 lockstart = start + len;
7617 if (lockstart < lockend)
7618 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007619 }
7620
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007621 if (unlock_extents)
7622 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7623 lockstart, lockend, &cached_state);
7624 else
7625 free_extent_state(cached_state);
7626
7627 /*
7628 * Translate extent map information to iomap.
7629 * We trim the extents (and move the addr) even though iomap code does
7630 * that, since we have locked only the parts we are performing I/O in.
7631 */
7632 if ((em->block_start == EXTENT_MAP_HOLE) ||
7633 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7634 iomap->addr = IOMAP_NULL_ADDR;
7635 iomap->type = IOMAP_HOLE;
7636 } else {
7637 iomap->addr = em->block_start + (start - em->start);
7638 iomap->type = IOMAP_MAPPED;
7639 }
7640 iomap->offset = start;
Anand Jaind24fa5c2021-08-24 13:05:19 +08007641 iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007642 iomap->length = len;
7643
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007644 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007645 iomap->flags |= IOMAP_F_ZONE_APPEND;
7646
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647 free_extent_map(em);
7648
7649 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007650
7651unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007652 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7653 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007654err:
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007655 kfree(dio_data);
7656
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007657 return ret;
7658}
7659
7660static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7661 ssize_t written, unsigned int flags, struct iomap *iomap)
7662{
7663 int ret = 0;
7664 struct btrfs_dio_data *dio_data = iomap->private;
7665 size_t submitted = dio_data->submitted;
7666 const bool write = !!(flags & IOMAP_WRITE);
7667
7668 if (!write && (iomap->type == IOMAP_HOLE)) {
7669 /* If reading from a hole, unlock and return */
7670 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7671 goto out;
7672 }
7673
7674 if (submitted < length) {
7675 pos += submitted;
7676 length -= submitted;
7677 if (write)
7678 __endio_write_update_ordered(BTRFS_I(inode), pos,
7679 length, false);
7680 else
7681 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7682 pos + length - 1);
7683 ret = -ENOTBLK;
7684 }
7685
Filipe Mananaf0bfa762021-10-28 16:03:41 +01007686 if (write)
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007687 extent_changeset_free(dio_data->data_reserved);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007688out:
7689 kfree(dio_data);
7690 iomap->private = NULL;
7691
Josef Bacikeb838e72012-07-31 16:28:48 -04007692 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007693}
7694
Omar Sandoval769b4f22020-04-16 14:46:22 -07007695static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7696{
7697 /*
7698 * This implies a barrier so that stores to dio_bio->bi_status before
7699 * this and loads of dio_bio->bi_status after this are fully ordered.
7700 */
7701 if (!refcount_dec_and_test(&dip->refs))
7702 return;
7703
Naohiro Aotacfe94442021-02-04 19:21:59 +09007704 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007705 __endio_write_update_ordered(BTRFS_I(dip->inode),
Qu Wenruo47926ab2021-10-08 15:29:59 +08007706 dip->file_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007707 dip->bytes,
7708 !dip->dio_bio->bi_status);
7709 } else {
7710 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
Qu Wenruo47926ab2021-10-08 15:29:59 +08007711 dip->file_offset,
7712 dip->file_offset + dip->bytes - 1);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007713 }
7714
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007715 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007716 kfree(dip);
7717}
7718
Omar Sandoval77d5d682020-04-16 14:46:25 -07007719static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7720 int mirror_num,
7721 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007722{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007723 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007725 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007726
Mike Christie37226b22016-06-05 14:31:52 -05007727 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007728
Omar Sandoval5c047a62020-04-16 14:46:24 -07007729 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007730 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007731 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007732
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007733 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007734 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007735 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007736 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007737 return ret;
7738}
7739
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007740static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007741 struct btrfs_bio *bbio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007742 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007743{
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007744 struct inode *inode = dip->inode;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007745 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7746 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007747 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007748 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7749 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007750 struct bio_vec bvec;
7751 struct bvec_iter iter;
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007752 const u64 orig_file_offset = dip->file_offset;
7753 u64 start = orig_file_offset;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007754 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007755 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007756
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007757 __bio_for_each_segment(bvec, &bbio->bio, iter, bbio->iter) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007758 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307759
Liu Bo17347ce2017-05-15 15:33:27 -07007760 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7761 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007762 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007763 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007764 if (uptodate &&
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007765 (!csum || !check_data_csum(inode, bbio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007766 bio_offset, bvec.bv_page,
7767 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007768 clean_io_failure(fs_info, failure_tree, io_tree,
7769 start, bvec.bv_page,
7770 btrfs_ino(BTRFS_I(inode)),
7771 pgoff);
7772 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08007773 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007774
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007775 ASSERT((start - orig_file_offset) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08007776 ret = btrfs_repair_one_sector(inode,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007777 &bbio->bio,
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007778 start - orig_file_offset,
Qu Wenruo150e4b02021-05-03 10:08:55 +08007779 bvec.bv_page, pgoff,
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007780 start, bbio->mirror_num,
Qu Wenruo150e4b02021-05-03 10:08:55 +08007781 submit_dio_repair_bio);
7782 if (ret)
7783 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007784 }
7785 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007786 ASSERT(bio_offset + sectorsize > bio_offset);
7787 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307788 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307789 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007790 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007791 return err;
7792}
7793
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007794static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007795 const u64 offset, const u64 bytes,
7796 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797{
Qu Wenruoe65f1522021-04-01 15:15:06 +08007798 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
7799 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00007800}
7801
Qu Wenruo8896a082020-10-21 14:24:53 +08007802static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08007803 struct bio *bio,
7804 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007805{
Qu Wenruo1941b642020-12-02 14:47:57 +08007806 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007807}
7808
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007809static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007810{
7811 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007812 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007813
Miao Xie8b110e32014-09-12 18:44:03 +08007814 if (err)
7815 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007816 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007817 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01007818 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08007819 bio->bi_iter.bi_size, err);
7820
Qu Wenruof4f39fc2021-10-08 15:30:00 +08007821 if (bio_op(bio) == REQ_OP_READ)
7822 err = btrfs_check_read_dio_bio(dip, btrfs_bio(bio), !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007823
Omar Sandoval769b4f22020-04-16 14:46:22 -07007824 if (err)
7825 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007826
Qu Wenruo47926ab2021-10-08 15:29:59 +08007827 btrfs_record_physical_zoned(dip->inode, dip->file_offset, bio);
Naohiro Aota544d24f2021-02-04 19:22:06 +09007828
Miao Xiee65e1532010-11-22 03:04:43 +00007829 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007830 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007831}
7832
David Sterbad0ee3932018-03-08 14:35:48 +01007833static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7834 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007835{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007836 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007837 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09007838 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007839 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007840
Liu Bo4c274bc2017-11-01 17:19:27 -06007841 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007842 if (async_submit)
7843 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7844
Josef Bacik5fd02042012-05-02 14:00:54 -04007845 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007846 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007847 if (ret)
7848 goto err;
7849 }
Miao Xiee65e1532010-11-22 03:04:43 +00007850
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007851 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007852 goto map;
7853
7854 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08007855 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02007856 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007857 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007858 } else if (write) {
7859 /*
7860 * If we aren't doing async submit, calculate the csum of the
7861 * bio now.
7862 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007863 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007864 if (ret)
7865 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007866 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007867 u64 csum_offset;
7868
Qu Wenruo47926ab2021-10-08 15:29:59 +08007869 csum_offset = file_offset - dip->file_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02007870 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02007871 csum_offset *= fs_info->csum_size;
Qu Wenruoc3a3b192021-09-15 15:17:18 +08007872 btrfs_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007873 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007874map:
Chris Mason08635ba2019-07-10 12:28:14 -07007875 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007876err:
Miao Xiee65e1532010-11-22 03:04:43 +00007877 return ret;
7878}
7879
Omar Sandovalc36cac22020-04-16 14:46:13 -07007880/*
7881 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7882 * or ordered extents whether or not we submit any bios.
7883 */
7884static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7885 struct inode *inode,
7886 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007887{
Naohiro Aotacfe94442021-02-04 19:21:59 +09007888 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007889 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7890 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007891 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007892
Omar Sandoval85879572020-04-16 14:46:21 -07007893 dip_size = sizeof(*dip);
7894 if (!write && csum) {
7895 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07007896 size_t nblocks;
7897
David Sterba265fdfa2020-07-01 21:19:09 +02007898 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02007899 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07007900 }
7901
7902 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007903 if (!dip)
7904 return NULL;
7905
Omar Sandovalc36cac22020-04-16 14:46:13 -07007906 dip->inode = inode;
Qu Wenruo47926ab2021-10-08 15:29:59 +08007907 dip->file_offset = file_offset;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007908 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01007909 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007910 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007911 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007912 return dip;
7913}
7914
Christoph Hellwig3e087732021-10-12 13:12:24 +02007915static void btrfs_submit_direct(const struct iomap_iter *iter,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007916 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007917{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07007918 struct inode *inode = iter->inode;
Naohiro Aotacfe94442021-02-04 19:21:59 +09007919 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007920 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007921 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7922 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007923 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007924 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007925 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007926 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007927 u64 submit_len;
Naohiro Aota42b5d732021-07-21 21:43:34 +09007928 u64 clone_offset = 0;
7929 u64 clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01007930 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307931 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007932 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007933 struct btrfs_io_geometry geom;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07007934 struct btrfs_dio_data *dio_data = iter->iomap.private;
Michal Rostecki42034312021-01-27 14:57:27 +01007935 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00007936
Omar Sandovalc36cac22020-04-16 14:46:13 -07007937 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7938 if (!dip) {
7939 if (!write) {
7940 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7941 file_offset + dio_bio->bi_iter.bi_size - 1);
7942 }
7943 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007944 bio_endio(dio_bio);
Christoph Hellwig3e087732021-10-12 13:12:24 +02007945 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007946 }
7947
Josef Bacik334c16d2020-10-16 11:29:14 -04007948 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07007949 /*
7950 * Load the csums up front to reduce csum tree searches and
7951 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04007952 *
7953 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07007954 */
Qu Wenruo62751932020-12-02 14:48:06 +08007955 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07007956 if (status != BLK_STS_OK)
7957 goto out_err;
7958 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007959
Omar Sandoval769b4f22020-04-16 14:46:22 -07007960 start_sector = dio_bio->bi_iter.bi_sector;
7961 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007962
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007963 do {
Michal Rostecki42034312021-01-27 14:57:27 +01007964 logical = start_sector << 9;
7965 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
7966 if (IS_ERR(em)) {
7967 status = errno_to_blk_status(PTR_ERR(em));
7968 em = NULL;
7969 goto out_err_em;
7970 }
7971 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08007972 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007973 if (ret) {
7974 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01007975 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07007976 }
Omar Sandoval769b4f22020-04-16 14:46:22 -07007977
Naohiro Aota42b5d732021-07-21 21:43:34 +09007978 clone_len = min(submit_len, geom.len);
7979 ASSERT(clone_len <= UINT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04007980
Liu Bo725130b2017-05-16 09:51:39 -07007981 /*
7982 * This will never fail as it's passing GPF_NOFS and
7983 * the allocation is backed by btrfs_bioset.
7984 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007985 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007986 bio->bi_private = dip;
7987 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007988
Naohiro Aota544d24f2021-02-04 19:22:06 +09007989 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
7990 status = extract_ordered_extent(BTRFS_I(inode), bio,
7991 file_offset);
7992 if (status) {
7993 bio_put(bio);
7994 goto out_err;
7995 }
7996 }
7997
Liu Bo725130b2017-05-16 09:51:39 -07007998 ASSERT(submit_len >= clone_len);
7999 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008000
Liu Bo725130b2017-05-16 09:51:39 -07008001 /*
8002 * Increase the count before we submit the bio so we know
8003 * the end IO handler won't happen before we increase the
8004 * count. Otherwise, the dip might get freed before we're
8005 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008006 *
8007 * We transfer the initial reference to the last bio, so we
8008 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008009 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008010 if (submit_len > 0) {
8011 refcount_inc(&dip->refs);
8012 /*
8013 * If we are submitting more than one bio, submit them
8014 * all asynchronously. The exception is RAID 5 or 6, as
8015 * asynchronous checksums make it difficult to collect
8016 * full stripe writes.
8017 */
8018 if (!raid56)
8019 async_submit = 1;
8020 }
Miao Xiee65e1532010-11-22 03:04:43 +00008021
David Sterbad0ee3932018-03-08 14:35:48 +01008022 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008023 async_submit);
8024 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008025 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008026 if (submit_len > 0)
8027 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008028 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008029 }
Liu Bo725130b2017-05-16 09:51:39 -07008030
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008031 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008032 clone_offset += clone_len;
8033 start_sector += clone_len >> 9;
8034 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008035
8036 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008037 } while (submit_len > 0);
Christoph Hellwig3e087732021-10-12 13:12:24 +02008038 return;
Miao Xiee65e1532010-11-22 03:04:43 +00008039
Michal Rostecki42034312021-01-27 14:57:27 +01008040out_err_em:
8041 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008042out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008043 dip->dio_bio->bi_status = status;
8044 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008045}
8046
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008047const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008048 .iomap_begin = btrfs_dio_iomap_begin,
8049 .iomap_end = btrfs_dio_iomap_end,
8050};
8051
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008052const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008053 .submit_io = btrfs_submit_direct,
8054};
8055
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008056static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008057 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008058{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008059 int ret;
8060
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008061 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008062 if (ret)
8063 return ret;
8064
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008065 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008066}
8067
Chris Mason9ebefb182007-06-15 13:50:00 -04008068int btrfs_readpage(struct file *file, struct page *page)
8069{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008070 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8071 u64 start = page_offset(page);
8072 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008073 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008074 int ret;
8075
Nikolay Borisov0f208812020-09-14 14:39:16 +03008076 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8077
Qu Wenruo390ed292021-04-14 16:42:15 +08008078 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8079 if (bio_ctrl.bio)
8080 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008081 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008082}
Chris Mason1832a6d2007-12-21 16:27:21 -05008083
Chris Mason39279cc2007-06-12 06:35:45 -04008084static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8085{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008086 struct inode *inode = page->mapping->host;
8087 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008088
8089 if (current->flags & PF_MEMALLOC) {
8090 redirty_page_for_writepage(wbc, page);
8091 unlock_page(page);
8092 return 0;
8093 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008094
8095 /*
8096 * If we are under memory pressure we will call this directly from the
8097 * VM, we need to make sure we have the inode referenced for the ordered
8098 * extent. If not just return like we didn't do anything.
8099 */
8100 if (!igrab(inode)) {
8101 redirty_page_for_writepage(wbc, page);
8102 return AOP_WRITEPAGE_ACTIVATE;
8103 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008104 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008105 btrfs_add_delayed_iput(inode);
8106 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008107}
Chris Mason39279cc2007-06-12 06:35:45 -04008108
Eric Sandeen48a3b632013-04-25 20:41:01 +00008109static int btrfs_writepages(struct address_space *mapping,
8110 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008111{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008112 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008113}
8114
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008115static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008116{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008117 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008118}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008119
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008120/*
8121 * For releasepage() and invalidatepage() we have a race window where
8122 * end_page_writeback() is called but the subpage spinlock is not yet released.
8123 * If we continue to release/invalidate the page, we could cause use-after-free
8124 * for subpage spinlock. So this function is to spin and wait for subpage
8125 * spinlock.
8126 */
8127static void wait_subpage_spinlock(struct page *page)
8128{
8129 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8130 struct btrfs_subpage *subpage;
8131
8132 if (fs_info->sectorsize == PAGE_SIZE)
8133 return;
8134
8135 ASSERT(PagePrivate(page) && page->private);
8136 subpage = (struct btrfs_subpage *)page->private;
8137
8138 /*
8139 * This may look insane as we just acquire the spinlock and release it,
8140 * without doing anything. But we just want to make sure no one is
8141 * still holding the subpage spinlock.
8142 * And since the page is not dirty nor writeback, and we have page
8143 * locked, the only possible way to hold a spinlock is from the endio
8144 * function to clear page writeback.
8145 *
8146 * Here we just acquire the spinlock so that all existing callers
8147 * should exit and we're safe to release/invalidate the page.
8148 */
8149 spin_lock_irq(&subpage->lock);
8150 spin_unlock_irq(&subpage->lock);
8151}
8152
Chris Masone6dcd2d2008-07-17 12:53:50 -04008153static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008154{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008155 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008156
8157 if (ret == 1) {
8158 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008159 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008160 }
Chris Masona52d9a82007-08-27 16:49:44 -04008161 return ret;
8162}
Chris Mason39279cc2007-06-12 06:35:45 -04008163
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8165{
Chris Mason98509cf2008-09-11 15:51:43 -04008166 if (PageWriteback(page) || PageDirty(page))
8167 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008168 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008169}
8170
Roman Gushchinf8e66082020-03-04 16:57:35 -08008171#ifdef CONFIG_MIGRATION
8172static int btrfs_migratepage(struct address_space *mapping,
8173 struct page *newpage, struct page *page,
8174 enum migrate_mode mode)
8175{
8176 int ret;
8177
8178 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8179 if (ret != MIGRATEPAGE_SUCCESS)
8180 return ret;
8181
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008182 if (page_has_private(page))
8183 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008184
Qu Wenruof57ad932021-04-07 19:22:13 +08008185 if (PageOrdered(page)) {
8186 ClearPageOrdered(page);
8187 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008188 }
8189
8190 if (mode != MIGRATE_SYNC_NO_COPY)
8191 migrate_page_copy(newpage, page);
8192 else
8193 migrate_page_states(newpage, page);
8194 return MIGRATEPAGE_SUCCESS;
8195}
8196#endif
8197
Lukas Czernerd47992f2013-05-21 23:17:23 -04008198static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8199 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008200{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008201 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008202 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008203 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008204 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008205 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008206 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008207 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008208 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008209
Chris Mason8b62b722009-09-02 16:53:46 -04008210 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008211 * We have page locked so no new ordered extent can be created on this
8212 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008213 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008214 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008215 * Furthermore, endio function won't skip page which has Ordered
8216 * (Private2) already cleared, so it's possible for endio and
8217 * invalidatepage to do the same ordered extent accounting twice
8218 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008219 *
8220 * So here we wait for any submitted bios to finish, so that we won't
8221 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008222 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008223 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008224 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008225
Qu Wenruobcd77452021-05-31 16:50:55 +08008226 /*
8227 * For subpage case, we have call sites like
8228 * btrfs_punch_hole_lock_range() which passes range not aligned to
8229 * sectorsize.
8230 * If the range doesn't cover the full page, we don't need to and
8231 * shouldn't clear page extent mapped, as page->private can still
8232 * record subpage dirty bits for other part of the range.
8233 *
8234 * For cases that can invalidate the full even the range doesn't
8235 * cover the full page, like invalidating the last page, we're
8236 * still safe to wait for ordered extent to finish.
8237 */
8238 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008239 btrfs_releasepage(page, GFP_NOFS);
8240 return;
8241 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008242
8243 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008244 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008245
Qu Wenruo3b835842021-04-06 19:54:53 +08008246 cur = page_start;
8247 while (cur < page_end) {
8248 struct btrfs_ordered_extent *ordered;
8249 bool delete_states;
8250 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008251 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008252
8253 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8254 page_end + 1 - cur);
8255 if (!ordered) {
8256 range_end = page_end;
8257 /*
8258 * No ordered extent covering this range, we are safe
8259 * to delete all extent states in the range.
8260 */
8261 delete_states = true;
8262 goto next;
8263 }
8264 if (ordered->file_offset > cur) {
8265 /*
8266 * There is a range between [cur, oe->file_offset) not
8267 * covered by any ordered extent.
8268 * We are safe to delete all extent states, and handle
8269 * the ordered extent in the next iteration.
8270 */
8271 range_end = ordered->file_offset - 1;
8272 delete_states = true;
8273 goto next;
8274 }
8275
8276 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8277 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008278 ASSERT(range_end + 1 - cur < U32_MAX);
8279 range_len = range_end + 1 - cur;
8280 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008281 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008282 * If Ordered (Private2) is cleared, it means endio has
8283 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008284 * We can't delete the extent states as
8285 * btrfs_finish_ordered_io() may still use some of them.
8286 */
8287 delete_states = false;
8288 goto next;
8289 }
Qu Wenruob945a462021-05-31 16:50:46 +08008290 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008291
Chris Masoneb84ae02008-07-17 13:53:27 -04008292 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008293 * IO on this page will never be started, so we need to account
8294 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8295 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008296 *
8297 * This will also unlock the range for incoming
8298 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008299 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008300 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008301 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008302 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008303 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008304 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008305
8306 spin_lock_irq(&inode->ordered_tree.lock);
8307 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8308 ordered->truncated_len = min(ordered->truncated_len,
8309 cur - ordered->file_offset);
8310 spin_unlock_irq(&inode->ordered_tree.lock);
8311
8312 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008313 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008314 btrfs_finish_ordered_io(ordered);
8315 /*
8316 * The ordered extent has finished, now we're again
8317 * safe to delete all extent states of the range.
8318 */
8319 delete_states = true;
8320 } else {
8321 /*
8322 * btrfs_finish_ordered_io() will get executed by endio
8323 * of other pages, thus we can't delete extent states
8324 * anymore
8325 */
8326 delete_states = false;
8327 }
8328next:
8329 if (ordered)
8330 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008331 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008332 * Qgroup reserved space handler
8333 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008334 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008335 * 1) Already written to disk or bio already finished
8336 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8337 * Qgroup will be handled by its qgroup_record then.
8338 * btrfs_qgroup_free_data() call will do nothing here.
8339 *
8340 * 2) Not written to disk yet
8341 * Then btrfs_qgroup_free_data() call will clear the
8342 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8343 * reserved data space.
8344 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008345 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008346 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008347 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008348 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008349 EXTENT_DELALLOC | EXTENT_UPTODATE |
8350 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008351 delete_states, &cached_state);
8352 }
8353 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008354 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008355 /*
8356 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008357 * should not have Ordered (Private2) anymore, or the above iteration
8358 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008359 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008360 ASSERT(!PageOrdered(page));
Qu Wenruoe4f94342021-09-27 15:21:49 +08008361 btrfs_page_clear_checked(fs_info, page, page_offset(page), PAGE_SIZE);
Qu Wenruo3b835842021-04-06 19:54:53 +08008362 if (!inode_evicting)
8363 __btrfs_releasepage(page, GFP_NOFS);
Qu Wenruo32443de2021-01-26 16:34:00 +08008364 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008365}
8366
Chris Mason9ebefb182007-06-15 13:50:00 -04008367/*
8368 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8369 * called from a page fault handler when a page is first dirtied. Hence we must
8370 * be careful to check for EOF conditions here. We set the page up correctly
8371 * for a written page which means we get ENOSPC checking when writing into
8372 * holes and correct delalloc and unwritten extent mapping on filesystems that
8373 * support these features.
8374 *
8375 * We are not allowed to take the i_mutex here so we have to play games to
8376 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008377 * truncate_setsize() writes the inode size before removing pages, once we have
8378 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008379 * beyond EOF, then the page is guaranteed safe against truncation until we
8380 * unlock the page.
8381 */
Souptick Joardera528a242018-06-06 19:54:44 +05308382vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008383{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008384 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008385 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008386 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008387 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8388 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008389 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008390 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008391 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008392 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308393 vm_fault_t ret;
8394 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008395 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308396 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008397 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008398 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308399 u64 end;
8400
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008401 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008402
Jan Karab2b5ef52012-06-12 16:20:45 +02008403 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008404 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008405 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308406 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008407
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308408 /*
8409 * Reserving delalloc space after obtaining the page lock can lead to
8410 * deadlock. For example, if a dirty page is locked by this function
8411 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8412 * dirty page write out, then the btrfs_writepage() function could
8413 * end up waiting indefinitely to get a lock on the page currently
8414 * being processed by btrfs_page_mkwrite() function.
8415 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008416 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8417 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308418 if (!ret2) {
8419 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008420 reserved = 1;
8421 }
Souptick Joardera528a242018-06-06 19:54:44 +05308422 if (ret2) {
8423 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008424 if (reserved)
8425 goto out;
8426 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008427 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008428
Nick Piggin56a76f82009-03-31 15:23:23 -07008429 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008430again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008431 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008432 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008433 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008434
Chris Mason9ebefb182007-06-15 13:50:00 -04008435 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008436 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008437 /* page got truncated out from underneath us */
8438 goto out_unlock;
8439 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008440 wait_on_page_writeback(page);
8441
David Sterbaff13db42015-12-03 14:30:40 +01008442 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008443 ret2 = set_page_extent_mapped(page);
8444 if (ret2 < 0) {
8445 ret = vmf_error(ret2);
8446 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8447 goto out_unlock;
8448 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008449
Chris Masoneb84ae02008-07-17 13:53:27 -04008450 /*
8451 * we can't set the delalloc bits if there are pending ordered
8452 * extents. Drop our locks and wait for them to finish
8453 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008454 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8455 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008456 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008457 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008458 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008459 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008460 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008461 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008462 btrfs_put_ordered_extent(ordered);
8463 goto again;
8464 }
8465
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008466 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008467 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008468 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008469 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308470 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008471 btrfs_delalloc_release_space(BTRFS_I(inode),
8472 data_reserved, page_start,
8473 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308474 }
8475 }
8476
Josef Bacikfbf19082009-10-01 17:10:23 -04008477 /*
Liu Bo54160342016-12-13 12:15:19 -08008478 * page_mkwrite gets called when the page is firstly dirtied after it's
8479 * faulted in, but write(2) could also dirty a page and set delalloc
8480 * bits, thus in this case for space account reason, we still need to
8481 * clear any delalloc bits within this page range since we have to
8482 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008483 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308484 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008485 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8486 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008487
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008488 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008489 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308490 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008491 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008492 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008493 ret = VM_FAULT_SIGBUS;
8494 goto out_unlock;
8495 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008496
8497 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008498 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008499 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008500 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008501 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008502
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008503 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008504 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008505 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008506 }
Qu Wenruoe4f94342021-09-27 15:21:49 +08008507 btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008508 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8509 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008510
Filipe Mananabc0939f2021-02-23 12:08:48 +00008511 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008512
David Sterbae43bbe52017-12-12 21:43:52 +01008513 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008514 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008515
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008516 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8517 sb_end_pagefault(inode->i_sb);
8518 extent_changeset_free(data_reserved);
8519 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008520
8521out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008522 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008523 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008524out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008525 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008526 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008527 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008528out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008529 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008530 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008531 return ret;
8532}
8533
Filipe Manana213e8c52018-02-06 20:40:31 +00008534static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008535{
Josef Bacikd9ac19c2021-12-03 17:18:09 -05008536 struct btrfs_truncate_control control = {
Josef Bacik71d18b52021-12-03 17:18:15 -05008537 .inode = BTRFS_I(inode),
Josef Bacik487e81d2021-12-03 17:18:14 -05008538 .ino = btrfs_ino(BTRFS_I(inode)),
Josef Bacikd9ac19c2021-12-03 17:18:09 -05008539 .min_type = BTRFS_EXTENT_DATA_KEY,
Josef Bacik655807b2021-12-03 17:18:13 -05008540 .clear_extent_range = true,
Josef Bacikd9ac19c2021-12-03 17:18:09 -05008541 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008543 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008544 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008545 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008546 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008547 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008548 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008549
Filipe Manana213e8c52018-02-06 20:40:31 +00008550 if (!skip_writeback) {
8551 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8552 (u64)-1);
8553 if (ret)
8554 return ret;
8555 }
Chris Mason39279cc2007-06-12 06:35:45 -04008556
Josef Bacikfcb80c22011-05-03 10:40:22 -04008557 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008558 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8559 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008560 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008561 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008562 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008563 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008564 * be free'd up by the truncate operation, but also have some slack
8565 * space reserved in case it uses space during the truncate (thank you
8566 * very much snapshotting).
8567 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008568 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008569 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008570 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008571 * doesn't end up using space reserved for updating the inode. We also
8572 * need to be able to stop the transaction and start a new one, which
8573 * means we need to be able to update the inode several times, and we
8574 * have no idea of knowing how many times that will be, so we can't just
8575 * reserve 1 item for the entirety of the operation, so that has to be
8576 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008577 *
8578 * So that leaves us with
8579 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008580 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008581 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008582 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008583 * updating the inode.
8584 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008585 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008586 if (!rsv)
8587 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008588 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008589 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008590
Josef Bacik907cbce2011-08-08 13:46:15 -04008591 /*
Josef Bacik07127182011-08-19 10:29:59 -04008592 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008593 * 1 for updating the inode.
8594 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008595 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008596 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008597 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008598 goto out;
8599 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008600
Josef Bacik907cbce2011-08-08 13:46:15 -04008601 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008602 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008603 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008604 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008605
Josef Bacikca7e70f2012-08-27 17:48:15 -04008606 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008607
Yan, Zheng80825102009-11-12 09:35:36 +00008608 while (1) {
Josef Bacik9a4a1422021-12-03 17:18:05 -05008609 struct extent_state *cached_state = NULL;
8610 const u64 new_size = inode->i_size;
8611 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
8612
Josef Bacikd9ac19c2021-12-03 17:18:09 -05008613 control.new_size = new_size;
Josef Bacik9a4a1422021-12-03 17:18:05 -05008614 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
8615 &cached_state);
8616 /*
8617 * We want to drop from the next block forward in case this new
8618 * size is not block aligned since we will be keeping the last
8619 * block of the extent just the way it is.
8620 */
8621 btrfs_drop_extent_cache(BTRFS_I(inode),
8622 ALIGN(new_size, fs_info->sectorsize),
8623 (u64)-1, 0);
8624
Josef Bacik71d18b52021-12-03 17:18:15 -05008625 ret = btrfs_truncate_inode_items(trans, root, &control);
Josef Bacikc2ddb6122021-12-03 17:18:10 -05008626
Josef Bacik462b7282021-12-03 17:18:11 -05008627 inode_sub_bytes(inode, control.sub_bytes);
Josef Bacikc2ddb6122021-12-03 17:18:10 -05008628 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size);
8629
Josef Bacik9a4a1422021-12-03 17:18:05 -05008630 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
8631 (u64)-1, &cached_state);
8632
Josef Bacikddfae632017-10-19 14:16:02 -04008633 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008634 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008635 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008636
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008637 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008638 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008639 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008640
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008641 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008642 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008643
8644 trans = btrfs_start_transaction(root, 2);
8645 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008646 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008647 trans = NULL;
8648 break;
8649 }
8650
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008651 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008652 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008653 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008654 BUG_ON(ret); /* shouldn't happen */
8655 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008656 }
8657
Josef Bacikddfae632017-10-19 14:16:02 -04008658 /*
8659 * We can't call btrfs_truncate_block inside a trans handle as we could
Josef Bacik54f03ab2021-12-03 17:18:04 -05008660 * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we
8661 * know we've truncated everything except the last little bit, and can
8662 * do btrfs_truncate_block and then update the disk_i_size.
Josef Bacikddfae632017-10-19 14:16:02 -04008663 */
Josef Bacik54f03ab2021-12-03 17:18:04 -05008664 if (ret == BTRFS_NEED_TRUNCATE_BLOCK) {
Josef Bacikddfae632017-10-19 14:16:02 -04008665 btrfs_end_transaction(trans);
8666 btrfs_btree_balance_dirty(fs_info);
8667
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008668 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008669 if (ret)
8670 goto out;
8671 trans = btrfs_start_transaction(root, 1);
8672 if (IS_ERR(trans)) {
8673 ret = PTR_ERR(trans);
8674 goto out;
8675 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008676 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008677 }
8678
Chris Mason917c16b2011-11-08 14:49:59 -05008679 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008680 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008681
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008682 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008683 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008684 if (ret2 && !ret)
8685 ret = ret2;
8686
8687 ret2 = btrfs_end_transaction(trans);
8688 if (ret2 && !ret)
8689 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008690 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008691 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008692out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008693 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008694 /*
8695 * So if we truncate and then write and fsync we normally would just
8696 * write the extents that changed, which is a problem if we need to
8697 * first truncate that entire inode. So set this flag so we write out
8698 * all of the extents in the inode to the sync log so we're completely
8699 * safe.
8700 *
8701 * If no extents were dropped or trimmed we don't need to force the next
8702 * fsync to truncate all the inode's items from the log and re-log them
8703 * all. This means the truncate operation did not change the file size,
8704 * or changed it to a smaller size but there was only an implicit hole
8705 * between the old i_size and the new i_size, and there were no prealloc
8706 * extents beyond i_size to drop.
8707 */
Josef Bacikd9ac19c2021-12-03 17:18:09 -05008708 if (control.extents_found > 0)
Filipe Manana0d7d3162021-05-24 11:35:55 +01008709 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008710
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008711 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008712}
8713
Sven Wegener3b963622008-06-09 21:57:42 -04008714/*
Chris Masond352ac62008-09-29 15:18:18 -04008715 * create a new subvolume directory/inode (helper for the ioctl).
8716 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008717int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008718 struct btrfs_root *new_root,
Christian Brauner4d4340c2021-07-27 12:48:52 +02008719 struct btrfs_root *parent_root,
8720 struct user_namespace *mnt_userns)
Chris Mason39279cc2007-06-12 06:35:45 -04008721{
Chris Mason39279cc2007-06-12 06:35:45 -04008722 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008723 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008724 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008725 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008726
Nikolay Borisov23125102020-12-07 17:32:36 +02008727 err = btrfs_get_free_objectid(new_root, &ino);
8728 if (err < 0)
8729 return err;
8730
Christian Brauner4d4340c2021-07-27 12:48:52 +02008731 inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02008732 ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008733 S_IFDIR | (~current_umask() & S_IRWXUGO),
8734 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008735 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008736 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008737 inode->i_op = &btrfs_dir_inode_operations;
8738 inode->i_fop = &btrfs_dir_file_operations;
8739
Miklos Szeredibfe86842011-10-28 14:13:29 +02008740 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008741 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008742 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008743
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008744 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8745 if (err)
8746 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008747 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008748 new_root->root_key.objectid, err);
8749
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008750 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008751
Yan, Zheng76dda932009-09-21 16:00:26 -04008752 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008753 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008754}
8755
Chris Mason39279cc2007-06-12 06:35:45 -04008756struct inode *btrfs_alloc_inode(struct super_block *sb)
8757{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008758 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008759 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008760 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008761
David Sterba712e36c2017-10-31 17:08:27 +01008762 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008763 if (!ei)
8764 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008765
8766 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008767 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008768 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008769 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008770 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008771 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008772 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008773 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008774 ei->disk_i_size = 0;
8775 ei->flags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -07008776 ei->ro_flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008777 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008778 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008779 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008780 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008781 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008782 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008783
Josef Bacik9e0baf62011-07-15 15:16:44 +00008784 spin_lock_init(&ei->lock);
8785 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008786 if (sb->s_magic != BTRFS_TEST_MAGIC)
8787 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8788 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008789 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008790 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008791 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008792
Miao Xie16cdcec2011-04-22 18:12:22 +08008793 ei->delayed_node = NULL;
8794
chandan r9cc97d62012-07-04 12:48:07 +05308795 ei->i_otime.tv_sec = 0;
8796 ei->i_otime.tv_nsec = 0;
8797
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008798 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008799 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008800 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8801 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8802 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008803 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8804 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008805 ei->io_tree.track_uptodate = true;
8806 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008807 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008808 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008810 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008811 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008812 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05008813 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008814
8815 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008816}
8817
Josef Bacikaaedb552013-10-11 14:44:09 -04008818#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8819void btrfs_test_destroy_inode(struct inode *inode)
8820{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008821 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008822 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8823}
8824#endif
8825
Al Viro26602ca2019-04-10 15:14:41 -04008826void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008827{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008828 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8829}
8830
Nikolay Borisov633cc812020-09-18 12:15:49 +03008831void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008832{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008834 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8835 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008836
Nikolay Borisov633cc812020-09-18 12:15:49 +03008837 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8838 WARN_ON(vfs_inode->i_data.nrpages);
8839 WARN_ON(inode->block_rsv.reserved);
8840 WARN_ON(inode->block_rsv.size);
8841 WARN_ON(inode->outstanding_extents);
Filipe Mananadc287222021-09-16 11:32:14 +01008842 if (!S_ISDIR(vfs_inode->i_mode)) {
8843 WARN_ON(inode->delalloc_bytes);
8844 WARN_ON(inode->new_delalloc_bytes);
8845 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008846 WARN_ON(inode->csum_bytes);
8847 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008848
Chris Mason5a3f23d2009-03-31 13:27:11 -04008849 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008850 * This can happen where we create an inode, but somebody else also
8851 * created the same inode and we need to destroy the one we already
8852 * created.
8853 */
8854 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008855 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008856
Chris Masond3977122009-01-05 21:25:51 -05008857 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008858 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859 if (!ordered)
8860 break;
8861 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008862 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008863 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008864 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008865 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008866 btrfs_put_ordered_extent(ordered);
8867 btrfs_put_ordered_extent(ordered);
8868 }
8869 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008870 btrfs_qgroup_check_reserved_leak(inode);
8871 inode_tree_del(inode);
8872 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8873 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8874 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008875}
8876
Al Viro45321ac2010-06-07 13:43:19 -04008877int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008878{
8879 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008880
Naohiro Aota6379ef92013-06-06 09:56:34 +00008881 if (root == NULL)
8882 return 1;
8883
Liu Bofa6ac872013-02-20 14:10:23 +00008884 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008885 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008886 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008887 else
Al Viro45321ac2010-06-07 13:43:19 -04008888 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008889}
8890
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008891static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008892{
8893 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8894
8895 inode_init_once(&ei->vfs_inode);
8896}
8897
David Sterbae67c7182018-02-19 17:24:18 +01008898void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008899{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008900 /*
8901 * Make sure all delayed rcu free inodes are flushed before we
8902 * destroy cache.
8903 */
8904 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008905 kmem_cache_destroy(btrfs_inode_cachep);
8906 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008907 kmem_cache_destroy(btrfs_path_cachep);
8908 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008909 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008910}
8911
Liu Bof5c29bd2017-11-02 17:21:50 -06008912int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008913{
David Sterba837e1972012-09-07 03:00:48 -06008914 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008915 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008916 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8917 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008918 if (!btrfs_inode_cachep)
8919 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008920
David Sterba837e1972012-09-07 03:00:48 -06008921 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008922 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008923 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008924 if (!btrfs_trans_handle_cachep)
8925 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008926
David Sterba837e1972012-09-07 03:00:48 -06008927 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008928 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008929 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008930 if (!btrfs_path_cachep)
8931 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008932
David Sterba837e1972012-09-07 03:00:48 -06008933 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008934 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008935 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008936 if (!btrfs_free_space_cachep)
8937 goto fail;
8938
Christophe Leroy3acd4852019-08-21 15:05:55 +00008939 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8940 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01008941 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008942 if (!btrfs_free_space_bitmap_cachep)
8943 goto fail;
8944
Chris Mason39279cc2007-06-12 06:35:45 -04008945 return 0;
8946fail:
8947 btrfs_destroy_cachep();
8948 return -ENOMEM;
8949}
8950
Christian Brauner549c7292021-01-21 14:19:43 +01008951static int btrfs_getattr(struct user_namespace *mnt_userns,
8952 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00008953 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008954{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008955 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008956 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008957 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008958 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008959 u32 bi_flags = BTRFS_I(inode)->flags;
Boris Burkov14605402021-06-30 13:01:49 -07008960 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
Yonghong Song04a87e32017-05-12 15:07:43 -07008961
8962 stat->result_mask |= STATX_BTIME;
8963 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8964 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8965 if (bi_flags & BTRFS_INODE_APPEND)
8966 stat->attributes |= STATX_ATTR_APPEND;
8967 if (bi_flags & BTRFS_INODE_COMPRESS)
8968 stat->attributes |= STATX_ATTR_COMPRESSED;
8969 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8970 stat->attributes |= STATX_ATTR_IMMUTABLE;
8971 if (bi_flags & BTRFS_INODE_NODUMP)
8972 stat->attributes |= STATX_ATTR_NODUMP;
Boris Burkov14605402021-06-30 13:01:49 -07008973 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
8974 stat->attributes |= STATX_ATTR_VERITY;
Yonghong Song04a87e32017-05-12 15:07:43 -07008975
8976 stat->attributes_mask |= (STATX_ATTR_APPEND |
8977 STATX_ATTR_COMPRESSED |
8978 STATX_ATTR_IMMUTABLE |
8979 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008980
Christian Braunerc020d2e2021-07-27 12:48:43 +02008981 generic_fillattr(mnt_userns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008982 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008983
8984 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008985 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008986 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008987 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00008988 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008989 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008990 return 0;
8991}
8992
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993static int btrfs_rename_exchange(struct inode *old_dir,
8994 struct dentry *old_dentry,
8995 struct inode *new_dir,
8996 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008997{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008998 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008999 struct btrfs_trans_handle *trans;
9000 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009001 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009002 struct inode *new_inode = new_dentry->d_inode;
9003 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009004 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009005 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9006 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009007 u64 old_idx = 0;
9008 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009009 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009010 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009011 bool root_log_pinned = false;
9012 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009013 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009014
NeilBrown3f79f6f2021-08-06 14:26:24 +10009015 /*
9016 * For non-subvolumes allow exchange only within one subvolume, in the
9017 * same inode namespace. Two subvolumes (represented as directory) can
9018 * be exchanged as they're a logical link and have a fixed inode number.
9019 */
9020 if (root != dest &&
9021 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9022 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009023 return -EXDEV;
9024
9025 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009026 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9027 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009028 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009029
9030 /*
9031 * We want to reserve the absolute worst case amount of items. So if
9032 * both inodes are subvols and we need to unlink them then that would
9033 * require 4 item modifications, but if they are both normal inodes it
9034 * would require 5 item modifications, so we'll assume their normal
9035 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9036 * should cover the worst case number of items we'll modify.
9037 */
9038 trans = btrfs_start_transaction(root, 12);
9039 if (IS_ERR(trans)) {
9040 ret = PTR_ERR(trans);
9041 goto out_notrans;
9042 }
9043
Josef Bacik00aa8e82021-03-12 15:25:02 -05009044 if (dest != root) {
9045 ret = btrfs_record_root_in_trans(trans, dest);
9046 if (ret)
9047 goto out_fail;
9048 }
Josef Bacik3e174092019-11-15 15:43:06 -05009049
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009050 /*
9051 * We need to find a free sequence number both in the source and
9052 * in the destination directory for the exchange.
9053 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009054 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009055 if (ret)
9056 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009057 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009058 if (ret)
9059 goto out_fail;
9060
9061 BTRFS_I(old_inode)->dir_index = 0ULL;
9062 BTRFS_I(new_inode)->dir_index = 0ULL;
9063
9064 /* Reference for the source. */
9065 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9066 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009067 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009068 } else {
9069 ret = btrfs_insert_inode_ref(trans, dest,
9070 new_dentry->d_name.name,
9071 new_dentry->d_name.len,
9072 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009073 btrfs_ino(BTRFS_I(new_dir)),
9074 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075 if (ret)
9076 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009077 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078 }
9079
9080 /* And now for the dest. */
9081 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9082 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009083 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009084 } else {
9085 ret = btrfs_insert_inode_ref(trans, root,
9086 old_dentry->d_name.name,
9087 old_dentry->d_name.len,
9088 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009089 btrfs_ino(BTRFS_I(old_dir)),
9090 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009091 if (ret) {
9092 if (need_abort)
9093 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009094 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009095 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009096 }
9097
9098 /* Update inode version and ctime/mtime. */
9099 inode_inc_iversion(old_dir);
9100 inode_inc_iversion(new_dir);
9101 inode_inc_iversion(old_inode);
9102 inode_inc_iversion(new_inode);
9103 old_dir->i_ctime = old_dir->i_mtime = ctime;
9104 new_dir->i_ctime = new_dir->i_mtime = ctime;
9105 old_inode->i_ctime = ctime;
9106 new_inode->i_ctime = ctime;
9107
9108 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009109 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9110 BTRFS_I(old_inode), 1);
9111 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9112 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009113 }
9114
Filipe Mananabd54f382021-07-27 11:24:45 +01009115 /*
9116 * Now pin the logs of the roots. We do it to ensure that no other task
9117 * can sync the logs while we are in progress with the rename, because
9118 * that could result in an inconsistency in case any of the inodes that
9119 * are part of this rename operation were logged before.
9120 *
9121 * We pin the logs even if at this precise moment none of the inodes was
9122 * logged before. This is because right after we checked for that, some
9123 * other task fsyncing some other inode not involved with this rename
9124 * operation could log that one of our inodes exists.
9125 *
9126 * We don't need to pin the logs before the above calls to
9127 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9128 */
9129 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9130 btrfs_pin_log_trans(root);
9131 root_log_pinned = true;
9132 }
9133 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9134 btrfs_pin_log_trans(dest);
9135 dest_log_pinned = true;
9136 }
9137
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009138 /* src is a subvolume */
9139 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009140 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009141 } else { /* src is an inode */
Filipe Manana4467af82021-10-25 17:31:50 +01009142 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009143 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009144 old_dentry->d_name.name,
9145 old_dentry->d_name.len);
9146 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009147 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009148 }
9149 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009150 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009151 goto out_fail;
9152 }
9153
9154 /* dest is a subvolume */
9155 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009156 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009157 } else { /* dest is an inode */
Filipe Manana4467af82021-10-25 17:31:50 +01009158 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009159 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009160 new_dentry->d_name.name,
9161 new_dentry->d_name.len);
9162 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009163 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009164 }
9165 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009166 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009167 goto out_fail;
9168 }
9169
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009170 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009171 new_dentry->d_name.name,
9172 new_dentry->d_name.len, 0, old_idx);
9173 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009174 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009175 goto out_fail;
9176 }
9177
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009178 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009179 old_dentry->d_name.name,
9180 old_dentry->d_name.len, 0, new_idx);
9181 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009182 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009183 goto out_fail;
9184 }
9185
9186 if (old_inode->i_nlink == 1)
9187 BTRFS_I(old_inode)->dir_index = old_idx;
9188 if (new_inode->i_nlink == 1)
9189 BTRFS_I(new_inode)->dir_index = new_idx;
9190
Filipe Manana86e8aa02016-05-05 02:02:27 +01009191 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009192 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9193 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009194 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009195 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009196 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009197 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009198 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9199 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009200 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009201 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009202 }
9203out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009204 /*
9205 * If we have pinned a log and an error happened, we unpin tasks
9206 * trying to sync the log and force them to fallback to a transaction
9207 * commit if the log currently contains any of the inodes involved in
9208 * this rename operation (to ensure we do not persist a log with an
9209 * inconsistent state for any of these inodes or leading to any
9210 * inconsistencies when replayed). If the transaction was aborted, the
9211 * abortion reason is propagated to userspace when attempting to commit
9212 * the transaction. If the log does not contain any of these inodes, we
9213 * allow the tasks to sync it.
9214 */
9215 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009216 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9217 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9218 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana1c167b82021-07-29 15:28:34 +01009219 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
David Sterba90787762019-03-20 13:28:05 +01009220 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009221
9222 if (root_log_pinned) {
9223 btrfs_end_log_trans(root);
9224 root_log_pinned = false;
9225 }
9226 if (dest_log_pinned) {
9227 btrfs_end_log_trans(dest);
9228 dest_log_pinned = false;
9229 }
9230 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009231 ret2 = btrfs_end_transaction(trans);
9232 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009233out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009234 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9235 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009236 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009237
9238 return ret;
9239}
9240
9241static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9242 struct btrfs_root *root,
Christian Braunerca072742021-07-27 12:48:42 +02009243 struct user_namespace *mnt_userns,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009244 struct inode *dir,
9245 struct dentry *dentry)
9246{
9247 int ret;
9248 struct inode *inode;
9249 u64 objectid;
9250 u64 index;
9251
Nikolay Borisov543068a2020-12-07 17:32:33 +02009252 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009253 if (ret)
9254 return ret;
9255
Christian Braunerca072742021-07-27 12:48:42 +02009256 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009257 dentry->d_name.name,
9258 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009259 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009260 objectid,
9261 S_IFCHR | WHITEOUT_MODE,
9262 &index);
9263
9264 if (IS_ERR(inode)) {
9265 ret = PTR_ERR(inode);
9266 return ret;
9267 }
9268
9269 inode->i_op = &btrfs_special_inode_operations;
9270 init_special_inode(inode, inode->i_mode,
9271 WHITEOUT_DEV);
9272
9273 ret = btrfs_init_inode_security(trans, inode, dir,
9274 &dentry->d_name);
9275 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009276 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009277
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009278 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9279 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009280 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009281 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009282
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009283 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009284out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009285 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009286 if (ret)
9287 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009288 iput(inode);
9289
Filipe Mananac9901612016-05-05 01:41:57 +01009290 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009291}
9292
Christian Braunerca072742021-07-27 12:48:42 +02009293static int btrfs_rename(struct user_namespace *mnt_userns,
9294 struct inode *old_dir, struct dentry *old_dentry,
9295 struct inode *new_dir, struct dentry *new_dentry,
9296 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009297{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009298 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009299 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009300 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009301 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9302 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009303 struct inode *new_inode = d_inode(new_dentry);
9304 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009305 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009306 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009307 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009308 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009309 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009310
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009311 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009312 return -EPERM;
9313
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009314 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009315 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009316 return -EXDEV;
9317
Li Zefan33345d012011-04-20 10:31:50 +08009318 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009319 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009320 return -ENOTEMPTY;
9321
Chris Mason39279cc2007-06-12 06:35:45 -04009322 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009323 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009324 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009325
9326
9327 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009328 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009329 new_dentry->d_name.name,
9330 new_dentry->d_name.len);
9331
9332 if (ret) {
9333 if (ret == -EEXIST) {
9334 /* we shouldn't get
9335 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309336 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009337 return ret;
9338 }
9339 } else {
9340 /* maybe -EOVERFLOW */
9341 return ret;
9342 }
9343 }
9344 ret = 0;
9345
Chris Mason5a3f23d2009-03-31 13:27:11 -04009346 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009347 * we're using rename to replace one file with another. Start IO on it
9348 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009349 */
Chris Mason8d875f92014-08-12 10:47:42 -07009350 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009351 filemap_flush(old_inode->i_mapping);
9352
Yan, Zheng76dda932009-09-21 16:00:26 -04009353 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009354 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009355 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009356 /*
9357 * We want to reserve the absolute worst case amount of items. So if
9358 * both inodes are subvols and we need to unlink them then that would
9359 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009360 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009361 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9362 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009363 * If our rename has the whiteout flag, we need more 5 units for the
9364 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9365 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009366 */
Filipe Manana5062af32016-05-05 10:26:26 +01009367 trans_num_items = 11;
9368 if (flags & RENAME_WHITEOUT)
9369 trans_num_items += 5;
9370 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009371 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009372 ret = PTR_ERR(trans);
9373 goto out_notrans;
9374 }
Chris Mason5f39d392007-10-15 16:14:19 -04009375
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009376 if (dest != root) {
9377 ret = btrfs_record_root_in_trans(trans, dest);
9378 if (ret)
9379 goto out_fail;
9380 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009381
Nikolay Borisov877574e2017-02-20 13:50:33 +02009382 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009383 if (ret)
9384 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009385
Miao Xie67de1172013-12-26 13:07:06 +08009386 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009387 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009388 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009389 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009390 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009391 ret = btrfs_insert_inode_ref(trans, dest,
9392 new_dentry->d_name.name,
9393 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009394 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009395 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009396 if (ret)
9397 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009398 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009399
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009400 inode_inc_iversion(old_dir);
9401 inode_inc_iversion(new_dir);
9402 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009403 old_dir->i_ctime = old_dir->i_mtime =
9404 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009405 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009406
Chris Mason12fcfd22009-03-24 10:24:20 -04009407 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009408 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9409 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009410
Li Zefan33345d012011-04-20 10:31:50 +08009411 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009412 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009413 } else {
Filipe Mananabd54f382021-07-27 11:24:45 +01009414 /*
9415 * Now pin the log. We do it to ensure that no other task can
9416 * sync the log while we are in progress with the rename, as
9417 * that could result in an inconsistency in case any of the
9418 * inodes that are part of this rename operation were logged
9419 * before.
9420 *
9421 * We pin the log even if at this precise moment none of the
9422 * inodes was logged before. This is because right after we
9423 * checked for that, some other task fsyncing some other inode
9424 * not involved with this rename operation could log that one of
9425 * our inodes exists.
9426 *
9427 * We don't need to pin the logs before the above call to
9428 * btrfs_insert_inode_ref(), since that does not need to change
9429 * a log.
9430 */
9431 btrfs_pin_log_trans(root);
9432 log_pinned = true;
Filipe Manana4467af82021-10-25 17:31:50 +01009433 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009434 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009435 old_dentry->d_name.name,
9436 old_dentry->d_name.len);
9437 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009438 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009439 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009440 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009441 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009442 goto out_fail;
9443 }
Chris Mason39279cc2007-06-12 06:35:45 -04009444
9445 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009446 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009447 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009448 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009449 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009450 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009451 BUG_ON(new_inode->i_nlink == 0);
9452 } else {
Filipe Manana4467af82021-10-25 17:31:50 +01009453 ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir),
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009454 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009455 new_dentry->d_name.name,
9456 new_dentry->d_name.len);
9457 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009458 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009459 ret = btrfs_orphan_add(trans,
9460 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009461 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009462 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009463 goto out_fail;
9464 }
Chris Mason39279cc2007-06-12 06:35:45 -04009465 }
Josef Bacikaec74772008-07-24 12:12:38 -04009466
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009467 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009468 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009469 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009470 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009471 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009472 goto out_fail;
9473 }
Chris Mason39279cc2007-06-12 06:35:45 -04009474
Miao Xie67de1172013-12-26 13:07:06 +08009475 if (old_inode->i_nlink == 1)
9476 BTRFS_I(old_inode)->dir_index = index;
9477
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009478 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009479 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9480 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009481 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009482 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009483 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009484
9485 if (flags & RENAME_WHITEOUT) {
Christian Braunerca072742021-07-27 12:48:42 +02009486 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9487 old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488
9489 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009490 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 goto out_fail;
9492 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009493 }
Chris Mason39279cc2007-06-12 06:35:45 -04009494out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009495 /*
9496 * If we have pinned the log and an error happened, we unpin tasks
9497 * trying to sync the log and force them to fallback to a transaction
9498 * commit if the log currently contains any of the inodes involved in
9499 * this rename operation (to ensure we do not persist a log with an
9500 * inconsistent state for any of these inodes or leading to any
9501 * inconsistencies when replayed). If the transaction was aborted, the
9502 * abortion reason is propagated to userspace when attempting to commit
9503 * the transaction. If the log does not contain any of these inodes, we
9504 * allow the tasks to sync it.
9505 */
9506 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009507 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9508 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9509 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009510 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009511 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009512 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009513
9514 btrfs_end_log_trans(root);
9515 log_pinned = false;
9516 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009517 ret2 = btrfs_end_transaction(trans);
9518 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009519out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009520 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009522
Chris Mason39279cc2007-06-12 06:35:45 -04009523 return ret;
9524}
9525
Christian Brauner549c7292021-01-21 14:19:43 +01009526static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9527 struct dentry *old_dentry, struct inode *new_dir,
9528 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009529{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009531 return -EINVAL;
9532
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009533 if (flags & RENAME_EXCHANGE)
9534 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9535 new_dentry);
9536
Christian Braunerca072742021-07-27 12:48:42 +02009537 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9538 new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009539}
9540
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009541struct btrfs_delalloc_work {
9542 struct inode *inode;
9543 struct completion completion;
9544 struct list_head list;
9545 struct btrfs_work work;
9546};
9547
Miao Xie8ccf6f192012-10-25 09:28:04 +00009548static void btrfs_run_delalloc_work(struct btrfs_work *work)
9549{
9550 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009551 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009552
9553 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9554 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009555 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009556 filemap_flush(inode->i_mapping);
9557 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9558 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009559 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009560
Nikolay Borisov076da912018-04-23 10:54:16 +03009561 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009562 complete(&delalloc_work->completion);
9563}
9564
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009565static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009566{
9567 struct btrfs_delalloc_work *work;
9568
David Sterba100d5702015-12-08 14:39:32 +01009569 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009570 if (!work)
9571 return NULL;
9572
9573 init_completion(&work->completion);
9574 INIT_LIST_HEAD(&work->list);
9575 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009576 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009577
9578 return work;
9579}
9580
Chris Masond352ac62008-09-29 15:18:18 -04009581/*
9582 * some fairly slow code that needs optimization. This walks the list
9583 * of all the inodes with pending delalloc and forces them to disk.
9584 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009585static int start_delalloc_inodes(struct btrfs_root *root,
9586 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009587 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009588{
Chris Masonea8c2812008-08-04 23:17:27 -04009589 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009590 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009591 struct btrfs_delalloc_work *work, *next;
9592 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009593 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009594 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009595 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009596
Miao Xie8ccf6f192012-10-25 09:28:04 +00009597 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009598 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009599
Miao Xie573bfb72014-03-06 13:55:03 +08009600 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009601 spin_lock(&root->delalloc_lock);
9602 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009603 while (!list_empty(&splice)) {
9604 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009605 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009606
Miao Xieeb73c1b2013-05-15 07:48:22 +00009607 list_move_tail(&binode->delalloc_inodes,
9608 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009609
9610 if (in_reclaim_context &&
9611 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9612 continue;
9613
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009614 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009615 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009616 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009617 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009618 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009619 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009620
Ethan Lien3cd24c62018-11-01 14:49:03 +08009621 if (snapshot)
9622 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9623 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009624 if (full_flush) {
9625 work = btrfs_alloc_delalloc_work(inode);
9626 if (!work) {
9627 iput(inode);
9628 ret = -ENOMEM;
9629 goto out;
9630 }
9631 list_add_tail(&work->list, &works);
9632 btrfs_queue_work(root->fs_info->flush_workers,
9633 &work->work);
9634 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009635 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009636 btrfs_add_delayed_iput(inode);
9637 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009638 goto out;
9639 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009640 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009641 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009642 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009643 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009644
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009645out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009646 list_for_each_entry_safe(work, next, &works, list) {
9647 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009648 wait_for_completion(&work->completion);
9649 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009650 }
9651
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009652 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009653 spin_lock(&root->delalloc_lock);
9654 list_splice_tail(&splice, &root->delalloc_inodes);
9655 spin_unlock(&root->delalloc_lock);
9656 }
Miao Xie573bfb72014-03-06 13:55:03 +08009657 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009658 return ret;
9659}
9660
Filipe Mananaf9baa502021-04-22 12:08:05 +01009661int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009662{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009663 struct writeback_control wbc = {
9664 .nr_to_write = LONG_MAX,
9665 .sync_mode = WB_SYNC_NONE,
9666 .range_start = 0,
9667 .range_end = LLONG_MAX,
9668 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009669 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009670
Josef Bacik84961532021-10-05 16:35:25 -04009671 if (BTRFS_FS_ERROR(fs_info))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009672 return -EROFS;
9673
Filipe Mananaf9baa502021-04-22 12:08:05 +01009674 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009675}
9676
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009677int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009678 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009679{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009680 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009681 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009682 .sync_mode = WB_SYNC_NONE,
9683 .range_start = 0,
9684 .range_end = LLONG_MAX,
9685 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009686 struct btrfs_root *root;
9687 struct list_head splice;
9688 int ret;
9689
Josef Bacik84961532021-10-05 16:35:25 -04009690 if (BTRFS_FS_ERROR(fs_info))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009691 return -EROFS;
9692
9693 INIT_LIST_HEAD(&splice);
9694
Miao Xie573bfb72014-03-06 13:55:03 +08009695 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009696 spin_lock(&fs_info->delalloc_root_lock);
9697 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009698 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009699 /*
9700 * Reset nr_to_write here so we know that we're doing a full
9701 * flush.
9702 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009703 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009704 wbc.nr_to_write = LONG_MAX;
9705
Miao Xieeb73c1b2013-05-15 07:48:22 +00009706 root = list_first_entry(&splice, struct btrfs_root,
9707 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009708 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009709 BUG_ON(!root);
9710 list_move_tail(&root->delalloc_root,
9711 &fs_info->delalloc_roots);
9712 spin_unlock(&fs_info->delalloc_root_lock);
9713
Josef Bacike076ab2a2021-01-07 17:08:30 -05009714 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009715 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009716 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009717 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009718 spin_lock(&fs_info->delalloc_root_lock);
9719 }
9720 spin_unlock(&fs_info->delalloc_root_lock);
9721
Miao Xie6c255e62014-03-06 13:55:01 +08009722 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009723out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009724 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009725 spin_lock(&fs_info->delalloc_root_lock);
9726 list_splice_tail(&splice, &fs_info->delalloc_roots);
9727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009728 }
Miao Xie573bfb72014-03-06 13:55:03 +08009729 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009730 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009731}
9732
Christian Brauner549c7292021-01-21 14:19:43 +01009733static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9734 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009735{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009736 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009737 struct btrfs_trans_handle *trans;
9738 struct btrfs_root *root = BTRFS_I(dir)->root;
9739 struct btrfs_path *path;
9740 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009741 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009742 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009743 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309744 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009745 int name_len;
9746 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009747 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009748 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009749 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009750
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009751 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009752 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009753 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009754
Josef Bacik9ed74f22009-09-11 16:12:44 -04009755 /*
9756 * 2 items for inode item and ref
9757 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009758 * 1 item for updating parent inode item
9759 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009760 * 1 item for xattr if selinux is on
9761 */
Filipe Manana9269d122015-12-31 18:16:29 +00009762 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009763 if (IS_ERR(trans))
9764 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009765
Nikolay Borisov543068a2020-12-07 17:32:33 +02009766 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009767 if (err)
9768 goto out_unlock;
9769
Christian Brauner5a052102021-07-27 12:48:47 +02009770 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02009771 dentry->d_name.name, dentry->d_name.len,
9772 btrfs_ino(BTRFS_I(dir)), objectid,
9773 S_IFLNK | S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009774 if (IS_ERR(inode)) {
9775 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009776 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009777 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009778 }
Chris Mason39279cc2007-06-12 06:35:45 -04009779
Casey Schauflerad19db72011-12-15 10:09:07 -05009780 /*
9781 * If the active LSM wants to access the inode during
9782 * d_instantiate it needs these. Smack checks to see
9783 * if the filesystem supports xattrs by looking at the
9784 * ops vector.
9785 */
9786 inode->i_fop = &btrfs_file_operations;
9787 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009788 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009789
9790 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9791 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009792 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009793
Chris Mason39279cc2007-06-12 06:35:45 -04009794 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009795 if (!path) {
9796 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009797 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009798 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009799 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009800 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009801 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009802 datasize = btrfs_file_extent_calc_inline_size(name_len);
9803 err = btrfs_insert_empty_item(trans, root, path, &key,
9804 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009805 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009806 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009807 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009808 }
Chris Mason5f39d392007-10-15 16:14:19 -04009809 leaf = path->nodes[0];
9810 ei = btrfs_item_ptr(leaf, path->slots[0],
9811 struct btrfs_file_extent_item);
9812 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9813 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009814 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009815 btrfs_set_file_extent_encryption(leaf, ei, 0);
9816 btrfs_set_file_extent_compression(leaf, ei, 0);
9817 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9818 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9819
Chris Mason39279cc2007-06-12 06:35:45 -04009820 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009821 write_extent_buffer(leaf, symname, ptr, name_len);
9822 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009823 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009824
Chris Mason39279cc2007-06-12 06:35:45 -04009825 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009826 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009827 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009828 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009829 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009830 /*
9831 * Last step, add directory indexes for our symlink inode. This is the
9832 * last step to avoid extra cleanup of these indexes if an error happens
9833 * elsewhere above.
9834 */
9835 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009836 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9837 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009838 if (err)
9839 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009840
Al Viro1e2e5472018-05-04 08:23:01 -04009841 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009842
9843out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009844 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009845 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009846 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009847 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009848 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009849 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009850 return err;
9851}
Chris Mason16432982008-04-10 10:23:21 -04009852
Filipe Manana8fccebf2020-09-08 11:27:20 +01009853static struct btrfs_trans_handle *insert_prealloc_file_extent(
9854 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009855 struct btrfs_inode *inode,
9856 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009857 u64 file_offset)
9858{
9859 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009860 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009861 struct btrfs_trans_handle *trans = trans_in;
9862 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009863 u64 start = ins->objectid;
9864 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009865 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009866 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009867
9868 memset(&stack_fi, 0, sizeof(stack_fi));
9869
9870 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9871 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9872 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9873 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9874 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9875 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9876 /* Encryption and other encoding is reserved and all 0 */
9877
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009878 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9879 if (qgroup_released < 0)
9880 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009881
9882 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009883 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009884 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009885 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009886 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009887 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009888 return trans;
9889 }
9890
9891 extent_info.disk_offset = start;
9892 extent_info.disk_len = len;
9893 extent_info.data_offset = 0;
9894 extent_info.data_len = len;
9895 extent_info.file_offset = file_offset;
9896 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009897 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009898 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009899 extent_info.insertions = 0;
9900
9901 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009902 if (!path) {
9903 ret = -ENOMEM;
9904 goto free_qgroup;
9905 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009906
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009907 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009908 file_offset + len - 1, &extent_info,
9909 &trans);
9910 btrfs_free_path(path);
9911 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009912 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009913 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009914
9915free_qgroup:
9916 /*
9917 * We have released qgroup data range at the beginning of the function,
9918 * and normally qgroup_released bytes will be freed when committing
9919 * transaction.
9920 * But if we error out early, we have to free what we have released
9921 * or we leak qgroup data reservation.
9922 */
9923 btrfs_qgroup_free_refroot(inode->root->fs_info,
9924 inode->root->root_key.objectid, qgroup_released,
9925 BTRFS_QGROUP_RSV_DATA);
9926 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009927}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009928
Josef Bacik0af3d002010-06-21 14:48:16 -04009929static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9930 u64 start, u64 num_bytes, u64 min_size,
9931 loff_t actual_len, u64 *alloc_hint,
9932 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009935 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9936 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009937 struct btrfs_root *root = BTRFS_I(inode)->root;
9938 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009939 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009940 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009941 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009942 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009943 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009944 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009945 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009946 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009947
Josef Bacik0af3d002010-06-21 14:48:16 -04009948 if (trans)
9949 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009950 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009951 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009952 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009953 /*
9954 * If we are severely fragmented we could end up with really
9955 * small allocations, so if the allocator is returning small
9956 * chunks lets make its job easier by only searching for those
9957 * sized chunks.
9958 */
9959 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009960 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9961 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009962 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009963 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009964
9965 /*
9966 * We've reserved this space, and thus converted it from
9967 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9968 * from here on out we will only need to clear our reservation
9969 * for the remaining unreserved area, so advance our
9970 * clear_offset by our extent size.
9971 */
9972 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009973
Josef Bacik0b670dc2015-09-23 17:11:16 -04009974 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009975 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9976 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009977 /*
9978 * Now that we inserted the prealloc extent we can finally
9979 * decrement the number of reservations in the block group.
9980 * If we did it before, we could race with relocation and have
9981 * relocation miss the reserved extent, making it fail later.
9982 */
9983 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009984 if (IS_ERR(trans)) {
9985 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009986 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009987 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009988 break;
9989 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009990
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009991 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009992 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009993
Josef Bacik5dc562c2012-08-17 13:14:17 -04009994 em = alloc_extent_map();
9995 if (!em) {
9996 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9997 &BTRFS_I(inode)->runtime_flags);
9998 goto next;
9999 }
10000
10001 em->start = cur_offset;
10002 em->orig_start = cur_offset;
10003 em->len = ins.offset;
10004 em->block_start = ins.objectid;
10005 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010006 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010007 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010008 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10009 em->generation = trans->transid;
10010
10011 while (1) {
10012 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010013 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010014 write_unlock(&em_tree->lock);
10015 if (ret != -EEXIST)
10016 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010017 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010018 cur_offset + ins.offset - 1,
10019 0);
10020 }
10021 free_extent_map(em);
10022next:
Yan Zhengd899e052008-10-30 14:25:28 -040010023 num_bytes -= ins.offset;
10024 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010025 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010026
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010027 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010028 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010029 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010030 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010031 (actual_len > inode->i_size) &&
10032 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010033 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010034 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010035 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010036 i_size = cur_offset;
10037 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010038 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010039 }
10040
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010041 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010042
10043 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010044 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010045 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010046 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010047 break;
10048 }
Yan Zhengd899e052008-10-30 14:25:28 -040010049
Filipe Manana8fccebf2020-09-08 11:27:20 +010010050 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010051 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010052 trans = NULL;
10053 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010054 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010055 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010056 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010057 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010058 return ret;
10059}
10060
Josef Bacik0af3d002010-06-21 14:48:16 -040010061int btrfs_prealloc_file_range(struct inode *inode, int mode,
10062 u64 start, u64 num_bytes, u64 min_size,
10063 loff_t actual_len, u64 *alloc_hint)
10064{
10065 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10066 min_size, actual_len, alloc_hint,
10067 NULL);
10068}
10069
10070int btrfs_prealloc_file_range_trans(struct inode *inode,
10071 struct btrfs_trans_handle *trans, int mode,
10072 u64 start, u64 num_bytes, u64 min_size,
10073 loff_t actual_len, u64 *alloc_hint)
10074{
10075 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10076 min_size, actual_len, alloc_hint, trans);
10077}
10078
Chris Masone6dcd2d2008-07-17 12:53:50 -040010079static int btrfs_set_page_dirty(struct page *page)
10080{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010081 return __set_page_dirty_nobuffers(page);
10082}
10083
Christian Brauner549c7292021-01-21 14:19:43 +010010084static int btrfs_permission(struct user_namespace *mnt_userns,
10085 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010086{
Li Zefanb83cc962010-12-20 16:04:08 +080010087 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010088 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010089
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010090 if (mask & MAY_WRITE &&
10091 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10092 if (btrfs_root_readonly(root))
10093 return -EROFS;
10094 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10095 return -EACCES;
10096 }
Christian Brauner3bc71ba2021-07-27 12:48:50 +020010097 return generic_permission(mnt_userns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010098}
Chris Mason39279cc2007-06-12 06:35:45 -040010099
Christian Brauner549c7292021-01-21 14:19:43 +010010100static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10101 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010102{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010103 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010104 struct btrfs_trans_handle *trans;
10105 struct btrfs_root *root = BTRFS_I(dir)->root;
10106 struct inode *inode = NULL;
10107 u64 objectid;
10108 u64 index;
10109 int ret = 0;
10110
10111 /*
10112 * 5 units required for adding orphan entry
10113 */
10114 trans = btrfs_start_transaction(root, 5);
10115 if (IS_ERR(trans))
10116 return PTR_ERR(trans);
10117
Nikolay Borisov543068a2020-12-07 17:32:33 +020010118 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010119 if (ret)
10120 goto out;
10121
Christian Brauner98b6ab52021-07-27 12:48:48 +020010122 inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010123 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010124 if (IS_ERR(inode)) {
10125 ret = PTR_ERR(inode);
10126 inode = NULL;
10127 goto out;
10128 }
10129
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010130 inode->i_fop = &btrfs_file_operations;
10131 inode->i_op = &btrfs_file_inode_operations;
10132
10133 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010134
Chris Masonb0d5d102014-09-08 13:08:51 -070010135 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10136 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010137 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010138
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010139 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010140 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010141 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010142 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010143 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010144 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010145
Filipe Manana5762b5c2014-08-01 00:10:32 +010010146 /*
10147 * We set number of links to 0 in btrfs_new_inode(), and here we set
10148 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10149 * through:
10150 *
10151 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10152 */
10153 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010154 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010155 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010156 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010157out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010158 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010159 if (ret && inode)
10160 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010161 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010162 return ret;
10163}
10164
Qu Wenruod2a91062021-05-31 16:50:49 +080010165void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010166{
Qu Wenruod2a91062021-05-31 16:50:49 +080010167 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010168 unsigned long index = start >> PAGE_SHIFT;
10169 unsigned long end_index = end >> PAGE_SHIFT;
10170 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010171 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010172
Qu Wenruod2a91062021-05-31 16:50:49 +080010173 ASSERT(end + 1 - start <= U32_MAX);
10174 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010175 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010176 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010177 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010178
10179 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010180 put_page(page);
10181 index++;
10182 }
10183}
10184
Omar Sandovaled46ff32016-11-03 10:28:14 -070010185#ifdef CONFIG_SWAP
10186/*
10187 * Add an entry indicating a block group or device which is pinned by a
10188 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10189 * negative errno on failure.
10190 */
10191static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10192 bool is_block_group)
10193{
10194 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10195 struct btrfs_swapfile_pin *sp, *entry;
10196 struct rb_node **p;
10197 struct rb_node *parent = NULL;
10198
10199 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10200 if (!sp)
10201 return -ENOMEM;
10202 sp->ptr = ptr;
10203 sp->inode = inode;
10204 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010205 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010206
10207 spin_lock(&fs_info->swapfile_pins_lock);
10208 p = &fs_info->swapfile_pins.rb_node;
10209 while (*p) {
10210 parent = *p;
10211 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10212 if (sp->ptr < entry->ptr ||
10213 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10214 p = &(*p)->rb_left;
10215 } else if (sp->ptr > entry->ptr ||
10216 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10217 p = &(*p)->rb_right;
10218 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010219 if (is_block_group)
10220 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010221 spin_unlock(&fs_info->swapfile_pins_lock);
10222 kfree(sp);
10223 return 1;
10224 }
10225 }
10226 rb_link_node(&sp->node, parent, p);
10227 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10228 spin_unlock(&fs_info->swapfile_pins_lock);
10229 return 0;
10230}
10231
10232/* Free all of the entries pinned by this swapfile. */
10233static void btrfs_free_swapfile_pins(struct inode *inode)
10234{
10235 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10236 struct btrfs_swapfile_pin *sp;
10237 struct rb_node *node, *next;
10238
10239 spin_lock(&fs_info->swapfile_pins_lock);
10240 node = rb_first(&fs_info->swapfile_pins);
10241 while (node) {
10242 next = rb_next(node);
10243 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10244 if (sp->inode == inode) {
10245 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010246 if (sp->is_block_group) {
10247 btrfs_dec_block_group_swap_extents(sp->ptr,
10248 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010249 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010250 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010251 kfree(sp);
10252 }
10253 node = next;
10254 }
10255 spin_unlock(&fs_info->swapfile_pins_lock);
10256}
10257
10258struct btrfs_swap_info {
10259 u64 start;
10260 u64 block_start;
10261 u64 block_len;
10262 u64 lowest_ppage;
10263 u64 highest_ppage;
10264 unsigned long nr_pages;
10265 int nr_extents;
10266};
10267
10268static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10269 struct btrfs_swap_info *bsi)
10270{
10271 unsigned long nr_pages;
Filipe Mananac2f82262021-12-16 15:00:32 +000010272 unsigned long max_pages;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010273 u64 first_ppage, first_ppage_reported, next_ppage;
10274 int ret;
10275
Filipe Mananac2f82262021-12-16 15:00:32 +000010276 /*
10277 * Our swapfile may have had its size extended after the swap header was
10278 * written. In that case activating the swapfile should not go beyond
10279 * the max size set in the swap header.
10280 */
10281 if (bsi->nr_pages >= sis->max)
10282 return 0;
10283
10284 max_pages = sis->max - bsi->nr_pages;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010285 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10286 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10287 PAGE_SIZE) >> PAGE_SHIFT;
10288
10289 if (first_ppage >= next_ppage)
10290 return 0;
10291 nr_pages = next_ppage - first_ppage;
Filipe Mananac2f82262021-12-16 15:00:32 +000010292 nr_pages = min(nr_pages, max_pages);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010293
10294 first_ppage_reported = first_ppage;
10295 if (bsi->start == 0)
10296 first_ppage_reported++;
10297 if (bsi->lowest_ppage > first_ppage_reported)
10298 bsi->lowest_ppage = first_ppage_reported;
10299 if (bsi->highest_ppage < (next_ppage - 1))
10300 bsi->highest_ppage = next_ppage - 1;
10301
10302 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10303 if (ret < 0)
10304 return ret;
10305 bsi->nr_extents += ret;
10306 bsi->nr_pages += nr_pages;
10307 return 0;
10308}
10309
10310static void btrfs_swap_deactivate(struct file *file)
10311{
10312 struct inode *inode = file_inode(file);
10313
10314 btrfs_free_swapfile_pins(inode);
10315 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10316}
10317
10318static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10319 sector_t *span)
10320{
10321 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010322 struct btrfs_root *root = BTRFS_I(inode)->root;
10323 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010324 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10325 struct extent_state *cached_state = NULL;
10326 struct extent_map *em = NULL;
10327 struct btrfs_device *device = NULL;
10328 struct btrfs_swap_info bsi = {
10329 .lowest_ppage = (sector_t)-1ULL,
10330 };
10331 int ret = 0;
10332 u64 isize;
10333 u64 start;
10334
10335 /*
10336 * If the swap file was just created, make sure delalloc is done. If the
10337 * file changes again after this, the user is doing something stupid and
10338 * we don't really care.
10339 */
10340 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10341 if (ret)
10342 return ret;
10343
10344 /*
10345 * The inode is locked, so these flags won't change after we check them.
10346 */
10347 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10348 btrfs_warn(fs_info, "swapfile must not be compressed");
10349 return -EINVAL;
10350 }
10351 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10352 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10353 return -EINVAL;
10354 }
10355 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10356 btrfs_warn(fs_info, "swapfile must not be checksummed");
10357 return -EINVAL;
10358 }
10359
10360 /*
10361 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010362 * under us. The exclop protection makes sure they aren't running/won't
10363 * run concurrently while we are mapping the swap extents, and
10364 * fs_info->swapfile_pins prevents them from running while the swap
10365 * file is active and moving the extents. Note that this also prevents
10366 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010367 * really worth the trouble to allow it.
10368 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010369 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010370 btrfs_warn(fs_info,
10371 "cannot activate swapfile while exclusive operation is running");
10372 return -EBUSY;
10373 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010374
10375 /*
10376 * Prevent snapshot creation while we are activating the swap file.
10377 * We do not want to race with snapshot creation. If snapshot creation
10378 * already started before we bumped nr_swapfiles from 0 to 1 and
10379 * completes before the first write into the swap file after it is
10380 * activated, than that write would fallback to COW.
10381 */
10382 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10383 btrfs_exclop_finish(fs_info);
10384 btrfs_warn(fs_info,
10385 "cannot activate swapfile because snapshot creation is in progress");
10386 return -EINVAL;
10387 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010388 /*
10389 * Snapshots can create extents which require COW even if NODATACOW is
10390 * set. We use this counter to prevent snapshots. We must increment it
10391 * before walking the extents because we don't want a concurrent
10392 * snapshot to run after we've already checked the extents.
10393 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010394 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010395
10396 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10397
10398 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10399 start = 0;
10400 while (start < isize) {
10401 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010402 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010403 u64 len = isize - start;
10404
Omar Sandoval39b07b52019-12-02 17:34:23 -080010405 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010406 if (IS_ERR(em)) {
10407 ret = PTR_ERR(em);
10408 goto out;
10409 }
10410
10411 if (em->block_start == EXTENT_MAP_HOLE) {
10412 btrfs_warn(fs_info, "swapfile must not have holes");
10413 ret = -EINVAL;
10414 goto out;
10415 }
10416 if (em->block_start == EXTENT_MAP_INLINE) {
10417 /*
10418 * It's unlikely we'll ever actually find ourselves
10419 * here, as a file small enough to fit inline won't be
10420 * big enough to store more than the swap header, but in
10421 * case something changes in the future, let's catch it
10422 * here rather than later.
10423 */
10424 btrfs_warn(fs_info, "swapfile must not be inline");
10425 ret = -EINVAL;
10426 goto out;
10427 }
10428 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10429 btrfs_warn(fs_info, "swapfile must not be compressed");
10430 ret = -EINVAL;
10431 goto out;
10432 }
10433
10434 logical_block_start = em->block_start + (start - em->start);
10435 len = min(len, em->len - (start - em->start));
10436 free_extent_map(em);
10437 em = NULL;
10438
Boris Burkova84d5d42020-08-18 11:00:05 -070010439 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010440 if (ret < 0) {
10441 goto out;
10442 } else if (ret) {
10443 ret = 0;
10444 } else {
10445 btrfs_warn(fs_info,
10446 "swapfile must not be copy-on-write");
10447 ret = -EINVAL;
10448 goto out;
10449 }
10450
10451 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10452 if (IS_ERR(em)) {
10453 ret = PTR_ERR(em);
10454 goto out;
10455 }
10456
10457 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10458 btrfs_warn(fs_info,
10459 "swapfile must have single data profile");
10460 ret = -EINVAL;
10461 goto out;
10462 }
10463
10464 if (device == NULL) {
10465 device = em->map_lookup->stripes[0].dev;
10466 ret = btrfs_add_swapfile_pin(inode, device, false);
10467 if (ret == 1)
10468 ret = 0;
10469 else if (ret)
10470 goto out;
10471 } else if (device != em->map_lookup->stripes[0].dev) {
10472 btrfs_warn(fs_info, "swapfile must be on one device");
10473 ret = -EINVAL;
10474 goto out;
10475 }
10476
10477 physical_block_start = (em->map_lookup->stripes[0].physical +
10478 (logical_block_start - em->start));
10479 len = min(len, em->len - (logical_block_start - em->start));
10480 free_extent_map(em);
10481 em = NULL;
10482
10483 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10484 if (!bg) {
10485 btrfs_warn(fs_info,
10486 "could not find block group containing swapfile");
10487 ret = -EINVAL;
10488 goto out;
10489 }
10490
Filipe Manana195a49e2021-02-05 12:55:37 +000010491 if (!btrfs_inc_block_group_swap_extents(bg)) {
10492 btrfs_warn(fs_info,
10493 "block group for swapfile at %llu is read-only%s",
10494 bg->start,
10495 atomic_read(&fs_info->scrubs_running) ?
10496 " (scrub running)" : "");
10497 btrfs_put_block_group(bg);
10498 ret = -EINVAL;
10499 goto out;
10500 }
10501
Omar Sandovaled46ff32016-11-03 10:28:14 -070010502 ret = btrfs_add_swapfile_pin(inode, bg, true);
10503 if (ret) {
10504 btrfs_put_block_group(bg);
10505 if (ret == 1)
10506 ret = 0;
10507 else
10508 goto out;
10509 }
10510
10511 if (bsi.block_len &&
10512 bsi.block_start + bsi.block_len == physical_block_start) {
10513 bsi.block_len += len;
10514 } else {
10515 if (bsi.block_len) {
10516 ret = btrfs_add_swap_extent(sis, &bsi);
10517 if (ret)
10518 goto out;
10519 }
10520 bsi.start = start;
10521 bsi.block_start = physical_block_start;
10522 bsi.block_len = len;
10523 }
10524
10525 start += len;
10526 }
10527
10528 if (bsi.block_len)
10529 ret = btrfs_add_swap_extent(sis, &bsi);
10530
10531out:
10532 if (!IS_ERR_OR_NULL(em))
10533 free_extent_map(em);
10534
10535 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10536
10537 if (ret)
10538 btrfs_swap_deactivate(file);
10539
Filipe Mananadd0734f2021-02-05 12:55:38 +000010540 btrfs_drew_write_unlock(&root->snapshot_lock);
10541
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010542 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010543
10544 if (ret)
10545 return ret;
10546
10547 if (device)
10548 sis->bdev = device->bdev;
10549 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10550 sis->max = bsi.nr_pages;
10551 sis->pages = bsi.nr_pages - 1;
10552 sis->highest_bit = bsi.nr_pages - 1;
10553 return bsi.nr_extents;
10554}
10555#else
10556static void btrfs_swap_deactivate(struct file *file)
10557{
10558}
10559
10560static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10561 sector_t *span)
10562{
10563 return -EOPNOTSUPP;
10564}
10565#endif
10566
Filipe Manana2766ff62020-11-04 11:07:34 +000010567/*
10568 * Update the number of bytes used in the VFS' inode. When we replace extents in
10569 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10570 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10571 * always get a correct value.
10572 */
10573void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10574 const u64 add_bytes,
10575 const u64 del_bytes)
10576{
10577 if (add_bytes == del_bytes)
10578 return;
10579
10580 spin_lock(&inode->lock);
10581 if (del_bytes > 0)
10582 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10583 if (add_bytes > 0)
10584 inode_add_bytes(&inode->vfs_inode, add_bytes);
10585 spin_unlock(&inode->lock);
10586}
10587
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010588static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010589 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .lookup = btrfs_lookup,
10591 .create = btrfs_create,
10592 .unlink = btrfs_unlink,
10593 .link = btrfs_link,
10594 .mkdir = btrfs_mkdir,
10595 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010596 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010597 .symlink = btrfs_symlink,
10598 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010599 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010600 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010601 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010602 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010603 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010604 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010605 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010606 .fileattr_get = btrfs_fileattr_get,
10607 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010608};
Yan, Zheng76dda932009-09-21 16:00:26 -040010609
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010610static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010611 .llseek = generic_file_llseek,
10612 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010613 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010614 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010615 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010616#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010617 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010618#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010619 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010620 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010621};
10622
Chris Mason35054392009-01-21 13:11:13 -050010623/*
10624 * btrfs doesn't support the bmap operation because swapfiles
10625 * use bmap to make a mapping of extents in the file. They assume
10626 * these extents won't change over the life of the file and they
10627 * use the bmap result to do IO directly to the drive.
10628 *
10629 * the btrfs bmap call would return logical addresses that aren't
10630 * suitable for IO and they also will change frequently as COW
10631 * operations happen. So, swapfile + btrfs == corruption.
10632 *
10633 * For now we're avoiding this by dropping bmap.
10634 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010635static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010636 .readpage = btrfs_readpage,
10637 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010638 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010639 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010640 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010641 .invalidatepage = btrfs_invalidatepage,
10642 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010643#ifdef CONFIG_MIGRATION
10644 .migratepage = btrfs_migratepage,
10645#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010646 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010647 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010648 .swap_activate = btrfs_swap_activate,
10649 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010650};
10651
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010652static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010653 .getattr = btrfs_getattr,
10654 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010655 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010656 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010657 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010658 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010659 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010660 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010661 .fileattr_get = btrfs_fileattr_get,
10662 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010663};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010664static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010665 .getattr = btrfs_getattr,
10666 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010667 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010668 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010669 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010670 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010671 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010672};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010673static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010674 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010675 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010676 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010677 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010678 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010679 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010680};
Yan, Zheng76dda932009-09-21 16:00:26 -040010681
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010682const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010683 .d_delete = btrfs_dentry_delete,
10684};