blob: c70ae0a5df92b419c624a156b65c99ecf48aa2f1 [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>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050033#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010052#include "space-info.h"
Naohiro Aotad8e3fb12021-02-04 19:22:05 +090053#include "zoned.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050062 loff_t length;
63 ssize_t submitted;
64 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000065};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030083static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050084 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030087static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
88 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080089 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030093static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080094 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050098 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
99 *
100 * ilock_flags can have the following bit set:
101 *
102 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
103 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
104 * return -EAGAIN
Josef Bacik8318ba72021-02-10 17:14:33 -0500105 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500106 */
107int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
108{
109 if (ilock_flags & BTRFS_ILOCK_SHARED) {
110 if (ilock_flags & BTRFS_ILOCK_TRY) {
111 if (!inode_trylock_shared(inode))
112 return -EAGAIN;
113 else
114 return 0;
115 }
116 inode_lock_shared(inode);
117 } else {
118 if (ilock_flags & BTRFS_ILOCK_TRY) {
119 if (!inode_trylock(inode))
120 return -EAGAIN;
121 else
122 return 0;
123 }
124 inode_lock(inode);
125 }
Josef Bacik8318ba72021-02-10 17:14:33 -0500126 if (ilock_flags & BTRFS_ILOCK_MMAP)
127 down_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500128 return 0;
129}
130
131/*
132 * btrfs_inode_unlock - unock inode i_rwsem
133 *
134 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
135 * to decide whether the lock acquired is shared or exclusive.
136 */
137void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
138{
Josef Bacik8318ba72021-02-10 17:14:33 -0500139 if (ilock_flags & BTRFS_ILOCK_MMAP)
140 up_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500141 if (ilock_flags & BTRFS_ILOCK_SHARED)
142 inode_unlock_shared(inode);
143 else
144 inode_unlock(inode);
145}
146
147/*
Qu Wenruo524272602017-03-08 10:25:52 +0800148 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100149 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800150 *
151 * NOTE: caller must ensure that when an error happens, it can not call
152 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
153 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
154 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200155 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800156 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300157static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200158 struct page *locked_page,
159 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800160{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900161 unsigned long index = offset >> PAGE_SHIFT;
162 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200163 u64 page_start = page_offset(locked_page);
164 u64 page_end = page_start + PAGE_SIZE - 1;
165
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900166 struct page *page;
167
168 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300169 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900170 index++;
171 if (!page)
172 continue;
173 ClearPagePrivate2(page);
174 put_page(page);
175 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200176
177 /*
178 * In case this page belongs to the delalloc range being instantiated
179 * then skip it, since the first page of a range is going to be
180 * properly cleaned up by the caller of run_delalloc_range
181 */
182 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
183 offset += PAGE_SIZE;
184 bytes -= PAGE_SIZE;
185 }
186
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300187 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800188}
189
Eric Sandeen48a3b632013-04-25 20:41:01 +0000190static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400191
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000192static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500193 struct inode *inode, struct inode *dir,
194 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500195{
196 int err;
197
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000198 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500199 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500200 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500201 return err;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/*
Chris Masonc8b97812008-10-29 14:49:59 -0400205 * this does all the hard work for inserting an inline extent into
206 * the btree. The caller should have done a btrfs_drop_extents so that
207 * no overlapping inline items exist in the btree
208 */
Chris Mason40f76582014-05-21 13:35:51 -0700209static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000210 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct btrfs_root *root, struct inode *inode,
212 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000213 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400214 struct page **compressed_pages)
215{
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct extent_buffer *leaf;
217 struct page *page = NULL;
218 char *kaddr;
219 unsigned long ptr;
220 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400221 int ret;
222 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400223 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400224
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800225 ASSERT((compressed_size > 0 && compressed_pages) ||
226 (compressed_size == 0 && !compressed_pages));
227
Li Zefanfe3f5662011-03-28 08:30:38 +0000228 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400229 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400230
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000231 if (!extent_inserted) {
232 struct btrfs_key key;
233 size_t datasize;
234
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200235 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000236 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200237 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000238
239 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000240 ret = btrfs_insert_empty_item(trans, root, path, &key,
241 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200242 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400244 }
245 leaf = path->nodes[0];
246 ei = btrfs_item_ptr(leaf, path->slots[0],
247 struct btrfs_file_extent_item);
248 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
249 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
250 btrfs_set_file_extent_encryption(leaf, ei, 0);
251 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
252 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
253 ptr = btrfs_file_extent_inline_start(ei);
254
Li Zefan261507a02010-12-17 14:21:50 +0800255 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400256 struct page *cpage;
257 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500258 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400259 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500260 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300261 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400262
Cong Wang7ac687d2011-11-25 23:14:28 +0800263 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400264 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800265 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400266
267 i++;
268 ptr += cur_size;
269 compressed_size -= cur_size;
270 }
271 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800272 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400273 } else {
274 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300275 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400276 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800277 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100278 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400279 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800280 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300281 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400282 }
283 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000284 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400285
Yan, Zhengc2167752009-11-12 09:34:21 +0000286 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500287 * We align size to sectorsize for inline extents just for simplicity
288 * sake.
289 */
290 size = ALIGN(size, root->fs_info->sectorsize);
291 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
292 if (ret)
293 goto fail;
294
295 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000296 * we're an inline extent, so nobody can
297 * extend the file past i_size without locking
298 * a page we already have locked.
299 *
300 * We must do any isize and inode updates
301 * before we unlock the pages. Otherwise we
302 * could end up racing with unlink.
303 */
Chris Masonc8b97812008-10-29 14:49:59 -0400304 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400305fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200306 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400307}
308
309
310/*
311 * conditionally insert an inline extent into the file. This
312 * does the checks required to make sure the data is small enough
313 * to fit as an inline extent.
314 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300315static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400316 u64 end, size_t compressed_size,
317 int compress_type,
318 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400319{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000320 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300321 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400322 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400323 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300324 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400325 u64 actual_end = min(end + 1, isize);
326 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400327 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400328 u64 data_len = inline_len;
329 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000330 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400331
332 if (compressed_size)
333 data_len = compressed_size;
334
335 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400336 actual_end > fs_info->sectorsize ||
337 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400338 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400339 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400340 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400341 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400342 return 1;
343 }
344
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000345 path = btrfs_alloc_path();
346 if (!path)
347 return -ENOMEM;
348
Josef Bacik00361582013-08-14 14:02:47 -0400349 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000350 if (IS_ERR(trans)) {
351 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400352 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000353 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300354 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400355
Filipe Manana5893dfb2020-11-04 11:07:32 +0000356 drop_args.path = path;
357 drop_args.start = start;
358 drop_args.end = aligned_end;
359 drop_args.drop_cache = true;
360 drop_args.replace_extent = true;
361
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000362 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000363 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000364 compressed_size);
365 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000366 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000367 inline_len);
368
Filipe Manana5893dfb2020-11-04 11:07:32 +0000369 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400370 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400371 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400372 goto out;
373 }
Chris Masonc8b97812008-10-29 14:49:59 -0400374
375 if (isize > actual_end)
376 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000377 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300378 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400379 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000380 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400381 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400382 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400383 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400384 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400385 ret = 1;
386 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100387 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400388
Filipe Manana2766ff62020-11-04 11:07:34 +0000389 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200390 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000391 if (ret && ret != -ENOSPC) {
392 btrfs_abort_transaction(trans, ret);
393 goto out;
394 } else if (ret == -ENOSPC) {
395 ret = 1;
396 goto out;
397 }
398
Nikolay Borisova0349402020-06-03 08:55:12 +0300399 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400400out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800401 /*
402 * Don't forget to free the reserved space, as for inlined extent
403 * it won't count as data extent, free them directly here.
404 * And at reserve time, it's always aligned to page size, so
405 * just free one page here.
406 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300407 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000408 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400409 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400410 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400411}
412
Chris Mason771ed682008-11-06 22:02:51 -0500413struct async_extent {
414 u64 start;
415 u64 ram_size;
416 u64 compressed_size;
417 struct page **pages;
418 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800419 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500420 struct list_head list;
421};
422
Nikolay Borisov97db1202019-03-12 17:20:24 +0200423struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500424 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500425 struct page *locked_page;
426 u64 start;
427 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600428 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500429 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700430 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500431 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200432 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500433};
434
Nikolay Borisov97db1202019-03-12 17:20:24 +0200435struct async_cow {
436 /* Number of chunks in flight; must be first in the structure */
437 atomic_t num_chunks;
438 struct async_chunk chunks[];
439};
440
441static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500442 u64 start, u64 ram_size,
443 u64 compressed_size,
444 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800445 unsigned long nr_pages,
446 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500447{
448 struct async_extent *async_extent;
449
450 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100451 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500452 async_extent->start = start;
453 async_extent->ram_size = ram_size;
454 async_extent->compressed_size = compressed_size;
455 async_extent->pages = pages;
456 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800457 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500458 list_add_tail(&async_extent->list, &cow->extents);
459 return 0;
460}
461
Qu Wenruo42c16da2019-07-01 05:12:46 +0000462/*
463 * Check if the inode has flags compatible with compression
464 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300465static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000466{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300467 if (inode->flags & BTRFS_INODE_NODATACOW ||
468 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000469 return false;
470 return true;
471}
472
473/*
474 * Check if the inode needs to be submitted to compression, based on mount
475 * options, defragmentation, properties or heuristics.
476 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300477static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
478 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800479{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300480 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800481
Nikolay Borisov808a1292020-06-03 08:55:27 +0300482 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000483 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
484 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300485 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000486 return 0;
487 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800488 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400489 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800490 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200491 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300492 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200493 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800494 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300495 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800496 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400497 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300498 inode->flags & BTRFS_INODE_COMPRESS ||
499 inode->prop_compress)
500 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800501 return 0;
502}
503
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200504static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800505 u64 start, u64 end, u64 num_bytes, u64 small_write)
506{
507 /* If this is a small write inside eof, kick off a defrag */
508 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200509 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800510 btrfs_add_inode_defrag(NULL, inode);
511}
512
Chris Masonc8b97812008-10-29 14:49:59 -0400513/*
Chris Mason771ed682008-11-06 22:02:51 -0500514 * we create compressed extents in two phases. The first
515 * phase compresses a range of pages that have already been
516 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400517 *
Chris Mason771ed682008-11-06 22:02:51 -0500518 * This is done inside an ordered work queue, and the compression
519 * is spread across many cpus. The actual IO submission is step
520 * two, and the ordered work queue takes care of making sure that
521 * happens in the same order things were put onto the queue by
522 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400523 *
Chris Mason771ed682008-11-06 22:02:51 -0500524 * If this code finds it can't get good compression, it puts an
525 * entry onto the work queue to write the uncompressed bytes. This
526 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300527 * are written in the same order that the flusher thread sent them
528 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400529 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300530static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400531{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200532 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400533 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400534 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200535 u64 start = async_chunk->start;
536 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400537 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400538 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400539 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400540 struct page **pages = NULL;
541 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400542 unsigned long total_compressed = 0;
543 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400544 int i;
545 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400546 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300547 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400548 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400549
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200550 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
551 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400552
Josef Bacikd98da492019-10-11 09:03:54 -0400553 /*
554 * We need to save i_size before now because it could change in between
555 * us evaluating the size and assigning it. This is because we lock and
556 * unlock the page in truncate and fallocate, and then modify the i_size
557 * later on.
558 *
559 * The barriers are to emulate READ_ONCE, remove that once i_size_read
560 * does that for us.
561 */
562 barrier();
563 i_size = i_size_read(inode);
564 barrier();
565 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400566again:
567 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300568 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100569 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
570 nr_pages = min_t(unsigned long, nr_pages,
571 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400572
Chris Masonf03d9301f2009-02-04 09:31:06 -0500573 /*
574 * we don't want to send crud past the end of i_size through
575 * compression, that's just a waste of CPU time. So, if the
576 * end of the file is before the start of our current
577 * requested range of bytes, we bail out to the uncompressed
578 * cleanup code that can deal with all of this.
579 *
580 * It isn't really the fastest way to fix things, but this is a
581 * very uncommon corner.
582 */
583 if (actual_end <= start)
584 goto cleanup_and_bail_uncompressed;
585
Chris Masonc8b97812008-10-29 14:49:59 -0400586 total_compressed = actual_end - start;
587
Shilong Wang4bcbb332014-10-07 18:44:35 -0400588 /*
589 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400590 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400591 */
592 if (total_compressed <= blocksize &&
593 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
594 goto cleanup_and_bail_uncompressed;
595
David Sterba069eac72017-02-14 19:36:54 +0100596 total_compressed = min_t(unsigned long, total_compressed,
597 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 total_in = 0;
599 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400600
Chris Mason771ed682008-11-06 22:02:51 -0500601 /*
602 * we do compression for mount -o compress and when the
603 * inode has not been flagged as nocompress. This flag can
604 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400605 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300606 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400607 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100608 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800609 if (!pages) {
610 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100611 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800612 goto cont;
613 }
Chris Mason179e29e2007-11-01 11:28:41 -0400614
David Sterbaeec63c62017-07-17 19:41:31 +0200615 if (BTRFS_I(inode)->defrag_compress)
616 compress_type = BTRFS_I(inode)->defrag_compress;
617 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200618 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800619
Chris Mason4adaa612013-03-26 13:07:00 -0400620 /*
621 * we need to call clear_page_dirty_for_io on each
622 * page in the range. Otherwise applications with the file
623 * mmap'd can wander in and change the page contents while
624 * we are compressing them.
625 *
626 * If the compression fails for any reason, we set the pages
627 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300628 *
629 * Note that the remaining part is redirtied, the start pointer
630 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400631 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300632 if (!redirty) {
633 extent_range_clear_dirty_for_io(inode, start, end);
634 redirty = 1;
635 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200636
637 /* Compression level is applied here and only here */
638 ret = btrfs_compress_pages(
639 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800640 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100641 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800643 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100644 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400645
646 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100647 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100648 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400649
650 /* zero the tail end of the last page, we might be
651 * sending it down to disk
652 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700653 if (offset)
654 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400655 will_compress = 1;
656 }
657 }
Li Zefan560f7d72011-09-08 10:22:01 +0800658cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400659 if (start == 0) {
660 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300661 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400662 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500663 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400664 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300665 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
666 0, BTRFS_COMPRESS_NONE,
667 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400668 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500669 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300670 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000671 total_compressed,
672 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100674 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400675 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400676 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
677 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100678 unsigned long page_error_op;
679
Filipe Mananae6eb4312014-10-10 10:45:12 +0100680 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400681
Chris Mason771ed682008-11-06 22:02:51 -0500682 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100683 * inline extent creation worked or returned error,
684 * we don't need to create any more async work items.
685 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400686 *
687 * We use DO_ACCOUNTING here because we need the
688 * delalloc_release_metadata to be done _after_ we drop
689 * our outstanding extent for clearing delalloc for this
690 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500691 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300692 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
693 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300694 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800695 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800696 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100697 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400698 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300699
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800700 /*
701 * Ensure we only free the compressed pages if we have
702 * them allocated, as we can still reach here with
703 * inode_need_compress() == false.
704 */
705 if (pages) {
706 for (i = 0; i < nr_pages; i++) {
707 WARN_ON(pages[i]->mapping);
708 put_page(pages[i]);
709 }
710 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300711 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300712 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400713 }
714 }
715
716 if (will_compress) {
717 /*
718 * we aren't doing an inline extent round the compressed size
719 * up to a block size boundary so the allocator does sane
720 * things
721 */
Qu Wenruofda28322013-02-26 08:10:22 +0000722 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400723
724 /*
725 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300726 * win, compare the page count read with the blocks on disk,
727 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400728 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300729 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300730 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300731 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732
733 /*
734 * The async work queues will take care of doing actual
735 * allocation on disk for these compressed pages, and
736 * will submit them to the elevator.
737 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100739 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 compress_type);
741
Timofey Titovets11708622017-10-03 18:06:01 +0300742 if (start + total_in < end) {
743 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700744 pages = NULL;
745 cond_resched();
746 goto again;
747 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300748 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400749 }
750 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700751 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400752 /*
753 * the compression code ran but failed to make things smaller,
754 * free any pages it allocated and our page pointer array
755 */
David Sterba4d3a8002017-02-14 19:04:07 +0100756 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400757 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300758 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400759 }
760 kfree(pages);
761 pages = NULL;
762 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100763 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400764
765 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200767 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500768 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500769 }
Chris Masonc8b97812008-10-29 14:49:59 -0400770 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500771cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700772 /*
773 * No compression, but we still need to write the pages in the file
774 * we've been given so far. redirty the locked page if it corresponds
775 * to our extent and set things up for the async work queue to run
776 * cow_file_range to do the normal delalloc dance.
777 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700778 if (async_chunk->locked_page &&
779 (page_offset(async_chunk->locked_page) >= start &&
780 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200781 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700782 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700783 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784
785 if (redirty)
786 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200787 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700788 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300789 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500790
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300791 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500792}
Chris Mason771ed682008-11-06 22:02:51 -0500793
Filipe Manana40ae8372014-10-06 22:14:24 +0100794static void free_async_extent_pages(struct async_extent *async_extent)
795{
796 int i;
797
798 if (!async_extent->pages)
799 return;
800
801 for (i = 0; i < async_extent->nr_pages; i++) {
802 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300803 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100804 }
805 kfree(async_extent->pages);
806 async_extent->nr_pages = 0;
807 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500808}
809
810/*
811 * phase two of compressed writeback. This is the ordered portion
812 * of the code, which only gets called in the order the work was
813 * queued. We walk all the async extents created by compress_file_range
814 * and send them down to the disk.
815 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200816static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500817{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300818 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
819 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct async_extent *async_extent;
821 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500822 struct btrfs_key ins;
823 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300824 struct btrfs_root *root = inode->root;
825 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500826 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500827
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200829 while (!list_empty(&async_chunk->extents)) {
830 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500831 struct async_extent, list);
832 list_del(&async_extent->list);
833
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500834retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200835 lock_extent(io_tree, async_extent->start,
836 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500837 /* did the compression code fall back to uncompressed IO? */
838 if (!async_extent->pages) {
839 int page_started = 0;
840 unsigned long nr_written = 0;
841
Chris Mason771ed682008-11-06 22:02:51 -0500842 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300843 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500844 async_extent->start,
845 async_extent->start +
846 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300847 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100849 /* JDM XXX */
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 /*
852 * if page_started, cow_file_range inserted an
853 * inline extent and took care of all the unlocking
854 * and IO for us. Otherwise, we need to submit
855 * all those pages down to the drive.
856 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500857 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300858 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200859 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500860 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500861 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500862 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700863 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200864 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500865 kfree(async_extent);
866 cond_resched();
867 continue;
868 }
869
Wang Xiaoguang18513092016-07-25 15:51:40 +0800870 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500871 async_extent->compressed_size,
872 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800873 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500874 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400877 if (ret == -ENOSPC) {
878 unlock_extent(io_tree, async_extent->start,
879 async_extent->start +
880 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800881
882 /*
883 * we need to redirty the pages if we decide to
884 * fallback to uncompressed IO, otherwise we
885 * will not submit these pages down to lower
886 * layers.
887 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300888 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800889 async_extent->start,
890 async_extent->start +
891 async_extent->ram_size - 1);
892
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400894 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500895 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500896 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000897 /*
898 * here we're doing allocation and writeback of the
899 * compressed pages
900 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300901 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800902 async_extent->ram_size, /* len */
903 async_extent->start, /* orig_start */
904 ins.objectid, /* block_start */
905 ins.offset, /* block_len */
906 ins.offset, /* orig_block_len */
907 async_extent->ram_size, /* ram_bytes */
908 async_extent->compress_type,
909 BTRFS_ORDERED_COMPRESSED);
910 if (IS_ERR(em))
911 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500912 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800913 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300915 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800916 async_extent->start,
917 ins.objectid,
918 async_extent->ram_size,
919 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800920 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100921 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300922 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100923 async_extent->start +
924 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500925 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100926 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400927 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500928
Chris Mason771ed682008-11-06 22:02:51 -0500929 /*
930 * clear dirty, set writeback and unlock the pages.
931 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300932 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400933 async_extent->start +
934 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400935 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800936 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300937 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500938 async_extent->ram_size,
939 ins.objectid,
940 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600941 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700942 async_chunk->write_flags,
943 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100944 struct page *p = async_extent->pages[0];
945 const u64 start = async_extent->start;
946 const u64 end = start + async_extent->ram_size - 1;
947
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300948 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200949 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200950
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100951 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300952 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100953 PAGE_END_WRITEBACK |
954 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100955 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957 alloc_hint = ins.objectid + ins.offset;
958 kfree(async_extent);
959 cond_resched();
960 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100961 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500962out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400963 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400964 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300966 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500967 async_extent->start +
968 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400969 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100970 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400971 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800972 PAGE_UNLOCK | PAGE_START_WRITEBACK |
973 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100974 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100975 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500976 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500977}
978
Nikolay Borisov43c69842020-06-03 08:55:02 +0300979static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400980 u64 num_bytes)
981{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300982 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400983 struct extent_map *em;
984 u64 alloc_hint = 0;
985
986 read_lock(&em_tree->lock);
987 em = search_extent_mapping(em_tree, start, num_bytes);
988 if (em) {
989 /*
990 * if block start isn't an actual block number then find the
991 * first block in this inode and use that as a hint. If that
992 * block is also bogus then just don't worry about it.
993 */
994 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
995 free_extent_map(em);
996 em = search_extent_mapping(em_tree, 0, 0);
997 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
998 alloc_hint = em->block_start;
999 if (em)
1000 free_extent_map(em);
1001 } else {
1002 alloc_hint = em->block_start;
1003 free_extent_map(em);
1004 }
1005 }
1006 read_unlock(&em_tree->lock);
1007
1008 return alloc_hint;
1009}
1010
Chris Mason771ed682008-11-06 22:02:51 -05001011/*
1012 * when extent_io.c finds a delayed allocation range in the file,
1013 * the call backs end up in this code. The basic idea is to
1014 * allocate extents on disk for the range, and create ordered data structs
1015 * in ram to track those extents.
1016 *
1017 * locked_page is the page that writepage had locked already. We use
1018 * it to make sure we don't do extra locks or unlocks.
1019 *
1020 * *page_started is set to one if we unlock locked_page and do everything
1021 * required to start IO on it. It may be clean and already done with
1022 * IO when we return.
1023 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001024static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001025 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001026 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001027 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001028{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001029 struct btrfs_root *root = inode->root;
1030 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001031 u64 alloc_hint = 0;
1032 u64 num_bytes;
1033 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001034 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001035 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001036 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001037 struct btrfs_key ins;
1038 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001039 unsigned clear_bits;
1040 unsigned long page_ops;
1041 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001042 int ret = 0;
1043
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001044 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001045 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001046 ret = -EINVAL;
1047 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001048 }
Chris Mason771ed682008-11-06 22:02:51 -05001049
Qu Wenruofda28322013-02-26 08:10:22 +00001050 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001051 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001052 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001053
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001054 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001055
Chris Mason771ed682008-11-06 22:02:51 -05001056 if (start == 0) {
1057 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001058 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001059 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001060 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001061 /*
1062 * We use DO_ACCOUNTING here because we need the
1063 * delalloc_release_metadata to be run _after_ we drop
1064 * our outstanding extent for clearing delalloc for this
1065 * range.
1066 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001067 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001068 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001069 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1070 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001071 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001072 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001074 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001075 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001078 }
1079 }
Chris Masonc8b97812008-10-29 14:49:59 -04001080
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001081 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1082 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001083
Filipe Manana432cd2a2020-06-08 13:32:55 +01001084 /*
1085 * Relocation relies on the relocated extents to have exactly the same
1086 * size as the original extents. Normally writeback for relocation data
1087 * extents follows a NOCOW path because relocation preallocates the
1088 * extents. However, due to an operation such as scrub turning a block
1089 * group to RO mode, it may fallback to COW mode, so we must make sure
1090 * an extent allocated during COW has exactly the requested size and can
1091 * not be split into smaller extents, otherwise relocation breaks and
1092 * fails during the stage where it updates the bytenr of file extent
1093 * items.
1094 */
1095 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1096 min_alloc_size = num_bytes;
1097 else
1098 min_alloc_size = fs_info->sectorsize;
1099
Anand Jain3752d222018-02-15 12:29:38 +08001100 while (num_bytes > 0) {
1101 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001102 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001103 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001104 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001105 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001107 cur_alloc_size = ins.offset;
1108 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001109
Chris Mason771ed682008-11-06 22:02:51 -05001110 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001111 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001112 start, /* orig_start */
1113 ins.objectid, /* block_start */
1114 ins.offset, /* block_len */
1115 ins.offset, /* orig_block_len */
1116 ram_size, /* ram_bytes */
1117 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001118 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001119 if (IS_ERR(em)) {
1120 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001121 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001122 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001123 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001124
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001125 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001126 ram_size, cur_alloc_size,
1127 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001128 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001129 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001130
Yan Zheng17d217f2008-12-12 10:03:38 -05001131 if (root->root_key.objectid ==
1132 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001133 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001134 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001135 /*
1136 * Only drop cache here, and process as normal.
1137 *
1138 * We must not allow extent_clear_unlock_delalloc()
1139 * at out_unlock label to free meta of this ordered
1140 * extent, as its meta should be freed by
1141 * btrfs_finish_ordered_io().
1142 *
1143 * So we must continue until @start is increased to
1144 * skip current ordered extent.
1145 */
Josef Bacik00361582013-08-14 14:02:47 -04001146 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001147 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001148 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001149 }
1150
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001151 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001152
Chris Masonc8b97812008-10-29 14:49:59 -04001153 /* we're not doing compressed IO, don't unlock the first
1154 * page (which the caller expects to stay locked), don't
1155 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001156 *
1157 * Do set the Private2 bit so we know this page was properly
1158 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001159 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001160 page_ops = unlock ? PAGE_UNLOCK : 0;
1161 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001162
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001163 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001164 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001165 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001166 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001167 if (num_bytes < cur_alloc_size)
1168 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001169 else
Anand Jain3752d222018-02-15 12:29:38 +08001170 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001171 alloc_hint = ins.objectid + ins.offset;
1172 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001173 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001174
1175 /*
1176 * btrfs_reloc_clone_csums() error, since start is increased
1177 * extent_clear_unlock_delalloc() at out_unlock label won't
1178 * free metadata of current ordered extent, we're OK to exit.
1179 */
1180 if (ret)
1181 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001182 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001185
Filipe Mananad9f85962014-08-25 10:43:00 +01001186out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001187 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001188out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001189 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001190 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001192 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1193 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001194 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001195 /*
1196 * If we reserved an extent for our delalloc range (or a subrange) and
1197 * failed to create the respective ordered extent, then it means that
1198 * when we reserved the extent we decremented the extent's size from
1199 * the data space_info's bytes_may_use counter and incremented the
1200 * space_info's bytes_reserved counter by the same amount. We must make
1201 * sure extent_clear_unlock_delalloc() does not try to decrement again
1202 * the data space_info's bytes_may_use counter, therefore we do not pass
1203 * it the flag EXTENT_CLEAR_DATA_RESV.
1204 */
1205 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001206 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001207 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001208 locked_page,
1209 clear_bits,
1210 page_ops);
1211 start += cur_alloc_size;
1212 if (start >= end)
1213 goto out;
1214 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001215 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001216 clear_bits | EXTENT_CLEAR_DATA_RESV,
1217 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001219}
Chris Masonc8b97812008-10-29 14:49:59 -04001220
Chris Mason771ed682008-11-06 22:02:51 -05001221/*
1222 * work queue call back to started compression on a file and pages
1223 */
1224static noinline void async_cow_start(struct btrfs_work *work)
1225{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001226 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001227 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001228
Nikolay Borisovb5326272019-03-12 17:20:25 +02001229 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001231 compressed_extents = compress_file_range(async_chunk);
1232 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 btrfs_add_delayed_iput(async_chunk->inode);
1234 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001235 }
Chris Mason771ed682008-11-06 22:02:51 -05001236}
1237
1238/*
1239 * work queue call back to submit previously compressed pages
1240 */
1241static noinline void async_cow_submit(struct btrfs_work *work)
1242{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001243 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1244 work);
1245 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247
Nikolay Borisovb5326272019-03-12 17:20:25 +02001248 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001249 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001250
David Sterba093258e2018-02-26 16:15:17 +01001251 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001252 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001253 5 * SZ_1M)
1254 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001255
Nikolay Borisov4546d172019-01-03 10:50:03 +02001256 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001257 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001258 * in which case we don't have anything to submit, yet we need to
1259 * always adjust ->async_delalloc_pages as its paired with the init
1260 * happening in cow_file_range_async
1261 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001262 if (async_chunk->inode)
1263 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001264}
Chris Masonc8b97812008-10-29 14:49:59 -04001265
Chris Mason771ed682008-11-06 22:02:51 -05001266static noinline void async_cow_free(struct btrfs_work *work)
1267{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001268 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001269
Nikolay Borisovb5326272019-03-12 17:20:25 +02001270 async_chunk = container_of(work, struct async_chunk, work);
1271 if (async_chunk->inode)
1272 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001273 if (async_chunk->blkcg_css)
1274 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001275 /*
1276 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001277 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001279 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001280 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001281}
1282
Nikolay Borisov751b6432020-06-03 08:55:22 +03001283static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001284 struct writeback_control *wbc,
1285 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001286 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001287 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001288{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001289 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001290 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001291 struct async_cow *ctx;
1292 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001293 unsigned long nr_pages;
1294 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1296 int i;
1297 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001298 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001299 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001300
Nikolay Borisov751b6432020-06-03 08:55:22 +03001301 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001302
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1305 num_chunks = 1;
1306 should_compress = false;
1307 } else {
1308 should_compress = true;
1309 }
1310
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001311 nofs_flag = memalloc_nofs_save();
1312 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1313 memalloc_nofs_restore(nofs_flag);
1314
Nikolay Borisov97db1202019-03-12 17:20:24 +02001315 if (!ctx) {
1316 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1317 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1318 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001319 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1320 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001321
Nikolay Borisov751b6432020-06-03 08:55:22 +03001322 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1323 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001324 return -ENOMEM;
1325 }
1326
1327 async_chunk = ctx->chunks;
1328 atomic_set(&ctx->num_chunks, num_chunks);
1329
1330 for (i = 0; i < num_chunks; i++) {
1331 if (should_compress)
1332 cur_end = min(end, start + SZ_512K - 1);
1333 else
1334 cur_end = end;
1335
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001336 /*
1337 * igrab is called higher up in the call chain, take only the
1338 * lightweight reference for the callback lifetime
1339 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001340 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001341 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001342 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001343 async_chunk[i].start = start;
1344 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001345 async_chunk[i].write_flags = write_flags;
1346 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001347
Chris Mason1d53c9e2019-07-10 12:28:16 -07001348 /*
1349 * The locked_page comes all the way from writepage and its
1350 * the original page we were actually given. As we spread
1351 * this large delalloc region across multiple async_chunk
1352 * structs, only the first struct needs a pointer to locked_page
1353 *
1354 * This way we don't need racey decisions about who is supposed
1355 * to unlock it.
1356 */
1357 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001358 /*
1359 * Depending on the compressibility, the pages might or
1360 * might not go through async. We want all of them to
1361 * be accounted against wbc once. Let's do it here
1362 * before the paths diverge. wbc accounting is used
1363 * only for foreign writeback detection and doesn't
1364 * need full accuracy. Just account the whole thing
1365 * against the first page.
1366 */
1367 wbc_account_cgroup_owner(wbc, locked_page,
1368 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001369 async_chunk[i].locked_page = locked_page;
1370 locked_page = NULL;
1371 } else {
1372 async_chunk[i].locked_page = NULL;
1373 }
1374
Chris Masonec39f762019-07-10 12:28:17 -07001375 if (blkcg_css != blkcg_root_css) {
1376 css_get(blkcg_css);
1377 async_chunk[i].blkcg_css = blkcg_css;
1378 } else {
1379 async_chunk[i].blkcg_css = NULL;
1380 }
1381
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001382 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1383 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001384
Nikolay Borisov97db1202019-03-12 17:20:24 +02001385 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001386 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001387
Nikolay Borisov97db1202019-03-12 17:20:24 +02001388 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001389
Chris Mason771ed682008-11-06 22:02:51 -05001390 *nr_written += nr_pages;
1391 start = cur_end + 1;
1392 }
1393 *page_started = 1;
1394 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001395}
1396
Naohiro Aota42c01102021-02-04 19:22:07 +09001397static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1398 struct page *locked_page, u64 start,
1399 u64 end, int *page_started,
1400 unsigned long *nr_written)
1401{
1402 int ret;
1403
1404 ret = cow_file_range(inode, locked_page, start, end, page_started,
1405 nr_written, 0);
1406 if (ret)
1407 return ret;
1408
1409 if (*page_started)
1410 return 0;
1411
1412 __set_page_dirty_nobuffers(locked_page);
1413 account_page_redirty(locked_page);
1414 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1415 *page_started = 1;
1416
1417 return 0;
1418}
1419
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001420static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001421 u64 bytenr, u64 num_bytes)
1422{
1423 int ret;
1424 struct btrfs_ordered_sum *sums;
1425 LIST_HEAD(list);
1426
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001428 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001429 if (ret == 0 && list_empty(&list))
1430 return 0;
1431
1432 while (!list_empty(&list)) {
1433 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1434 list_del(&sums->list);
1435 kfree(sums);
1436 }
Liu Bo58113752018-01-31 17:09:13 -07001437 if (ret < 0)
1438 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001439 return 1;
1440}
1441
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001442static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001443 const u64 start, const u64 end,
1444 int *page_started, unsigned long *nr_written)
1445{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001446 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1447 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001448 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001449 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001450 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001451 u64 range_start = start;
1452 u64 count;
1453
1454 /*
1455 * If EXTENT_NORESERVE is set it means that when the buffered write was
1456 * made we had not enough available data space and therefore we did not
1457 * reserve data space for it, since we though we could do NOCOW for the
1458 * respective file range (either there is prealloc extent or the inode
1459 * has the NOCOW bit set).
1460 *
1461 * However when we need to fallback to COW mode (because for example the
1462 * block group for the corresponding extent was turned to RO mode by a
1463 * scrub or relocation) we need to do the following:
1464 *
1465 * 1) We increment the bytes_may_use counter of the data space info.
1466 * If COW succeeds, it allocates a new data extent and after doing
1467 * that it decrements the space info's bytes_may_use counter and
1468 * increments its bytes_reserved counter by the same amount (we do
1469 * this at btrfs_add_reserved_bytes()). So we need to increment the
1470 * bytes_may_use counter to compensate (when space is reserved at
1471 * buffered write time, the bytes_may_use counter is incremented);
1472 *
1473 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1474 * that if the COW path fails for any reason, it decrements (through
1475 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1476 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001477 *
1478 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001479 * space cache inode or an inode of the data relocation tree, we must
1480 * also increment bytes_may_use of the data space_info for the same
1481 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001482 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001483 * group that contains that extent to RO mode and therefore force COW
1484 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001485 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001486 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001487 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001488 if (count > 0 || is_space_ino || is_reloc_ino) {
1489 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001490 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001491 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1492
Filipe Manana6bd335b2020-06-08 13:33:05 +01001493 if (is_space_ino || is_reloc_ino)
1494 bytes = range_bytes;
1495
Filipe Manana467dc472020-05-27 11:16:07 +01001496 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001497 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001498 spin_unlock(&sinfo->lock);
1499
Filipe Manana2166e5e2020-05-27 11:16:19 +01001500 if (count > 0)
1501 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1502 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001503 }
1504
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001505 return cow_file_range(inode, locked_page, start, end, page_started,
1506 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * when nowcow writeback call back. This checks for snapshots or COW copies
1511 * of the extents that exist in the file, and COWs the file as required.
1512 *
1513 * If no cow copies or snapshots exist, we write directly to the existing
1514 * blocks on disk
1515 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001516static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001517 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001518 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001519 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001520 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001521{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001522 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1523 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001524 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001525 u64 cow_start = (u64)-1;
1526 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001527 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001528 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001529 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1530 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001531 bool nocow = false;
1532 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001533 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001534
1535 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001537 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001538 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001539 EXTENT_DO_ACCOUNTING |
1540 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001541 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001542 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001543 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001544 }
Li Zefan82d59022011-04-20 10:33:24 +08001545
Yan Zheng80ff3852008-10-30 14:20:02 -04001546 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001547 struct btrfs_key found_key;
1548 struct btrfs_file_extent_item *fi;
1549 struct extent_buffer *leaf;
1550 u64 extent_end;
1551 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001552 u64 num_bytes = 0;
1553 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001554 u64 ram_bytes;
1555 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001556
1557 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001558
Liu Boe4c3b2d2017-01-30 12:25:28 -08001559 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001561 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001563
1564 /*
1565 * If there is no extent for our range when doing the initial
1566 * search, then go back to the previous slot as it will be the
1567 * one containing the search offset
1568 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001569 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1570 leaf = path->nodes[0];
1571 btrfs_item_key_to_cpu(leaf, &found_key,
1572 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001573 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 found_key.type == BTRFS_EXTENT_DATA_KEY)
1575 path->slots[0]--;
1576 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001577 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001578next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001579 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001580 leaf = path->nodes[0];
1581 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1582 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001583 if (ret < 0) {
1584 if (cow_start != (u64)-1)
1585 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001586 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001587 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001588 if (ret > 0)
1589 break;
1590 leaf = path->nodes[0];
1591 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001592
Yan Zheng80ff3852008-10-30 14:20:02 -04001593 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001594
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001595 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001596 if (found_key.objectid > ino)
1597 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001598 /*
1599 * Keep searching until we find an EXTENT_ITEM or there are no
1600 * more extents for this inode
1601 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001602 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1603 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1604 path->slots[0]++;
1605 goto next_slot;
1606 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001607
1608 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001609 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001610 found_key.offset > end)
1611 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001612
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001613 /*
1614 * If the found extent starts after requested offset, then
1615 * adjust extent_end to be right before this extent begins
1616 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001617 if (found_key.offset > cur_offset) {
1618 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001619 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001620 goto out_check;
1621 }
Chris Masonc31f8832008-01-08 15:46:31 -05001622
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001623 /*
1624 * Found extent which begins before our range and potentially
1625 * intersect it
1626 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001627 fi = btrfs_item_ptr(leaf, path->slots[0],
1628 struct btrfs_file_extent_item);
1629 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001630
Josef Bacikcc95bef2013-04-04 14:31:27 -04001631 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001632 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1633 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001634 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001636 extent_end = found_key.offset +
1637 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001638 disk_num_bytes =
1639 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001640 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001641 * If the extent we got ends before our current offset,
1642 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001643 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001644 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001645 path->slots[0]++;
1646 goto next_slot;
1647 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001648 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001649 if (disk_bytenr == 0)
1650 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001651 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 if (btrfs_file_extent_compression(leaf, fi) ||
1653 btrfs_file_extent_encryption(leaf, fi) ||
1654 btrfs_file_extent_other_encoding(leaf, fi))
1655 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001656 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001657 * If extent is created before the last volume's snapshot
1658 * this implies the extent is shared, hence we can't do
1659 * nocow. This is the same check as in
1660 * btrfs_cross_ref_exist but without calling
1661 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001662 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001663 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001664 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001665 btrfs_root_last_snapshot(&root->root_item))
1666 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001667 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1668 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001669
1670 /*
1671 * The following checks can be expensive, as they need to
1672 * take other locks and do btree or rbtree searches, so
1673 * release the path to avoid blocking other tasks for too
1674 * long.
1675 */
1676 btrfs_release_path(path);
1677
Liu Bo58113752018-01-31 17:09:13 -07001678 ret = btrfs_cross_ref_exist(root, ino,
1679 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001680 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001681 if (ret) {
1682 /*
1683 * ret could be -EIO if the above fails to read
1684 * metadata.
1685 */
1686 if (ret < 0) {
1687 if (cow_start != (u64)-1)
1688 cur_offset = cow_start;
1689 goto error;
1690 }
1691
Nikolay Borisov3e024842019-08-21 10:42:03 +03001692 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001693 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001694 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001695 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001696 disk_bytenr += cur_offset - found_key.offset;
1697 num_bytes = min(end + 1, extent_end) - cur_offset;
1698 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001699 * If there are pending snapshots for this root, we
1700 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001701 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001702 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001703 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001704 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001705 * force cow if csum exists in the range.
1706 * this ensure that csum for a given extent are
1707 * either valid or do not exist.
1708 */
Liu Bo58113752018-01-31 17:09:13 -07001709 ret = csum_exist_in_range(fs_info, disk_bytenr,
1710 num_bytes);
1711 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001712 /*
1713 * ret could be -EIO if the above fails to read
1714 * metadata.
1715 */
1716 if (ret < 0) {
1717 if (cow_start != (u64)-1)
1718 cur_offset = cow_start;
1719 goto error;
1720 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001721 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001722 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001723 }
Filipe Manana20903032021-02-05 12:55:36 +00001724 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001725 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001726 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001727 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001728 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001729 extent_end = found_key.offset + ram_bytes;
1730 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001731 /* Skip extents outside of our requested range */
1732 if (extent_end <= start) {
1733 path->slots[0]++;
1734 goto next_slot;
1735 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001736 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001737 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001738 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001739 }
1740out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001741 /*
1742 * If nocow is false then record the beginning of the range
1743 * that needs to be COWed
1744 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001745 if (!nocow) {
1746 if (cow_start == (u64)-1)
1747 cow_start = cur_offset;
1748 cur_offset = extent_end;
1749 if (cur_offset > end)
1750 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001751 if (!path->nodes[0])
1752 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001753 path->slots[0]++;
1754 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001755 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001756
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001757 /*
1758 * COW range from cow_start to found_key.offset - 1. As the key
1759 * will contain the beginning of the first extent that can be
1760 * NOCOW, following one which needs to be COW'ed
1761 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001762 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001763 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001764 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001765 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001766 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001767 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001768 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001769 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001770
Yan Zhengd899e052008-10-30 14:25:28 -04001771 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001772 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001773 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001774
Nikolay Borisov968322c2020-06-03 08:55:21 +03001775 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001776 orig_start,
1777 disk_bytenr, /* block_start */
1778 num_bytes, /* block_len */
1779 disk_num_bytes, /* orig_block_len */
1780 ram_bytes, BTRFS_COMPRESS_NONE,
1781 BTRFS_ORDERED_PREALLOC);
1782 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001783 ret = PTR_ERR(em);
1784 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001785 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001786 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001787 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001788 disk_bytenr, num_bytes,
1789 num_bytes,
1790 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001791 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001792 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001793 cur_offset + num_bytes - 1,
1794 0);
1795 goto error;
1796 }
Yan Zhengd899e052008-10-30 14:25:28 -04001797 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001798 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001799 disk_bytenr, num_bytes,
1800 num_bytes,
1801 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001802 if (ret)
1803 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001804 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001805
Filipe Mananaf78c4362016-05-09 13:15:41 +01001806 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001807 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001808 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001809
Yan, Zhengefa56462010-05-16 10:49:59 -04001810 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001811 BTRFS_DATA_RELOC_TREE_OBJECTID)
1812 /*
1813 * Error handled later, as we must prevent
1814 * extent_clear_unlock_delalloc() in error handler
1815 * from freeing metadata of created ordered extent.
1816 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001817 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001818 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001819
Nikolay Borisov968322c2020-06-03 08:55:21 +03001820 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001821 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001822 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001823 EXTENT_DELALLOC |
1824 EXTENT_CLEAR_DATA_RESV,
1825 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1826
Yan Zheng80ff3852008-10-30 14:20:02 -04001827 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001828
1829 /*
1830 * btrfs_reloc_clone_csums() error, now we're OK to call error
1831 * handler, as metadata for created ordered extent will only
1832 * be freed by btrfs_finish_ordered_io().
1833 */
1834 if (ret)
1835 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001836 if (cur_offset > end)
1837 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001838 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001839 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001840
Robbie Ko506481b2018-10-30 18:04:04 +08001841 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001842 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001843
Yan Zheng80ff3852008-10-30 14:20:02 -04001844 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001845 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001846 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1847 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001848 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001849 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001850 }
1851
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001852error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001853 if (nocow)
1854 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1855
Josef Bacik17ca04a2012-05-31 15:58:55 -04001856 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001857 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001858 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001859 EXTENT_DELALLOC | EXTENT_DEFRAG |
1860 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001861 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001862 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001863 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001864 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001865}
1866
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001867static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001868{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001869 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1870 if (inode->defrag_bytes &&
1871 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1872 0, NULL))
1873 return false;
1874 return true;
1875 }
1876 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001880 * Function to process delayed allocation (create CoW) for ranges which are
1881 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001883int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001884 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1885 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001886{
Chris Masonbe20aa92007-12-17 20:14:01 -05001887 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001888 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001889
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001890 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001891 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001892 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001893 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001894 } else if (!inode_can_compress(inode) ||
1895 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001896 if (zoned)
1897 ret = run_delalloc_zoned(inode, locked_page, start, end,
1898 page_started, nr_written);
1899 else
1900 ret = cow_file_range(inode, locked_page, start, end,
1901 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001902 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001903 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1904 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001905 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001906 }
Qu Wenruo524272602017-03-08 10:25:52 +08001907 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001908 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001909 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001910 return ret;
1911}
1912
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001913void btrfs_split_delalloc_extent(struct inode *inode,
1914 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001915{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001916 u64 size;
1917
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001918 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001919 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001920 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001921
Josef Bacikdcab6a32015-02-11 15:08:59 -05001922 size = orig->end - orig->start + 1;
1923 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001924 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001925 u64 new_size;
1926
1927 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001928 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001929 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001930 */
1931 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001932 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001933 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001934 num_extents += count_max_extents(new_size);
1935 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001936 return;
1937 }
1938
Josef Bacik9e0baf62011-07-15 15:16:44 +00001939 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001940 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001941 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001942}
1943
1944/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001945 * Handle merged delayed allocation extents so we can keep track of new extents
1946 * that are just merged onto old extents, such as when we are doing sequential
1947 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001948 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001949void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1950 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001951{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001952 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001953 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001954
Josef Bacik9ed74f22009-09-11 16:12:44 -04001955 /* not delalloc, ignore it */
1956 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001957 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001958
Josef Bacik8461a3d2015-03-13 15:12:08 -04001959 if (new->start > other->start)
1960 new_size = new->end - other->start + 1;
1961 else
1962 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001963
1964 /* we're not bigger than the max, unreserve the space and go */
1965 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1966 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001967 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001968 spin_unlock(&BTRFS_I(inode)->lock);
1969 return;
1970 }
1971
1972 /*
Josef Bacikba117212015-03-13 15:01:24 -04001973 * We have to add up either side to figure out how many extents were
1974 * accounted for before we merged into one big extent. If the number of
1975 * extents we accounted for is <= the amount we need for the new range
1976 * then we can return, otherwise drop. Think of it like this
1977 *
1978 * [ 4k][MAX_SIZE]
1979 *
1980 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1981 * need 2 outstanding extents, on one side we have 1 and the other side
1982 * we have 1 so they are == and we can return. But in this case
1983 *
1984 * [MAX_SIZE+4k][MAX_SIZE+4k]
1985 *
1986 * Each range on their own accounts for 2 extents, but merged together
1987 * they are only 3 extents worth of accounting, so we need to drop in
1988 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001989 */
Josef Bacikba117212015-03-13 15:01:24 -04001990 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001991 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001992 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001993 num_extents += count_max_extents(old_size);
1994 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001995 return;
1996
Josef Bacik9e0baf62011-07-15 15:16:44 +00001997 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001998 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001999 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002000}
2001
Miao Xieeb73c1b2013-05-15 07:48:22 +00002002static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2003 struct inode *inode)
2004{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002005 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2006
Miao Xieeb73c1b2013-05-15 07:48:22 +00002007 spin_lock(&root->delalloc_lock);
2008 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2009 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2010 &root->delalloc_inodes);
2011 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2012 &BTRFS_I(inode)->runtime_flags);
2013 root->nr_delalloc_inodes++;
2014 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002015 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002016 BUG_ON(!list_empty(&root->delalloc_root));
2017 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002018 &fs_info->delalloc_roots);
2019 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002020 }
2021 }
2022 spin_unlock(&root->delalloc_lock);
2023}
2024
Nikolay Borisov2b877332018-04-27 12:21:51 +03002025
2026void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2027 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002028{
David Sterba3ffbd682018-06-29 10:56:42 +02002029 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002030
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002031 if (!list_empty(&inode->delalloc_inodes)) {
2032 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002033 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002034 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002035 root->nr_delalloc_inodes--;
2036 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002037 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002038 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002039 BUG_ON(list_empty(&root->delalloc_root));
2040 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002041 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002042 }
2043 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002044}
2045
2046static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2047 struct btrfs_inode *inode)
2048{
2049 spin_lock(&root->delalloc_lock);
2050 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002051 spin_unlock(&root->delalloc_lock);
2052}
2053
Chris Masond352ac62008-09-29 15:18:18 -04002054/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002055 * Properly track delayed allocation bytes in the inode and to maintain the
2056 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002057 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002058void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2059 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002061 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2062
Wang Shilong47059d92014-07-03 18:22:07 +08002063 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2064 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002065 /*
2066 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002067 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002068 * bit, which is only set or cleared with irqs on
2069 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002070 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002071 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002072 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002073 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002074 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002075
Josef Bacik8b62f872017-10-19 14:15:55 -04002076 spin_lock(&BTRFS_I(inode)->lock);
2077 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2078 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002079
Josef Bacik6a3891c2015-03-16 17:38:52 -04002080 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002081 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002082 return;
2083
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002084 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2085 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002086 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002087 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002088 if (*bits & EXTENT_DEFRAG)
2089 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002090 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002091 &BTRFS_I(inode)->runtime_flags))
2092 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002093 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002094 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002095
2096 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2097 (*bits & EXTENT_DELALLOC_NEW)) {
2098 spin_lock(&BTRFS_I(inode)->lock);
2099 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2100 state->start;
2101 spin_unlock(&BTRFS_I(inode)->lock);
2102 }
Chris Mason291d6732008-01-29 15:55:23 -05002103}
2104
Chris Masond352ac62008-09-29 15:18:18 -04002105/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002106 * Once a range is no longer delalloc this function ensures that proper
2107 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002108 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002109void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2110 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002111{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002112 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2113 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002114 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002115 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002116
Filipe Manana4a4b9642017-07-27 19:52:55 +01002117 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2118 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002119 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002120 spin_unlock(&inode->lock);
2121 }
Wang Shilong47059d92014-07-03 18:22:07 +08002122
Chris Mason75eff682008-12-15 15:54:40 -05002123 /*
2124 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002125 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002126 * bit, which is only set or cleared with irqs on
2127 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002128 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002129 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002130 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002131
Josef Bacik8b62f872017-10-19 14:15:55 -04002132 spin_lock(&inode->lock);
2133 btrfs_mod_outstanding_extents(inode, -num_extents);
2134 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002135
Josef Bacikb6d08f02013-09-27 14:57:43 -04002136 /*
2137 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002138 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002139 * error.
2140 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002141 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002142 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002143 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002144
Josef Bacik6a3891c2015-03-16 17:38:52 -04002145 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002146 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002147 return;
2148
Filipe Mananaa315e682017-03-06 23:04:20 +00002149 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2150 do_list && !(state->state & EXTENT_NORESERVE) &&
2151 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002152 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002153
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002154 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2155 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002156 spin_lock(&inode->lock);
2157 inode->delalloc_bytes -= len;
2158 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002159 test_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 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002162 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002163 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002164
2165 if ((state->state & EXTENT_DELALLOC_NEW) &&
2166 (*bits & EXTENT_DELALLOC_NEW)) {
2167 spin_lock(&inode->lock);
2168 ASSERT(inode->new_delalloc_bytes >= len);
2169 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002170 if (*bits & EXTENT_ADD_INODE_BYTES)
2171 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002172 spin_unlock(&inode->lock);
2173 }
Chris Mason291d6732008-01-29 15:55:23 -05002174}
2175
Chris Masond352ac62008-09-29 15:18:18 -04002176/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002177 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2178 * in a chunk's stripe. This function ensures that bios do not span a
2179 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002180 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002181 * @page - The page we are about to add to the bio
2182 * @size - size we want to add to the bio
2183 * @bio - bio we want to ensure is smaller than a stripe
2184 * @bio_flags - flags of the bio
2185 *
2186 * return 1 if page cannot be added to the bio
2187 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002188 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002189 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002190int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2191 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002193 struct inode *inode = page->mapping->host;
2194 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002195 u64 logical = bio->bi_iter.bi_sector << 9;
Michal Rostecki42034312021-01-27 14:57:27 +01002196 struct extent_map *em;
Chris Mason239b14b2008-03-24 15:02:07 -04002197 u64 length = 0;
2198 u64 map_length;
Michal Rostecki42034312021-01-27 14:57:27 +01002199 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002200 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002201
Chris Mason771ed682008-11-06 22:02:51 -05002202 if (bio_flags & EXTENT_BIO_COMPRESSED)
2203 return 0;
2204
Kent Overstreet4f024f32013-10-11 15:44:27 -07002205 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002206 map_length = length;
Michal Rostecki42034312021-01-27 14:57:27 +01002207 em = btrfs_get_chunk_map(fs_info, logical, map_length);
2208 if (IS_ERR(em))
2209 return PTR_ERR(em);
2210 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical,
2211 map_length, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002212 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002213 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002214
2215 if (geom.len < length + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002216 ret = 1;
2217out:
2218 free_extent_map(em);
2219 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002220}
2221
Chris Masond352ac62008-09-29 15:18:18 -04002222/*
2223 * in order to insert checksums into the metadata in large chunks,
2224 * we wait until bio submission time. All the pages in the bio are
2225 * checksummed and sums are attached onto the ordered extent record.
2226 *
2227 * At IO completion time the cums attached on the ordered extent record
2228 * are inserted into the btree
2229 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002230static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002231 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002232{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002233 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002234}
Chris Masone0156402008-04-16 11:15:20 -04002235
Johannes Thumshirncacb2ce2021-02-04 19:22:01 +09002236bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio,
2237 unsigned int size)
2238{
2239 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2240 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2241 struct btrfs_ordered_extent *ordered;
2242 u64 len = bio->bi_iter.bi_size + size;
2243 bool ret = true;
2244
2245 ASSERT(btrfs_is_zoned(fs_info));
2246 ASSERT(fs_info->max_zone_append_size > 0);
2247 ASSERT(bio_op(bio) == REQ_OP_ZONE_APPEND);
2248
2249 /* Ordered extent not yet created, so we're good */
2250 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
2251 if (!ordered)
2252 return ret;
2253
2254 if ((bio->bi_iter.bi_sector << SECTOR_SHIFT) + len >
2255 ordered->disk_bytenr + ordered->disk_num_bytes)
2256 ret = false;
2257
2258 btrfs_put_ordered_extent(ordered);
2259
2260 return ret;
2261}
2262
Naohiro Aotad22002f2021-02-04 19:22:00 +09002263static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2264 struct bio *bio, loff_t file_offset)
2265{
2266 struct btrfs_ordered_extent *ordered;
2267 struct extent_map *em = NULL, *em_new = NULL;
2268 struct extent_map_tree *em_tree = &inode->extent_tree;
2269 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2270 u64 len = bio->bi_iter.bi_size;
2271 u64 end = start + len;
2272 u64 ordered_end;
2273 u64 pre, post;
2274 int ret = 0;
2275
2276 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2277 if (WARN_ON_ONCE(!ordered))
2278 return BLK_STS_IOERR;
2279
2280 /* No need to split */
2281 if (ordered->disk_num_bytes == len)
2282 goto out;
2283
2284 /* We cannot split once end_bio'd ordered extent */
2285 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2286 ret = -EINVAL;
2287 goto out;
2288 }
2289
2290 /* We cannot split a compressed ordered extent */
2291 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2292 ret = -EINVAL;
2293 goto out;
2294 }
2295
2296 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2297 /* bio must be in one ordered extent */
2298 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2299 ret = -EINVAL;
2300 goto out;
2301 }
2302
2303 /* Checksum list should be empty */
2304 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2305 ret = -EINVAL;
2306 goto out;
2307 }
2308
2309 pre = start - ordered->disk_bytenr;
2310 post = ordered_end - end;
2311
2312 ret = btrfs_split_ordered_extent(ordered, pre, post);
2313 if (ret)
2314 goto out;
2315
2316 read_lock(&em_tree->lock);
2317 em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
2318 if (!em) {
2319 read_unlock(&em_tree->lock);
2320 ret = -EIO;
2321 goto out;
2322 }
2323 read_unlock(&em_tree->lock);
2324
2325 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2326 /*
2327 * We cannot reuse em_new here but have to create a new one, as
2328 * unpin_extent_cache() expects the start of the extent map to be the
2329 * logical offset of the file, which does not hold true anymore after
2330 * splitting.
2331 */
2332 em_new = create_io_em(inode, em->start + pre, len,
2333 em->start + pre, em->block_start + pre, len,
2334 len, len, BTRFS_COMPRESS_NONE,
2335 BTRFS_ORDERED_REGULAR);
2336 if (IS_ERR(em_new)) {
2337 ret = PTR_ERR(em_new);
2338 goto out;
2339 }
2340 free_extent_map(em_new);
2341
2342out:
2343 free_extent_map(em);
2344 btrfs_put_ordered_extent(ordered);
2345
2346 return errno_to_blk_status(ret);
2347}
2348
Chris Mason4a69a412008-11-06 22:03:00 -05002349/*
Chris Masoncad321a2008-12-17 14:51:42 -05002350 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002351 * on write, or reading the csums from the tree before a read.
2352 *
2353 * Rules about async/sync submit,
2354 * a) read: sync submit
2355 *
2356 * b) write without checksum: sync submit
2357 *
2358 * c) write with checksum:
2359 * c-1) if bio is issued by fsync: sync submit
2360 * (sync_writers != 0)
2361 *
2362 * c-2) if root is reloc root: sync submit
2363 * (only in case of buffered IO)
2364 *
2365 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002366 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002367blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2368 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002369
Chris Mason44b8bd72008-04-16 11:14:51 -04002370{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002372 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302373 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002374 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002375 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002376 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002377
Josef Bacik42437a62020-10-16 11:29:18 -04002378 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2379 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002380
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002381 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302382 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002383
Naohiro Aotad22002f2021-02-04 19:22:00 +09002384 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2385 struct page *page = bio_first_bvec_all(bio)->bv_page;
2386 loff_t file_offset = page_offset(page);
2387
2388 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2389 if (ret)
2390 goto out;
2391 }
2392
Naohiro Aotacfe94442021-02-04 19:21:59 +09002393 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002395 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002396 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002397
Chris Masond20f7042008-12-08 16:58:54 -05002398 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002399 ret = btrfs_submit_compressed_read(inode, bio,
2400 mirror_num,
2401 bio_flags);
2402 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002403 } else {
2404 /*
2405 * Lookup bio sums does extra checks around whether we
2406 * need to csum or not, which is why we ignore skip_sum
2407 * here.
2408 */
Qu Wenruo62751932020-12-02 14:48:06 +08002409 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002410 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002411 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002412 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002413 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002414 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002415 /* csum items have already been cloned */
2416 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2417 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002418 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002419 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2420 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002421 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002422 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002423 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002424 if (ret)
2425 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002426 }
2427
Chris Mason0b86a832008-03-24 15:01:56 -04002428mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002429 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002430
2431out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002432 if (ret) {
2433 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002434 bio_endio(bio);
2435 }
Stefan Behrens61891922012-11-05 18:51:52 +01002436 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002437}
Chris Mason6885f302008-02-20 16:11:05 -05002438
Chris Masond352ac62008-09-29 15:18:18 -04002439/*
2440 * given a list of ordered sums record them in the inode. This happens
2441 * at IO completion time based on sums calculated at bio submission time.
2442 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002443static int add_pending_csums(struct btrfs_trans_handle *trans,
2444 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002445{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002446 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002447 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002448
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002449 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002450 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002451 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002452 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002453 if (ret)
2454 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002455 }
2456 return 0;
2457}
2458
Filipe Mananac3347302020-11-04 11:07:31 +00002459static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2460 const u64 start,
2461 const u64 len,
2462 struct extent_state **cached_state)
2463{
2464 u64 search_start = start;
2465 const u64 end = start + len - 1;
2466
2467 while (search_start < end) {
2468 const u64 search_len = end - search_start + 1;
2469 struct extent_map *em;
2470 u64 em_len;
2471 int ret = 0;
2472
2473 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2474 if (IS_ERR(em))
2475 return PTR_ERR(em);
2476
2477 if (em->block_start != EXTENT_MAP_HOLE)
2478 goto next;
2479
2480 em_len = em->len;
2481 if (em->start < search_start)
2482 em_len -= search_start - em->start;
2483 if (em_len > search_len)
2484 em_len = search_len;
2485
2486 ret = set_extent_bit(&inode->io_tree, search_start,
2487 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002488 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2489 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002490next:
2491 search_start = extent_map_end(em);
2492 free_extent_map(em);
2493 if (ret)
2494 return ret;
2495 }
2496 return 0;
2497}
2498
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002499int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002500 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002501 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002502{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002503 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002504
2505 if (start >= i_size_read(&inode->vfs_inode) &&
2506 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2507 /*
2508 * There can't be any extents following eof in this case so just
2509 * set the delalloc new bit for the range directly.
2510 */
2511 extra_bits |= EXTENT_DELALLOC_NEW;
2512 } else {
2513 int ret;
2514
2515 ret = btrfs_find_new_delalloc_bytes(inode, start,
2516 end + 1 - start,
2517 cached_state);
2518 if (ret)
2519 return ret;
2520 }
2521
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002522 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2523 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002524}
2525
Chris Masond352ac62008-09-29 15:18:18 -04002526/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002527struct btrfs_writepage_fixup {
2528 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002529 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002530 struct btrfs_work work;
2531};
2532
Christoph Hellwigb2950862008-12-02 09:54:17 -05002533static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002534{
2535 struct btrfs_writepage_fixup *fixup;
2536 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002537 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002538 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002539 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002540 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002541 u64 page_start;
2542 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002543 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002544 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002545
2546 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2547 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002548 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002549 page_start = page_offset(page);
2550 page_end = page_offset(page) + PAGE_SIZE - 1;
2551
2552 /*
2553 * This is similar to page_mkwrite, we need to reserve the space before
2554 * we take the page lock.
2555 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002556 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2557 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002558again:
Chris Mason247e7432008-07-17 12:53:51 -04002559 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002560
Chris Mason25f3c502020-01-21 11:51:42 -05002561 /*
2562 * Before we queued this fixup, we took a reference on the page.
2563 * page->mapping may go NULL, but it shouldn't be moved to a different
2564 * address space.
2565 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002566 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2567 /*
2568 * Unfortunately this is a little tricky, either
2569 *
2570 * 1) We got here and our page had already been dealt with and
2571 * we reserved our space, thus ret == 0, so we need to just
2572 * drop our space reservation and bail. This can happen the
2573 * first time we come into the fixup worker, or could happen
2574 * while waiting for the ordered extent.
2575 * 2) Our page was already dealt with, but we happened to get an
2576 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2577 * this case we obviously don't have anything to release, but
2578 * because the page was already dealt with we don't want to
2579 * mark the page with an error, so make sure we're resetting
2580 * ret to 0. This is why we have this check _before_ the ret
2581 * check, because we do not want to have a surprise ENOSPC
2582 * when the page was already properly dealt with.
2583 */
2584 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002585 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2586 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002587 page_start, PAGE_SIZE,
2588 true);
2589 }
2590 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002591 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002592 }
Chris Mason25f3c502020-01-21 11:51:42 -05002593
2594 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002595 * We can't mess with the page state unless it is locked, so now that
2596 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002597 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002598 if (ret)
2599 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002600
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002601 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002602
2603 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002604 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002605 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002606
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002607 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002608 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002609 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2610 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002611 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002612 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002613 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002614 goto again;
2615 }
Chris Mason247e7432008-07-17 12:53:51 -04002616
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002617 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002618 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002619 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002620 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002621
Chris Mason25f3c502020-01-21 11:51:42 -05002622 /*
2623 * Everything went as planned, we're now the owner of a dirty page with
2624 * delayed allocation bits set and space reserved for our COW
2625 * destination.
2626 *
2627 * The page was dirty when we started, nothing should have cleaned it.
2628 */
2629 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002630 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002631out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002632 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002633 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002634 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2635 PAGE_SIZE, true);
2636 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002637 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002638out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002639 if (ret) {
2640 /*
2641 * We hit ENOSPC or other errors. Update the mapping and page
2642 * to reflect the errors and clean the page.
2643 */
2644 mapping_set_error(page->mapping, ret);
2645 end_extent_writepage(page, ret, page_start, page_end);
2646 clear_page_dirty_for_io(page);
2647 SetPageError(page);
2648 }
2649 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002650 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002651 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002652 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002653 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002654 /*
2655 * As a precaution, do a delayed iput in case it would be the last iput
2656 * that could need flushing space. Recursing back to fixup worker would
2657 * deadlock.
2658 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002659 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002660}
2661
2662/*
2663 * There are a few paths in the higher layers of the kernel that directly
2664 * set the page dirty bit without asking the filesystem if it is a
2665 * good idea. This causes problems because we want to make sure COW
2666 * properly happens and the data=ordered rules are followed.
2667 *
Chris Masonc8b97812008-10-29 14:49:59 -04002668 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002669 * hasn't been properly setup for IO. We kick off an async process
2670 * to fix it up. The async helper will wait for ordered extents, set
2671 * the delalloc bit and make it safe to write the page.
2672 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002673int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002674{
2675 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002676 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002677 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002678
Chris Mason8b62b722009-09-02 16:53:46 -04002679 /* this page is properly in the ordered list */
2680 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002681 return 0;
2682
Chris Mason25f3c502020-01-21 11:51:42 -05002683 /*
2684 * PageChecked is set below when we create a fixup worker for this page,
2685 * don't try to create another one if we're already PageChecked()
2686 *
2687 * The extent_io writepage code will redirty the page if we send back
2688 * EAGAIN.
2689 */
Chris Mason247e7432008-07-17 12:53:51 -04002690 if (PageChecked(page))
2691 return -EAGAIN;
2692
2693 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2694 if (!fixup)
2695 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002696
Josef Bacikf4b13632020-01-21 14:34:52 -05002697 /*
2698 * We are already holding a reference to this inode from
2699 * write_cache_pages. We need to hold it because the space reservation
2700 * takes place outside of the page lock, and we can't trust
2701 * page->mapping outside of the page lock.
2702 */
2703 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002704 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002705 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002706 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002707 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002708 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002710
2711 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002712}
2713
Yan Zhengd899e052008-10-30 14:25:28 -04002714static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002715 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002716 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002717 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002718 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002719{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002720 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002721 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002722 struct btrfs_path *path;
2723 struct extent_buffer *leaf;
2724 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002725 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2726 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2727 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2728 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002729 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002730 int ret;
2731
2732 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002733 if (!path)
2734 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002735
Chris Masona1ed8352009-09-11 12:27:37 -04002736 /*
2737 * we may be replacing one extent in the tree with another.
2738 * The new extent is pinned in the extent map, and we don't want
2739 * to drop it from the cache until it is completely in the btree.
2740 *
2741 * So, tell btrfs_drop_extents to leave this extent in the cache.
2742 * the caller is expected to unpin it and allow it to be merged
2743 * with the others.
2744 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002745 drop_args.path = path;
2746 drop_args.start = file_pos;
2747 drop_args.end = file_pos + num_bytes;
2748 drop_args.replace_extent = true;
2749 drop_args.extent_item_size = sizeof(*stack_fi);
2750 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002751 if (ret)
2752 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002753
Filipe Manana5893dfb2020-11-04 11:07:32 +00002754 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002755 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002756 ins.offset = file_pos;
2757 ins.type = BTRFS_EXTENT_DATA_KEY;
2758
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002759 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002760 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002761 if (ret)
2762 goto out;
2763 }
Yan Zhengd899e052008-10-30 14:25:28 -04002764 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002765 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2766 write_extent_buffer(leaf, stack_fi,
2767 btrfs_item_ptr_offset(leaf, path->slots[0]),
2768 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002769
Yan Zhengd899e052008-10-30 14:25:28 -04002770 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002771 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002772
Filipe Manana2766ff62020-11-04 11:07:34 +00002773 /*
2774 * If we dropped an inline extent here, we know the range where it is
2775 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2776 * number of bytes only for that range contaning the inline extent.
2777 * The remaining of the range will be processed when clearning the
2778 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2779 */
2780 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2781 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2782
2783 inline_size = drop_args.bytes_found - inline_size;
2784 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2785 drop_args.bytes_found -= inline_size;
2786 num_bytes -= sectorsize;
2787 }
2788
2789 if (update_inode_bytes)
2790 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002791
2792 ins.objectid = disk_bytenr;
2793 ins.offset = disk_num_bytes;
2794 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002795
Nikolay Borisovc553f942020-06-03 08:55:19 +03002796 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002797 if (ret)
2798 goto out;
2799
Nikolay Borisovc553f942020-06-03 08:55:19 +03002800 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002801 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002802out:
Yan Zhengd899e052008-10-30 14:25:28 -04002803 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002804
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002805 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002806}
2807
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002808static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002809 u64 start, u64 len)
2810{
David Sterba32da53862019-10-29 19:20:18 +01002811 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002813 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002814 ASSERT(cache);
2815
2816 spin_lock(&cache->lock);
2817 cache->delalloc_bytes -= len;
2818 spin_unlock(&cache->lock);
2819
2820 btrfs_put_block_group(cache);
2821}
2822
Qu Wenruo203f44c52020-06-10 09:04:40 +08002823static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002824 struct btrfs_ordered_extent *oe)
2825{
2826 struct btrfs_file_extent_item stack_fi;
2827 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002828 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002829
2830 memset(&stack_fi, 0, sizeof(stack_fi));
2831 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2832 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2833 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2834 oe->disk_num_bytes);
2835 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2836 logical_len = oe->truncated_len;
2837 else
2838 logical_len = oe->num_bytes;
2839 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2840 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2841 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2842 /* Encryption and other encoding is reserved and all 0 */
2843
Filipe Manana2766ff62020-11-04 11:07:34 +00002844 /*
2845 * For delalloc, when completing an ordered extent we update the inode's
2846 * bytes when clearing the range in the inode's io tree, so pass false
2847 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2848 * except if the ordered extent was truncated.
2849 */
2850 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2851 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2852
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002853 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2854 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002855 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002856}
2857
2858/*
2859 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002860 * an ordered extent if the range of bytes in the file it covers are
2861 * fully written.
2862 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002863static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002864{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002865 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2866 struct btrfs_root *root = inode->root;
2867 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002868 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002869 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002870 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002871 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002872 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002873 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002874 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002875 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002876 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002877 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002878 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002879
Omar Sandovalbffe6332019-12-02 17:34:19 -08002880 start = ordered_extent->file_offset;
2881 end = start + ordered_extent->num_bytes - 1;
2882
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002883 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2884 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2885 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002886 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002887
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002888 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002889
Josef Bacik5fd02042012-05-02 14:00:54 -04002890 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2891 ret = -EIO;
2892 goto out;
2893 }
2894
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002895 if (ordered_extent->disk)
2896 btrfs_rewrite_logical_zoned(ordered_extent);
2897
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002898 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002899
Josef Bacik77cef2e2013-08-29 13:57:21 -04002900 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2901 truncated = true;
2902 logical_len = ordered_extent->truncated_len;
2903 /* Truncated the entire extent, don't bother adding */
2904 if (!logical_len)
2905 goto out;
2906 }
2907
Yan, Zhengc2167752009-11-12 09:34:21 +00002908 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002910
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002911 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002912 if (freespace_inode)
2913 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002914 else
2915 trans = btrfs_join_transaction(root);
2916 if (IS_ERR(trans)) {
2917 ret = PTR_ERR(trans);
2918 trans = NULL;
2919 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002920 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002921 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002922 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002923 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002924 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002925 goto out;
2926 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002927
Filipe Manana2766ff62020-11-04 11:07:34 +00002928 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002929 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002930
Nikolay Borisov8d510122019-10-08 20:43:06 +03002931 if (freespace_inode)
2932 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002933 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002934 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002935 if (IS_ERR(trans)) {
2936 ret = PTR_ERR(trans);
2937 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002938 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002939 }
Chris Masona79b7d42014-05-22 16:18:52 -07002940
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002941 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002942
Chris Masonc8b97812008-10-29 14:49:59 -04002943 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002944 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002945 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002946 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002947 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002948 ordered_extent->file_offset,
2949 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002950 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002951 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002952 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002953 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002954 if (!ret) {
2955 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002956 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002957 ordered_extent->disk_bytenr,
2958 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002959 }
Yan Zhengd899e052008-10-30 14:25:28 -04002960 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002961 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002962 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002963 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002964 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002965 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002966 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002967
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002968 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002969 if (ret) {
2970 btrfs_abort_transaction(trans, ret);
2971 goto out;
2972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Filipe Manana2766ff62020-11-04 11:07:34 +00002974 /*
2975 * If this is a new delalloc range, clear its new delalloc flag to
2976 * update the inode's number of bytes. This needs to be done first
2977 * before updating the inode item.
2978 */
2979 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2980 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002981 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002982 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2983 0, 0, &cached_state);
2984
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002985 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002986 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002987 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002988 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002989 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002990 }
2991 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002992out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002993 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002994 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002995 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002996
Miao Xiea698d0752012-09-20 01:51:59 -06002997 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002998 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002999
Josef Bacik77cef2e2013-08-29 13:57:21 -04003000 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003001 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003002
Josef Bacikd61bec02021-05-19 09:38:27 -04003003 /*
3004 * If we failed to finish this ordered extent for any reason we
3005 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3006 * extent, and mark the inode with the error if it wasn't
3007 * already set. Any error during writeback would have already
3008 * set the mapping error, so we need to set it if we're the ones
3009 * marking this ordered extent as failed.
3010 */
3011 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3012 &ordered_extent->flags))
3013 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3014
Josef Bacik77cef2e2013-08-29 13:57:21 -04003015 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003016 unwritten_start += logical_len;
3017 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003018
3019 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003020 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003021
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003022 /*
3023 * If the ordered extent had an IOERR or something else went
3024 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003025 * back to the allocator. We only free the extent in the
3026 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003027 *
3028 * If we made it past insert_reserved_file_extent before we
3029 * errored out then we don't need to do this as the accounting
3030 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003031 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003032 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003033 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003034 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003035 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3036 /*
3037 * Discard the range before returning it back to the
3038 * free space pool
3039 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003040 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003041 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003042 ordered_extent->disk_bytenr,
3043 ordered_extent->disk_num_bytes,
3044 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003045 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003046 ordered_extent->disk_bytenr,
3047 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003048 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003049 }
3050
Josef Bacik5fd02042012-05-02 14:00:54 -04003051 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003052 * This needs to be done to make sure anybody waiting knows we are done
3053 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003054 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003055 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003056
Chris Masone6dcd2d2008-07-17 12:53:50 -04003057 /* once for us */
3058 btrfs_put_ordered_extent(ordered_extent);
3059 /* once for the tree */
3060 btrfs_put_ordered_extent(ordered_extent);
3061
Josef Bacik5fd02042012-05-02 14:00:54 -04003062 return ret;
3063}
3064
3065static void finish_ordered_fn(struct btrfs_work *work)
3066{
3067 struct btrfs_ordered_extent *ordered_extent;
3068 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3069 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003070}
3071
Nikolay Borisovc6297322018-11-08 10:18:08 +02003072void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3073 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003074{
Nikolay Borisov3347c482020-08-31 14:42:44 +03003075 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3076 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04003077 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003078 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003079
liubo1abe9b82011-03-24 11:18:59 +00003080 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3081
Chris Mason8b62b722009-09-02 16:53:46 -04003082 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03003083 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3084 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003085 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003086
Nikolay Borisov3347c482020-08-31 14:42:44 +03003087 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003089 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003090 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003091
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003092 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003093 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003094}
3095
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003096/*
3097 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003098 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003099 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003100 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003101 * @page: page where is the data to be verified
3102 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003103 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003104 *
3105 * The length of such check is always one sector size.
3106 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003107static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003108 u32 bio_offset, struct page *page, u32 pgoff,
3109 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003110{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003111 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3112 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003113 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003114 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003115 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003116 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003117 u8 *csum_expected;
3118 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003119
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003120 ASSERT(pgoff + len <= PAGE_SIZE);
3121
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003122 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3123 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003124
3125 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003126 shash->tfm = fs_info->csum_shash;
3127
Eric Biggersfd080012020-04-30 23:51:59 -07003128 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003129
3130 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003131 goto zeroit;
3132
3133 kunmap_atomic(kaddr);
3134 return 0;
3135zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003136 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3137 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003138 if (io_bio->device)
3139 btrfs_dev_stat_inc_and_print(io_bio->device,
3140 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003141 memset(kaddr + pgoff, 1, len);
3142 flush_dcache_page(page);
3143 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003144 return -EIO;
3145}
3146
Chris Masond352ac62008-09-29 15:18:18 -04003147/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003148 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003149 * if there's a match, we allow the bio to finish. If not, the code in
3150 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003151 *
3152 * @bio_offset: offset to the beginning of the bio (in bytes)
3153 * @start: file offset of the range start
3154 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003155 *
3156 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3157 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003158 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003159unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3160 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003161{
Chris Mason07157aa2007-08-30 08:50:51 -04003162 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003163 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003164 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003165 const u32 sectorsize = root->fs_info->sectorsize;
3166 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003167 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003168
Chris Masond20f7042008-12-08 16:58:54 -05003169 if (PageChecked(page)) {
3170 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003171 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003172 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003173
3174 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003175 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003176
Josef Bacik42437a62020-10-16 11:29:18 -04003177 if (!root->fs_info->csum_root)
3178 return 0;
3179
Yan Zheng17d217f2008-12-12 10:03:38 -05003180 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003181 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003182 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003183 return 0;
3184 }
3185
Qu Wenruof44cf412020-12-02 14:47:59 +08003186 ASSERT(page_offset(page) <= start &&
3187 end <= page_offset(page) + PAGE_SIZE - 1);
3188 for (pg_off = offset_in_page(start);
3189 pg_off < offset_in_page(end);
3190 pg_off += sectorsize, bio_offset += sectorsize) {
3191 int ret;
3192
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003193 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3194 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003195 if (ret < 0) {
3196 const int nr_bit = (pg_off - offset_in_page(start)) >>
3197 root->fs_info->sectorsize_bits;
3198
3199 result |= (1U << nr_bit);
3200 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003201 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003202 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003203}
Chris Masonb888db22007-08-27 16:49:44 -04003204
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003205/*
3206 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3207 *
3208 * @inode: The inode we want to perform iput on
3209 *
3210 * This function uses the generic vfs_inode::i_count to track whether we should
3211 * just decrement it (in case it's > 1) or if this is the last iput then link
3212 * the inode to the delayed iput machinery. Delayed iputs are processed at
3213 * transaction commit time/superblock commit/cleaner kthread.
3214 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003215void btrfs_add_delayed_iput(struct inode *inode)
3216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003217 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003218 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219
3220 if (atomic_add_unless(&inode->i_count, -1, 1))
3221 return;
3222
Josef Bacik034f7842018-12-03 11:06:52 -05003223 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003225 ASSERT(list_empty(&binode->delayed_iput));
3226 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003227 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003228 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3229 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003230}
3231
Josef Bacik63611e72019-06-18 10:59:18 -04003232static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3233 struct btrfs_inode *inode)
3234{
3235 list_del_init(&inode->delayed_iput);
3236 spin_unlock(&fs_info->delayed_iput_lock);
3237 iput(&inode->vfs_inode);
3238 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3239 wake_up(&fs_info->delayed_iputs_wait);
3240 spin_lock(&fs_info->delayed_iput_lock);
3241}
3242
3243static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3244 struct btrfs_inode *inode)
3245{
3246 if (!list_empty(&inode->delayed_iput)) {
3247 spin_lock(&fs_info->delayed_iput_lock);
3248 if (!list_empty(&inode->delayed_iput))
3249 run_delayed_iput_locked(fs_info, inode);
3250 spin_unlock(&fs_info->delayed_iput_lock);
3251 }
3252}
3253
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003254void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003255{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003257 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003258 while (!list_empty(&fs_info->delayed_iputs)) {
3259 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260
David Sterba8089fe62015-11-19 14:15:51 +01003261 inode = list_first_entry(&fs_info->delayed_iputs,
3262 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003263 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003264 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265 }
David Sterba8089fe62015-11-19 14:15:51 +01003266 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003267}
3268
Josef Bacik034f7842018-12-03 11:06:52 -05003269/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003270 * Wait for flushing all delayed iputs
3271 *
3272 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003273 *
3274 * This will wait on any delayed iputs that are currently running with KILLABLE
3275 * set. Once they are all done running we will return, unless we are killed in
3276 * which case we return EINTR. This helps in user operations like fallocate etc
3277 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003278 *
3279 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003280 */
3281int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3282{
3283 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3284 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3285 if (ret)
3286 return -EINTR;
3287 return 0;
3288}
3289
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003291 * This creates an orphan entry for the given inode in case something goes wrong
3292 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003293 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003294int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003295 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003296{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003298
Omar Sandoval27919062018-05-11 13:13:37 -07003299 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3300 if (ret && ret != -EEXIST) {
3301 btrfs_abort_transaction(trans, ret);
3302 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 }
3304
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003306}
3307
3308/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003309 * We have done the delete so we can go ahead and remove the orphan item for
3310 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003311 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003312static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003313 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003314{
Omar Sandoval27919062018-05-11 13:13:37 -07003315 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003316}
3317
3318/*
3319 * this cleans up any orphans that may be left on the list from the last use
3320 * of this root.
3321 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003322int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003323{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003324 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003325 struct btrfs_path *path;
3326 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 struct btrfs_key key, found_key;
3328 struct btrfs_trans_handle *trans;
3329 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003330 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003331 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003332
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003334 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003335
3336 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003337 if (!path) {
3338 ret = -ENOMEM;
3339 goto out;
3340 }
David Sterbae4058b52015-11-27 16:31:35 +01003341 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003342
3343 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003344 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003345 key.offset = (u64)-1;
3346
Josef Bacik7b128762008-07-24 12:17:14 -04003347 while (1) {
3348 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003349 if (ret < 0)
3350 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003351
3352 /*
3353 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003354 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003355 * find the key and see if we have stuff that matches
3356 */
3357 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003358 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003359 if (path->slots[0] == 0)
3360 break;
3361 path->slots[0]--;
3362 }
3363
3364 /* pull out the item */
3365 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3367
3368 /* make sure the item matches what we want */
3369 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3370 break;
David Sterba962a2982014-06-04 18:41:45 +02003371 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003372 break;
3373
3374 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003375 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003376
3377 /*
3378 * this is where we are basically btrfs_lookup, without the
3379 * crossing root thing. we store the inode number in the
3380 * offset of the orphan item.
3381 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003382
3383 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003384 btrfs_err(fs_info,
3385 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003386 ret = -EINVAL;
3387 goto out;
3388 }
3389
3390 last_objectid = found_key.offset;
3391
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003392 found_key.objectid = found_key.offset;
3393 found_key.type = BTRFS_INODE_ITEM_KEY;
3394 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003395 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303396 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003397 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003399
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003401 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003402 int is_dead_root = 0;
3403
3404 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003405 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003406 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003407 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003408 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003409 * We need to distinguish those two, as the orphan item
3410 * for a root must not get deleted before the deletion
3411 * of the snapshot/subvolume's tree completes.
3412 *
3413 * btrfs_find_orphan_roots() ran before us, which has
3414 * found all deleted roots and loaded them into
3415 * fs_info->fs_roots_radix. So here we can find if an
3416 * orphan item corresponds to a deleted root by looking
3417 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003418 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003419
3420 spin_lock(&fs_info->fs_roots_radix_lock);
3421 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3422 (unsigned long)found_key.objectid);
3423 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3424 is_dead_root = 1;
3425 spin_unlock(&fs_info->fs_roots_radix_lock);
3426
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003427 if (is_dead_root) {
3428 /* prevent this orphan from being found again */
3429 key.offset = found_key.objectid - 1;
3430 continue;
3431 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003432
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003433 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003434
Josef Bacika8c9e572011-09-21 16:55:59 -04003435 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003436 * If we have an inode with links, there are a couple of
3437 * possibilities. Old kernels (before v3.12) used to create an
3438 * orphan item for truncate indicating that there were possibly
3439 * extent items past i_size that needed to be deleted. In v3.12,
3440 * truncate was changed to update i_size in sync with the extent
3441 * items, but the (useless) orphan item was still created. Since
3442 * v4.18, we don't create the orphan item for truncate at all.
3443 *
3444 * So, this item could mean that we need to do a truncate, but
3445 * only if this filesystem was last used on a pre-v3.12 kernel
3446 * and was not cleanly unmounted. The odds of that are quite
3447 * slim, and it's a pain to do the truncate now, so just delete
3448 * the orphan item.
3449 *
3450 * It's also possible that this orphan item was supposed to be
3451 * deleted but wasn't. The inode number may have been reused,
3452 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003453 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003454 if (ret == -ENOENT || inode->i_nlink) {
3455 if (!ret)
3456 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003457 trans = btrfs_start_transaction(root, 1);
3458 if (IS_ERR(trans)) {
3459 ret = PTR_ERR(trans);
3460 goto out;
3461 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003462 btrfs_debug(fs_info, "auto deleting %Lu",
3463 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003464 ret = btrfs_del_orphan_item(trans, root,
3465 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003466 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003467 if (ret)
3468 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003469 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470 }
Josef Bacik7b128762008-07-24 12:17:14 -04003471
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003472 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003473
3474 /* this will do delete_inode and everything for us */
3475 iput(inode);
3476 }
Miao Xie3254c872011-11-10 20:45:05 -05003477 /* release the path since we're done with it */
3478 btrfs_release_path(path);
3479
Yan, Zhengd68fc572010-05-16 10:49:58 -04003480 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3481
Omar Sandovala575cee2018-05-11 13:13:38 -07003482 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003483 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003485 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003486 }
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003489 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003490
3491out:
3492 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003493 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003494 btrfs_free_path(path);
3495 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003496}
3497
Chris Masond352ac62008-09-29 15:18:18 -04003498/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003499 * very simple check to peek ahead in the leaf looking for xattrs. If we
3500 * don't find any xattrs, we know there can't be any acls.
3501 *
3502 * slot is the slot the inode is in, objectid is the objectid of the inode
3503 */
3504static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003505 int slot, u64 objectid,
3506 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003507{
3508 u32 nritems = btrfs_header_nritems(leaf);
3509 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003510 static u64 xattr_access = 0;
3511 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003512 int scanned = 0;
3513
Josef Bacikf23b5a52013-06-19 10:16:26 -04003514 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003515 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3516 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3517 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3518 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003519 }
3520
Chris Mason46a53cc2009-04-27 11:47:50 -04003521 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003522 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003523 while (slot < nritems) {
3524 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3525
3526 /* we found a different objectid, there must not be acls */
3527 if (found_key.objectid != objectid)
3528 return 0;
3529
3530 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003531 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003532 if (*first_xattr_slot == -1)
3533 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003534 if (found_key.offset == xattr_access ||
3535 found_key.offset == xattr_default)
3536 return 1;
3537 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003538
3539 /*
3540 * we found a key greater than an xattr key, there can't
3541 * be any acls later on
3542 */
3543 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3544 return 0;
3545
3546 slot++;
3547 scanned++;
3548
3549 /*
3550 * it goes inode, inode backrefs, xattrs, extents,
3551 * so if there are a ton of hard links to an inode there can
3552 * be a lot of backrefs. Don't waste time searching too hard,
3553 * this is just an optimization
3554 */
3555 if (scanned >= 8)
3556 break;
3557 }
3558 /* we hit the end of the leaf before we found an xattr or
3559 * something larger than an xattr. We have to assume the inode
3560 * has acls
3561 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003562 if (*first_xattr_slot == -1)
3563 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003564 return 1;
3565}
3566
3567/*
Chris Masond352ac62008-09-29 15:18:18 -04003568 * read an inode from the btree into the in-memory inode
3569 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003570static int btrfs_read_locked_inode(struct inode *inode,
3571 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003573 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003574 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003575 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003576 struct btrfs_inode_item *inode_item;
3577 struct btrfs_root *root = BTRFS_I(inode)->root;
3578 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003579 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003580 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003581 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003582 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003583 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003584 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003585
3586 ret = btrfs_fill_inode(inode, &rdev);
3587 if (!ret)
3588 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003589
Filipe Manana4222ea72018-10-24 10:13:03 +01003590 if (!path) {
3591 path = btrfs_alloc_path();
3592 if (!path)
3593 return -ENOMEM;
3594 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003595
Chris Mason39279cc2007-06-12 06:35:45 -04003596 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003597
Chris Mason39279cc2007-06-12 06:35:45 -04003598 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003599 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003600 if (path != in_path)
3601 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003602 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003603 }
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
3607 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003608 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3611 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003613 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003614 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3615 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003616 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003617 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3618 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbaa937b972014-12-12 17:39:12 +01003620 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3621 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
David Sterbaa937b972014-12-12 17:39:12 +01003623 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3624 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003625
David Sterbaa937b972014-12-12 17:39:12 +01003626 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3627 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
chandan r9cc97d62012-07-04 12:48:07 +05303629 BTRFS_I(inode)->i_otime.tv_sec =
3630 btrfs_timespec_sec(leaf, &inode_item->otime);
3631 BTRFS_I(inode)->i_otime.tv_nsec =
3632 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003634 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003635 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003636 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3637
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003638 inode_set_iversion_queried(inode,
3639 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003640 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003641 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003642 rdev = btrfs_inode_rdev(leaf, inode_item);
3643
Josef Bacikaec74772008-07-24 12:12:38 -04003644 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003645 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003646
3647cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003648 /*
3649 * If we were modified in the current generation and evicted from memory
3650 * and then re-read we need to do a full sync since we don't have any
3651 * idea about which extents were modified before we were evicted from
3652 * cache.
3653 *
3654 * This is required for both inode re-read from disk and delayed inode
3655 * in delayed_nodes_tree.
3656 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003657 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003658 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3659 &BTRFS_I(inode)->runtime_flags);
3660
Filipe Mananabde6c242015-07-24 00:00:19 +01003661 /*
3662 * We don't persist the id of the transaction where an unlink operation
3663 * against the inode was last made. So here we assume the inode might
3664 * have been evicted, and therefore the exact value of last_unlink_trans
3665 * lost, and set it to last_trans to avoid metadata inconsistencies
3666 * between the inode and its parent if the inode is fsync'ed and the log
3667 * replayed. For example, in the scenario:
3668 *
3669 * touch mydir/foo
3670 * ln mydir/foo mydir/bar
3671 * sync
3672 * unlink mydir/bar
3673 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3674 * xfs_io -c fsync mydir/foo
3675 * <power failure>
3676 * mount fs, triggers fsync log replay
3677 *
3678 * We must make sure that when we fsync our inode foo we also log its
3679 * parent inode, otherwise after log replay the parent still has the
3680 * dentry with the "bar" name but our inode foo has a link count of 1
3681 * and doesn't have an inode ref with the name "bar" anymore.
3682 *
3683 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003684 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003685 * transaction commits on fsync if our inode is a directory, or if our
3686 * inode is not a directory, logging its parent unnecessarily.
3687 */
3688 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3689
Filipe Manana3ebac172020-07-15 12:30:43 +01003690 /*
3691 * Same logic as for last_unlink_trans. We don't persist the generation
3692 * of the last transaction where this inode was used for a reflink
3693 * operation, so after eviction and reloading the inode we must be
3694 * pessimistic and assume the last transaction that modified the inode.
3695 */
3696 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3697
Miao Xie67de1172013-12-26 13:07:06 +08003698 path->slots[0]++;
3699 if (inode->i_nlink != 1 ||
3700 path->slots[0] >= btrfs_header_nritems(leaf))
3701 goto cache_acl;
3702
3703 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003704 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003705 goto cache_acl;
3706
3707 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3708 if (location.type == BTRFS_INODE_REF_KEY) {
3709 struct btrfs_inode_ref *ref;
3710
3711 ref = (struct btrfs_inode_ref *)ptr;
3712 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3713 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3714 struct btrfs_inode_extref *extref;
3715
3716 extref = (struct btrfs_inode_extref *)ptr;
3717 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3718 extref);
3719 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003720cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003721 /*
3722 * try to precache a NULL acl entry for files that don't have
3723 * any xattrs or acls
3724 */
Li Zefan33345d012011-04-20 10:31:50 +08003725 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003726 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003727 if (first_xattr_slot != -1) {
3728 path->slots[0] = first_xattr_slot;
3729 ret = btrfs_load_inode_props(inode, path);
3730 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003731 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003732 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003733 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003734 root->root_key.objectid, ret);
3735 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003736 if (path != in_path)
3737 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003738
Al Viro72c04902009-06-24 16:58:48 -04003739 if (!maybe_acls)
3740 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003741
Chris Mason39279cc2007-06-12 06:35:45 -04003742 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003743 case S_IFREG:
3744 inode->i_mapping->a_ops = &btrfs_aops;
3745 inode->i_fop = &btrfs_file_operations;
3746 inode->i_op = &btrfs_file_inode_operations;
3747 break;
3748 case S_IFDIR:
3749 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003750 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003751 break;
3752 case S_IFLNK:
3753 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003754 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003755 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003756 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003757 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003758 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003759 init_special_inode(inode, inode->i_mode, rdev);
3760 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003762
David Sterba7b6a2212018-03-26 18:40:21 +02003763 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003764 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003765}
3766
Chris Masond352ac62008-09-29 15:18:18 -04003767/*
3768 * given a leaf and an inode, copy the inode fields into the leaf
3769 */
Chris Masone02119d2008-09-05 16:13:11 -04003770static void fill_inode_item(struct btrfs_trans_handle *trans,
3771 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003772 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003773 struct inode *inode)
3774{
Liu Bo51fab692012-12-27 09:01:21 +00003775 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003776
David Sterbac82f8232019-08-09 17:48:21 +02003777 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003778
David Sterbacc4c13d2020-04-29 02:15:56 +02003779 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3780 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3781 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3782 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3783 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
David Sterbacc4c13d2020-04-29 02:15:56 +02003785 btrfs_set_token_timespec_sec(&token, &item->atime,
3786 inode->i_atime.tv_sec);
3787 btrfs_set_token_timespec_nsec(&token, &item->atime,
3788 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbacc4c13d2020-04-29 02:15:56 +02003790 btrfs_set_token_timespec_sec(&token, &item->mtime,
3791 inode->i_mtime.tv_sec);
3792 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3793 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003794
David Sterbacc4c13d2020-04-29 02:15:56 +02003795 btrfs_set_token_timespec_sec(&token, &item->ctime,
3796 inode->i_ctime.tv_sec);
3797 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3798 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003799
David Sterbacc4c13d2020-04-29 02:15:56 +02003800 btrfs_set_token_timespec_sec(&token, &item->otime,
3801 BTRFS_I(inode)->i_otime.tv_sec);
3802 btrfs_set_token_timespec_nsec(&token, &item->otime,
3803 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303804
David Sterbacc4c13d2020-04-29 02:15:56 +02003805 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3806 btrfs_set_token_inode_generation(&token, item,
3807 BTRFS_I(inode)->generation);
3808 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3809 btrfs_set_token_inode_transid(&token, item, trans->transid);
3810 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3811 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3812 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003813}
3814
Chris Masond352ac62008-09-29 15:18:18 -04003815/*
3816 * copy everything in the in-memory inode into the btree.
3817 */
Chris Mason21151332011-11-10 20:39:08 -05003818static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003819 struct btrfs_root *root,
3820 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003821{
3822 struct btrfs_inode_item *inode_item;
3823 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003824 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003825 int ret;
3826
3827 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003828 if (!path)
3829 return -ENOMEM;
3830
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003831 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 if (ret) {
3833 if (ret > 0)
3834 ret = -ENOENT;
3835 goto failed;
3836 }
3837
Chris Mason5f39d392007-10-15 16:14:19 -04003838 leaf = path->nodes[0];
3839 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003840 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003841
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003842 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003843 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003844 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003845 ret = 0;
3846failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003847 btrfs_free_path(path);
3848 return ret;
3849}
3850
Chris Masond352ac62008-09-29 15:18:18 -04003851/*
Chris Mason21151332011-11-10 20:39:08 -05003852 * copy everything in the in-memory inode into the btree.
3853 */
3854noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003855 struct btrfs_root *root,
3856 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003858 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003859 int ret;
3860
3861 /*
3862 * If the inode is a free space inode, we can deadlock during commit
3863 * if we put it into the delayed code.
3864 *
3865 * The data relocation inode should also be directly updated
3866 * without delay
3867 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003868 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003869 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003870 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003871 btrfs_update_root_times(trans, root);
3872
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003873 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003874 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003875 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003876 return ret;
3877 }
3878
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003879 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003880}
3881
Nikolay Borisov729f7962020-11-02 16:49:06 +02003882int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003884{
3885 int ret;
3886
Nikolay Borisov729f7962020-11-02 16:49:06 +02003887 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003888 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003889 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003890 return ret;
3891}
3892
3893/*
Chris Masond352ac62008-09-29 15:18:18 -04003894 * unlink helper that gets used here in inode.c and in the tree logging
3895 * recovery code. It remove a link in a directory with a given name, and
3896 * also drops the back refs in the inode to the directory
3897 */
Al Viro92986792011-03-04 17:14:37 +00003898static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003900 struct btrfs_inode *dir,
3901 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003902 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003904 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003908 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003909 u64 ino = btrfs_ino(inode);
3910 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003911
3912 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003913 if (!path) {
3914 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003915 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003916 }
3917
Li Zefan33345d012011-04-20 10:31:50 +08003918 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003919 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003920 if (IS_ERR_OR_NULL(di)) {
3921 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003922 goto err;
3923 }
Chris Mason39279cc2007-06-12 06:35:45 -04003924 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003925 if (ret)
3926 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003927 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003928
Miao Xie67de1172013-12-26 13:07:06 +08003929 /*
3930 * If we don't have dir index, we have to get it by looking up
3931 * the inode ref, since we get the inode ref, remove it directly,
3932 * it is unnecessary to do delayed deletion.
3933 *
3934 * But if we have dir index, needn't search inode ref to get it.
3935 * Since the inode ref is close to the inode item, it is better
3936 * that we delay to delete it, and just do this deletion when
3937 * we update the inode item.
3938 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003939 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003940 ret = btrfs_delayed_delete_inode_ref(inode);
3941 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003942 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003943 goto skip_backref;
3944 }
3945 }
3946
Li Zefan33345d012011-04-20 10:31:50 +08003947 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3948 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003949 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003950 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003951 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003952 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003953 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003954 goto err;
3955 }
Miao Xie67de1172013-12-26 13:07:06 +08003956skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003957 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003959 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003960 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003961 }
Chris Mason39279cc2007-06-12 06:35:45 -04003962
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003963 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3964 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003965 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003966 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 goto err;
3968 }
Chris Masone02119d2008-09-05 16:13:11 -04003969
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003970 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3971 index);
Chris Mason6418c962010-10-30 07:34:24 -04003972 if (ret == -ENOENT)
3973 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003974 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003975 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003976
3977 /*
3978 * If we have a pending delayed iput we could end up with the final iput
3979 * being run in btrfs-cleaner context. If we have enough of these built
3980 * up we can end up burning a lot of time in btrfs-cleaner without any
3981 * way to throttle the unlinks. Since we're currently holding a ref on
3982 * the inode we can run the delayed iput here without any issues as the
3983 * final iput won't be done until after we drop the ref we're currently
3984 * holding.
3985 */
3986 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003987err:
3988 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003989 if (ret)
3990 goto out;
3991
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003992 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003993 inode_inc_iversion(&inode->vfs_inode);
3994 inode_inc_iversion(&dir->vfs_inode);
3995 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3996 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003997 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003998out:
Chris Mason39279cc2007-06-12 06:35:45 -04003999 return ret;
4000}
4001
Al Viro92986792011-03-04 17:14:37 +00004002int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004004 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004005 const char *name, int name_len)
4006{
4007 int ret;
4008 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4009 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004010 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004011 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004012 }
4013 return ret;
4014}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004015
4016/*
4017 * helper to start transaction for unlink and rmdir.
4018 *
Josef Bacikd52be812013-05-29 14:54:47 -04004019 * unlink and rmdir are special in btrfs, they do not always free space, so
4020 * if we cannot make our reservations the normal way try and see if there is
4021 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4022 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 */
Josef Bacikd52be812013-05-29 14:54:47 -04004024static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027
Josef Bacike70bea52011-10-11 14:18:24 -04004028 /*
4029 * 1 for the possible orphan item
4030 * 1 for the dir item
4031 * 1 for the dir index
4032 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004033 * 1 for the inode
4034 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004035 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004036}
4037
Chris Mason39279cc2007-06-12 06:35:45 -04004038static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4039{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004040 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004042 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 int ret;
4044
Josef Bacikd52be812013-05-29 14:54:47 -04004045 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004047 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004048
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004049 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4050 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004051
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004052 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4053 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4054 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004055 if (ret)
4056 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004057
Yan, Zhenga22285a2010-05-16 10:48:46 -04004058 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004059 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004060 if (ret)
4061 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 }
Josef Bacik7b128762008-07-24 12:17:14 -04004063
Tsutomu Itohb5324022011-07-19 07:27:20 +00004064out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004065 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004066 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004067 return ret;
4068}
4069
Misono Tomohirof60a2362018-04-18 11:34:52 +09004070static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004071 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004073 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004074 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075 struct btrfs_path *path;
4076 struct extent_buffer *leaf;
4077 struct btrfs_dir_item *di;
4078 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004079 const char *name = dentry->d_name.name;
4080 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 u64 index;
4082 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004083 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004084 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085
Josef Bacik045d3962019-12-18 17:20:27 -05004086 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4087 objectid = inode->root->root_key.objectid;
4088 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4089 objectid = inode->location.objectid;
4090 } else {
4091 WARN_ON(1);
4092 return -EINVAL;
4093 }
4094
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004095 path = btrfs_alloc_path();
4096 if (!path)
4097 return -ENOMEM;
4098
Li Zefan33345d012011-04-20 10:31:50 +08004099 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004102 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 goto out;
4104 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
4106 leaf = path->nodes[0];
4107 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4108 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4109 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004111 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 goto out;
4113 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004114 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115
Josef Bacikd49d3282019-12-18 17:20:28 -05004116 /*
4117 * This is a placeholder inode for a subvolume we didn't have a
4118 * reference to at the time of the snapshot creation. In the meantime
4119 * we could have renamed the real subvol link into our snapshot, so
4120 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4121 * Instead simply lookup the dir_index_item for this entry so we can
4122 * remove it. Otherwise we know we have a ref to the root and we can
4123 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4124 */
4125 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004126 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (IS_ERR_OR_NULL(di)) {
4129 if (!di)
4130 ret = -ENOENT;
4131 else
4132 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004133 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 goto out;
4135 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136
4137 leaf = path->nodes[0];
4138 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004139 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004140 btrfs_release_path(path);
4141 } else {
4142 ret = btrfs_del_root_ref(trans, objectid,
4143 root->root_key.objectid, dir_ino,
4144 &index, name, name_len);
4145 if (ret) {
4146 btrfs_abort_transaction(trans, ret);
4147 goto out;
4148 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150
Lu Fengqi9add2942018-08-01 11:32:26 +08004151 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004153 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 goto out;
4155 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004156
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004157 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004158 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004159 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004160 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004162 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004164 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166}
4167
Misono Tomohiroec42f162018-04-18 11:34:13 +09004168/*
4169 * Helper to check if the subvolume references other subvolumes or if it's
4170 * default.
4171 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004172static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004173{
4174 struct btrfs_fs_info *fs_info = root->fs_info;
4175 struct btrfs_path *path;
4176 struct btrfs_dir_item *di;
4177 struct btrfs_key key;
4178 u64 dir_id;
4179 int ret;
4180
4181 path = btrfs_alloc_path();
4182 if (!path)
4183 return -ENOMEM;
4184
4185 /* Make sure this root isn't set as the default subvol */
4186 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4187 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4188 dir_id, "default", 7, 0);
4189 if (di && !IS_ERR(di)) {
4190 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4191 if (key.objectid == root->root_key.objectid) {
4192 ret = -EPERM;
4193 btrfs_err(fs_info,
4194 "deleting default subvolume %llu is not allowed",
4195 key.objectid);
4196 goto out;
4197 }
4198 btrfs_release_path(path);
4199 }
4200
4201 key.objectid = root->root_key.objectid;
4202 key.type = BTRFS_ROOT_REF_KEY;
4203 key.offset = (u64)-1;
4204
4205 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4206 if (ret < 0)
4207 goto out;
4208 BUG_ON(ret == 0);
4209
4210 ret = 0;
4211 if (path->slots[0] > 0) {
4212 path->slots[0]--;
4213 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4214 if (key.objectid == root->root_key.objectid &&
4215 key.type == BTRFS_ROOT_REF_KEY)
4216 ret = -ENOTEMPTY;
4217 }
4218out:
4219 btrfs_free_path(path);
4220 return ret;
4221}
4222
Nikolay Borisov20a68002018-04-27 14:36:24 +03004223/* Delete all dentries for inodes belonging to the root */
4224static void btrfs_prune_dentries(struct btrfs_root *root)
4225{
4226 struct btrfs_fs_info *fs_info = root->fs_info;
4227 struct rb_node *node;
4228 struct rb_node *prev;
4229 struct btrfs_inode *entry;
4230 struct inode *inode;
4231 u64 objectid = 0;
4232
4233 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4234 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4235
4236 spin_lock(&root->inode_lock);
4237again:
4238 node = root->inode_tree.rb_node;
4239 prev = NULL;
4240 while (node) {
4241 prev = node;
4242 entry = rb_entry(node, struct btrfs_inode, rb_node);
4243
David Sterba37508512018-06-29 10:56:40 +02004244 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004245 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004246 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004247 node = node->rb_right;
4248 else
4249 break;
4250 }
4251 if (!node) {
4252 while (prev) {
4253 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004254 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004255 node = prev;
4256 break;
4257 }
4258 prev = rb_next(prev);
4259 }
4260 }
4261 while (node) {
4262 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004263 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004264 inode = igrab(&entry->vfs_inode);
4265 if (inode) {
4266 spin_unlock(&root->inode_lock);
4267 if (atomic_read(&inode->i_count) > 1)
4268 d_prune_aliases(inode);
4269 /*
4270 * btrfs_drop_inode will have it removed from the inode
4271 * cache when its usage count hits zero.
4272 */
4273 iput(inode);
4274 cond_resched();
4275 spin_lock(&root->inode_lock);
4276 goto again;
4277 }
4278
4279 if (cond_resched_lock(&root->inode_lock))
4280 goto again;
4281
4282 node = rb_next(node);
4283 }
4284 spin_unlock(&root->inode_lock);
4285}
4286
Misono Tomohirof60a2362018-04-18 11:34:52 +09004287int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4288{
4289 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4290 struct btrfs_root *root = BTRFS_I(dir)->root;
4291 struct inode *inode = d_inode(dentry);
4292 struct btrfs_root *dest = BTRFS_I(inode)->root;
4293 struct btrfs_trans_handle *trans;
4294 struct btrfs_block_rsv block_rsv;
4295 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004296 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004297
4298 /*
4299 * Don't allow to delete a subvolume with send in progress. This is
4300 * inside the inode lock so the error handling that has to drop the bit
4301 * again is not run concurrently.
4302 */
4303 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004304 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004305 spin_unlock(&dest->root_item_lock);
4306 btrfs_warn(fs_info,
4307 "attempt to delete subvolume %llu during send",
4308 dest->root_key.objectid);
4309 return -EPERM;
4310 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004311 root_flags = btrfs_root_flags(&dest->root_item);
4312 btrfs_set_root_flags(&dest->root_item,
4313 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4314 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004315
4316 down_write(&fs_info->subvol_sem);
4317
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004318 ret = may_destroy_subvol(dest);
4319 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004320 goto out_up_write;
4321
4322 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4323 /*
4324 * One for dir inode,
4325 * two for dir entries,
4326 * two for root ref/backref.
4327 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004328 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4329 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004330 goto out_up_write;
4331
4332 trans = btrfs_start_transaction(root, 0);
4333 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004334 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004335 goto out_release;
4336 }
4337 trans->block_rsv = &block_rsv;
4338 trans->bytes_reserved = block_rsv.size;
4339
4340 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4341
Josef Bacik045d3962019-12-18 17:20:27 -05004342 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004343 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004344 btrfs_abort_transaction(trans, ret);
4345 goto out_end_trans;
4346 }
4347
Josef Bacik2731f512021-03-12 15:25:04 -05004348 ret = btrfs_record_root_in_trans(trans, dest);
4349 if (ret) {
4350 btrfs_abort_transaction(trans, ret);
4351 goto out_end_trans;
4352 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004353
4354 memset(&dest->root_item.drop_progress, 0,
4355 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004356 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004357 btrfs_set_root_refs(&dest->root_item, 0);
4358
4359 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4360 ret = btrfs_insert_orphan_item(trans,
4361 fs_info->tree_root,
4362 dest->root_key.objectid);
4363 if (ret) {
4364 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004365 goto out_end_trans;
4366 }
4367 }
4368
Lu Fengqid1957792018-05-29 15:01:54 +08004369 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004370 BTRFS_UUID_KEY_SUBVOL,
4371 dest->root_key.objectid);
4372 if (ret && ret != -ENOENT) {
4373 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004374 goto out_end_trans;
4375 }
4376 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004377 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004378 dest->root_item.received_uuid,
4379 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4380 dest->root_key.objectid);
4381 if (ret && ret != -ENOENT) {
4382 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004383 goto out_end_trans;
4384 }
4385 }
4386
Qu Wenruo082b6c92020-06-16 10:17:37 +08004387 free_anon_bdev(dest->anon_dev);
4388 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004389out_end_trans:
4390 trans->block_rsv = NULL;
4391 trans->bytes_reserved = 0;
4392 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004393 inode->i_flags |= S_DEAD;
4394out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004395 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004396out_up_write:
4397 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004398 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004399 spin_lock(&dest->root_item_lock);
4400 root_flags = btrfs_root_flags(&dest->root_item);
4401 btrfs_set_root_flags(&dest->root_item,
4402 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4403 spin_unlock(&dest->root_item_lock);
4404 } else {
4405 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004406 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004407 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004408 }
4409
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004410 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004411}
4412
Chris Mason39279cc2007-06-12 06:35:45 -04004413static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4414{
David Howells2b0143b2015-03-17 22:25:59 +00004415 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004416 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004418 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004419 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004420
David Sterbab3ae2442012-09-13 16:04:34 -06004421 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004422 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004423 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004424 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004425
Josef Bacikd52be812013-05-29 14:54:47 -04004426 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004427 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004428 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004429
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004430 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004431 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004432 goto out;
4433 }
4434
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004435 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004436 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004437 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004438
Filipe Manana44f714d2016-06-06 16:11:13 +01004439 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4440
Chris Mason39279cc2007-06-12 06:35:45 -04004441 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004442 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4443 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4444 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004445 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004446 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004447 /*
4448 * Propagate the last_unlink_trans value of the deleted dir to
4449 * its parent directory. This is to prevent an unrecoverable
4450 * log tree in the case we do something like this:
4451 * 1) create dir foo
4452 * 2) create snapshot under dir foo
4453 * 3) delete the snapshot
4454 * 4) rmdir foo
4455 * 5) mkdir foo
4456 * 6) fsync foo or some file inside foo
4457 */
4458 if (last_unlink_trans >= trans->transid)
4459 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4460 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004461out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004462 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004463 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004464
Chris Mason39279cc2007-06-12 06:35:45 -04004465 return err;
4466}
4467
Josef Bacikddfae632017-10-19 14:16:02 -04004468/*
4469 * Return this if we need to call truncate_block for the last bit of the
4470 * truncate.
4471 */
4472#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004473
Chris Mason323ac952008-10-01 19:05:46 -04004474/*
Chris Mason39279cc2007-06-12 06:35:45 -04004475 * this can truncate away extent items, csum items and directory items.
4476 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004477 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004478 *
4479 * csum items that cross the new i_size are truncated to the new size
4480 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004481 *
4482 * min_type is the minimum key type to truncate down to. If set to 0, this
4483 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004484 */
Yan, Zheng80825102009-11-12 09:35:36 +00004485int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4486 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004487 struct btrfs_inode *inode,
Yan, Zheng80825102009-11-12 09:35:36 +00004488 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004489{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004490 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004491 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004492 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004494 struct btrfs_key key;
4495 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004496 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004497 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004498 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004500 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004501 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004502 int found_extent;
4503 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004504 int pending_del_nr = 0;
4505 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004506 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004507 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004508 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004509 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004510 bool be_nice = false;
4511 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004512 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4513 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004514
4515 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004516
Chris Mason28ed1342014-12-17 09:41:04 -08004517 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004518 * For non-free space inodes and non-shareable roots, we want to back
4519 * off from time to time. This means all inodes in subvolume roots,
4520 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004521 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004522 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004523 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004524 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004525
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004526 path = btrfs_alloc_path();
4527 if (!path)
4528 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004529 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004530
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004531 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004532 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004533 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004534
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004535 /*
4536 * We want to drop from the next block forward in case this
4537 * new size is not block aligned since we will be keeping the
4538 * last block of the extent just the way it is.
4539 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004540 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004541 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004542 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004543 }
Yan, Zheng80825102009-11-12 09:35:36 +00004544
Miao Xie16cdcec2011-04-22 18:12:22 +08004545 /*
4546 * This function is also used to drop the items in the log tree before
4547 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004548 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004549 * items.
4550 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004551 if (min_type == 0 && root == inode->root)
4552 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004553
Li Zefan33345d012011-04-20 10:31:50 +08004554 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004555 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004556 key.type = (u8)-1;
4557
Chris Mason85e21ba2008-01-29 15:11:36 -05004558search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004559 /*
4560 * with a 16K leaf size and 128MB extents, you can actually queue
4561 * up a huge file in a single leaf. Most of the time that
4562 * bytes_deleted is > 0, it will be huge by the time we get here
4563 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004564 if (be_nice && bytes_deleted > SZ_32M &&
4565 btrfs_should_end_transaction(trans)) {
4566 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004567 goto out;
4568 }
Chris Masond3977122009-01-05 21:25:51 -05004569
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004570 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4571 if (ret < 0)
4572 goto out;
4573
Chris Mason85e21ba2008-01-29 15:11:36 -05004574 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004575 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004576 /* there are no items in the tree for us to truncate, we're
4577 * done
4578 */
Yan, Zheng80825102009-11-12 09:35:36 +00004579 if (path->slots[0] == 0)
4580 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004581 path->slots[0]--;
4582 }
4583
Chris Masond3977122009-01-05 21:25:51 -05004584 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004585 u64 clear_start = 0, clear_len = 0;
4586
Chris Mason39279cc2007-06-12 06:35:45 -04004587 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004588 leaf = path->nodes[0];
4589 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004590 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004591
Li Zefan33345d012011-04-20 10:31:50 +08004592 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004593 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004594
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004596 break;
4597
Chris Mason5f39d392007-10-15 16:14:19 -04004598 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004599 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004600 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004601 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004602 extent_type = btrfs_file_extent_type(leaf, fi);
4603 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004604 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004605 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004606
4607 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004608 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004609 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004610 item_end += btrfs_file_extent_ram_bytes(leaf,
4611 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004612
4613 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004614 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004615 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 }
Yan008630c2007-11-07 13:31:09 -05004617 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004618 }
Yan, Zheng80825102009-11-12 09:35:36 +00004619 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004620 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004621 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004622 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004623 break;
4624 if (found_key.offset >= new_size)
4625 del_item = 1;
4626 else
4627 del_item = 0;
4628 }
Chris Mason39279cc2007-06-12 06:35:45 -04004629 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004630 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004631 if (found_type != BTRFS_EXTENT_DATA_KEY)
4632 goto delete;
4633
4634 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004635 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004636
4637 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004638 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004639 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004640 u64 orig_num_bytes =
4641 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004642 extent_num_bytes = ALIGN(new_size -
4643 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004645 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004646 btrfs_set_file_extent_num_bytes(leaf, fi,
4647 extent_num_bytes);
4648 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004649 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004650 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004651 &root->state) &&
4652 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004653 inode_sub_bytes(&inode->vfs_inode,
4654 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004655 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004656 } else {
Chris Masondb945352007-10-15 16:15:53 -04004657 extent_num_bytes =
4658 btrfs_file_extent_disk_num_bytes(leaf,
4659 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004660 extent_offset = found_key.offset -
4661 btrfs_file_extent_offset(leaf, fi);
4662
Chris Mason39279cc2007-06-12 06:35:45 -04004663 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004664 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004665 if (extent_start != 0) {
4666 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004667 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004668 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004669 inode_sub_bytes(&inode->vfs_inode,
4670 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004671 }
4672 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004673 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004674 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004675 /*
4676 * we can't truncate inline items that have had
4677 * special encodings
4678 */
4679 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004680 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004681 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4682 btrfs_file_extent_compression(leaf, fi) == 0) {
4683 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004684
Josef Bacikddfae632017-10-19 14:16:02 -04004685 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4686 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004687 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004688 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004689 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004690 * We have to bail so the last_size is set to
4691 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004692 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004693 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004694 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004695 } else {
4696 /*
4697 * Inline extents are special, we just treat
4698 * them as a full sector worth in the file
4699 * extent tree just for simplicity sake.
4700 */
4701 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004702 }
Josef Bacikddfae632017-10-19 14:16:02 -04004703
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004704 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004705 inode_sub_bytes(&inode->vfs_inode,
4706 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004707 }
Chris Mason179e29e2007-11-01 11:28:41 -04004708delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004709 /*
4710 * We use btrfs_truncate_inode_items() to clean up log trees for
4711 * multiple fsyncs, and in this case we don't want to clear the
4712 * file extent range because it's just the log.
4713 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004714 if (root == inode->root) {
4715 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004716 clear_start, clear_len);
4717 if (ret) {
4718 btrfs_abort_transaction(trans, ret);
4719 break;
4720 }
4721 }
4722
Josef Bacikddfae632017-10-19 14:16:02 -04004723 if (del_item)
4724 last_size = found_key.offset;
4725 else
4726 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004727 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 if (!pending_del_nr) {
4729 /* no pending yet, add ourselves */
4730 pending_del_slot = path->slots[0];
4731 pending_del_nr = 1;
4732 } else if (pending_del_nr &&
4733 path->slots[0] + 1 == pending_del_slot) {
4734 /* hop on the pending chunk */
4735 pending_del_nr++;
4736 pending_del_slot = path->slots[0];
4737 } else {
Chris Masond3977122009-01-05 21:25:51 -05004738 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004739 }
Chris Mason39279cc2007-06-12 06:35:45 -04004740 } else {
4741 break;
4742 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004743 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004744
Miao Xie27cdeb72014-04-02 19:51:05 +08004745 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004746 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004747 struct btrfs_ref ref = { 0 };
4748
Chris Mason28ed1342014-12-17 09:41:04 -08004749 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004750
4751 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4752 extent_start, extent_num_bytes, 0);
4753 ref.real_root = root->root_key.objectid;
4754 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4755 ino, extent_offset);
4756 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004757 if (ret) {
4758 btrfs_abort_transaction(trans, ret);
4759 break;
4760 }
Chris Mason28f75a02015-02-04 06:59:29 -08004761 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004762 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004763 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004764 }
Chris Mason39279cc2007-06-12 06:35:45 -04004765 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004766
Yan, Zheng80825102009-11-12 09:35:36 +00004767 if (found_type == BTRFS_INODE_ITEM_KEY)
4768 break;
4769
4770 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004771 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004772 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004773 if (pending_del_nr) {
4774 ret = btrfs_del_items(trans, root, path,
4775 pending_del_slot,
4776 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004777 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004778 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004779 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004780 }
Yan, Zheng80825102009-11-12 09:35:36 +00004781 pending_del_nr = 0;
4782 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004783 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004784
Chris Mason28f75a02015-02-04 06:59:29 -08004785 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004786 * We can generate a lot of delayed refs, so we need to
4787 * throttle every once and a while and make sure we're
4788 * adding enough space to keep up with the work we are
4789 * generating. Since we hold a transaction here we
4790 * can't flush, and we don't want to FLUSH_LIMIT because
4791 * we could have generated too many delayed refs to
4792 * actually allocate, so just bail if we're short and
4793 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004794 */
Josef Bacik28bad212018-12-03 10:20:38 -05004795 if (should_throttle) {
4796 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4797 BTRFS_RESERVE_NO_FLUSH);
4798 if (ret) {
4799 ret = -EAGAIN;
4800 break;
4801 }
Chris Mason28f75a02015-02-04 06:59:29 -08004802 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004803 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004804 } else {
4805 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004806 }
Chris Mason39279cc2007-06-12 06:35:45 -04004807 }
Yan, Zheng80825102009-11-12 09:35:36 +00004808out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004809 if (ret >= 0 && pending_del_nr) {
4810 int err;
4811
4812 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004813 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004814 if (err) {
4815 btrfs_abort_transaction(trans, err);
4816 ret = err;
4817 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004818 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004819 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4820 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004821 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004822 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004823 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4824 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4825 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004826 }
Chris Mason28ed1342014-12-17 09:41:04 -08004827
Chris Mason39279cc2007-06-12 06:35:45 -04004828 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004829 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004830}
4831
Chris Masona52d9a82007-08-27 16:49:44 -04004832/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004834 * @inode - inode that we're zeroing
4835 * @from - the offset to start zeroing
4836 * @len - the length to zero, 0 to zero the entire range respective to the
4837 * offset
4838 * @front - zero up to the offset instead of from the offset on
4839 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004841 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004842 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004843int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4844 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004845{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004846 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4847 struct address_space *mapping = inode->vfs_inode.i_mapping;
4848 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004849 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004850 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004851 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004852 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004853 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004854 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004856 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004857 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004858 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004859 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 u64 block_start;
4861 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004862
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004863 if (IS_ALIGNED(offset, blocksize) &&
4864 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004865 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304866
Josef Bacik8b62f872017-10-19 14:15:55 -04004867 block_start = round_down(from, blocksize);
4868 block_end = block_start + blocksize - 1;
4869
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004870 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4871 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004872 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004873 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004874 /* For nocow case, no need to reserve data space */
4875 only_release_metadata = true;
4876 } else {
4877 goto out;
4878 }
4879 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004880 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004881 if (ret < 0) {
4882 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004883 btrfs_free_reserved_data_space(inode, data_reserved,
4884 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004885 goto out;
4886 }
Chris Mason211c17f2008-05-15 09:13:45 -04004887again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004888 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004889 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004890 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4891 blocksize, true);
4892 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004893 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004894 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004895 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004896 ret = set_page_extent_mapped(page);
4897 if (ret < 0)
4898 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004899
Chris Masona52d9a82007-08-27 16:49:44 -04004900 if (!PageUptodate(page)) {
4901 ret = btrfs_readpage(NULL, page);
4902 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004903 if (page->mapping != mapping) {
4904 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004905 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004906 goto again;
4907 }
Chris Masona52d9a82007-08-27 16:49:44 -04004908 if (!PageUptodate(page)) {
4909 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004910 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004911 }
4912 }
Chris Mason211c17f2008-05-15 09:13:45 -04004913 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004914
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304915 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004916
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004917 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004918 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304919 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004920 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004921 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004922 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004923 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004924 btrfs_put_ordered_extent(ordered);
4925 goto again;
4926 }
4927
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004928 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004929 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4930 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004931
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004932 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004933 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004934 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004936 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004937 goto out_unlock;
4938 }
4939
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304940 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004941 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304942 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04004943 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07004944 memzero_page(page, (block_start - page_offset(page)),
4945 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004946 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07004947 memzero_page(page, (block_start - page_offset(page)) + offset,
4948 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004949 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004950 }
Chris Mason247e7432008-07-17 12:53:51 -04004951 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004952 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004953 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004954
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004955 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004956 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004957 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004958
Chris Mason89642222008-07-24 09:41:53 -04004959out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004960 if (ret) {
4961 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004962 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004963 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004964 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004965 block_start, blocksize, true);
4966 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004967 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004968 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004969 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004970out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004971 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004972 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004973 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004974 return ret;
4975}
4976
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004977static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004978 u64 offset, u64 len)
4979{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004980 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004981 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004982 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004983 int ret;
4984
4985 /*
4986 * Still need to make sure the inode looks like it's been updated so
4987 * that any holes get logged if we fsync.
4988 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004989 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004990 inode->last_trans = fs_info->generation;
4991 inode->last_sub_trans = root->log_transid;
4992 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004993 return 0;
4994 }
4995
4996 /*
4997 * 1 - for the one we're dropping
4998 * 1 - for the one we're adding
4999 * 1 - for updating the inode.
5000 */
5001 trans = btrfs_start_transaction(root, 3);
5002 if (IS_ERR(trans))
5003 return PTR_ERR(trans);
5004
Filipe Manana5893dfb2020-11-04 11:07:32 +00005005 drop_args.start = offset;
5006 drop_args.end = offset + len;
5007 drop_args.drop_cache = true;
5008
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005009 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005010 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005011 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005012 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005013 return ret;
5014 }
5015
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005016 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005017 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005018 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005019 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005020 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005021 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5022 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005023 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005024 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005025 return ret;
5026}
5027
Josef Bacik695a0d02011-03-04 15:46:53 -05005028/*
5029 * This function puts in dummy file extents for the area we're creating a hole
5030 * for. So if we are truncating this file to a larger size we need to insert
5031 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5032 * the range between oldsize and size
5033 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005034int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005035{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005036 struct btrfs_root *root = inode->root;
5037 struct btrfs_fs_info *fs_info = root->fs_info;
5038 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005039 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005040 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005041 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005042 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5043 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005044 u64 last_byte;
5045 u64 cur_offset;
5046 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005047 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005048
Josef Bacika71754f2013-06-17 17:14:39 -04005049 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305050 * If our size started in the middle of a block we need to zero out the
5051 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005052 * expose stale data.
5053 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005054 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005055 if (err)
5056 return err;
5057
Yan Zheng9036c102008-10-30 14:19:41 -04005058 if (size <= hole_start)
5059 return 0;
5060
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005061 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5062 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005063 cur_offset = hole_start;
5064 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005065 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005066 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005067 if (IS_ERR(em)) {
5068 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005069 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005070 break;
5071 }
Yan Zheng9036c102008-10-30 14:19:41 -04005072 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005073 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005074 hole_size = last_byte - cur_offset;
5075
Yan, Zheng80825102009-11-12 09:35:36 +00005076 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005077 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005078
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005079 err = maybe_insert_hole(root, inode, cur_offset,
5080 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005081 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005082 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005083
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005084 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005085 cur_offset, hole_size);
5086 if (err)
5087 break;
5088
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005089 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005090 cur_offset + hole_size - 1, 0);
5091 hole_em = alloc_extent_map();
5092 if (!hole_em) {
5093 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005094 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005095 goto next;
5096 }
5097 hole_em->start = cur_offset;
5098 hole_em->len = hole_size;
5099 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005100
Josef Bacik5dc562c2012-08-17 13:14:17 -04005101 hole_em->block_start = EXTENT_MAP_HOLE;
5102 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005103 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005104 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005105 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005106 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005107
5108 while (1) {
5109 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005110 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005111 write_unlock(&em_tree->lock);
5112 if (err != -EEXIST)
5113 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005114 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005115 cur_offset +
5116 hole_size - 1, 0);
5117 }
5118 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005119 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005120 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005121 cur_offset, hole_size);
5122 if (err)
5123 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005124 }
Josef Bacik16e75492013-10-22 12:18:51 -04005125next:
Yan Zheng9036c102008-10-30 14:19:41 -04005126 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005127 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005128 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005129 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005130 break;
5131 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005132 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005133 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005134 return err;
5135}
5136
Eric Sandeen3972f262013-01-12 02:57:22 +00005137static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005138{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005139 struct btrfs_root *root = BTRFS_I(inode)->root;
5140 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005141 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005142 loff_t newsize = attr->ia_size;
5143 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005144 int ret;
5145
Eric Sandeen3972f262013-01-12 02:57:22 +00005146 /*
5147 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5148 * special case where we need to update the times despite not having
5149 * these flags set. For all other operations the VFS set these flags
5150 * explicitly if it wants a timestamp update.
5151 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005152 if (newsize != oldsize) {
5153 inode_inc_iversion(inode);
5154 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5155 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005156 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005157 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005158
Josef Bacika41ad392011-01-31 15:30:16 -05005159 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005160 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005161 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005162 * This is to ensure the snapshot captures a fully consistent
5163 * state of this file - if the snapshot captures this expanding
5164 * truncation, it must capture all writes that happened before
5165 * this truncation.
5166 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005167 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005168 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005169 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005170 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005171 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005172 }
Yan, Zheng80825102009-11-12 09:35:36 +00005173
Miao Xief4a2f4c2011-12-14 20:12:01 -05005174 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005175 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005176 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005177 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005178 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005179
5180 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005181 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305182 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005183 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005184 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005185 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005186 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5188
5189 if (btrfs_is_zoned(fs_info)) {
5190 ret = btrfs_wait_ordered_range(inode,
5191 ALIGN(newsize, fs_info->sectorsize),
5192 (u64)-1);
5193 if (ret)
5194 return ret;
5195 }
Yan, Zheng80825102009-11-12 09:35:36 +00005196
Josef Bacika41ad392011-01-31 15:30:16 -05005197 /*
5198 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005199 * zero. Make sure any new writes to the file get on disk
5200 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005201 */
5202 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005203 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005204 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005205
Josef Bacika41ad392011-01-31 15:30:16 -05005206 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005207
Miao Xie2e60a512013-02-08 07:01:08 +00005208 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005209
Filipe Manana213e8c52018-02-06 20:40:31 +00005210 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005211 if (ret && inode->i_nlink) {
5212 int err;
5213
5214 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005215 * Truncate failed, so fix up the in-memory size. We
5216 * adjusted disk_i_size down as we removed extents, so
5217 * wait for disk_i_size to be stable and then update the
5218 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005219 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005220 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005221 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005222 return err;
5223 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005224 }
Yan, Zheng80825102009-11-12 09:35:36 +00005225 }
5226
Josef Bacika41ad392011-01-31 15:30:16 -05005227 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005228}
5229
Christian Brauner549c7292021-01-21 14:19:43 +01005230static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5231 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005232{
David Howells2b0143b2015-03-17 22:25:59 +00005233 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005234 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005235 int err;
5236
Li Zefanb83cc962010-12-20 16:04:08 +08005237 if (btrfs_root_readonly(root))
5238 return -EROFS;
5239
Christian Brauner2f221d62021-01-21 14:19:26 +01005240 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005241 if (err)
5242 return err;
5243
Chris Mason5a3f23d2009-03-31 13:27:11 -04005244 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005245 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005246 if (err)
5247 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005248 }
Yan Zheng9036c102008-10-30 14:19:41 -04005249
Christoph Hellwig10257742010-06-04 11:30:02 +02005250 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005251 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005252 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005253 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005254
Josef Bacik22c44fe2011-11-30 10:45:38 -05005255 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005256 err = posix_acl_chmod(&init_user_ns, inode,
5257 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005258 }
5259
Chris Mason39279cc2007-06-12 06:35:45 -04005260 return err;
5261}
Chris Mason61295eb2008-01-14 16:24:38 -05005262
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263/*
5264 * While truncating the inode pages during eviction, we get the VFS calling
5265 * btrfs_invalidatepage() against each page of the inode. This is slow because
5266 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5267 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5268 * extent_state structures over and over, wasting lots of time.
5269 *
5270 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5271 * those expensive operations on a per page basis and do only the ordered io
5272 * finishing, while we release here the extent_map and extent_state structures,
5273 * without the excessive merging and splitting.
5274 */
5275static void evict_inode_truncate_pages(struct inode *inode)
5276{
5277 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5278 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5279 struct rb_node *node;
5280
5281 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005282 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005283
5284 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005285 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005286 struct extent_map *em;
5287
Liu Bo07e1ce02018-08-23 03:51:52 +08005288 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005289 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005290 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5291 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005292 remove_extent_mapping(map_tree, em);
5293 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005294 if (need_resched()) {
5295 write_unlock(&map_tree->lock);
5296 cond_resched();
5297 write_lock(&map_tree->lock);
5298 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005299 }
5300 write_unlock(&map_tree->lock);
5301
Filipe Manana6ca07092015-05-26 00:55:42 +01005302 /*
5303 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005304 * We can have ongoing bios started by readahead that have
5305 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005306 * still in progress (unlocked the pages in the bio but did not yet
5307 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005308 * ranges can still be locked and eviction started because before
5309 * submitting those bios, which are executed by a separate task (work
5310 * queue kthread), inode references (inode->i_count) were not taken
5311 * (which would be dropped in the end io callback of each bio).
5312 * Therefore here we effectively end up waiting for those bios and
5313 * anyone else holding locked ranges without having bumped the inode's
5314 * reference count - if we don't do it, when they access the inode's
5315 * io_tree to unlock a range it may be too late, leading to an
5316 * use-after-free issue.
5317 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005318 spin_lock(&io_tree->lock);
5319 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5320 struct extent_state *state;
5321 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005322 u64 start;
5323 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005324 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005325
5326 node = rb_first(&io_tree->state);
5327 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005328 start = state->start;
5329 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005330 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005331 spin_unlock(&io_tree->lock);
5332
David Sterbaff13db42015-12-03 14:30:40 +01005333 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005334
5335 /*
5336 * If still has DELALLOC flag, the extent didn't reach disk,
5337 * and its reserved space won't be freed by delayed_ref.
5338 * So we need to free its reserved space here.
5339 * (Refer to comment in btrfs_invalidatepage, case 2)
5340 *
5341 * Note, end is the bytenr of last byte, so we need + 1 here.
5342 */
Filipe Manana421f0922018-10-12 13:02:48 +01005343 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005344 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5345 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005346
Filipe Manana6ca07092015-05-26 00:55:42 +01005347 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005348 EXTENT_LOCKED | EXTENT_DELALLOC |
5349 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5350 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005351
Filipe Manana7064dd52014-08-08 02:47:05 +01005352 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005353 spin_lock(&io_tree->lock);
5354 }
5355 spin_unlock(&io_tree->lock);
5356}
5357
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005358static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005359 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005360{
5361 struct btrfs_fs_info *fs_info = root->fs_info;
5362 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005363 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005364 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005365 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005366
Josef Bacikd3984c92019-08-01 18:19:37 -04005367 /*
5368 * Eviction should be taking place at some place safe because of our
5369 * delayed iputs. However the normal flushing code will run delayed
5370 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5371 *
5372 * We reserve the delayed_refs_extra here again because we can't use
5373 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5374 * above. We reserve our extra bit here because we generate a ton of
5375 * delayed refs activity by truncating.
5376 *
5377 * If we cannot make our reservation we'll attempt to steal from the
5378 * global reserve, because we really want to be able to free up space.
5379 */
5380 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5381 BTRFS_RESERVE_FLUSH_EVICT);
5382 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005383 /*
5384 * Try to steal from the global reserve if there is space for
5385 * it.
5386 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005387 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5388 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5389 btrfs_warn(fs_info,
5390 "could not allocate space for delete; will truncate on mount");
5391 return ERR_PTR(-ENOSPC);
5392 }
5393 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005394 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005395
5396 trans = btrfs_join_transaction(root);
5397 if (IS_ERR(trans))
5398 return trans;
5399
5400 if (delayed_refs_extra) {
5401 trans->block_rsv = &fs_info->trans_block_rsv;
5402 trans->bytes_reserved = delayed_refs_extra;
5403 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5404 delayed_refs_extra, 1);
5405 }
5406 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005407}
5408
Al Virobd555972010-06-07 11:35:40 -04005409void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005411 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005412 struct btrfs_trans_handle *trans;
5413 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005414 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005415 int ret;
5416
liubo1abe9b82011-03-24 11:18:59 +00005417 trace_btrfs_inode_evict(inode);
5418
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005419 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005420 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005421 return;
5422 }
5423
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005424 evict_inode_truncate_pages(inode);
5425
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005426 if (inode->i_nlink &&
5427 ((btrfs_root_refs(&root->root_item) != 0 &&
5428 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005429 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005430 goto no_delete;
5431
Omar Sandoval27919062018-05-11 13:13:37 -07005432 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005433 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005434
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005435 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005436
Omar Sandoval7b40b692018-05-11 13:13:33 -07005437 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005438 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005439
Yan, Zheng76dda932009-09-21 16:00:26 -04005440 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005441 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5442 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005443 goto no_delete;
5444 }
5445
Nikolay Borisovaa790212017-01-10 20:35:40 +02005446 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005447 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005448 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005449
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005451 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005452 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005453 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005454 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005455
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005456 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005457
Yan, Zheng80825102009-11-12 09:35:36 +00005458 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005459 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005460 if (IS_ERR(trans))
5461 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005462
5463 trans->block_rsv = rsv;
5464
Nikolay Borisov50743392020-11-02 16:48:55 +02005465 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5466 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005467 trans->block_rsv = &fs_info->trans_block_rsv;
5468 btrfs_end_transaction(trans);
5469 btrfs_btree_balance_dirty(fs_info);
5470 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5471 goto free_rsv;
5472 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005473 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005474 }
5475
Josef Bacik4ef31a42013-08-13 14:10:08 -04005476 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005477 * Errors here aren't a big deal, it just means we leave orphan items in
5478 * the tree. They will be cleaned up on the next mount. If the inode
5479 * number gets reused, cleanup deletes the orphan item without doing
5480 * anything, and unlink reuses the existing orphan item.
5481 *
5482 * If it turns out that we are dropping too many of these, we might want
5483 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005484 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005485 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005486 if (!IS_ERR(trans)) {
5487 trans->block_rsv = rsv;
5488 btrfs_orphan_del(trans, BTRFS_I(inode));
5489 trans->block_rsv = &fs_info->trans_block_rsv;
5490 btrfs_end_transaction(trans);
5491 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005492
Omar Sandoval27919062018-05-11 13:13:37 -07005493free_rsv:
5494 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005495no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005496 /*
5497 * If we didn't successfully delete, the orphan item will still be in
5498 * the tree and we'll retry on the next mount. Again, we might also want
5499 * to retry these periodically in the future.
5500 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005501 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005502 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005503}
5504
5505/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005506 * Return the key found in the dir entry in the location pointer, fill @type
5507 * with BTRFS_FT_*, and return 0.
5508 *
Su Yue005d6712018-03-05 17:13:37 +08005509 * If no dir entries were found, returns -ENOENT.
5510 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005511 */
5512static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005513 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005514{
5515 const char *name = dentry->d_name.name;
5516 int namelen = dentry->d_name.len;
5517 struct btrfs_dir_item *di;
5518 struct btrfs_path *path;
5519 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005520 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005521
5522 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005523 if (!path)
5524 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005525
David Sterbaf85b7372017-01-20 14:54:07 +01005526 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5527 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005528 if (IS_ERR_OR_NULL(di)) {
5529 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005530 goto out;
5531 }
Chris Masond3977122009-01-05 21:25:51 -05005532
Chris Mason5f39d392007-10-15 16:14:19 -04005533 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005534 if (location->type != BTRFS_INODE_ITEM_KEY &&
5535 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005536 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005537 btrfs_warn(root->fs_info,
5538"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5539 __func__, name, btrfs_ino(BTRFS_I(dir)),
5540 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005541 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005542 if (!ret)
5543 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005544out:
Chris Mason39279cc2007-06-12 06:35:45 -04005545 btrfs_free_path(path);
5546 return ret;
5547}
5548
5549/*
5550 * when we hit a tree root in a directory, the btrfs part of the inode
5551 * needs to be changed to reflect the root directory of the tree root. This
5552 * is kind of like crossing a mount point.
5553 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005554static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555 struct inode *dir,
5556 struct dentry *dentry,
5557 struct btrfs_key *location,
5558 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005559{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560 struct btrfs_path *path;
5561 struct btrfs_root *new_root;
5562 struct btrfs_root_ref *ref;
5563 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005564 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 int ret;
5566 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005567
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 path = btrfs_alloc_path();
5569 if (!path) {
5570 err = -ENOMEM;
5571 goto out;
5572 }
Chris Mason39279cc2007-06-12 06:35:45 -04005573
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005574 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005575 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5576 key.type = BTRFS_ROOT_REF_KEY;
5577 key.offset = location->objectid;
5578
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005579 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005580 if (ret) {
5581 if (ret < 0)
5582 err = ret;
5583 goto out;
5584 }
Chris Mason39279cc2007-06-12 06:35:45 -04005585
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005586 leaf = path->nodes[0];
5587 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005588 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005589 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5590 goto out;
5591
5592 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5593 (unsigned long)(ref + 1),
5594 dentry->d_name.len);
5595 if (ret)
5596 goto out;
5597
David Sterbab3b4aa72011-04-21 01:20:15 +02005598 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599
David Sterba56e93572020-05-15 19:35:55 +02005600 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005601 if (IS_ERR(new_root)) {
5602 err = PTR_ERR(new_root);
5603 goto out;
5604 }
5605
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606 *sub_root = new_root;
5607 location->objectid = btrfs_root_dirid(&new_root->root_item);
5608 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005609 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005610 err = 0;
5611out:
5612 btrfs_free_path(path);
5613 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005614}
5615
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616static void inode_tree_add(struct inode *inode)
5617{
5618 struct btrfs_root *root = BTRFS_I(inode)->root;
5619 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005620 struct rb_node **p;
5621 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005622 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005623 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624
Al Viro1d3382cb2010-10-23 15:19:20 -04005625 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005627 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005628 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005629 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005630 while (*p) {
5631 parent = *p;
5632 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5633
David Sterba37508512018-06-29 10:56:40 +02005634 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005635 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005636 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005637 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 else {
5639 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005640 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005641 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005642 RB_CLEAR_NODE(parent);
5643 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005644 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645 }
5646 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005647 rb_link_node(new, parent, p);
5648 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005649 spin_unlock(&root->inode_lock);
5650}
5651
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005652static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005653{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005654 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005656
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005657 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005658 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5659 rb_erase(&inode->rb_node, &root->inode_tree);
5660 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005661 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005662 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005663 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005664
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005665 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005666 spin_lock(&root->inode_lock);
5667 empty = RB_EMPTY_ROOT(&root->inode_tree);
5668 spin_unlock(&root->inode_lock);
5669 if (empty)
5670 btrfs_add_dead_root(root);
5671 }
5672}
5673
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005674
Chris Masone02119d2008-09-05 16:13:11 -04005675static int btrfs_init_locked_inode(struct inode *inode, void *p)
5676{
5677 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005678
5679 inode->i_ino = args->ino;
5680 BTRFS_I(inode)->location.objectid = args->ino;
5681 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5682 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005683 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5684 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005685 return 0;
5686}
5687
5688static int btrfs_find_actor(struct inode *inode, void *opaque)
5689{
5690 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005691
5692 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005693 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005694}
5695
David Sterba0202e832020-05-15 19:35:59 +02005696static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005698{
5699 struct inode *inode;
5700 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005701 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005702
David Sterba0202e832020-05-15 19:35:59 +02005703 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005704 args.root = root;
5705
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005706 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005707 btrfs_init_locked_inode,
5708 (void *)&args);
5709 return inode;
5710}
5711
David Sterba4c66e0d2019-10-03 19:09:35 +02005712/*
David Sterba0202e832020-05-15 19:35:59 +02005713 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005714 * Path can be preallocated to prevent recursing back to iget through
5715 * allocator. NULL is also valid but may require an additional allocation
5716 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305717 */
David Sterba0202e832020-05-15 19:35:59 +02005718struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005719 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305720{
5721 struct inode *inode;
5722
David Sterba0202e832020-05-15 19:35:59 +02005723 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305724 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005725 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305726
5727 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005728 int ret;
5729
Filipe Manana4222ea72018-10-24 10:13:03 +01005730 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005731 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005732 inode_tree_add(inode);
5733 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005734 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005735 iget_failed(inode);
5736 /*
5737 * ret > 0 can come from btrfs_search_slot called by
5738 * btrfs_read_locked_inode, this means the inode item
5739 * was not found.
5740 */
5741 if (ret > 0)
5742 ret = -ENOENT;
5743 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005744 }
5745 }
5746
Balaji Rao1a54ef82008-07-21 02:01:04 +05305747 return inode;
5748}
5749
David Sterba0202e832020-05-15 19:35:59 +02005750struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005751{
David Sterba0202e832020-05-15 19:35:59 +02005752 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005753}
5754
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755static struct inode *new_simple_dir(struct super_block *s,
5756 struct btrfs_key *key,
5757 struct btrfs_root *root)
5758{
5759 struct inode *inode = new_inode(s);
5760
5761 if (!inode)
5762 return ERR_PTR(-ENOMEM);
5763
Josef Bacik5c8fd992020-02-14 16:11:43 -05005764 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005766 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767
5768 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005769 /*
5770 * We only need lookup, the rest is read-only and there's no inode
5771 * associated with the dentry
5772 */
5773 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005774 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005775 inode->i_fop = &simple_dir_operations;
5776 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005777 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305778 inode->i_atime = inode->i_mtime;
5779 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005780 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005781
5782 return inode;
5783}
5784
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005785static inline u8 btrfs_inode_type(struct inode *inode)
5786{
5787 /*
5788 * Compile-time asserts that generic FT_* types still match
5789 * BTRFS_FT_* types
5790 */
5791 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5792 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5793 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5794 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5795 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5796 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5797 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5798 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5799
5800 return fs_umode_to_ftype(inode->i_mode);
5801}
5802
Chris Mason3de45862008-11-17 21:02:50 -05005803struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005804{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005805 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005806 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005808 struct btrfs_root *sub_root = root;
5809 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005810 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005811 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005812
5813 if (dentry->d_name.len > BTRFS_NAME_LEN)
5814 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005815
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005816 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005817 if (ret < 0)
5818 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005819
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005820 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005821 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005822 if (IS_ERR(inode))
5823 return inode;
5824
5825 /* Do extra check against inode mode with di_type */
5826 if (btrfs_inode_type(inode) != di_type) {
5827 btrfs_crit(fs_info,
5828"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5829 inode->i_mode, btrfs_inode_type(inode),
5830 di_type);
5831 iput(inode);
5832 return ERR_PTR(-EUCLEAN);
5833 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005834 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005835 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005836
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005837 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005838 &location, &sub_root);
5839 if (ret < 0) {
5840 if (ret != -ENOENT)
5841 inode = ERR_PTR(ret);
5842 else
5843 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5844 } else {
David Sterba0202e832020-05-15 19:35:59 +02005845 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 }
Josef Bacik87270022020-01-24 09:32:31 -05005847 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005848 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005849
Julia Lawall34d19ba2011-01-24 19:55:19 +00005850 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005851 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005852 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005853 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005854 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005855 if (ret) {
5856 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005857 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005858 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005859 }
5860
Chris Mason3de45862008-11-17 21:02:50 -05005861 return inode;
5862}
5863
Nick Pigginfe15ce42011-01-07 17:49:23 +11005864static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005865{
5866 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005867 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005868
Li Zefan848cce02012-02-21 17:04:28 +08005869 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005870 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005871
Li Zefan848cce02012-02-21 17:04:28 +08005872 if (inode) {
5873 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005874 if (btrfs_root_refs(&root->root_item) == 0)
5875 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005876
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005877 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005878 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005879 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005880 return 0;
5881}
5882
Chris Mason3de45862008-11-17 21:02:50 -05005883static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005884 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005885{
Al Viro3837d202018-10-10 16:38:27 -04005886 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005887
Al Viro3837d202018-10-10 16:38:27 -04005888 if (inode == ERR_PTR(-ENOENT))
5889 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005890 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005891}
5892
Josef Bacik23b5ec72017-07-24 15:14:25 -04005893/*
5894 * All this infrastructure exists because dir_emit can fault, and we are holding
5895 * the tree lock when doing readdir. For now just allocate a buffer and copy
5896 * our information into that, and then dir_emit from the buffer. This is
5897 * similar to what NFS does, only we don't keep the buffer around in pagecache
5898 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5899 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5900 * tree lock.
5901 */
5902static int btrfs_opendir(struct inode *inode, struct file *file)
5903{
5904 struct btrfs_file_private *private;
5905
5906 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5907 if (!private)
5908 return -ENOMEM;
5909 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5910 if (!private->filldir_buf) {
5911 kfree(private);
5912 return -ENOMEM;
5913 }
5914 file->private_data = private;
5915 return 0;
5916}
5917
5918struct dir_entry {
5919 u64 ino;
5920 u64 offset;
5921 unsigned type;
5922 int name_len;
5923};
5924
5925static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5926{
5927 while (entries--) {
5928 struct dir_entry *entry = addr;
5929 char *name = (char *)(entry + 1);
5930
David Sterba92d32172018-04-16 21:10:14 +02005931 ctx->pos = get_unaligned(&entry->offset);
5932 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5933 get_unaligned(&entry->ino),
5934 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005936 addr += sizeof(struct dir_entry) +
5937 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005938 ctx->pos++;
5939 }
5940 return 0;
5941}
5942
Al Viro9cdda8d2013-05-22 16:48:09 -04005943static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005944{
Al Viro9cdda8d2013-05-22 16:48:09 -04005945 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005946 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005947 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005948 struct btrfs_dir_item *di;
5949 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005950 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005951 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005952 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005953 struct list_head ins_list;
5954 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005955 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005956 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005957 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005958 char *name_ptr;
5959 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005960 int entries = 0;
5961 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005962 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005964
Al Viro9cdda8d2013-05-22 16:48:09 -04005965 if (!dir_emit_dots(file, ctx))
5966 return 0;
5967
David Woodhouse49593bf2008-08-17 17:08:36 +01005968 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005969 if (!path)
5970 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005971
Josef Bacik23b5ec72017-07-24 15:14:25 -04005972 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005973 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005974
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005975 INIT_LIST_HEAD(&ins_list);
5976 INIT_LIST_HEAD(&del_list);
5977 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005978
Josef Bacik23b5ec72017-07-24 15:14:25 -04005979again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005980 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005981 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005982 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005983
Chris Mason39279cc2007-06-12 06:35:45 -04005984 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5985 if (ret < 0)
5986 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005987
5988 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005989 struct dir_entry *entry;
5990
Chris Mason5f39d392007-10-15 16:14:19 -04005991 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005992 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005993 if (slot >= btrfs_header_nritems(leaf)) {
5994 ret = btrfs_next_leaf(root, path);
5995 if (ret < 0)
5996 goto err;
5997 else if (ret > 0)
5998 break;
5999 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006000 }
Chris Mason3de45862008-11-17 21:02:50 -05006001
Chris Mason5f39d392007-10-15 16:14:19 -04006002 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6003
6004 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006005 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006006 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006007 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006008 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006009 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006010 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006011 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006012 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006013 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6015 PAGE_SIZE) {
6016 btrfs_release_path(path);
6017 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6018 if (ret)
6019 goto nopos;
6020 addr = private->filldir_buf;
6021 entries = 0;
6022 total_len = 0;
6023 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006024 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006025
6026 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006027 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006028 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006029 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6030 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006031 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006032 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006033 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006034 put_unaligned(location.objectid, &entry->ino);
6035 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006036 entries++;
6037 addr += sizeof(struct dir_entry) + name_len;
6038 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006039next:
6040 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006041 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006042 btrfs_release_path(path);
6043
6044 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6045 if (ret)
6046 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006047
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006048 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006049 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006050 goto nopos;
6051
Zach Browndb62efb2013-07-11 16:19:42 -07006052 /*
6053 * Stop new entries from being returned after we return the last
6054 * entry.
6055 *
6056 * New directory entries are assigned a strictly increasing
6057 * offset. This means that new entries created during readdir
6058 * are *guaranteed* to be seen in the future by that readdir.
6059 * This has broken buggy programs which operate on names as
6060 * they're returned by readdir. Until we re-use freed offsets
6061 * we have this hack to stop new entries from being returned
6062 * under the assumption that they'll never reach this huge
6063 * offset.
6064 *
6065 * This is being careful not to overflow 32bit loff_t unless the
6066 * last entry requires it because doing so has broken 32bit apps
6067 * in the past.
6068 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006069 if (ctx->pos >= INT_MAX)
6070 ctx->pos = LLONG_MAX;
6071 else
6072 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006073nopos:
6074 ret = 0;
6075err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006076 if (put)
6077 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006078 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006079 return ret;
6080}
6081
Chris Mason39279cc2007-06-12 06:35:45 -04006082/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006083 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006084 * inode changes. But, it is most likely to find the inode in cache.
6085 * FIXME, needs more benchmarking...there are no reasons other than performance
6086 * to keep or drop this code.
6087 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006088static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006089{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006090 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006093 int ret;
6094
Josef Bacik72ac3c02012-05-23 14:13:11 -04006095 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006096 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006098 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006099 if (IS_ERR(trans))
6100 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006101
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006102 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006103 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006104 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006105 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006106 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006107 if (IS_ERR(trans))
6108 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006109
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006110 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006111 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006112 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006113 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006114 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115
6116 return ret;
6117}
6118
6119/*
6120 * This is a copy of file_update_time. We need this so we can return error on
6121 * ENOSPC for updating the inode in the case of file write and mmap writes.
6122 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006123static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006124 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125{
Alexander Block2bc5565282012-06-15 09:49:33 +02006126 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006127 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006128
6129 if (btrfs_root_readonly(root))
6130 return -EROFS;
6131
Josef Bacike41f9412012-03-26 09:46:47 -04006132 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006133 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006134 if (flags & S_CTIME)
6135 inode->i_ctime = *now;
6136 if (flags & S_MTIME)
6137 inode->i_mtime = *now;
6138 if (flags & S_ATIME)
6139 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006140 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006141}
6142
Chris Masond352ac62008-09-29 15:18:18 -04006143/*
6144 * find the highest existing sequence number in a directory
6145 * and then set the in-memory index_cnt variable to reflect
6146 * free sequence numbers
6147 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006148static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006149{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006150 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006151 struct btrfs_key key, found_key;
6152 struct btrfs_path *path;
6153 struct extent_buffer *leaf;
6154 int ret;
6155
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006156 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006157 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006158 key.offset = (u64)-1;
6159
6160 path = btrfs_alloc_path();
6161 if (!path)
6162 return -ENOMEM;
6163
6164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6165 if (ret < 0)
6166 goto out;
6167 /* FIXME: we should be able to handle this */
6168 if (ret == 0)
6169 goto out;
6170 ret = 0;
6171
6172 /*
6173 * MAGIC NUMBER EXPLANATION:
6174 * since we search a directory based on f_pos we have to start at 2
6175 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6176 * else has to start at 2
6177 */
6178 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006179 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006180 goto out;
6181 }
6182
6183 path->slots[0]--;
6184
6185 leaf = path->nodes[0];
6186 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6187
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006188 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006189 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006190 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006191 goto out;
6192 }
6193
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006194 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006195out:
6196 btrfs_free_path(path);
6197 return ret;
6198}
6199
Chris Masond352ac62008-09-29 15:18:18 -04006200/*
6201 * helper to find a free sequence number in a given directory. This current
6202 * code is very simple, later versions will do smarter things in the btree
6203 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006204int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006205{
6206 int ret = 0;
6207
Nikolay Borisov877574e2017-02-20 13:50:33 +02006208 if (dir->index_cnt == (u64)-1) {
6209 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006210 if (ret) {
6211 ret = btrfs_set_inode_index_count(dir);
6212 if (ret)
6213 return ret;
6214 }
Josef Bacikaec74772008-07-24 12:12:38 -04006215 }
6216
Nikolay Borisov877574e2017-02-20 13:50:33 +02006217 *index = dir->index_cnt;
6218 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006219
6220 return ret;
6221}
6222
Chris Masonb0d5d102014-09-08 13:08:51 -07006223static int btrfs_insert_inode_locked(struct inode *inode)
6224{
6225 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006226
6227 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006228 args.root = BTRFS_I(inode)->root;
6229
6230 return insert_inode_locked4(inode,
6231 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6232 btrfs_find_actor, &args);
6233}
6234
Anand Jain19aee8d2017-07-18 17:37:05 +08006235/*
6236 * Inherit flags from the parent inode.
6237 *
6238 * Currently only the compression flags and the cow flags are inherited.
6239 */
6240static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6241{
6242 unsigned int flags;
6243
6244 if (!dir)
6245 return;
6246
6247 flags = BTRFS_I(dir)->flags;
6248
6249 if (flags & BTRFS_INODE_NOCOMPRESS) {
6250 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6251 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6252 } else if (flags & BTRFS_INODE_COMPRESS) {
6253 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6254 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6255 }
6256
6257 if (flags & BTRFS_INODE_NODATACOW) {
6258 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6259 if (S_ISREG(inode->i_mode))
6260 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6261 }
6262
David Sterba7b6a2212018-03-26 18:40:21 +02006263 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006264}
6265
Chris Mason39279cc2007-06-12 06:35:45 -04006266static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6267 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006268 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006269 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006270 u64 ref_objectid, u64 objectid,
6271 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006273 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006274 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006275 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006276 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006277 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006278 struct btrfs_inode_ref *ref;
6279 struct btrfs_key key[2];
6280 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006281 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006282 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006283 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006284 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006285
Chris Mason5f39d392007-10-15 16:14:19 -04006286 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006287 if (!path)
6288 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006289
Josef Bacik11a19a92019-09-09 10:12:04 -04006290 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006292 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006293 if (!inode) {
6294 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006295 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006296 }
Chris Mason39279cc2007-06-12 06:35:45 -04006297
Li Zefan581bb052011-04-20 10:06:11 +08006298 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006299 * O_TMPFILE, set link count to 0, so that after this point,
6300 * we fill in an inode item with the correct link count.
6301 */
6302 if (!name)
6303 set_nlink(inode, 0);
6304
6305 /*
Li Zefan581bb052011-04-20 10:06:11 +08006306 * we have to initialize this early, so we can reclaim the inode
6307 * number if we fail afterwards in this function.
6308 */
6309 inode->i_ino = objectid;
6310
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006311 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006312 trace_btrfs_inode_request(dir);
6313
Nikolay Borisov877574e2017-02-20 13:50:33 +02006314 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006315 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006316 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006317 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006318 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006319 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006320 } else if (dir) {
6321 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006322 }
6323 /*
6324 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006325 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006326 * number
6327 */
6328 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006329 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006330 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006331 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006332 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006333
Josef Bacik5dc562c2012-08-17 13:14:17 -04006334 /*
6335 * We could have gotten an inode number from somebody who was fsynced
6336 * and then removed in this same transaction, so let's just set full
6337 * sync since it will be a full sync anyway and this will blow away the
6338 * old info in the log.
6339 */
6340 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6341
Chris Mason9c583092008-01-29 15:15:18 -05006342 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006343 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006344 key[0].offset = 0;
6345
Chris Mason9c583092008-01-29 15:15:18 -05006346 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006347
6348 if (name) {
6349 /*
6350 * Start new inodes with an inode_ref. This is slightly more
6351 * efficient for small numbers of hard links since they will
6352 * be packed into one item. Extended refs will kick in if we
6353 * add more hard links than can fit in the ref item.
6354 */
6355 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006356 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006357 key[1].offset = ref_objectid;
6358
6359 sizes[1] = name_len + sizeof(*ref);
6360 }
Chris Mason9c583092008-01-29 15:15:18 -05006361
Chris Masonb0d5d102014-09-08 13:08:51 -07006362 location = &BTRFS_I(inode)->location;
6363 location->objectid = objectid;
6364 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006365 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006366
6367 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006368 if (ret < 0) {
6369 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006370 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006371 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006372
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006373 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006374 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006375 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006376
Christian Brauner21cb47b2021-01-21 14:19:25 +01006377 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006378 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306379
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006380 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306381 inode->i_atime = inode->i_mtime;
6382 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006383 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306384
Chris Mason5f39d392007-10-15 16:14:19 -04006385 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6386 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006387 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006388 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006389 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006390
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006391 if (name) {
6392 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6393 struct btrfs_inode_ref);
6394 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6395 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6396 ptr = (unsigned long)(ref + 1);
6397 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6398 }
Chris Mason9c583092008-01-29 15:15:18 -05006399
Chris Mason5f39d392007-10-15 16:14:19 -04006400 btrfs_mark_buffer_dirty(path->nodes[0]);
6401 btrfs_free_path(path);
6402
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006403 btrfs_inherit_iflags(inode, dir);
6404
Al Viro569254b2011-07-24 17:08:40 -04006405 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006406 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006407 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006408 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006409 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6410 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006411 }
6412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006413 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006414
6415 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006416 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006417
Alexander Block8ea05e32012-07-25 17:35:53 +02006418 btrfs_update_root_times(trans, root);
6419
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006420 ret = btrfs_inode_inherit_props(trans, inode, dir);
6421 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006422 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006423 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006424 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006425
Chris Mason39279cc2007-06-12 06:35:45 -04006426 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006427
6428fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006429 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006430fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006431 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006432 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006433 btrfs_free_path(path);
6434 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006435}
6436
Chris Masond352ac62008-09-29 15:18:18 -04006437/*
6438 * utility function to add 'inode' into 'parent_inode' with
6439 * a give name and a given sequence number.
6440 * if 'add_backref' is true, also insert a backref from the
6441 * inode to the parent directory.
6442 */
Chris Masone02119d2008-09-05 16:13:11 -04006443int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006444 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006445 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006446{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006447 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006448 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006449 struct btrfs_root *root = parent_inode->root;
6450 u64 ino = btrfs_ino(inode);
6451 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006452
Li Zefan33345d012011-04-20 10:31:50 +08006453 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006454 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006455 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006456 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006457 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006458 key.offset = 0;
6459 }
Chris Mason39279cc2007-06-12 06:35:45 -04006460
Li Zefan33345d012011-04-20 10:31:50 +08006461 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006462 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006463 root->root_key.objectid, parent_ino,
6464 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006465 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006466 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6467 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006468 }
6469
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006470 /* Nothing to clean up yet */
6471 if (ret)
6472 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006473
Lu Fengqi684572d2018-08-04 21:10:57 +08006474 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006476 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477 goto fail_dir_item;
6478 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006479 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006482
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006483 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006485 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006486 /*
6487 * If we are replaying a log tree, we do not want to update the mtime
6488 * and ctime of the parent directory with the current time, since the
6489 * log replay procedure is responsible for setting them to their correct
6490 * values (the ones it had when the fsync was done).
6491 */
6492 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6493 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6494
6495 parent_inode->vfs_inode.i_mtime = now;
6496 parent_inode->vfs_inode.i_ctime = now;
6497 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006498 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006499 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006500 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006501 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006502
6503fail_dir_item:
6504 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6505 u64 local_index;
6506 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006507 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006508 root->root_key.objectid, parent_ino,
6509 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006510 if (err)
6511 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006512 } else if (add_backref) {
6513 u64 local_index;
6514 int err;
6515
6516 err = btrfs_del_inode_ref(trans, root, name, name_len,
6517 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006518 if (err)
6519 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006520 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006521
6522 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006523 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006524}
6525
6526static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006527 struct btrfs_inode *dir, struct dentry *dentry,
6528 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006529{
Josef Bacika1b075d2010-11-19 20:36:11 +00006530 int err = btrfs_add_link(trans, dir, inode,
6531 dentry->d_name.name, dentry->d_name.len,
6532 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006533 if (err > 0)
6534 err = -EEXIST;
6535 return err;
6536}
6537
Christian Brauner549c7292021-01-21 14:19:43 +01006538static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6539 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006540{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006541 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006542 struct btrfs_trans_handle *trans;
6543 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006544 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006545 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006546 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006547 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006548
Josef Bacik9ed74f22009-09-11 16:12:44 -04006549 /*
6550 * 2 for inode item and ref
6551 * 2 for dir items
6552 * 1 for xattr if selinux is on
6553 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006554 trans = btrfs_start_transaction(root, 5);
6555 if (IS_ERR(trans))
6556 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006557
Nikolay Borisov543068a2020-12-07 17:32:33 +02006558 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006559 if (err)
6560 goto out_unlock;
6561
Josef Bacikaec74772008-07-24 12:12:38 -04006562 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006563 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6564 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006565 if (IS_ERR(inode)) {
6566 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006567 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006568 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006569 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006570
Casey Schauflerad19db72011-12-15 10:09:07 -05006571 /*
6572 * If the active LSM wants to access the inode during
6573 * d_instantiate it needs these. Smack checks to see
6574 * if the filesystem supports xattrs by looking at the
6575 * ops vector.
6576 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006577 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006578 init_special_inode(inode, inode->i_mode, rdev);
6579
6580 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006581 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006582 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006583
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006584 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6585 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006586 if (err)
6587 goto out_unlock;
6588
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006589 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006590 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006591
Josef Bacik618e21d2007-07-11 10:18:17 -04006592out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006593 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006594 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006595 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006596 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006597 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006598 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 return err;
6600}
6601
Christian Brauner549c7292021-01-21 14:19:43 +01006602static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6603 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006604{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006605 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 struct btrfs_trans_handle *trans;
6607 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006608 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006609 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006610 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006611 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006612
Josef Bacik9ed74f22009-09-11 16:12:44 -04006613 /*
6614 * 2 for inode item and ref
6615 * 2 for dir items
6616 * 1 for xattr if selinux is on
6617 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006618 trans = btrfs_start_transaction(root, 5);
6619 if (IS_ERR(trans))
6620 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006621
Nikolay Borisov543068a2020-12-07 17:32:33 +02006622 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006623 if (err)
6624 goto out_unlock;
6625
Josef Bacikaec74772008-07-24 12:12:38 -04006626 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006627 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6628 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006629 if (IS_ERR(inode)) {
6630 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006631 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006632 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006633 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006634 /*
6635 * If the active LSM wants to access the inode during
6636 * d_instantiate it needs these. Smack checks to see
6637 * if the filesystem supports xattrs by looking at the
6638 * ops vector.
6639 */
6640 inode->i_fop = &btrfs_file_operations;
6641 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006642 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006643
6644 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6645 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006646 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006647
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006648 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006649 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006650 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006651
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006652 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6653 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006654 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006655 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006656
Al Viro1e2e5472018-05-04 08:23:01 -04006657 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006658
Chris Mason39279cc2007-06-12 06:35:45 -04006659out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006660 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006661 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006662 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006663 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006664 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006665 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006666 return err;
6667}
6668
6669static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6670 struct dentry *dentry)
6671{
Filipe Manana271dba452016-01-05 16:24:05 +00006672 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006673 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006674 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006675 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006676 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006677 int err;
6678 int drop_inode = 0;
6679
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006680 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006681 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006682 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006683
Mark Fashehf1863732012-08-08 11:32:27 -07006684 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006685 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006686
Nikolay Borisov877574e2017-02-20 13:50:33 +02006687 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006688 if (err)
6689 goto fail;
6690
Yan, Zhenga22285a2010-05-16 10:48:46 -04006691 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006692 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006693 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006694 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006695 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006697 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006698 if (IS_ERR(trans)) {
6699 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006700 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006701 goto fail;
6702 }
Chris Mason5f39d392007-10-15 16:14:19 -04006703
Miao Xie67de1172013-12-26 13:07:06 +08006704 /* There are several dir indexes for this inode, clear the cache. */
6705 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006706 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006707 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006708 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006709 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006710 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006711
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006712 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6713 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006714
Yan, Zhenga5719522009-09-24 09:17:31 -04006715 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006716 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006717 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006718 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006719
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006720 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006721 if (err)
6722 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006723 if (inode->i_nlink == 1) {
6724 /*
6725 * If new hard link count is 1, it's a file created
6726 * with open(2) O_TMPFILE flag.
6727 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006728 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006729 if (err)
6730 goto fail;
6731 }
Al Viro08c422c2011-12-23 07:58:13 -05006732 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006733 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006734 }
Chris Mason39279cc2007-06-12 06:35:45 -04006735
Chris Mason1832a6d2007-12-21 16:27:21 -05006736fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006737 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006738 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 if (drop_inode) {
6740 inode_dec_link_count(inode);
6741 iput(inode);
6742 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006743 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006744 return err;
6745}
6746
Christian Brauner549c7292021-01-21 14:19:43 +01006747static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6748 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006749{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006750 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006751 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006752 struct btrfs_trans_handle *trans;
6753 struct btrfs_root *root = BTRFS_I(dir)->root;
6754 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006755 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006756 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006757
Josef Bacik9ed74f22009-09-11 16:12:44 -04006758 /*
6759 * 2 items for inode and ref
6760 * 2 items for dir items
6761 * 1 for xattr if selinux is on
6762 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006763 trans = btrfs_start_transaction(root, 5);
6764 if (IS_ERR(trans))
6765 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006766
Nikolay Borisov543068a2020-12-07 17:32:33 +02006767 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006768 if (err)
6769 goto out_fail;
6770
Josef Bacikaec74772008-07-24 12:12:38 -04006771 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006772 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6773 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006774 if (IS_ERR(inode)) {
6775 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006776 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006777 goto out_fail;
6778 }
Chris Mason5f39d392007-10-15 16:14:19 -04006779
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 /* these must be set before we unlock the inode */
6781 inode->i_op = &btrfs_dir_inode_operations;
6782 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006783
Eric Paris2a7dba32011-02-01 11:05:39 -05006784 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006785 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006786 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006788 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006789 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006790 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006791 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006792
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006793 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6794 dentry->d_name.name,
6795 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006796 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006797 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006798
Al Viro1e2e5472018-05-04 08:23:01 -04006799 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006800
6801out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006802 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006803 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006804 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006805 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006806 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006807 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006808 return err;
6809}
6810
Chris Masonc8b97812008-10-29 14:49:59 -04006811static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006812 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006813 size_t pg_offset, u64 extent_offset,
6814 struct btrfs_file_extent_item *item)
6815{
6816 int ret;
6817 struct extent_buffer *leaf = path->nodes[0];
6818 char *tmp;
6819 size_t max_size;
6820 unsigned long inline_size;
6821 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006822 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006823
6824 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006825 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006826 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6827 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006828 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006829 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006830 if (!tmp)
6831 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006832 ptr = btrfs_file_extent_inline_start(item);
6833
6834 read_extent_buffer(leaf, tmp, ptr, inline_size);
6835
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006836 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006837 ret = btrfs_decompress(compress_type, tmp, page,
6838 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006839
6840 /*
6841 * decompression code contains a memset to fill in any space between the end
6842 * of the uncompressed data and the end of max_size in case the decompressed
6843 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6844 * the end of an inline extent and the beginning of the next block, so we
6845 * cover that region here.
6846 */
6847
Ira Weinyd048b9c2021-05-04 18:40:07 -07006848 if (max_size + pg_offset < PAGE_SIZE)
6849 memzero_page(page, pg_offset + max_size,
6850 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006851 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006852 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006853}
6854
Omar Sandoval39b07b52019-12-02 17:34:23 -08006855/**
6856 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6857 * @inode: file to search in
6858 * @page: page to read extent data into if the extent is inline
6859 * @pg_offset: offset into @page to copy to
6860 * @start: file offset
6861 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006862 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006863 * This returns the first &struct extent_map which overlaps with the given
6864 * range, reading it from the B-tree and caching it if necessary. Note that
6865 * there may be more extents which overlap the given range after the returned
6866 * extent_map.
6867 *
6868 * If @page is not NULL and the extent is inline, this also reads the extent
6869 * data directly into the page and marks the extent up to date in the io_tree.
6870 *
6871 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006872 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006873struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006874 struct page *page, size_t pg_offset,
6875 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006876{
David Sterba3ffbd682018-06-29 10:56:42 +02006877 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006878 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 u64 extent_start = 0;
6880 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006881 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006882 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006883 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006884 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006885 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006886 struct extent_buffer *leaf;
6887 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006888 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006889 struct extent_map_tree *em_tree = &inode->extent_tree;
6890 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006891
Chris Mason890871b2009-09-02 16:24:52 -04006892 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006893 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006894 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006895
Chris Masona52d9a82007-08-27 16:49:44 -04006896 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006897 if (em->start > start || em->start + em->len <= start)
6898 free_extent_map(em);
6899 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006900 free_extent_map(em);
6901 else
6902 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006903 }
David Sterba172ddd62011-04-21 00:48:27 +02006904 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006905 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006906 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006907 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 }
Chris Masond1310b22008-01-24 16:13:08 -05006909 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006910 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006911 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006912 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006913
Liu Bobee6ec82018-08-17 05:05:28 +08006914 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006915 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006916 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006917 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006918 }
6919
Liu Bobee6ec82018-08-17 05:05:28 +08006920 /* Chances are we'll be called again, so go ahead and do readahead */
6921 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006922
6923 /*
6924 * The same explanation in load_free_space_cache applies here as well,
6925 * we only read when we're loading the free space cache, and at that
6926 * point the commit_root has everything we need.
6927 */
6928 if (btrfs_is_free_space_inode(inode)) {
6929 path->search_commit_root = 1;
6930 path->skip_locking = 1;
6931 }
Josef Bacik51899412020-08-20 11:46:01 -04006932
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006933 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006934 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006935 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006936 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006937 if (path->slots[0] == 0)
6938 goto not_found;
6939 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006940 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006941 }
6942
Chris Mason5f39d392007-10-15 16:14:19 -04006943 leaf = path->nodes[0];
6944 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006945 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006947 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006948 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006949 /*
6950 * If we backup past the first extent we want to move forward
6951 * and see if there is an extent in front of us, otherwise we'll
6952 * say there is a hole for our whole search range which can
6953 * cause problems.
6954 */
6955 extent_end = start;
6956 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006957 }
6958
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006959 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006960 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006961 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006962 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6963 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006964 /* Only regular file could have regular/prealloc extent */
6965 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006966 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006967 btrfs_crit(fs_info,
6968 "regular/prealloc extent found for non-regular inode %llu",
6969 btrfs_ino(inode));
6970 goto out;
6971 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006972 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6973 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006974 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006975 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6976 path->slots[0],
6977 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006978 }
Josef Bacik25a50342013-10-14 12:08:38 -04006979next:
Yan Zheng9036c102008-10-30 14:19:41 -04006980 if (start >= extent_end) {
6981 path->slots[0]++;
6982 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6983 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006984 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006985 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006986 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006987 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006988
Yan Zheng9036c102008-10-30 14:19:41 -04006989 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Yan Zheng9036c102008-10-30 14:19:41 -04006991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6992 if (found_key.objectid != objectid ||
6993 found_key.type != BTRFS_EXTENT_DATA_KEY)
6994 goto not_found;
6995 if (start + len <= found_key.offset)
6996 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006997 if (start > found_key.offset)
6998 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006999
7000 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007001 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007002 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007003 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007004 em->block_start = EXTENT_MAP_HOLE;
7005 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007006 }
7007
Omar Sandoval39b07b52019-12-02 17:34:23 -08007008 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007009
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007010 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7011 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007012 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007013 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007014 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007015 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007016 size_t size;
7017 size_t extent_offset;
7018 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007019
Omar Sandoval39b07b52019-12-02 17:34:23 -08007020 if (!page)
Yan689f9342007-10-29 11:41:07 -04007021 goto out;
Yan689f9342007-10-29 11:41:07 -04007022
Qu Wenruoe41ca582018-06-06 15:41:49 +08007023 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007024 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007025 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7026 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007027 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007028 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007029 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007030 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007031 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007032
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007033 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007034 if (btrfs_file_extent_compression(leaf, item) !=
7035 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007036 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007037 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007038 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007039 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007040 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007041 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007042 read_extent_buffer(leaf, map + pg_offset, ptr,
7043 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007044 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007045 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007046 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007047 copy_size);
7048 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007049 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007050 }
Chris Mason179e29e2007-11-01 11:28:41 -04007051 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007052 }
Chris Masond1310b22008-01-24 16:13:08 -05007053 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007054 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007055 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007056 }
7057not_found:
7058 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007059 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007060 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007061 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007062insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007063 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007064 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007065 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007066 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007067 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7068 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007069 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007070 goto out;
7071 }
Chris Masond1310b22008-01-24 16:13:08 -05007072
Chris Mason890871b2009-09-02 16:24:52 -04007073 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007074 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007075 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007076out:
Liu Boc6414282018-08-23 07:36:17 +08007077 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007078
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007079 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007080
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007081 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007082 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007083 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007084 }
7085 return em;
7086}
7087
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007088struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007089 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007090{
7091 struct extent_map *em;
7092 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007093 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007094 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007095 u64 delalloc_len;
7096 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007097 int err = 0;
7098
Omar Sandoval39b07b52019-12-02 17:34:23 -08007099 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007100 if (IS_ERR(em))
7101 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007102 /*
7103 * If our em maps to:
7104 * - a hole or
7105 * - a pre-alloc extent,
7106 * there might actually be delalloc bytes behind it.
7107 */
7108 if (em->block_start != EXTENT_MAP_HOLE &&
7109 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7110 return em;
7111 else
7112 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007113
7114 /* check to see if we've wrapped (len == -1 or similar) */
7115 end = start + len;
7116 if (end < start)
7117 end = (u64)-1;
7118 else
7119 end -= 1;
7120
7121 em = NULL;
7122
7123 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007124 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007125 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007126 delalloc_end = delalloc_start + delalloc_len;
7127 if (delalloc_end < delalloc_start)
7128 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007129
7130 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007131 * We didn't find anything useful, return the original results from
7132 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007133 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007134 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007135 em = hole_em;
7136 hole_em = NULL;
7137 goto out;
7138 }
7139
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007140 /*
7141 * Adjust the delalloc_start to make sure it doesn't go backwards from
7142 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007143 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007144 delalloc_start = max(start, delalloc_start);
7145 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007146
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007147 if (delalloc_len > 0) {
7148 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007149 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007150 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007151
David Sterba172ddd62011-04-21 00:48:27 +02007152 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007153 if (!em) {
7154 err = -ENOMEM;
7155 goto out;
7156 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007157
7158 ASSERT(hole_em);
7159 /*
7160 * When btrfs_get_extent can't find anything it returns one
7161 * huge hole
7162 *
7163 * Make sure what it found really fits our range, and adjust to
7164 * make sure it is based on the start from the caller
7165 */
7166 if (hole_end <= start || hole_em->start > end) {
7167 free_extent_map(hole_em);
7168 hole_em = NULL;
7169 } else {
7170 hole_start = max(hole_em->start, start);
7171 hole_len = hole_end - hole_start;
7172 }
7173
7174 if (hole_em && delalloc_start > hole_start) {
7175 /*
7176 * Our hole starts before our delalloc, so we have to
7177 * return just the parts of the hole that go until the
7178 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007179 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007180 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007181 em->start = hole_start;
7182 em->orig_start = hole_start;
7183 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007184 * Don't adjust block start at all, it is fixed at
7185 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007186 */
7187 em->block_start = hole_em->block_start;
7188 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007189 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7190 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007191 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007192 /*
7193 * Hole is out of passed range or it starts after
7194 * delalloc range
7195 */
7196 em->start = delalloc_start;
7197 em->len = delalloc_len;
7198 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007199 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007200 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007201 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007202 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007203 return hole_em;
7204 }
7205out:
7206
7207 free_extent_map(hole_em);
7208 if (err) {
7209 free_extent_map(em);
7210 return ERR_PTR(err);
7211 }
7212 return em;
7213}
7214
Nikolay Borisov64f54182020-06-03 08:55:31 +03007215static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007216 const u64 start,
7217 const u64 len,
7218 const u64 orig_start,
7219 const u64 block_start,
7220 const u64 block_len,
7221 const u64 orig_block_len,
7222 const u64 ram_bytes,
7223 const int type)
7224{
7225 struct extent_map *em = NULL;
7226 int ret;
7227
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007228 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007229 em = create_io_em(inode, start, len, orig_start, block_start,
7230 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007231 BTRFS_COMPRESS_NONE, /* compress_type */
7232 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007233 if (IS_ERR(em))
7234 goto out;
7235 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007236 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7237 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007238 if (ret) {
7239 if (em) {
7240 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007241 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007242 }
7243 em = ERR_PTR(ret);
7244 }
7245 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007246
7247 return em;
7248}
7249
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007250static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251 u64 start, u64 len)
7252{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007253 struct btrfs_root *root = inode->root;
7254 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007255 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007256 struct btrfs_key ins;
7257 u64 alloc_hint;
7258 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007260 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007261 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007262 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007263 if (ret)
7264 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007265
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007266 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007267 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007268 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007269 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007270 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007271 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7272 1);
Josef Bacik00361582013-08-14 14:02:47 -04007273
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 return em;
7275}
7276
Anand Jainf4639632021-02-10 21:25:16 -08007277static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007278{
7279 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007280 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007281
7282 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7283 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007284 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007285 if (block_group)
7286 btrfs_put_block_group(block_group);
7287 return readonly;
7288}
7289
Chris Mason46bfbb52010-05-26 11:04:10 -04007290/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007291 * Check if we can do nocow write into the range [@offset, @offset + @len)
7292 *
7293 * @offset: File offset
7294 * @len: The length to write, will be updated to the nocow writeable
7295 * range
7296 * @orig_start: (optional) Return the original file offset of the file extent
7297 * @orig_len: (optional) Return the original on-disk length of the file extent
7298 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007299 * @strict: if true, omit optimizations that might force us into unnecessary
7300 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007301 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007302 * Return:
7303 * >0 and update @len if we can do nocow write
7304 * 0 if we can't do nocow write
7305 * <0 if error happened
7306 *
7307 * NOTE: This only checks the file extents, caller is responsible to wait for
7308 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007309 */
Josef Bacik00361582013-08-14 14:02:47 -04007310noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007311 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007312 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007313{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 struct btrfs_path *path;
7316 int ret;
7317 struct extent_buffer *leaf;
7318 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007319 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007320 struct btrfs_file_extent_item *fi;
7321 struct btrfs_key key;
7322 u64 disk_bytenr;
7323 u64 backref_offset;
7324 u64 extent_end;
7325 u64 num_bytes;
7326 int slot;
7327 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007328 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007329
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 path = btrfs_alloc_path();
7331 if (!path)
7332 return -ENOMEM;
7333
David Sterbaf85b7372017-01-20 14:54:07 +01007334 ret = btrfs_lookup_file_extent(NULL, root, path,
7335 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007336 if (ret < 0)
7337 goto out;
7338
7339 slot = path->slots[0];
7340 if (ret == 1) {
7341 if (slot == 0) {
7342 /* can't find the item, must cow */
7343 ret = 0;
7344 goto out;
7345 }
7346 slot--;
7347 }
7348 ret = 0;
7349 leaf = path->nodes[0];
7350 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007351 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007352 key.type != BTRFS_EXTENT_DATA_KEY) {
7353 /* not our file or wrong item type, must cow */
7354 goto out;
7355 }
7356
7357 if (key.offset > offset) {
7358 /* Wrong offset, must cow */
7359 goto out;
7360 }
7361
7362 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7363 found_type = btrfs_file_extent_type(leaf, fi);
7364 if (found_type != BTRFS_FILE_EXTENT_REG &&
7365 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7366 /* not a regular extent, must cow */
7367 goto out;
7368 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007369
7370 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7371 goto out;
7372
Miao Xiee77751a2013-12-27 21:11:50 +08007373 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7374 if (extent_end <= offset)
7375 goto out;
7376
Chris Mason46bfbb52010-05-26 11:04:10 -04007377 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007378 if (disk_bytenr == 0)
7379 goto out;
7380
7381 if (btrfs_file_extent_compression(leaf, fi) ||
7382 btrfs_file_extent_encryption(leaf, fi) ||
7383 btrfs_file_extent_other_encoding(leaf, fi))
7384 goto out;
7385
Ethan Lien78d42952018-05-17 14:58:29 +08007386 /*
7387 * Do the same check as in btrfs_cross_ref_exist but without the
7388 * unnecessary search.
7389 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007390 if (!strict &&
7391 (btrfs_file_extent_generation(leaf, fi) <=
7392 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007393 goto out;
7394
Chris Mason46bfbb52010-05-26 11:04:10 -04007395 backref_offset = btrfs_file_extent_offset(leaf, fi);
7396
Josef Bacik7ee9e442013-06-21 16:37:03 -04007397 if (orig_start) {
7398 *orig_start = key.offset - backref_offset;
7399 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7400 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7401 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007402
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007403 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007404 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007405
7406 num_bytes = min(offset + *len, extent_end) - offset;
7407 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7408 u64 range_end;
7409
Jeff Mahoneyda170662016-06-15 09:22:56 -04007410 range_end = round_up(offset + num_bytes,
7411 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007412 ret = test_range_bit(io_tree, offset, range_end,
7413 EXTENT_DELALLOC, 0, NULL);
7414 if (ret) {
7415 ret = -EAGAIN;
7416 goto out;
7417 }
7418 }
7419
Josef Bacik1bda19e2013-10-18 12:10:36 -04007420 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007421
7422 /*
7423 * look for other files referencing this extent, if we
7424 * find any we must cow
7425 */
Josef Bacik00361582013-08-14 14:02:47 -04007426
Liu Boe4c3b2d2017-01-30 12:25:28 -08007427 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007428 key.offset - backref_offset, disk_bytenr,
7429 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007430 if (ret) {
7431 ret = 0;
7432 goto out;
7433 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007434
7435 /*
7436 * adjust disk_bytenr and num_bytes to cover just the bytes
7437 * in this extent we are about to write. If there
7438 * are any csums in that range we have to cow in order
7439 * to keep the csums correct
7440 */
7441 disk_bytenr += backref_offset;
7442 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007443 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7444 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007445 /*
7446 * all of the above have passed, it is safe to overwrite this extent
7447 * without cow
7448 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007449 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007450 ret = 1;
7451out:
7452 btrfs_free_path(path);
7453 return ret;
7454}
7455
Josef Bacikeb838e72012-07-31 16:28:48 -04007456static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007457 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007458{
7459 struct btrfs_ordered_extent *ordered;
7460 int ret = 0;
7461
7462 while (1) {
7463 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007464 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007465 /*
7466 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007467 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 * extents in this range.
7469 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007470 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007471 lockend - lockstart + 1);
7472
7473 /*
7474 * We need to make sure there are no buffered pages in this
7475 * range either, we could have raced between the invalidate in
7476 * generic_file_direct_write and locking the extent. The
7477 * invalidate needs to happen so that reads after a write do not
7478 * get stale data.
7479 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007480 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007481 (!writing || !filemap_range_has_page(inode->i_mapping,
7482 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007483 break;
7484
7485 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007486 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007487
7488 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007489 /*
7490 * If we are doing a DIO read and the ordered extent we
7491 * found is for a buffered write, we can not wait for it
7492 * to complete and retry, because if we do so we can
7493 * deadlock with concurrent buffered writes on page
7494 * locks. This happens only if our DIO read covers more
7495 * than one extent map, if at this point has already
7496 * created an ordered extent for a previous extent map
7497 * and locked its range in the inode's io tree, and a
7498 * concurrent write against that previous extent map's
7499 * range and this range started (we unlock the ranges
7500 * in the io tree only when the bios complete and
7501 * buffered writes always lock pages before attempting
7502 * to lock range in the io tree).
7503 */
7504 if (writing ||
7505 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007506 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007507 else
7508 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007509 btrfs_put_ordered_extent(ordered);
7510 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007512 * We could trigger writeback for this range (and wait
7513 * for it to complete) and then invalidate the pages for
7514 * this range (through invalidate_inode_pages2_range()),
7515 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007516 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007517 * triggered a readahead) on a page lock due to an
7518 * ordered dio extent we created before but did not have
7519 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007520 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007521 * ordered extent to complete while holding a lock on
7522 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007523 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007524 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007525 }
7526
Filipe Mananaade77022016-02-18 14:28:55 +00007527 if (ret)
7528 break;
7529
Josef Bacikeb838e72012-07-31 16:28:48 -04007530 cond_resched();
7531 }
7532
7533 return ret;
7534}
7535
Liu Bo6f9994d2017-01-31 07:50:22 -08007536/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007537static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7538 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007539 u64 block_len, u64 orig_block_len,
7540 u64 ram_bytes, int compress_type,
7541 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007542{
7543 struct extent_map_tree *em_tree;
7544 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007545 int ret;
7546
Liu Bo6f9994d2017-01-31 07:50:22 -08007547 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7548 type == BTRFS_ORDERED_COMPRESSED ||
7549 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007550 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007551
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007552 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007553 em = alloc_extent_map();
7554 if (!em)
7555 return ERR_PTR(-ENOMEM);
7556
7557 em->start = start;
7558 em->orig_start = orig_start;
7559 em->len = len;
7560 em->block_len = block_len;
7561 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007562 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007563 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007564 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007565 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007566 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007567 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007568 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007569 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7570 em->compress_type = compress_type;
7571 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007572
7573 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007574 btrfs_drop_extent_cache(inode, em->start,
7575 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007576 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007577 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007578 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007579 /*
7580 * The caller has taken lock_extent(), who could race with us
7581 * to add em?
7582 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007583 } while (ret == -EEXIST);
7584
7585 if (ret) {
7586 free_extent_map(em);
7587 return ERR_PTR(ret);
7588 }
7589
Liu Bo6f9994d2017-01-31 07:50:22 -08007590 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007591 return em;
7592}
7593
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007594
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007595static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007596 struct inode *inode,
7597 struct btrfs_dio_data *dio_data,
7598 u64 start, u64 len)
7599{
7600 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7601 struct extent_map *em = *map;
7602 int ret = 0;
7603
7604 /*
7605 * We don't allocate a new extent in the following cases
7606 *
7607 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7608 * existing extent.
7609 * 2) The extent is marked as PREALLOC. We're good to go here and can
7610 * just use the extent.
7611 *
7612 */
7613 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7614 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7615 em->block_start != EXTENT_MAP_HOLE)) {
7616 int type;
7617 u64 block_start, orig_start, orig_block_len, ram_bytes;
7618
7619 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7620 type = BTRFS_ORDERED_PREALLOC;
7621 else
7622 type = BTRFS_ORDERED_NOCOW;
7623 len = min(len, em->len - (start - em->start));
7624 block_start = em->block_start + (start - em->start);
7625
7626 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007627 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007628 btrfs_inc_nocow_writers(fs_info, block_start)) {
7629 struct extent_map *em2;
7630
Nikolay Borisov64f54182020-06-03 08:55:31 +03007631 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007632 orig_start, block_start,
7633 len, orig_block_len,
7634 ram_bytes, type);
7635 btrfs_dec_nocow_writers(fs_info, block_start);
7636 if (type == BTRFS_ORDERED_PREALLOC) {
7637 free_extent_map(em);
7638 *map = em = em2;
7639 }
7640
7641 if (em2 && IS_ERR(em2)) {
7642 ret = PTR_ERR(em2);
7643 goto out;
7644 }
7645 /*
7646 * For inode marked NODATACOW or extent marked PREALLOC,
7647 * use the existing or preallocated extent, so does not
7648 * need to adjust btrfs_space_info's bytes_may_use.
7649 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007650 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007651 goto skip_cow;
7652 }
7653 }
7654
7655 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007656 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007657 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007658 if (IS_ERR(em)) {
7659 ret = PTR_ERR(em);
7660 goto out;
7661 }
7662
7663 len = min(len, em->len - (start - em->start));
7664
7665skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007666 /*
7667 * Need to update the i_size under the extent lock so buffered
7668 * readers will get the updated i_size when we unlock.
7669 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007670 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007671 i_size_write(inode, start + len);
7672
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007673 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007674out:
7675 return ret;
7676}
7677
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007678static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7679 loff_t length, unsigned int flags, struct iomap *iomap,
7680 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007682 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007683 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007684 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307685 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007687 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007688 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007689 u64 len = length;
7690 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007691
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007692 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007693 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007694
Josef Bacikc3298612012-08-03 16:49:19 -04007695 lockstart = start;
7696 lockend = start + len - 1;
7697
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007698 /*
7699 * The generic stuff only does filemap_write_and_wait_range, which
7700 * isn't enough if we've written compressed pages to this area, so we
7701 * need to flush the dirty pages again to make absolutely sure that any
7702 * outstanding dirty pages are on disk.
7703 */
7704 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7705 &BTRFS_I(inode)->runtime_flags)) {
7706 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7707 start + length - 1);
7708 if (ret)
7709 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007710 }
7711
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007712 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7713 if (!dio_data)
7714 return -ENOMEM;
7715
7716 dio_data->length = length;
7717 if (write) {
7718 dio_data->reserve = round_up(length, fs_info->sectorsize);
7719 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7720 &dio_data->data_reserved,
7721 start, dio_data->reserve);
7722 if (ret) {
7723 extent_changeset_free(dio_data->data_reserved);
7724 kfree(dio_data);
7725 return ret;
7726 }
7727 }
7728 iomap->private = dio_data;
7729
7730
Josef Bacikeb838e72012-07-31 16:28:48 -04007731 /*
7732 * If this errors out it's because we couldn't invalidate pagecache for
7733 * this range and we need to fallback to buffered.
7734 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007735 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007736 ret = -ENOTBLK;
7737 goto err;
7738 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007739
Omar Sandoval39b07b52019-12-02 17:34:23 -08007740 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007741 if (IS_ERR(em)) {
7742 ret = PTR_ERR(em);
7743 goto unlock_err;
7744 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745
7746 /*
7747 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7748 * io. INLINE is special, and we could probably kludge it in here, but
7749 * it's still buffered so for safety lets just fall back to the generic
7750 * buffered path.
7751 *
7752 * For COMPRESSED we _have_ to read the entire extent in so we can
7753 * decompress it, so there will be buffering required no matter what we
7754 * do, so go ahead and fallback to buffered.
7755 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007756 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007757 * to buffered IO. Don't blame me, this is the price we pay for using
7758 * the generic code.
7759 */
7760 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7761 em->block_start == EXTENT_MAP_INLINE) {
7762 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007763 ret = -ENOTBLK;
7764 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 }
7766
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007767 len = min(len, em->len - (start - em->start));
7768 if (write) {
7769 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7770 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007771 if (ret < 0)
7772 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007773 unlock_extents = true;
7774 /* Recalc len in case the new em is smaller than requested */
7775 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007776 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007777 /*
7778 * We need to unlock only the end area that we aren't using.
7779 * The rest is going to be unlocked by the endio routine.
7780 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007781 lockstart = start + len;
7782 if (lockstart < lockend)
7783 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784 }
7785
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007786 if (unlock_extents)
7787 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7788 lockstart, lockend, &cached_state);
7789 else
7790 free_extent_state(cached_state);
7791
7792 /*
7793 * Translate extent map information to iomap.
7794 * We trim the extents (and move the addr) even though iomap code does
7795 * that, since we have locked only the parts we are performing I/O in.
7796 */
7797 if ((em->block_start == EXTENT_MAP_HOLE) ||
7798 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7799 iomap->addr = IOMAP_NULL_ADDR;
7800 iomap->type = IOMAP_HOLE;
7801 } else {
7802 iomap->addr = em->block_start + (start - em->start);
7803 iomap->type = IOMAP_MAPPED;
7804 }
7805 iomap->offset = start;
7806 iomap->bdev = fs_info->fs_devices->latest_bdev;
7807 iomap->length = len;
7808
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007809 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007810 iomap->flags |= IOMAP_F_ZONE_APPEND;
7811
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812 free_extent_map(em);
7813
7814 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007815
7816unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007817 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7818 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007819err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007820 if (dio_data) {
7821 btrfs_delalloc_release_space(BTRFS_I(inode),
7822 dio_data->data_reserved, start,
7823 dio_data->reserve, true);
7824 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7825 extent_changeset_free(dio_data->data_reserved);
7826 kfree(dio_data);
7827 }
7828 return ret;
7829}
7830
7831static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7832 ssize_t written, unsigned int flags, struct iomap *iomap)
7833{
7834 int ret = 0;
7835 struct btrfs_dio_data *dio_data = iomap->private;
7836 size_t submitted = dio_data->submitted;
7837 const bool write = !!(flags & IOMAP_WRITE);
7838
7839 if (!write && (iomap->type == IOMAP_HOLE)) {
7840 /* If reading from a hole, unlock and return */
7841 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7842 goto out;
7843 }
7844
7845 if (submitted < length) {
7846 pos += submitted;
7847 length -= submitted;
7848 if (write)
7849 __endio_write_update_ordered(BTRFS_I(inode), pos,
7850 length, false);
7851 else
7852 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7853 pos + length - 1);
7854 ret = -ENOTBLK;
7855 }
7856
7857 if (write) {
7858 if (dio_data->reserve)
7859 btrfs_delalloc_release_space(BTRFS_I(inode),
7860 dio_data->data_reserved, pos,
7861 dio_data->reserve, true);
7862 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7863 extent_changeset_free(dio_data->data_reserved);
7864 }
7865out:
7866 kfree(dio_data);
7867 iomap->private = NULL;
7868
Josef Bacikeb838e72012-07-31 16:28:48 -04007869 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007870}
7871
Omar Sandoval769b4f22020-04-16 14:46:22 -07007872static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7873{
7874 /*
7875 * This implies a barrier so that stores to dio_bio->bi_status before
7876 * this and loads of dio_bio->bi_status after this are fully ordered.
7877 */
7878 if (!refcount_dec_and_test(&dip->refs))
7879 return;
7880
Naohiro Aotacfe94442021-02-04 19:21:59 +09007881 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007882 __endio_write_update_ordered(BTRFS_I(dip->inode),
7883 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007884 dip->bytes,
7885 !dip->dio_bio->bi_status);
7886 } else {
7887 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7888 dip->logical_offset,
7889 dip->logical_offset + dip->bytes - 1);
7890 }
7891
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007892 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007893 kfree(dip);
7894}
7895
Omar Sandoval77d5d682020-04-16 14:46:25 -07007896static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7897 int mirror_num,
7898 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007899{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007900 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007902 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007903
Mike Christie37226b22016-06-05 14:31:52 -05007904 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007905
Omar Sandoval5c047a62020-04-16 14:46:24 -07007906 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007907 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007908 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007909
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007910 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007911 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007912 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007913 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007914 return ret;
7915}
7916
Omar Sandoval769b4f22020-04-16 14:46:22 -07007917static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7918 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007919 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007920{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007921 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7922 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007923 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7925 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007926 struct bio_vec bvec;
7927 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007928 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007929 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007930 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007931
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007932 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7933 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307934
Liu Bo17347ce2017-05-15 15:33:27 -07007935 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7936 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007937 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007938 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007939 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007940 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007941 bio_offset, bvec.bv_page,
7942 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007943 clean_io_failure(fs_info, failure_tree, io_tree,
7944 start, bvec.bv_page,
7945 btrfs_ino(BTRFS_I(inode)),
7946 pgoff);
7947 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08007948 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007949
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007950 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08007951 ret = btrfs_repair_one_sector(inode,
7952 &io_bio->bio,
7953 start - io_bio->logical,
7954 bvec.bv_page, pgoff,
7955 start, io_bio->mirror_num,
7956 submit_dio_repair_bio);
7957 if (ret)
7958 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007959 }
7960 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007961 ASSERT(bio_offset + sectorsize > bio_offset);
7962 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307963 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007965 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007966 return err;
7967}
7968
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007969static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007970 const u64 offset, const u64 bytes,
7971 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007972{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007973 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007974 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007975 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007976 u64 ordered_offset = offset;
7977 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007978 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007979
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007980 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007981 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007982 else
Qu Wenruo524272602017-03-08 10:25:52 +08007983 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007984
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007985 while (ordered_offset < offset + bytes) {
7986 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007987 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007988 &ordered_offset,
7989 ordered_bytes,
7990 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007991 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7992 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007993 btrfs_queue_work(wq, &ordered->work);
7994 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08007995
7996 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007997 if (ordered_offset == last_offset)
7998 return;
7999 /*
8000 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008001 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008002 */
8003 if (ordered_offset < offset + bytes) {
8004 ordered_bytes = offset + bytes - ordered_offset;
8005 ordered = NULL;
8006 }
Chris Mason163cf092010-11-28 19:56:33 -05008007 }
Filipe Manana14543772015-11-24 16:23:54 +00008008}
8009
Qu Wenruo8896a082020-10-21 14:24:53 +08008010static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008011 struct bio *bio,
8012 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008013{
Qu Wenruo1941b642020-12-02 14:47:57 +08008014 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008015}
8016
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008017static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008018{
8019 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008020 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008021
Miao Xie8b110e32014-09-12 18:44:03 +08008022 if (err)
8023 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008024 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008025 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008026 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008027 bio->bi_iter.bi_size, err);
8028
Omar Sandoval769b4f22020-04-16 14:46:22 -07008029 if (bio_op(bio) == REQ_OP_READ) {
8030 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008031 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008032 }
8033
Omar Sandoval769b4f22020-04-16 14:46:22 -07008034 if (err)
8035 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008036
Naohiro Aota544d24f2021-02-04 19:22:06 +09008037 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8038
Miao Xiee65e1532010-11-22 03:04:43 +00008039 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008040 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008041}
8042
David Sterbad0ee3932018-03-08 14:35:48 +01008043static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8044 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008047 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008048 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008049 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008050
Liu Bo4c274bc2017-11-01 17:19:27 -06008051 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008052 if (async_submit)
8053 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8054
Josef Bacik5fd02042012-05-02 14:00:54 -04008055 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008056 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008057 if (ret)
8058 goto err;
8059 }
Miao Xiee65e1532010-11-22 03:04:43 +00008060
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008061 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008062 goto map;
8063
8064 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008065 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008066 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008067 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008068 } else if (write) {
8069 /*
8070 * If we aren't doing async submit, calculate the csum of the
8071 * bio now.
8072 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008073 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008074 if (ret)
8075 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008076 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008077 u64 csum_offset;
8078
8079 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008080 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008081 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008082 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008083 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008084map:
Chris Mason08635ba2019-07-10 12:28:14 -07008085 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008086err:
Miao Xiee65e1532010-11-22 03:04:43 +00008087 return ret;
8088}
8089
Omar Sandovalc36cac22020-04-16 14:46:13 -07008090/*
8091 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8092 * or ordered extents whether or not we submit any bios.
8093 */
8094static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8095 struct inode *inode,
8096 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008097{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008098 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008099 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8100 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008101 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008102
Omar Sandoval85879572020-04-16 14:46:21 -07008103 dip_size = sizeof(*dip);
8104 if (!write && csum) {
8105 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008106 size_t nblocks;
8107
David Sterba265fdfa2020-07-01 21:19:09 +02008108 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008109 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008110 }
8111
8112 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008113 if (!dip)
8114 return NULL;
8115
Omar Sandovalc36cac22020-04-16 14:46:13 -07008116 dip->inode = inode;
8117 dip->logical_offset = file_offset;
8118 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008119 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008120 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008121 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008122 return dip;
8123}
8124
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008125static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8126 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008127{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008128 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008130 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8131 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008132 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008133 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008134 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008135 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008136 u64 submit_len;
8137 int clone_offset = 0;
8138 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008139 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308140 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008141 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008142 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008143 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008144 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008145
Omar Sandovalc36cac22020-04-16 14:46:13 -07008146 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8147 if (!dip) {
8148 if (!write) {
8149 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8150 file_offset + dio_bio->bi_iter.bi_size - 1);
8151 }
8152 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008153 bio_endio(dio_bio);
8154 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008155 }
8156
Josef Bacik334c16d2020-10-16 11:29:14 -04008157 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008158 /*
8159 * Load the csums up front to reduce csum tree searches and
8160 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008161 *
8162 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008163 */
Qu Wenruo62751932020-12-02 14:48:06 +08008164 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008165 if (status != BLK_STS_OK)
8166 goto out_err;
8167 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008168
Omar Sandoval769b4f22020-04-16 14:46:22 -07008169 start_sector = dio_bio->bi_iter.bi_sector;
8170 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008171
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008172 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008173 logical = start_sector << 9;
8174 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8175 if (IS_ERR(em)) {
8176 status = errno_to_blk_status(PTR_ERR(em));
8177 em = NULL;
8178 goto out_err_em;
8179 }
8180 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
8181 logical, submit_len, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008182 if (ret) {
8183 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008184 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008185 }
8186 ASSERT(geom.len <= INT_MAX);
8187
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008188 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008189
Liu Bo725130b2017-05-16 09:51:39 -07008190 /*
8191 * This will never fail as it's passing GPF_NOFS and
8192 * the allocation is backed by btrfs_bioset.
8193 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008194 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008195 bio->bi_private = dip;
8196 bio->bi_end_io = btrfs_end_dio_bio;
8197 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008198
Naohiro Aota544d24f2021-02-04 19:22:06 +09008199 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8200 status = extract_ordered_extent(BTRFS_I(inode), bio,
8201 file_offset);
8202 if (status) {
8203 bio_put(bio);
8204 goto out_err;
8205 }
8206 }
8207
Liu Bo725130b2017-05-16 09:51:39 -07008208 ASSERT(submit_len >= clone_len);
8209 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008210
Liu Bo725130b2017-05-16 09:51:39 -07008211 /*
8212 * Increase the count before we submit the bio so we know
8213 * the end IO handler won't happen before we increase the
8214 * count. Otherwise, the dip might get freed before we're
8215 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008216 *
8217 * We transfer the initial reference to the last bio, so we
8218 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008219 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008220 if (submit_len > 0) {
8221 refcount_inc(&dip->refs);
8222 /*
8223 * If we are submitting more than one bio, submit them
8224 * all asynchronously. The exception is RAID 5 or 6, as
8225 * asynchronous checksums make it difficult to collect
8226 * full stripe writes.
8227 */
8228 if (!raid56)
8229 async_submit = 1;
8230 }
Miao Xiee65e1532010-11-22 03:04:43 +00008231
David Sterbad0ee3932018-03-08 14:35:48 +01008232 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008233 async_submit);
8234 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008235 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008236 if (submit_len > 0)
8237 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008238 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008239 }
Liu Bo725130b2017-05-16 09:51:39 -07008240
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008241 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008242 clone_offset += clone_len;
8243 start_sector += clone_len >> 9;
8244 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008245
8246 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008247 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008248 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008249
Michal Rostecki42034312021-01-27 14:57:27 +01008250out_err_em:
8251 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008252out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008253 dip->dio_bio->bi_status = status;
8254 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008255
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008256 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008257}
8258
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008259const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008260 .iomap_begin = btrfs_dio_iomap_begin,
8261 .iomap_end = btrfs_dio_iomap_end,
8262};
8263
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008264const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008265 .submit_io = btrfs_submit_direct,
8266};
8267
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008268static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008269 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008270{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008271 int ret;
8272
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008273 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008274 if (ret)
8275 return ret;
8276
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008277 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008278}
8279
Chris Mason9ebefb182007-06-15 13:50:00 -04008280int btrfs_readpage(struct file *file, struct page *page)
8281{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008282 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8283 u64 start = page_offset(page);
8284 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008285 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008286 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008287 int ret;
8288
Nikolay Borisov0f208812020-09-14 14:39:16 +03008289 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8290
8291 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008292 if (bio)
8293 ret = submit_one_bio(bio, 0, bio_flags);
8294 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008295}
Chris Mason1832a6d2007-12-21 16:27:21 -05008296
Chris Mason39279cc2007-06-12 06:35:45 -04008297static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8298{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008299 struct inode *inode = page->mapping->host;
8300 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008301
8302 if (current->flags & PF_MEMALLOC) {
8303 redirty_page_for_writepage(wbc, page);
8304 unlock_page(page);
8305 return 0;
8306 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008307
8308 /*
8309 * If we are under memory pressure we will call this directly from the
8310 * VM, we need to make sure we have the inode referenced for the ordered
8311 * extent. If not just return like we didn't do anything.
8312 */
8313 if (!igrab(inode)) {
8314 redirty_page_for_writepage(wbc, page);
8315 return AOP_WRITEPAGE_ACTIVATE;
8316 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008317 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008318 btrfs_add_delayed_iput(inode);
8319 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008320}
Chris Mason39279cc2007-06-12 06:35:45 -04008321
Eric Sandeen48a3b632013-04-25 20:41:01 +00008322static int btrfs_writepages(struct address_space *mapping,
8323 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008324{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008325 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008326}
8327
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008328static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008329{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008330 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008331}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008332
Chris Masone6dcd2d2008-07-17 12:53:50 -04008333static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008334{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008335 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008336 if (ret == 1)
Qu Wenruo32443de2021-01-26 16:34:00 +08008337 clear_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008338 return ret;
8339}
Chris Mason39279cc2007-06-12 06:35:45 -04008340
Chris Masone6dcd2d2008-07-17 12:53:50 -04008341static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8342{
Chris Mason98509cf2008-09-11 15:51:43 -04008343 if (PageWriteback(page) || PageDirty(page))
8344 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008345 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008346}
8347
Roman Gushchinf8e66082020-03-04 16:57:35 -08008348#ifdef CONFIG_MIGRATION
8349static int btrfs_migratepage(struct address_space *mapping,
8350 struct page *newpage, struct page *page,
8351 enum migrate_mode mode)
8352{
8353 int ret;
8354
8355 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8356 if (ret != MIGRATEPAGE_SUCCESS)
8357 return ret;
8358
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008359 if (page_has_private(page))
8360 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008361
8362 if (PagePrivate2(page)) {
8363 ClearPagePrivate2(page);
8364 SetPagePrivate2(newpage);
8365 }
8366
8367 if (mode != MIGRATE_SYNC_NO_COPY)
8368 migrate_page_copy(newpage, page);
8369 else
8370 migrate_page_states(newpage, page);
8371 return MIGRATEPAGE_SUCCESS;
8372}
8373#endif
8374
Lukas Czernerd47992f2013-05-21 23:17:23 -04008375static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8376 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008377{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008378 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8379 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008380 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008381 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008382 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008383 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308384 u64 start;
8385 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008386 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008387 bool found_ordered = false;
8388 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008389
Chris Mason8b62b722009-09-02 16:53:46 -04008390 /*
8391 * we have the page locked, so new writeback can't start,
8392 * and the dirty bit won't be cleared while we are here.
8393 *
8394 * Wait for IO on this page so that we can safely clear
8395 * the PagePrivate2 bit and do ordered accounting
8396 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008397 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008398
Chris Masone6dcd2d2008-07-17 12:53:50 -04008399 if (offset) {
8400 btrfs_releasepage(page, GFP_NOFS);
8401 return;
8402 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008403
8404 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008405 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008406
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308407 start = page_start;
Qu Wenruo951c80f2021-01-27 14:38:48 +08008408again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008409 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008410 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008411 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008412 end = min(page_end,
8413 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008414 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008415 * IO on this page will never be started, so we need to account
8416 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8417 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008418 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008419 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308420 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008421 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008422 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008423 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008424 /*
8425 * whoever cleared the private bit is responsible
8426 * for the finish_ordered_io
8427 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008428 if (TestClearPagePrivate2(page)) {
Qu Wenruod239bcb2021-03-25 15:14:35 +08008429 spin_lock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008430 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Qu Wenruoac5804e2021-03-25 15:14:34 +08008431 ordered->truncated_len = min(ordered->truncated_len,
8432 start - ordered->file_offset);
Qu Wenruod239bcb2021-03-25 15:14:35 +08008433 spin_unlock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008434
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008435 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8436 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008437 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008438 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008439 completed_ordered = true;
8440 }
Chris Mason8b62b722009-09-02 16:53:46 -04008441 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008442 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008443 if (!inode_evicting) {
8444 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308445 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008446 &cached_state);
8447 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308448
8449 start = end + 1;
8450 if (start < page_end)
8451 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008452 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008453
Qu Wenruob9d0b382015-09-29 10:35:16 +08008454 /*
8455 * Qgroup reserved space handler
8456 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008457 * 1) Already written to disk or ordered extent already submitted
8458 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8459 * Qgroup will be handled by its qgroup_record then.
8460 * btrfs_qgroup_free_data() call will do nothing here.
8461 *
8462 * 2) Not written to disk yet
8463 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8464 * bit of its io_tree, and free the qgroup reserved data space.
8465 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008466 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008467 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008468 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008469 bool delete = true;
8470
8471 /*
8472 * If there's an ordered extent for this range and we have not
8473 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8474 * in the range for the ordered extent completion. We must also
8475 * not delete the range, otherwise we would lose that bit (and
8476 * any other bits set in the range). Make sure EXTENT_UPTODATE
8477 * is cleared if we don't delete, otherwise it can lead to
8478 * corruptions if the i_size is extented later.
8479 */
8480 if (found_ordered && !completed_ordered)
8481 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008482 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008483 EXTENT_DELALLOC | EXTENT_UPTODATE |
8484 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8485 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008486
8487 __btrfs_releasepage(page, GFP_NOFS);
8488 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008489
Chris Mason4a096752008-07-21 10:29:44 -04008490 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008491 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008492}
8493
Chris Mason9ebefb182007-06-15 13:50:00 -04008494/*
8495 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8496 * called from a page fault handler when a page is first dirtied. Hence we must
8497 * be careful to check for EOF conditions here. We set the page up correctly
8498 * for a written page which means we get ENOSPC checking when writing into
8499 * holes and correct delalloc and unwritten extent mapping on filesystems that
8500 * support these features.
8501 *
8502 * We are not allowed to take the i_mutex here so we have to play games to
8503 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008504 * truncate_setsize() writes the inode size before removing pages, once we have
8505 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008506 * beyond EOF, then the page is guaranteed safe against truncation until we
8507 * unlock the page.
8508 */
Souptick Joardera528a242018-06-06 19:54:44 +05308509vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008510{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008511 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008512 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008513 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008514 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8515 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008516 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008517 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008518 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008519 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308520 vm_fault_t ret;
8521 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008522 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308523 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008524 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008525 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308526 u64 end;
8527
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008528 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008529
Jan Karab2b5ef52012-06-12 16:20:45 +02008530 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008531 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008532 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308533 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008534
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308535 /*
8536 * Reserving delalloc space after obtaining the page lock can lead to
8537 * deadlock. For example, if a dirty page is locked by this function
8538 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8539 * dirty page write out, then the btrfs_writepage() function could
8540 * end up waiting indefinitely to get a lock on the page currently
8541 * being processed by btrfs_page_mkwrite() function.
8542 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008543 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8544 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308545 if (!ret2) {
8546 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008547 reserved = 1;
8548 }
Souptick Joardera528a242018-06-06 19:54:44 +05308549 if (ret2) {
8550 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008551 if (reserved)
8552 goto out;
8553 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008554 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008555
Nick Piggin56a76f82009-03-31 15:23:23 -07008556 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008558 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008559 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008560 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008561
Chris Mason9ebefb182007-06-15 13:50:00 -04008562 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008564 /* page got truncated out from underneath us */
8565 goto out_unlock;
8566 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008567 wait_on_page_writeback(page);
8568
David Sterbaff13db42015-12-03 14:30:40 +01008569 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008570 ret2 = set_page_extent_mapped(page);
8571 if (ret2 < 0) {
8572 ret = vmf_error(ret2);
8573 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8574 goto out_unlock;
8575 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008576
Chris Masoneb84ae02008-07-17 13:53:27 -04008577 /*
8578 * we can't set the delalloc bits if there are pending ordered
8579 * extents. Drop our locks and wait for them to finish
8580 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008581 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8582 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008583 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008584 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008585 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008586 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008587 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008588 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008589 btrfs_put_ordered_extent(ordered);
8590 goto again;
8591 }
8592
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008593 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008594 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008595 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008596 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308597 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008598 btrfs_delalloc_release_space(BTRFS_I(inode),
8599 data_reserved, page_start,
8600 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308601 }
8602 }
8603
Josef Bacikfbf19082009-10-01 17:10:23 -04008604 /*
Liu Bo54160342016-12-13 12:15:19 -08008605 * page_mkwrite gets called when the page is firstly dirtied after it's
8606 * faulted in, but write(2) could also dirty a page and set delalloc
8607 * bits, thus in this case for space account reason, we still need to
8608 * clear any delalloc bits within this page range since we have to
8609 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008610 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308611 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008612 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8613 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008614
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008615 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008616 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308617 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008618 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008619 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008620 ret = VM_FAULT_SIGBUS;
8621 goto out_unlock;
8622 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008623
8624 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008625 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008626 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008627 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008628 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008629
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008630 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008631 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008633 }
Chris Mason247e7432008-07-17 12:53:51 -04008634 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008635 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008636 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008637
Filipe Mananabc0939f2021-02-23 12:08:48 +00008638 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008639
David Sterbae43bbe52017-12-12 21:43:52 +01008640 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008641 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008642
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008643 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8644 sb_end_pagefault(inode->i_sb);
8645 extent_changeset_free(data_reserved);
8646 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008647
8648out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008649 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008650 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008651out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008652 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008653 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008654 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008655out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008656 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008657 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008658 return ret;
8659}
8660
Filipe Manana213e8c52018-02-06 20:40:31 +00008661static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008662{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008663 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008664 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008665 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008666 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008667 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008668 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008669 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008670
Filipe Manana213e8c52018-02-06 20:40:31 +00008671 if (!skip_writeback) {
8672 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8673 (u64)-1);
8674 if (ret)
8675 return ret;
8676 }
Chris Mason39279cc2007-06-12 06:35:45 -04008677
Josef Bacikfcb80c22011-05-03 10:40:22 -04008678 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008679 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8680 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008681 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008682 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008683 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008684 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008685 * be free'd up by the truncate operation, but also have some slack
8686 * space reserved in case it uses space during the truncate (thank you
8687 * very much snapshotting).
8688 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008689 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008690 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008691 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008692 * doesn't end up using space reserved for updating the inode. We also
8693 * need to be able to stop the transaction and start a new one, which
8694 * means we need to be able to update the inode several times, and we
8695 * have no idea of knowing how many times that will be, so we can't just
8696 * reserve 1 item for the entirety of the operation, so that has to be
8697 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008698 *
8699 * So that leaves us with
8700 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008701 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008702 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008703 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008704 * updating the inode.
8705 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008706 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008707 if (!rsv)
8708 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008709 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008710 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008711
Josef Bacik907cbce2011-08-08 13:46:15 -04008712 /*
Josef Bacik07127182011-08-19 10:29:59 -04008713 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008714 * 1 for updating the inode.
8715 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008716 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008717 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008718 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008719 goto out;
8720 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008721
Josef Bacik907cbce2011-08-08 13:46:15 -04008722 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008723 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008724 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008725 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008726
Chris Mason5a3f23d2009-03-31 13:27:11 -04008727 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008728 * So if we truncate and then write and fsync we normally would just
8729 * write the extents that changed, which is a problem if we need to
8730 * first truncate that entire inode. So set this flag so we write out
8731 * all of the extents in the inode to the sync log so we're completely
8732 * safe.
8733 */
8734 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008735 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008736
Yan, Zheng80825102009-11-12 09:35:36 +00008737 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008738 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008739 inode->i_size,
8740 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008741 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008742 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008743 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008744
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008745 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008746 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008747 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008748
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008749 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008750 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008751
8752 trans = btrfs_start_transaction(root, 2);
8753 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008754 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008755 trans = NULL;
8756 break;
8757 }
8758
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008759 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008760 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008761 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008762 BUG_ON(ret); /* shouldn't happen */
8763 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008764 }
8765
Josef Bacikddfae632017-10-19 14:16:02 -04008766 /*
8767 * We can't call btrfs_truncate_block inside a trans handle as we could
8768 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8769 * we've truncated everything except the last little bit, and can do
8770 * btrfs_truncate_block and then update the disk_i_size.
8771 */
8772 if (ret == NEED_TRUNCATE_BLOCK) {
8773 btrfs_end_transaction(trans);
8774 btrfs_btree_balance_dirty(fs_info);
8775
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008776 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008777 if (ret)
8778 goto out;
8779 trans = btrfs_start_transaction(root, 1);
8780 if (IS_ERR(trans)) {
8781 ret = PTR_ERR(trans);
8782 goto out;
8783 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008784 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008785 }
8786
Chris Mason917c16b2011-11-08 14:49:59 -05008787 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008788 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008789
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008790 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008791 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008792 if (ret2 && !ret)
8793 ret = ret2;
8794
8795 ret2 = btrfs_end_transaction(trans);
8796 if (ret2 && !ret)
8797 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008798 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008799 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008800out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008801 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008802
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008803 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008804}
8805
Sven Wegener3b963622008-06-09 21:57:42 -04008806/*
Chris Masond352ac62008-09-29 15:18:18 -04008807 * create a new subvolume directory/inode (helper for the ioctl).
8808 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008809int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008810 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008811 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008812{
Chris Mason39279cc2007-06-12 06:35:45 -04008813 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008814 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008815 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008816 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008817
Nikolay Borisov23125102020-12-07 17:32:36 +02008818 err = btrfs_get_free_objectid(new_root, &ino);
8819 if (err < 0)
8820 return err;
8821
8822 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008823 S_IFDIR | (~current_umask() & S_IRWXUGO),
8824 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008825 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008826 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008827 inode->i_op = &btrfs_dir_inode_operations;
8828 inode->i_fop = &btrfs_dir_file_operations;
8829
Miklos Szeredibfe86842011-10-28 14:13:29 +02008830 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008831 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008832 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008833
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008834 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8835 if (err)
8836 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008837 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008838 new_root->root_key.objectid, err);
8839
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008840 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008841
Yan, Zheng76dda932009-09-21 16:00:26 -04008842 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008843 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008844}
8845
Chris Mason39279cc2007-06-12 06:35:45 -04008846struct inode *btrfs_alloc_inode(struct super_block *sb)
8847{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008848 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008849 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008850 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008851
David Sterba712e36c2017-10-31 17:08:27 +01008852 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008853 if (!ei)
8854 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008855
8856 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008857 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008858 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008859 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008860 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008861 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008862 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008863 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008864 ei->disk_i_size = 0;
8865 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008866 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008867 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008868 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008869 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008870 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008871 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008872
Josef Bacik9e0baf62011-07-15 15:16:44 +00008873 spin_lock_init(&ei->lock);
8874 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008875 if (sb->s_magic != BTRFS_TEST_MAGIC)
8876 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8877 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008878 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008879 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008880 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008881
Miao Xie16cdcec2011-04-22 18:12:22 +08008882 ei->delayed_node = NULL;
8883
chandan r9cc97d62012-07-04 12:48:07 +05308884 ei->i_otime.tv_sec = 0;
8885 ei->i_otime.tv_nsec = 0;
8886
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008887 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008888 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008889 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8890 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8891 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008892 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8893 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008894 ei->io_tree.track_uptodate = true;
8895 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008896 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008897 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008898 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008899 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008900 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008901 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05008902 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008903
8904 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008905}
8906
Josef Bacikaaedb552013-10-11 14:44:09 -04008907#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8908void btrfs_test_destroy_inode(struct inode *inode)
8909{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008910 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008911 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8912}
8913#endif
8914
Al Viro26602ca2019-04-10 15:14:41 -04008915void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008916{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008917 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8918}
8919
Nikolay Borisov633cc812020-09-18 12:15:49 +03008920void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008921{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008923 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8924 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008925
Nikolay Borisov633cc812020-09-18 12:15:49 +03008926 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8927 WARN_ON(vfs_inode->i_data.nrpages);
8928 WARN_ON(inode->block_rsv.reserved);
8929 WARN_ON(inode->block_rsv.size);
8930 WARN_ON(inode->outstanding_extents);
8931 WARN_ON(inode->delalloc_bytes);
8932 WARN_ON(inode->new_delalloc_bytes);
8933 WARN_ON(inode->csum_bytes);
8934 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008935
Chris Mason5a3f23d2009-03-31 13:27:11 -04008936 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008937 * This can happen where we create an inode, but somebody else also
8938 * created the same inode and we need to destroy the one we already
8939 * created.
8940 */
8941 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008942 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008943
Chris Masond3977122009-01-05 21:25:51 -05008944 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008945 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946 if (!ordered)
8947 break;
8948 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008949 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008950 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008951 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008952 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 btrfs_put_ordered_extent(ordered);
8954 btrfs_put_ordered_extent(ordered);
8955 }
8956 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008957 btrfs_qgroup_check_reserved_leak(inode);
8958 inode_tree_del(inode);
8959 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8960 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8961 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008962}
8963
Al Viro45321ac2010-06-07 13:43:19 -04008964int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008965{
8966 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008967
Naohiro Aota6379ef92013-06-06 09:56:34 +00008968 if (root == NULL)
8969 return 1;
8970
Liu Bofa6ac872013-02-20 14:10:23 +00008971 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008972 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008973 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008974 else
Al Viro45321ac2010-06-07 13:43:19 -04008975 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008976}
8977
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008978static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008979{
8980 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8981
8982 inode_init_once(&ei->vfs_inode);
8983}
8984
David Sterbae67c7182018-02-19 17:24:18 +01008985void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008986{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008987 /*
8988 * Make sure all delayed rcu free inodes are flushed before we
8989 * destroy cache.
8990 */
8991 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008992 kmem_cache_destroy(btrfs_inode_cachep);
8993 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008994 kmem_cache_destroy(btrfs_path_cachep);
8995 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008996 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008997}
8998
Liu Bof5c29bd2017-11-02 17:21:50 -06008999int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009000{
David Sterba837e1972012-09-07 03:00:48 -06009001 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009002 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009003 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9004 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009005 if (!btrfs_inode_cachep)
9006 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009007
David Sterba837e1972012-09-07 03:00:48 -06009008 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009009 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009010 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009011 if (!btrfs_trans_handle_cachep)
9012 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009013
David Sterba837e1972012-09-07 03:00:48 -06009014 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009015 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009016 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009017 if (!btrfs_path_cachep)
9018 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009019
David Sterba837e1972012-09-07 03:00:48 -06009020 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009021 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009022 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009023 if (!btrfs_free_space_cachep)
9024 goto fail;
9025
Christophe Leroy3acd4852019-08-21 15:05:55 +00009026 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9027 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009028 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009029 if (!btrfs_free_space_bitmap_cachep)
9030 goto fail;
9031
Chris Mason39279cc2007-06-12 06:35:45 -04009032 return 0;
9033fail:
9034 btrfs_destroy_cachep();
9035 return -ENOMEM;
9036}
9037
Christian Brauner549c7292021-01-21 14:19:43 +01009038static int btrfs_getattr(struct user_namespace *mnt_userns,
9039 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009040 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009041{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009042 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009043 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009044 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009045 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009046 u32 bi_flags = BTRFS_I(inode)->flags;
9047
9048 stat->result_mask |= STATX_BTIME;
9049 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9050 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9051 if (bi_flags & BTRFS_INODE_APPEND)
9052 stat->attributes |= STATX_ATTR_APPEND;
9053 if (bi_flags & BTRFS_INODE_COMPRESS)
9054 stat->attributes |= STATX_ATTR_COMPRESSED;
9055 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9056 stat->attributes |= STATX_ATTR_IMMUTABLE;
9057 if (bi_flags & BTRFS_INODE_NODUMP)
9058 stat->attributes |= STATX_ATTR_NODUMP;
9059
9060 stat->attributes_mask |= (STATX_ATTR_APPEND |
9061 STATX_ATTR_COMPRESSED |
9062 STATX_ATTR_IMMUTABLE |
9063 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009064
Christian Brauner0d56a452021-01-21 14:19:30 +01009065 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009066 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009067
9068 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009069 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009070 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009071 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009072 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009073 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009074 return 0;
9075}
9076
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009077static int btrfs_rename_exchange(struct inode *old_dir,
9078 struct dentry *old_dentry,
9079 struct inode *new_dir,
9080 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009082 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009083 struct btrfs_trans_handle *trans;
9084 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009085 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009086 struct inode *new_inode = new_dentry->d_inode;
9087 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009088 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009089 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9090 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009091 u64 old_idx = 0;
9092 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009093 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009094 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009095 bool root_log_pinned = false;
9096 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009097 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009098
9099 /* we only allow rename subvolume link between subvolumes */
9100 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9101 return -EXDEV;
9102
9103 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009104 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9105 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009106 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009107
9108 /*
9109 * We want to reserve the absolute worst case amount of items. So if
9110 * both inodes are subvols and we need to unlink them then that would
9111 * require 4 item modifications, but if they are both normal inodes it
9112 * would require 5 item modifications, so we'll assume their normal
9113 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9114 * should cover the worst case number of items we'll modify.
9115 */
9116 trans = btrfs_start_transaction(root, 12);
9117 if (IS_ERR(trans)) {
9118 ret = PTR_ERR(trans);
9119 goto out_notrans;
9120 }
9121
Josef Bacik00aa8e82021-03-12 15:25:02 -05009122 if (dest != root) {
9123 ret = btrfs_record_root_in_trans(trans, dest);
9124 if (ret)
9125 goto out_fail;
9126 }
Josef Bacik3e174092019-11-15 15:43:06 -05009127
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009128 /*
9129 * We need to find a free sequence number both in the source and
9130 * in the destination directory for the exchange.
9131 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009132 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009133 if (ret)
9134 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009135 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009136 if (ret)
9137 goto out_fail;
9138
9139 BTRFS_I(old_inode)->dir_index = 0ULL;
9140 BTRFS_I(new_inode)->dir_index = 0ULL;
9141
9142 /* Reference for the source. */
9143 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9144 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009145 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009146 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009147 btrfs_pin_log_trans(root);
9148 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009149 ret = btrfs_insert_inode_ref(trans, dest,
9150 new_dentry->d_name.name,
9151 new_dentry->d_name.len,
9152 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009153 btrfs_ino(BTRFS_I(new_dir)),
9154 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009155 if (ret)
9156 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009157 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009158 }
9159
9160 /* And now for the dest. */
9161 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9162 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009163 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009164 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009165 btrfs_pin_log_trans(dest);
9166 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009167 ret = btrfs_insert_inode_ref(trans, root,
9168 old_dentry->d_name.name,
9169 old_dentry->d_name.len,
9170 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009171 btrfs_ino(BTRFS_I(old_dir)),
9172 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009173 if (ret) {
9174 if (need_abort)
9175 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009176 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009177 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009178 }
9179
9180 /* Update inode version and ctime/mtime. */
9181 inode_inc_iversion(old_dir);
9182 inode_inc_iversion(new_dir);
9183 inode_inc_iversion(old_inode);
9184 inode_inc_iversion(new_inode);
9185 old_dir->i_ctime = old_dir->i_mtime = ctime;
9186 new_dir->i_ctime = new_dir->i_mtime = ctime;
9187 old_inode->i_ctime = ctime;
9188 new_inode->i_ctime = ctime;
9189
9190 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009191 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9192 BTRFS_I(old_inode), 1);
9193 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9194 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009195 }
9196
9197 /* src is a subvolume */
9198 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009199 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009200 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009201 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9202 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009203 old_dentry->d_name.name,
9204 old_dentry->d_name.len);
9205 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009206 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009207 }
9208 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009209 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009210 goto out_fail;
9211 }
9212
9213 /* dest is a subvolume */
9214 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009215 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009216 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009217 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9218 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009219 new_dentry->d_name.name,
9220 new_dentry->d_name.len);
9221 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009222 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009223 }
9224 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009225 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009226 goto out_fail;
9227 }
9228
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009229 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009230 new_dentry->d_name.name,
9231 new_dentry->d_name.len, 0, old_idx);
9232 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009233 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009234 goto out_fail;
9235 }
9236
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009237 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009238 old_dentry->d_name.name,
9239 old_dentry->d_name.len, 0, new_idx);
9240 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009241 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009242 goto out_fail;
9243 }
9244
9245 if (old_inode->i_nlink == 1)
9246 BTRFS_I(old_inode)->dir_index = old_idx;
9247 if (new_inode->i_nlink == 1)
9248 BTRFS_I(new_inode)->dir_index = new_idx;
9249
Filipe Manana86e8aa02016-05-05 02:02:27 +01009250 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009251 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9252 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009253 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009254 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009255 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009256 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009257 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9258 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009259 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009260 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009261 }
9262out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009263 /*
9264 * If we have pinned a log and an error happened, we unpin tasks
9265 * trying to sync the log and force them to fallback to a transaction
9266 * commit if the log currently contains any of the inodes involved in
9267 * this rename operation (to ensure we do not persist a log with an
9268 * inconsistent state for any of these inodes or leading to any
9269 * inconsistencies when replayed). If the transaction was aborted, the
9270 * abortion reason is propagated to userspace when attempting to commit
9271 * the transaction. If the log does not contain any of these inodes, we
9272 * allow the tasks to sync it.
9273 */
9274 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009275 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9276 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9277 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009278 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009279 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009280 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009281
9282 if (root_log_pinned) {
9283 btrfs_end_log_trans(root);
9284 root_log_pinned = false;
9285 }
9286 if (dest_log_pinned) {
9287 btrfs_end_log_trans(dest);
9288 dest_log_pinned = false;
9289 }
9290 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009291 ret2 = btrfs_end_transaction(trans);
9292 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009293out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009294 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9295 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009296 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009297
9298 return ret;
9299}
9300
9301static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9302 struct btrfs_root *root,
9303 struct inode *dir,
9304 struct dentry *dentry)
9305{
9306 int ret;
9307 struct inode *inode;
9308 u64 objectid;
9309 u64 index;
9310
Nikolay Borisov543068a2020-12-07 17:32:33 +02009311 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009312 if (ret)
9313 return ret;
9314
9315 inode = btrfs_new_inode(trans, root, dir,
9316 dentry->d_name.name,
9317 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009318 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009319 objectid,
9320 S_IFCHR | WHITEOUT_MODE,
9321 &index);
9322
9323 if (IS_ERR(inode)) {
9324 ret = PTR_ERR(inode);
9325 return ret;
9326 }
9327
9328 inode->i_op = &btrfs_special_inode_operations;
9329 init_special_inode(inode, inode->i_mode,
9330 WHITEOUT_DEV);
9331
9332 ret = btrfs_init_inode_security(trans, inode, dir,
9333 &dentry->d_name);
9334 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009335 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009336
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009337 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9338 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009339 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009340 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009342 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009343out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009344 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009345 if (ret)
9346 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009347 iput(inode);
9348
Filipe Mananac9901612016-05-05 01:41:57 +01009349 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009350}
9351
Chris Mason39279cc2007-06-12 06:35:45 -04009352static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009353 struct inode *new_dir, struct dentry *new_dentry,
9354 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009355{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009356 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009357 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009358 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009359 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9360 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009361 struct inode *new_inode = d_inode(new_dentry);
9362 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009363 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009364 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009365 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009366 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009367 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009368
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009369 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009370 return -EPERM;
9371
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009372 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009373 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009374 return -EXDEV;
9375
Li Zefan33345d012011-04-20 10:31:50 +08009376 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009377 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009378 return -ENOTEMPTY;
9379
Chris Mason39279cc2007-06-12 06:35:45 -04009380 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009381 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009382 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009383
9384
9385 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009386 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009387 new_dentry->d_name.name,
9388 new_dentry->d_name.len);
9389
9390 if (ret) {
9391 if (ret == -EEXIST) {
9392 /* we shouldn't get
9393 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309394 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009395 return ret;
9396 }
9397 } else {
9398 /* maybe -EOVERFLOW */
9399 return ret;
9400 }
9401 }
9402 ret = 0;
9403
Chris Mason5a3f23d2009-03-31 13:27:11 -04009404 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009405 * we're using rename to replace one file with another. Start IO on it
9406 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009407 */
Chris Mason8d875f92014-08-12 10:47:42 -07009408 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009409 filemap_flush(old_inode->i_mapping);
9410
Yan, Zheng76dda932009-09-21 16:00:26 -04009411 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009412 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009413 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009414 /*
9415 * We want to reserve the absolute worst case amount of items. So if
9416 * both inodes are subvols and we need to unlink them then that would
9417 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009418 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009419 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9420 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009421 * If our rename has the whiteout flag, we need more 5 units for the
9422 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9423 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009424 */
Filipe Manana5062af32016-05-05 10:26:26 +01009425 trans_num_items = 11;
9426 if (flags & RENAME_WHITEOUT)
9427 trans_num_items += 5;
9428 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009429 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009430 ret = PTR_ERR(trans);
9431 goto out_notrans;
9432 }
Chris Mason5f39d392007-10-15 16:14:19 -04009433
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009434 if (dest != root) {
9435 ret = btrfs_record_root_in_trans(trans, dest);
9436 if (ret)
9437 goto out_fail;
9438 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009439
Nikolay Borisov877574e2017-02-20 13:50:33 +02009440 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009441 if (ret)
9442 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009443
Miao Xie67de1172013-12-26 13:07:06 +08009444 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009445 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009446 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009447 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009448 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009449 btrfs_pin_log_trans(root);
9450 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009451 ret = btrfs_insert_inode_ref(trans, dest,
9452 new_dentry->d_name.name,
9453 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009454 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009455 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009456 if (ret)
9457 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009458 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009459
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009460 inode_inc_iversion(old_dir);
9461 inode_inc_iversion(new_dir);
9462 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009463 old_dir->i_ctime = old_dir->i_mtime =
9464 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009465 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009466
Chris Mason12fcfd22009-03-24 10:24:20 -04009467 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009468 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9469 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009470
Li Zefan33345d012011-04-20 10:31:50 +08009471 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009472 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009473 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009474 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9475 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009476 old_dentry->d_name.name,
9477 old_dentry->d_name.len);
9478 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009479 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009480 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009481 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009482 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009483 goto out_fail;
9484 }
Chris Mason39279cc2007-06-12 06:35:45 -04009485
9486 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009487 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009488 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009489 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009490 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009491 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009492 BUG_ON(new_inode->i_nlink == 0);
9493 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009494 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9495 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009496 new_dentry->d_name.name,
9497 new_dentry->d_name.len);
9498 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009499 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009500 ret = btrfs_orphan_add(trans,
9501 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009502 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009503 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009504 goto out_fail;
9505 }
Chris Mason39279cc2007-06-12 06:35:45 -04009506 }
Josef Bacikaec74772008-07-24 12:12:38 -04009507
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009508 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009509 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009510 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009511 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009512 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009513 goto out_fail;
9514 }
Chris Mason39279cc2007-06-12 06:35:45 -04009515
Miao Xie67de1172013-12-26 13:07:06 +08009516 if (old_inode->i_nlink == 1)
9517 BTRFS_I(old_inode)->dir_index = index;
9518
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009519 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009520 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9521 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009522 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009523 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009524 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525
9526 if (flags & RENAME_WHITEOUT) {
9527 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9528 old_dentry);
9529
9530 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009531 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 goto out_fail;
9533 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009534 }
Chris Mason39279cc2007-06-12 06:35:45 -04009535out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009536 /*
9537 * If we have pinned the log and an error happened, we unpin tasks
9538 * trying to sync the log and force them to fallback to a transaction
9539 * commit if the log currently contains any of the inodes involved in
9540 * this rename operation (to ensure we do not persist a log with an
9541 * inconsistent state for any of these inodes or leading to any
9542 * inconsistencies when replayed). If the transaction was aborted, the
9543 * abortion reason is propagated to userspace when attempting to commit
9544 * the transaction. If the log does not contain any of these inodes, we
9545 * allow the tasks to sync it.
9546 */
9547 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009548 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9549 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9550 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009551 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009552 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009553 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009554
9555 btrfs_end_log_trans(root);
9556 log_pinned = false;
9557 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009558 ret2 = btrfs_end_transaction(trans);
9559 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009560out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009561 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009562 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009563
Chris Mason39279cc2007-06-12 06:35:45 -04009564 return ret;
9565}
9566
Christian Brauner549c7292021-01-21 14:19:43 +01009567static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9568 struct dentry *old_dentry, struct inode *new_dir,
9569 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009570{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009571 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009572 return -EINVAL;
9573
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 if (flags & RENAME_EXCHANGE)
9575 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9576 new_dentry);
9577
9578 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009579}
9580
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009581struct btrfs_delalloc_work {
9582 struct inode *inode;
9583 struct completion completion;
9584 struct list_head list;
9585 struct btrfs_work work;
9586};
9587
Miao Xie8ccf6f192012-10-25 09:28:04 +00009588static void btrfs_run_delalloc_work(struct btrfs_work *work)
9589{
9590 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009591 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009592
9593 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9594 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009595 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009596 filemap_flush(inode->i_mapping);
9597 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9598 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009599 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009600
Nikolay Borisov076da912018-04-23 10:54:16 +03009601 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009602 complete(&delalloc_work->completion);
9603}
9604
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009605static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009606{
9607 struct btrfs_delalloc_work *work;
9608
David Sterba100d5702015-12-08 14:39:32 +01009609 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009610 if (!work)
9611 return NULL;
9612
9613 init_completion(&work->completion);
9614 INIT_LIST_HEAD(&work->list);
9615 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009616 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009617
9618 return work;
9619}
9620
Chris Masond352ac62008-09-29 15:18:18 -04009621/*
9622 * some fairly slow code that needs optimization. This walks the list
9623 * of all the inodes with pending delalloc and forces them to disk.
9624 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009625static int start_delalloc_inodes(struct btrfs_root *root,
9626 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009627 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009628{
Chris Masonea8c2812008-08-04 23:17:27 -04009629 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009630 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009631 struct btrfs_delalloc_work *work, *next;
9632 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009633 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009634 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009635 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009636
Miao Xie8ccf6f192012-10-25 09:28:04 +00009637 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009638 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009639
Miao Xie573bfb72014-03-06 13:55:03 +08009640 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009641 spin_lock(&root->delalloc_lock);
9642 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009643 while (!list_empty(&splice)) {
9644 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009645 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009646
Miao Xieeb73c1b2013-05-15 07:48:22 +00009647 list_move_tail(&binode->delalloc_inodes,
9648 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009649
9650 if (in_reclaim_context &&
9651 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9652 continue;
9653
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009654 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009655 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009656 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009657 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009658 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009659 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009660
Ethan Lien3cd24c62018-11-01 14:49:03 +08009661 if (snapshot)
9662 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9663 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009664 if (full_flush) {
9665 work = btrfs_alloc_delalloc_work(inode);
9666 if (!work) {
9667 iput(inode);
9668 ret = -ENOMEM;
9669 goto out;
9670 }
9671 list_add_tail(&work->list, &works);
9672 btrfs_queue_work(root->fs_info->flush_workers,
9673 &work->work);
9674 } else {
9675 ret = sync_inode(inode, wbc);
9676 if (!ret &&
9677 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9678 &BTRFS_I(inode)->runtime_flags))
9679 ret = sync_inode(inode, wbc);
9680 btrfs_add_delayed_iput(inode);
9681 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009682 goto out;
9683 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009684 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009685 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009686 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009687 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009688
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009689out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009690 list_for_each_entry_safe(work, next, &works, list) {
9691 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009692 wait_for_completion(&work->completion);
9693 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009694 }
9695
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009696 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009697 spin_lock(&root->delalloc_lock);
9698 list_splice_tail(&splice, &root->delalloc_inodes);
9699 spin_unlock(&root->delalloc_lock);
9700 }
Miao Xie573bfb72014-03-06 13:55:03 +08009701 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009702 return ret;
9703}
9704
Filipe Mananaf9baa502021-04-22 12:08:05 +01009705int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009706{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009707 struct writeback_control wbc = {
9708 .nr_to_write = LONG_MAX,
9709 .sync_mode = WB_SYNC_NONE,
9710 .range_start = 0,
9711 .range_end = LLONG_MAX,
9712 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009714
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009716 return -EROFS;
9717
Filipe Mananaf9baa502021-04-22 12:08:05 +01009718 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009719}
9720
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009721int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009722 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009723{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009724 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009725 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009726 .sync_mode = WB_SYNC_NONE,
9727 .range_start = 0,
9728 .range_end = LLONG_MAX,
9729 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009730 struct btrfs_root *root;
9731 struct list_head splice;
9732 int ret;
9733
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009734 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009735 return -EROFS;
9736
9737 INIT_LIST_HEAD(&splice);
9738
Miao Xie573bfb72014-03-06 13:55:03 +08009739 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009740 spin_lock(&fs_info->delalloc_root_lock);
9741 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009742 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009743 /*
9744 * Reset nr_to_write here so we know that we're doing a full
9745 * flush.
9746 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009747 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009748 wbc.nr_to_write = LONG_MAX;
9749
Miao Xieeb73c1b2013-05-15 07:48:22 +00009750 root = list_first_entry(&splice, struct btrfs_root,
9751 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009752 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009753 BUG_ON(!root);
9754 list_move_tail(&root->delalloc_root,
9755 &fs_info->delalloc_roots);
9756 spin_unlock(&fs_info->delalloc_root_lock);
9757
Josef Bacike076ab2a2021-01-07 17:08:30 -05009758 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009759 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009760 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009761 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009762 spin_lock(&fs_info->delalloc_root_lock);
9763 }
9764 spin_unlock(&fs_info->delalloc_root_lock);
9765
Miao Xie6c255e62014-03-06 13:55:01 +08009766 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009767out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009768 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009769 spin_lock(&fs_info->delalloc_root_lock);
9770 list_splice_tail(&splice, &fs_info->delalloc_roots);
9771 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009772 }
Miao Xie573bfb72014-03-06 13:55:03 +08009773 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009774 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009775}
9776
Christian Brauner549c7292021-01-21 14:19:43 +01009777static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9778 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009780 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009781 struct btrfs_trans_handle *trans;
9782 struct btrfs_root *root = BTRFS_I(dir)->root;
9783 struct btrfs_path *path;
9784 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009785 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009786 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009787 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309788 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009789 int name_len;
9790 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009791 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009792 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009793 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009794
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009795 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009796 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009797 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009798
Josef Bacik9ed74f22009-09-11 16:12:44 -04009799 /*
9800 * 2 items for inode item and ref
9801 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009802 * 1 item for updating parent inode item
9803 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009804 * 1 item for xattr if selinux is on
9805 */
Filipe Manana9269d122015-12-31 18:16:29 +00009806 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009807 if (IS_ERR(trans))
9808 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009809
Nikolay Borisov543068a2020-12-07 17:32:33 +02009810 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009811 if (err)
9812 goto out_unlock;
9813
Josef Bacikaec74772008-07-24 12:12:38 -04009814 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009815 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9816 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009817 if (IS_ERR(inode)) {
9818 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009819 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009820 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009821 }
Chris Mason39279cc2007-06-12 06:35:45 -04009822
Casey Schauflerad19db72011-12-15 10:09:07 -05009823 /*
9824 * If the active LSM wants to access the inode during
9825 * d_instantiate it needs these. Smack checks to see
9826 * if the filesystem supports xattrs by looking at the
9827 * ops vector.
9828 */
9829 inode->i_fop = &btrfs_file_operations;
9830 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009831 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009832
9833 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9834 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009835 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009836
Chris Mason39279cc2007-06-12 06:35:45 -04009837 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009838 if (!path) {
9839 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009840 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009841 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009842 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009843 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009844 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009845 datasize = btrfs_file_extent_calc_inline_size(name_len);
9846 err = btrfs_insert_empty_item(trans, root, path, &key,
9847 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009848 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009849 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009850 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009851 }
Chris Mason5f39d392007-10-15 16:14:19 -04009852 leaf = path->nodes[0];
9853 ei = btrfs_item_ptr(leaf, path->slots[0],
9854 struct btrfs_file_extent_item);
9855 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9856 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009857 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009858 btrfs_set_file_extent_encryption(leaf, ei, 0);
9859 btrfs_set_file_extent_compression(leaf, ei, 0);
9860 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9861 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9862
Chris Mason39279cc2007-06-12 06:35:45 -04009863 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009864 write_extent_buffer(leaf, symname, ptr, name_len);
9865 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009866 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009867
Chris Mason39279cc2007-06-12 06:35:45 -04009868 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009869 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009870 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009871 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009872 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009873 /*
9874 * Last step, add directory indexes for our symlink inode. This is the
9875 * last step to avoid extra cleanup of these indexes if an error happens
9876 * elsewhere above.
9877 */
9878 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009879 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9880 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009881 if (err)
9882 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009883
Al Viro1e2e5472018-05-04 08:23:01 -04009884 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009885
9886out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009887 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009888 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009889 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009890 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009891 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009892 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009893 return err;
9894}
Chris Mason16432982008-04-10 10:23:21 -04009895
Filipe Manana8fccebf2020-09-08 11:27:20 +01009896static struct btrfs_trans_handle *insert_prealloc_file_extent(
9897 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009898 struct btrfs_inode *inode,
9899 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009900 u64 file_offset)
9901{
9902 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009903 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009904 struct btrfs_trans_handle *trans = trans_in;
9905 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009906 u64 start = ins->objectid;
9907 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009908 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009909 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009910
9911 memset(&stack_fi, 0, sizeof(stack_fi));
9912
9913 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9914 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9915 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9916 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9917 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9918 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9919 /* Encryption and other encoding is reserved and all 0 */
9920
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009921 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9922 if (qgroup_released < 0)
9923 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009924
9925 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009926 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009927 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009928 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009929 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009930 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009931 return trans;
9932 }
9933
9934 extent_info.disk_offset = start;
9935 extent_info.disk_len = len;
9936 extent_info.data_offset = 0;
9937 extent_info.data_len = len;
9938 extent_info.file_offset = file_offset;
9939 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009940 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009941 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009942 extent_info.insertions = 0;
9943
9944 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009945 if (!path) {
9946 ret = -ENOMEM;
9947 goto free_qgroup;
9948 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009949
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009950 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009951 file_offset + len - 1, &extent_info,
9952 &trans);
9953 btrfs_free_path(path);
9954 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009955 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009956 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009957
9958free_qgroup:
9959 /*
9960 * We have released qgroup data range at the beginning of the function,
9961 * and normally qgroup_released bytes will be freed when committing
9962 * transaction.
9963 * But if we error out early, we have to free what we have released
9964 * or we leak qgroup data reservation.
9965 */
9966 btrfs_qgroup_free_refroot(inode->root->fs_info,
9967 inode->root->root_key.objectid, qgroup_released,
9968 BTRFS_QGROUP_RSV_DATA);
9969 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009970}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009971
Josef Bacik0af3d002010-06-21 14:48:16 -04009972static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9973 u64 start, u64 num_bytes, u64 min_size,
9974 loff_t actual_len, u64 *alloc_hint,
9975 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009978 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9979 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009980 struct btrfs_root *root = BTRFS_I(inode)->root;
9981 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009982 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009983 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009984 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009985 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009986 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009987 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009988 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009989 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009990
Josef Bacik0af3d002010-06-21 14:48:16 -04009991 if (trans)
9992 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009993 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009994 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009995 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009996 /*
9997 * If we are severely fragmented we could end up with really
9998 * small allocations, so if the allocator is returning small
9999 * chunks lets make its job easier by only searching for those
10000 * sized chunks.
10001 */
10002 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010003 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10004 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010005 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010006 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010007
10008 /*
10009 * We've reserved this space, and thus converted it from
10010 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10011 * from here on out we will only need to clear our reservation
10012 * for the remaining unreserved area, so advance our
10013 * clear_offset by our extent size.
10014 */
10015 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010016
Josef Bacik0b670dc2015-09-23 17:11:16 -040010017 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010018 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10019 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010020 /*
10021 * Now that we inserted the prealloc extent we can finally
10022 * decrement the number of reservations in the block group.
10023 * If we did it before, we could race with relocation and have
10024 * relocation miss the reserved extent, making it fail later.
10025 */
10026 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010027 if (IS_ERR(trans)) {
10028 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010029 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010030 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010031 break;
10032 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010033
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010034 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010035 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010036
Josef Bacik5dc562c2012-08-17 13:14:17 -040010037 em = alloc_extent_map();
10038 if (!em) {
10039 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10040 &BTRFS_I(inode)->runtime_flags);
10041 goto next;
10042 }
10043
10044 em->start = cur_offset;
10045 em->orig_start = cur_offset;
10046 em->len = ins.offset;
10047 em->block_start = ins.objectid;
10048 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010049 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010050 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010051 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10052 em->generation = trans->transid;
10053
10054 while (1) {
10055 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010056 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010057 write_unlock(&em_tree->lock);
10058 if (ret != -EEXIST)
10059 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010060 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010061 cur_offset + ins.offset - 1,
10062 0);
10063 }
10064 free_extent_map(em);
10065next:
Yan Zhengd899e052008-10-30 14:25:28 -040010066 num_bytes -= ins.offset;
10067 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010068 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010069
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010070 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010071 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010072 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010073 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010074 (actual_len > inode->i_size) &&
10075 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010076 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010077 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010078 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010079 i_size = cur_offset;
10080 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010081 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010082 }
10083
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010084 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010085
10086 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010087 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010088 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010089 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010090 break;
10091 }
Yan Zhengd899e052008-10-30 14:25:28 -040010092
Filipe Manana8fccebf2020-09-08 11:27:20 +010010093 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010094 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010095 trans = NULL;
10096 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010097 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010098 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010099 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010100 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010101 return ret;
10102}
10103
Josef Bacik0af3d002010-06-21 14:48:16 -040010104int btrfs_prealloc_file_range(struct inode *inode, int mode,
10105 u64 start, u64 num_bytes, u64 min_size,
10106 loff_t actual_len, u64 *alloc_hint)
10107{
10108 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10109 min_size, actual_len, alloc_hint,
10110 NULL);
10111}
10112
10113int btrfs_prealloc_file_range_trans(struct inode *inode,
10114 struct btrfs_trans_handle *trans, int mode,
10115 u64 start, u64 num_bytes, u64 min_size,
10116 loff_t actual_len, u64 *alloc_hint)
10117{
10118 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10119 min_size, actual_len, alloc_hint, trans);
10120}
10121
Chris Masone6dcd2d2008-07-17 12:53:50 -040010122static int btrfs_set_page_dirty(struct page *page)
10123{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010124 return __set_page_dirty_nobuffers(page);
10125}
10126
Christian Brauner549c7292021-01-21 14:19:43 +010010127static int btrfs_permission(struct user_namespace *mnt_userns,
10128 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010129{
Li Zefanb83cc962010-12-20 16:04:08 +080010130 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010131 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010132
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010133 if (mask & MAY_WRITE &&
10134 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10135 if (btrfs_root_readonly(root))
10136 return -EROFS;
10137 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10138 return -EACCES;
10139 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010140 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010141}
Chris Mason39279cc2007-06-12 06:35:45 -040010142
Christian Brauner549c7292021-01-21 14:19:43 +010010143static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10144 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010145{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010146 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010147 struct btrfs_trans_handle *trans;
10148 struct btrfs_root *root = BTRFS_I(dir)->root;
10149 struct inode *inode = NULL;
10150 u64 objectid;
10151 u64 index;
10152 int ret = 0;
10153
10154 /*
10155 * 5 units required for adding orphan entry
10156 */
10157 trans = btrfs_start_transaction(root, 5);
10158 if (IS_ERR(trans))
10159 return PTR_ERR(trans);
10160
Nikolay Borisov543068a2020-12-07 17:32:33 +020010161 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010162 if (ret)
10163 goto out;
10164
10165 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010166 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010167 if (IS_ERR(inode)) {
10168 ret = PTR_ERR(inode);
10169 inode = NULL;
10170 goto out;
10171 }
10172
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010173 inode->i_fop = &btrfs_file_operations;
10174 inode->i_op = &btrfs_file_inode_operations;
10175
10176 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010177
Chris Masonb0d5d102014-09-08 13:08:51 -070010178 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10179 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010180 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010181
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010182 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010183 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010184 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010185 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010186 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010187 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010188
Filipe Manana5762b5c2014-08-01 00:10:32 +010010189 /*
10190 * We set number of links to 0 in btrfs_new_inode(), and here we set
10191 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10192 * through:
10193 *
10194 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10195 */
10196 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010197 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010198 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010199 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010200out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010201 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010202 if (ret && inode)
10203 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010204 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010205 return ret;
10206}
10207
David Sterba5cdc84b2018-07-18 20:32:52 +020010208void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010209{
David Sterba5cdc84b2018-07-18 20:32:52 +020010210 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010211 unsigned long index = start >> PAGE_SHIFT;
10212 unsigned long end_index = end >> PAGE_SHIFT;
10213 struct page *page;
10214
10215 while (index <= end_index) {
10216 page = find_get_page(inode->i_mapping, index);
10217 ASSERT(page); /* Pages should be in the extent_io_tree */
10218 set_page_writeback(page);
10219 put_page(page);
10220 index++;
10221 }
10222}
10223
Omar Sandovaled46ff32016-11-03 10:28:14 -070010224#ifdef CONFIG_SWAP
10225/*
10226 * Add an entry indicating a block group or device which is pinned by a
10227 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10228 * negative errno on failure.
10229 */
10230static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10231 bool is_block_group)
10232{
10233 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10234 struct btrfs_swapfile_pin *sp, *entry;
10235 struct rb_node **p;
10236 struct rb_node *parent = NULL;
10237
10238 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10239 if (!sp)
10240 return -ENOMEM;
10241 sp->ptr = ptr;
10242 sp->inode = inode;
10243 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010244 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010245
10246 spin_lock(&fs_info->swapfile_pins_lock);
10247 p = &fs_info->swapfile_pins.rb_node;
10248 while (*p) {
10249 parent = *p;
10250 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10251 if (sp->ptr < entry->ptr ||
10252 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10253 p = &(*p)->rb_left;
10254 } else if (sp->ptr > entry->ptr ||
10255 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10256 p = &(*p)->rb_right;
10257 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010258 if (is_block_group)
10259 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010260 spin_unlock(&fs_info->swapfile_pins_lock);
10261 kfree(sp);
10262 return 1;
10263 }
10264 }
10265 rb_link_node(&sp->node, parent, p);
10266 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10267 spin_unlock(&fs_info->swapfile_pins_lock);
10268 return 0;
10269}
10270
10271/* Free all of the entries pinned by this swapfile. */
10272static void btrfs_free_swapfile_pins(struct inode *inode)
10273{
10274 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10275 struct btrfs_swapfile_pin *sp;
10276 struct rb_node *node, *next;
10277
10278 spin_lock(&fs_info->swapfile_pins_lock);
10279 node = rb_first(&fs_info->swapfile_pins);
10280 while (node) {
10281 next = rb_next(node);
10282 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10283 if (sp->inode == inode) {
10284 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010285 if (sp->is_block_group) {
10286 btrfs_dec_block_group_swap_extents(sp->ptr,
10287 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010288 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010289 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010290 kfree(sp);
10291 }
10292 node = next;
10293 }
10294 spin_unlock(&fs_info->swapfile_pins_lock);
10295}
10296
10297struct btrfs_swap_info {
10298 u64 start;
10299 u64 block_start;
10300 u64 block_len;
10301 u64 lowest_ppage;
10302 u64 highest_ppage;
10303 unsigned long nr_pages;
10304 int nr_extents;
10305};
10306
10307static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10308 struct btrfs_swap_info *bsi)
10309{
10310 unsigned long nr_pages;
10311 u64 first_ppage, first_ppage_reported, next_ppage;
10312 int ret;
10313
10314 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10315 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10316 PAGE_SIZE) >> PAGE_SHIFT;
10317
10318 if (first_ppage >= next_ppage)
10319 return 0;
10320 nr_pages = next_ppage - first_ppage;
10321
10322 first_ppage_reported = first_ppage;
10323 if (bsi->start == 0)
10324 first_ppage_reported++;
10325 if (bsi->lowest_ppage > first_ppage_reported)
10326 bsi->lowest_ppage = first_ppage_reported;
10327 if (bsi->highest_ppage < (next_ppage - 1))
10328 bsi->highest_ppage = next_ppage - 1;
10329
10330 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10331 if (ret < 0)
10332 return ret;
10333 bsi->nr_extents += ret;
10334 bsi->nr_pages += nr_pages;
10335 return 0;
10336}
10337
10338static void btrfs_swap_deactivate(struct file *file)
10339{
10340 struct inode *inode = file_inode(file);
10341
10342 btrfs_free_swapfile_pins(inode);
10343 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10344}
10345
10346static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10347 sector_t *span)
10348{
10349 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010350 struct btrfs_root *root = BTRFS_I(inode)->root;
10351 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10353 struct extent_state *cached_state = NULL;
10354 struct extent_map *em = NULL;
10355 struct btrfs_device *device = NULL;
10356 struct btrfs_swap_info bsi = {
10357 .lowest_ppage = (sector_t)-1ULL,
10358 };
10359 int ret = 0;
10360 u64 isize;
10361 u64 start;
10362
10363 /*
10364 * If the swap file was just created, make sure delalloc is done. If the
10365 * file changes again after this, the user is doing something stupid and
10366 * we don't really care.
10367 */
10368 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10369 if (ret)
10370 return ret;
10371
10372 /*
10373 * The inode is locked, so these flags won't change after we check them.
10374 */
10375 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10376 btrfs_warn(fs_info, "swapfile must not be compressed");
10377 return -EINVAL;
10378 }
10379 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10380 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10381 return -EINVAL;
10382 }
10383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10384 btrfs_warn(fs_info, "swapfile must not be checksummed");
10385 return -EINVAL;
10386 }
10387
10388 /*
10389 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010390 * under us. The exclop protection makes sure they aren't running/won't
10391 * run concurrently while we are mapping the swap extents, and
10392 * fs_info->swapfile_pins prevents them from running while the swap
10393 * file is active and moving the extents. Note that this also prevents
10394 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010395 * really worth the trouble to allow it.
10396 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010397 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010398 btrfs_warn(fs_info,
10399 "cannot activate swapfile while exclusive operation is running");
10400 return -EBUSY;
10401 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010402
10403 /*
10404 * Prevent snapshot creation while we are activating the swap file.
10405 * We do not want to race with snapshot creation. If snapshot creation
10406 * already started before we bumped nr_swapfiles from 0 to 1 and
10407 * completes before the first write into the swap file after it is
10408 * activated, than that write would fallback to COW.
10409 */
10410 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10411 btrfs_exclop_finish(fs_info);
10412 btrfs_warn(fs_info,
10413 "cannot activate swapfile because snapshot creation is in progress");
10414 return -EINVAL;
10415 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010416 /*
10417 * Snapshots can create extents which require COW even if NODATACOW is
10418 * set. We use this counter to prevent snapshots. We must increment it
10419 * before walking the extents because we don't want a concurrent
10420 * snapshot to run after we've already checked the extents.
10421 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010422 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010423
10424 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10425
10426 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10427 start = 0;
10428 while (start < isize) {
10429 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010430 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010431 u64 len = isize - start;
10432
Omar Sandoval39b07b52019-12-02 17:34:23 -080010433 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010434 if (IS_ERR(em)) {
10435 ret = PTR_ERR(em);
10436 goto out;
10437 }
10438
10439 if (em->block_start == EXTENT_MAP_HOLE) {
10440 btrfs_warn(fs_info, "swapfile must not have holes");
10441 ret = -EINVAL;
10442 goto out;
10443 }
10444 if (em->block_start == EXTENT_MAP_INLINE) {
10445 /*
10446 * It's unlikely we'll ever actually find ourselves
10447 * here, as a file small enough to fit inline won't be
10448 * big enough to store more than the swap header, but in
10449 * case something changes in the future, let's catch it
10450 * here rather than later.
10451 */
10452 btrfs_warn(fs_info, "swapfile must not be inline");
10453 ret = -EINVAL;
10454 goto out;
10455 }
10456 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10457 btrfs_warn(fs_info, "swapfile must not be compressed");
10458 ret = -EINVAL;
10459 goto out;
10460 }
10461
10462 logical_block_start = em->block_start + (start - em->start);
10463 len = min(len, em->len - (start - em->start));
10464 free_extent_map(em);
10465 em = NULL;
10466
Boris Burkova84d5d42020-08-18 11:00:05 -070010467 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010468 if (ret < 0) {
10469 goto out;
10470 } else if (ret) {
10471 ret = 0;
10472 } else {
10473 btrfs_warn(fs_info,
10474 "swapfile must not be copy-on-write");
10475 ret = -EINVAL;
10476 goto out;
10477 }
10478
10479 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10480 if (IS_ERR(em)) {
10481 ret = PTR_ERR(em);
10482 goto out;
10483 }
10484
10485 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10486 btrfs_warn(fs_info,
10487 "swapfile must have single data profile");
10488 ret = -EINVAL;
10489 goto out;
10490 }
10491
10492 if (device == NULL) {
10493 device = em->map_lookup->stripes[0].dev;
10494 ret = btrfs_add_swapfile_pin(inode, device, false);
10495 if (ret == 1)
10496 ret = 0;
10497 else if (ret)
10498 goto out;
10499 } else if (device != em->map_lookup->stripes[0].dev) {
10500 btrfs_warn(fs_info, "swapfile must be on one device");
10501 ret = -EINVAL;
10502 goto out;
10503 }
10504
10505 physical_block_start = (em->map_lookup->stripes[0].physical +
10506 (logical_block_start - em->start));
10507 len = min(len, em->len - (logical_block_start - em->start));
10508 free_extent_map(em);
10509 em = NULL;
10510
10511 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10512 if (!bg) {
10513 btrfs_warn(fs_info,
10514 "could not find block group containing swapfile");
10515 ret = -EINVAL;
10516 goto out;
10517 }
10518
Filipe Manana195a49e2021-02-05 12:55:37 +000010519 if (!btrfs_inc_block_group_swap_extents(bg)) {
10520 btrfs_warn(fs_info,
10521 "block group for swapfile at %llu is read-only%s",
10522 bg->start,
10523 atomic_read(&fs_info->scrubs_running) ?
10524 " (scrub running)" : "");
10525 btrfs_put_block_group(bg);
10526 ret = -EINVAL;
10527 goto out;
10528 }
10529
Omar Sandovaled46ff32016-11-03 10:28:14 -070010530 ret = btrfs_add_swapfile_pin(inode, bg, true);
10531 if (ret) {
10532 btrfs_put_block_group(bg);
10533 if (ret == 1)
10534 ret = 0;
10535 else
10536 goto out;
10537 }
10538
10539 if (bsi.block_len &&
10540 bsi.block_start + bsi.block_len == physical_block_start) {
10541 bsi.block_len += len;
10542 } else {
10543 if (bsi.block_len) {
10544 ret = btrfs_add_swap_extent(sis, &bsi);
10545 if (ret)
10546 goto out;
10547 }
10548 bsi.start = start;
10549 bsi.block_start = physical_block_start;
10550 bsi.block_len = len;
10551 }
10552
10553 start += len;
10554 }
10555
10556 if (bsi.block_len)
10557 ret = btrfs_add_swap_extent(sis, &bsi);
10558
10559out:
10560 if (!IS_ERR_OR_NULL(em))
10561 free_extent_map(em);
10562
10563 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10564
10565 if (ret)
10566 btrfs_swap_deactivate(file);
10567
Filipe Mananadd0734f2021-02-05 12:55:38 +000010568 btrfs_drew_write_unlock(&root->snapshot_lock);
10569
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010570 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010571
10572 if (ret)
10573 return ret;
10574
10575 if (device)
10576 sis->bdev = device->bdev;
10577 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10578 sis->max = bsi.nr_pages;
10579 sis->pages = bsi.nr_pages - 1;
10580 sis->highest_bit = bsi.nr_pages - 1;
10581 return bsi.nr_extents;
10582}
10583#else
10584static void btrfs_swap_deactivate(struct file *file)
10585{
10586}
10587
10588static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10589 sector_t *span)
10590{
10591 return -EOPNOTSUPP;
10592}
10593#endif
10594
Filipe Manana2766ff62020-11-04 11:07:34 +000010595/*
10596 * Update the number of bytes used in the VFS' inode. When we replace extents in
10597 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10598 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10599 * always get a correct value.
10600 */
10601void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10602 const u64 add_bytes,
10603 const u64 del_bytes)
10604{
10605 if (add_bytes == del_bytes)
10606 return;
10607
10608 spin_lock(&inode->lock);
10609 if (del_bytes > 0)
10610 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10611 if (add_bytes > 0)
10612 inode_add_bytes(&inode->vfs_inode, add_bytes);
10613 spin_unlock(&inode->lock);
10614}
10615
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010616static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010617 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010618 .lookup = btrfs_lookup,
10619 .create = btrfs_create,
10620 .unlink = btrfs_unlink,
10621 .link = btrfs_link,
10622 .mkdir = btrfs_mkdir,
10623 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010624 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010625 .symlink = btrfs_symlink,
10626 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010627 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010628 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010629 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010630 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010631 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010632 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010634 .fileattr_get = btrfs_fileattr_get,
10635 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010636};
Yan, Zheng76dda932009-09-21 16:00:26 -040010637
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010638static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010639 .llseek = generic_file_llseek,
10640 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010641 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010642 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010643 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010644#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010645 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010646#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010647 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010648 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
10650
Chris Mason35054392009-01-21 13:11:13 -050010651/*
10652 * btrfs doesn't support the bmap operation because swapfiles
10653 * use bmap to make a mapping of extents in the file. They assume
10654 * these extents won't change over the life of the file and they
10655 * use the bmap result to do IO directly to the drive.
10656 *
10657 * the btrfs bmap call would return logical addresses that aren't
10658 * suitable for IO and they also will change frequently as COW
10659 * operations happen. So, swapfile + btrfs == corruption.
10660 *
10661 * For now we're avoiding this by dropping bmap.
10662 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010663static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010664 .readpage = btrfs_readpage,
10665 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010666 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010667 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010668 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010669 .invalidatepage = btrfs_invalidatepage,
10670 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010671#ifdef CONFIG_MIGRATION
10672 .migratepage = btrfs_migratepage,
10673#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010674 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010675 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010676 .swap_activate = btrfs_swap_activate,
10677 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010678};
10679
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010680static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010681 .getattr = btrfs_getattr,
10682 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010683 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010684 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010685 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010686 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010687 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010688 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010689 .fileattr_get = btrfs_fileattr_get,
10690 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010691};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010692static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010693 .getattr = btrfs_getattr,
10694 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010695 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010696 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010697 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010698 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010699 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010700};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010701static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010702 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010703 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010704 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010705 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010706 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010707 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010708};
Yan, Zheng76dda932009-09-21 16:00:26 -040010709
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010710const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010711 .d_delete = btrfs_dentry_delete,
10712};