blob: bd09ad1fed07b78532c2fbabaae2adf582448274 [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);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002210 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002211 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002212 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002213
2214 if (geom.len < length + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002215 ret = 1;
2216out:
2217 free_extent_map(em);
2218 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002219}
2220
Chris Masond352ac62008-09-29 15:18:18 -04002221/*
2222 * in order to insert checksums into the metadata in large chunks,
2223 * we wait until bio submission time. All the pages in the bio are
2224 * checksummed and sums are attached onto the ordered extent record.
2225 *
2226 * At IO completion time the cums attached on the ordered extent record
2227 * are inserted into the btree
2228 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002229static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002230 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002231{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002232 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002233}
Chris Masone0156402008-04-16 11:15:20 -04002234
Johannes Thumshirncacb2ce2021-02-04 19:22:01 +09002235bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio,
2236 unsigned int size)
2237{
2238 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2239 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2240 struct btrfs_ordered_extent *ordered;
2241 u64 len = bio->bi_iter.bi_size + size;
2242 bool ret = true;
2243
2244 ASSERT(btrfs_is_zoned(fs_info));
2245 ASSERT(fs_info->max_zone_append_size > 0);
2246 ASSERT(bio_op(bio) == REQ_OP_ZONE_APPEND);
2247
2248 /* Ordered extent not yet created, so we're good */
2249 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
2250 if (!ordered)
2251 return ret;
2252
2253 if ((bio->bi_iter.bi_sector << SECTOR_SHIFT) + len >
2254 ordered->disk_bytenr + ordered->disk_num_bytes)
2255 ret = false;
2256
2257 btrfs_put_ordered_extent(ordered);
2258
2259 return ret;
2260}
2261
Naohiro Aotad22002f2021-02-04 19:22:00 +09002262static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2263 struct bio *bio, loff_t file_offset)
2264{
2265 struct btrfs_ordered_extent *ordered;
2266 struct extent_map *em = NULL, *em_new = NULL;
2267 struct extent_map_tree *em_tree = &inode->extent_tree;
2268 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2269 u64 len = bio->bi_iter.bi_size;
2270 u64 end = start + len;
2271 u64 ordered_end;
2272 u64 pre, post;
2273 int ret = 0;
2274
2275 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2276 if (WARN_ON_ONCE(!ordered))
2277 return BLK_STS_IOERR;
2278
2279 /* No need to split */
2280 if (ordered->disk_num_bytes == len)
2281 goto out;
2282
2283 /* We cannot split once end_bio'd ordered extent */
2284 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2285 ret = -EINVAL;
2286 goto out;
2287 }
2288
2289 /* We cannot split a compressed ordered extent */
2290 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2291 ret = -EINVAL;
2292 goto out;
2293 }
2294
2295 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2296 /* bio must be in one ordered extent */
2297 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2298 ret = -EINVAL;
2299 goto out;
2300 }
2301
2302 /* Checksum list should be empty */
2303 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2304 ret = -EINVAL;
2305 goto out;
2306 }
2307
2308 pre = start - ordered->disk_bytenr;
2309 post = ordered_end - end;
2310
2311 ret = btrfs_split_ordered_extent(ordered, pre, post);
2312 if (ret)
2313 goto out;
2314
2315 read_lock(&em_tree->lock);
2316 em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
2317 if (!em) {
2318 read_unlock(&em_tree->lock);
2319 ret = -EIO;
2320 goto out;
2321 }
2322 read_unlock(&em_tree->lock);
2323
2324 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2325 /*
2326 * We cannot reuse em_new here but have to create a new one, as
2327 * unpin_extent_cache() expects the start of the extent map to be the
2328 * logical offset of the file, which does not hold true anymore after
2329 * splitting.
2330 */
2331 em_new = create_io_em(inode, em->start + pre, len,
2332 em->start + pre, em->block_start + pre, len,
2333 len, len, BTRFS_COMPRESS_NONE,
2334 BTRFS_ORDERED_REGULAR);
2335 if (IS_ERR(em_new)) {
2336 ret = PTR_ERR(em_new);
2337 goto out;
2338 }
2339 free_extent_map(em_new);
2340
2341out:
2342 free_extent_map(em);
2343 btrfs_put_ordered_extent(ordered);
2344
2345 return errno_to_blk_status(ret);
2346}
2347
Chris Mason4a69a412008-11-06 22:03:00 -05002348/*
Chris Masoncad321a2008-12-17 14:51:42 -05002349 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002350 * on write, or reading the csums from the tree before a read.
2351 *
2352 * Rules about async/sync submit,
2353 * a) read: sync submit
2354 *
2355 * b) write without checksum: sync submit
2356 *
2357 * c) write with checksum:
2358 * c-1) if bio is issued by fsync: sync submit
2359 * (sync_writers != 0)
2360 *
2361 * c-2) if root is reloc root: sync submit
2362 * (only in case of buffered IO)
2363 *
2364 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002365 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002366blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2367 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002368
Chris Mason44b8bd72008-04-16 11:14:51 -04002369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002371 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302372 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002373 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002374 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002375 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002376
Josef Bacik42437a62020-10-16 11:29:18 -04002377 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2378 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002379
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002380 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302381 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002382
Naohiro Aotad22002f2021-02-04 19:22:00 +09002383 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2384 struct page *page = bio_first_bvec_all(bio)->bv_page;
2385 loff_t file_offset = page_offset(page);
2386
2387 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2388 if (ret)
2389 goto out;
2390 }
2391
Naohiro Aotacfe94442021-02-04 19:21:59 +09002392 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002394 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002395 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002396
Chris Masond20f7042008-12-08 16:58:54 -05002397 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002398 ret = btrfs_submit_compressed_read(inode, bio,
2399 mirror_num,
2400 bio_flags);
2401 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002402 } else {
2403 /*
2404 * Lookup bio sums does extra checks around whether we
2405 * need to csum or not, which is why we ignore skip_sum
2406 * here.
2407 */
Qu Wenruo62751932020-12-02 14:48:06 +08002408 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002409 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002410 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002411 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002412 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002413 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002414 /* csum items have already been cloned */
2415 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2416 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002417 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002418 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2419 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002420 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002421 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002422 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002423 if (ret)
2424 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002425 }
2426
Chris Mason0b86a832008-03-24 15:01:56 -04002427mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002428 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002429
2430out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002431 if (ret) {
2432 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002433 bio_endio(bio);
2434 }
Stefan Behrens61891922012-11-05 18:51:52 +01002435 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002436}
Chris Mason6885f302008-02-20 16:11:05 -05002437
Chris Masond352ac62008-09-29 15:18:18 -04002438/*
2439 * given a list of ordered sums record them in the inode. This happens
2440 * at IO completion time based on sums calculated at bio submission time.
2441 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002442static int add_pending_csums(struct btrfs_trans_handle *trans,
2443 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002444{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002445 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002446 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002447
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002448 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002449 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002450 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002451 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002452 if (ret)
2453 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002454 }
2455 return 0;
2456}
2457
Filipe Mananac3347302020-11-04 11:07:31 +00002458static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2459 const u64 start,
2460 const u64 len,
2461 struct extent_state **cached_state)
2462{
2463 u64 search_start = start;
2464 const u64 end = start + len - 1;
2465
2466 while (search_start < end) {
2467 const u64 search_len = end - search_start + 1;
2468 struct extent_map *em;
2469 u64 em_len;
2470 int ret = 0;
2471
2472 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2473 if (IS_ERR(em))
2474 return PTR_ERR(em);
2475
2476 if (em->block_start != EXTENT_MAP_HOLE)
2477 goto next;
2478
2479 em_len = em->len;
2480 if (em->start < search_start)
2481 em_len -= search_start - em->start;
2482 if (em_len > search_len)
2483 em_len = search_len;
2484
2485 ret = set_extent_bit(&inode->io_tree, search_start,
2486 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002487 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2488 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002489next:
2490 search_start = extent_map_end(em);
2491 free_extent_map(em);
2492 if (ret)
2493 return ret;
2494 }
2495 return 0;
2496}
2497
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002498int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002499 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002500 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002501{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002502 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002503
2504 if (start >= i_size_read(&inode->vfs_inode) &&
2505 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2506 /*
2507 * There can't be any extents following eof in this case so just
2508 * set the delalloc new bit for the range directly.
2509 */
2510 extra_bits |= EXTENT_DELALLOC_NEW;
2511 } else {
2512 int ret;
2513
2514 ret = btrfs_find_new_delalloc_bytes(inode, start,
2515 end + 1 - start,
2516 cached_state);
2517 if (ret)
2518 return ret;
2519 }
2520
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002521 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2522 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002523}
2524
Chris Masond352ac62008-09-29 15:18:18 -04002525/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002526struct btrfs_writepage_fixup {
2527 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002528 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002529 struct btrfs_work work;
2530};
2531
Christoph Hellwigb2950862008-12-02 09:54:17 -05002532static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002533{
2534 struct btrfs_writepage_fixup *fixup;
2535 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002536 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002537 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002538 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002539 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002540 u64 page_start;
2541 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002542 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002543 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002544
2545 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2546 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002547 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002548 page_start = page_offset(page);
2549 page_end = page_offset(page) + PAGE_SIZE - 1;
2550
2551 /*
2552 * This is similar to page_mkwrite, we need to reserve the space before
2553 * we take the page lock.
2554 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002555 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2556 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002557again:
Chris Mason247e7432008-07-17 12:53:51 -04002558 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002559
Chris Mason25f3c502020-01-21 11:51:42 -05002560 /*
2561 * Before we queued this fixup, we took a reference on the page.
2562 * page->mapping may go NULL, but it shouldn't be moved to a different
2563 * address space.
2564 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002565 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2566 /*
2567 * Unfortunately this is a little tricky, either
2568 *
2569 * 1) We got here and our page had already been dealt with and
2570 * we reserved our space, thus ret == 0, so we need to just
2571 * drop our space reservation and bail. This can happen the
2572 * first time we come into the fixup worker, or could happen
2573 * while waiting for the ordered extent.
2574 * 2) Our page was already dealt with, but we happened to get an
2575 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2576 * this case we obviously don't have anything to release, but
2577 * because the page was already dealt with we don't want to
2578 * mark the page with an error, so make sure we're resetting
2579 * ret to 0. This is why we have this check _before_ the ret
2580 * check, because we do not want to have a surprise ENOSPC
2581 * when the page was already properly dealt with.
2582 */
2583 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002584 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2585 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002586 page_start, PAGE_SIZE,
2587 true);
2588 }
2589 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002590 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002591 }
Chris Mason25f3c502020-01-21 11:51:42 -05002592
2593 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002594 * We can't mess with the page state unless it is locked, so now that
2595 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002596 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002597 if (ret)
2598 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002599
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002600 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002601
2602 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002603 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002604 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002605
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002606 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002607 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002608 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2609 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002610 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002611 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002612 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002613 goto again;
2614 }
Chris Mason247e7432008-07-17 12:53:51 -04002615
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002616 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002617 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002618 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002619 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002620
Chris Mason25f3c502020-01-21 11:51:42 -05002621 /*
2622 * Everything went as planned, we're now the owner of a dirty page with
2623 * delayed allocation bits set and space reserved for our COW
2624 * destination.
2625 *
2626 * The page was dirty when we started, nothing should have cleaned it.
2627 */
2628 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002629 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002630out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002631 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002632 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002633 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2634 PAGE_SIZE, true);
2635 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002636 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002637out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002638 if (ret) {
2639 /*
2640 * We hit ENOSPC or other errors. Update the mapping and page
2641 * to reflect the errors and clean the page.
2642 */
2643 mapping_set_error(page->mapping, ret);
2644 end_extent_writepage(page, ret, page_start, page_end);
2645 clear_page_dirty_for_io(page);
2646 SetPageError(page);
2647 }
2648 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002649 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002650 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002651 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002652 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002653 /*
2654 * As a precaution, do a delayed iput in case it would be the last iput
2655 * that could need flushing space. Recursing back to fixup worker would
2656 * deadlock.
2657 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002658 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002659}
2660
2661/*
2662 * There are a few paths in the higher layers of the kernel that directly
2663 * set the page dirty bit without asking the filesystem if it is a
2664 * good idea. This causes problems because we want to make sure COW
2665 * properly happens and the data=ordered rules are followed.
2666 *
Chris Masonc8b97812008-10-29 14:49:59 -04002667 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002668 * hasn't been properly setup for IO. We kick off an async process
2669 * to fix it up. The async helper will wait for ordered extents, set
2670 * the delalloc bit and make it safe to write the page.
2671 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002672int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002673{
2674 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002676 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002677
Chris Mason8b62b722009-09-02 16:53:46 -04002678 /* this page is properly in the ordered list */
2679 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002680 return 0;
2681
Chris Mason25f3c502020-01-21 11:51:42 -05002682 /*
2683 * PageChecked is set below when we create a fixup worker for this page,
2684 * don't try to create another one if we're already PageChecked()
2685 *
2686 * The extent_io writepage code will redirty the page if we send back
2687 * EAGAIN.
2688 */
Chris Mason247e7432008-07-17 12:53:51 -04002689 if (PageChecked(page))
2690 return -EAGAIN;
2691
2692 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2693 if (!fixup)
2694 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002695
Josef Bacikf4b13632020-01-21 14:34:52 -05002696 /*
2697 * We are already holding a reference to this inode from
2698 * write_cache_pages. We need to hold it because the space reservation
2699 * takes place outside of the page lock, and we can't trust
2700 * page->mapping outside of the page lock.
2701 */
2702 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002703 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002704 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002705 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002706 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002707 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002708 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002709
2710 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002711}
2712
Yan Zhengd899e052008-10-30 14:25:28 -04002713static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002714 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002715 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002716 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002717 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002718{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002719 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002720 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002721 struct btrfs_path *path;
2722 struct extent_buffer *leaf;
2723 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002724 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2725 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2726 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2727 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002728 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002729 int ret;
2730
2731 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002732 if (!path)
2733 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002734
Chris Masona1ed8352009-09-11 12:27:37 -04002735 /*
2736 * we may be replacing one extent in the tree with another.
2737 * The new extent is pinned in the extent map, and we don't want
2738 * to drop it from the cache until it is completely in the btree.
2739 *
2740 * So, tell btrfs_drop_extents to leave this extent in the cache.
2741 * the caller is expected to unpin it and allow it to be merged
2742 * with the others.
2743 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002744 drop_args.path = path;
2745 drop_args.start = file_pos;
2746 drop_args.end = file_pos + num_bytes;
2747 drop_args.replace_extent = true;
2748 drop_args.extent_item_size = sizeof(*stack_fi);
2749 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002750 if (ret)
2751 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002752
Filipe Manana5893dfb2020-11-04 11:07:32 +00002753 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002754 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002755 ins.offset = file_pos;
2756 ins.type = BTRFS_EXTENT_DATA_KEY;
2757
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002758 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002759 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002760 if (ret)
2761 goto out;
2762 }
Yan Zhengd899e052008-10-30 14:25:28 -04002763 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002764 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2765 write_extent_buffer(leaf, stack_fi,
2766 btrfs_item_ptr_offset(leaf, path->slots[0]),
2767 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002768
Yan Zhengd899e052008-10-30 14:25:28 -04002769 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002770 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002771
Filipe Manana2766ff62020-11-04 11:07:34 +00002772 /*
2773 * If we dropped an inline extent here, we know the range where it is
2774 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2775 * number of bytes only for that range contaning the inline extent.
2776 * The remaining of the range will be processed when clearning the
2777 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2778 */
2779 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2780 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2781
2782 inline_size = drop_args.bytes_found - inline_size;
2783 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2784 drop_args.bytes_found -= inline_size;
2785 num_bytes -= sectorsize;
2786 }
2787
2788 if (update_inode_bytes)
2789 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002790
2791 ins.objectid = disk_bytenr;
2792 ins.offset = disk_num_bytes;
2793 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002794
Nikolay Borisovc553f942020-06-03 08:55:19 +03002795 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002796 if (ret)
2797 goto out;
2798
Nikolay Borisovc553f942020-06-03 08:55:19 +03002799 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002800 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002801out:
Yan Zhengd899e052008-10-30 14:25:28 -04002802 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002803
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002804 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002805}
2806
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002807static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002808 u64 start, u64 len)
2809{
David Sterba32da53862019-10-29 19:20:18 +01002810 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002812 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002813 ASSERT(cache);
2814
2815 spin_lock(&cache->lock);
2816 cache->delalloc_bytes -= len;
2817 spin_unlock(&cache->lock);
2818
2819 btrfs_put_block_group(cache);
2820}
2821
Qu Wenruo203f44c52020-06-10 09:04:40 +08002822static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002823 struct btrfs_ordered_extent *oe)
2824{
2825 struct btrfs_file_extent_item stack_fi;
2826 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002827 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002828
2829 memset(&stack_fi, 0, sizeof(stack_fi));
2830 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2831 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2832 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2833 oe->disk_num_bytes);
2834 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2835 logical_len = oe->truncated_len;
2836 else
2837 logical_len = oe->num_bytes;
2838 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2839 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2840 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2841 /* Encryption and other encoding is reserved and all 0 */
2842
Filipe Manana2766ff62020-11-04 11:07:34 +00002843 /*
2844 * For delalloc, when completing an ordered extent we update the inode's
2845 * bytes when clearing the range in the inode's io tree, so pass false
2846 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2847 * except if the ordered extent was truncated.
2848 */
2849 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2850 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2851
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002852 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2853 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002854 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002855}
2856
2857/*
2858 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002859 * an ordered extent if the range of bytes in the file it covers are
2860 * fully written.
2861 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002862static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002864 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2865 struct btrfs_root *root = inode->root;
2866 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002867 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002868 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002869 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002870 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002871 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002873 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002874 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002875 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002876 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002877 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002878
Omar Sandovalbffe6332019-12-02 17:34:19 -08002879 start = ordered_extent->file_offset;
2880 end = start + ordered_extent->num_bytes - 1;
2881
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002882 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2883 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2884 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002885 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002886
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002887 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002888
Josef Bacik5fd02042012-05-02 14:00:54 -04002889 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2890 ret = -EIO;
2891 goto out;
2892 }
2893
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002894 if (ordered_extent->disk)
2895 btrfs_rewrite_logical_zoned(ordered_extent);
2896
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002897 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002898
Josef Bacik77cef2e2013-08-29 13:57:21 -04002899 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2900 truncated = true;
2901 logical_len = ordered_extent->truncated_len;
2902 /* Truncated the entire extent, don't bother adding */
2903 if (!logical_len)
2904 goto out;
2905 }
2906
Yan, Zhengc2167752009-11-12 09:34:21 +00002907 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002908 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002909
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002910 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002911 if (freespace_inode)
2912 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002913 else
2914 trans = btrfs_join_transaction(root);
2915 if (IS_ERR(trans)) {
2916 ret = PTR_ERR(trans);
2917 trans = NULL;
2918 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002919 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002920 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002921 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002922 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002923 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002924 goto out;
2925 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926
Filipe Manana2766ff62020-11-04 11:07:34 +00002927 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002928 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002929
Nikolay Borisov8d510122019-10-08 20:43:06 +03002930 if (freespace_inode)
2931 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002932 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002933 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 if (IS_ERR(trans)) {
2935 ret = PTR_ERR(trans);
2936 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002937 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002938 }
Chris Masona79b7d42014-05-22 16:18:52 -07002939
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002940 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002941
Chris Masonc8b97812008-10-29 14:49:59 -04002942 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002943 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002944 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002945 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002946 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002947 ordered_extent->file_offset,
2948 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002949 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002950 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002952 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002953 if (!ret) {
2954 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002955 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002956 ordered_extent->disk_bytenr,
2957 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002958 }
Yan Zhengd899e052008-10-30 14:25:28 -04002959 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002960 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002961 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002962 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002963 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002964 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002965 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002966
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002967 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002968 if (ret) {
2969 btrfs_abort_transaction(trans, ret);
2970 goto out;
2971 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002972
Filipe Manana2766ff62020-11-04 11:07:34 +00002973 /*
2974 * If this is a new delalloc range, clear its new delalloc flag to
2975 * update the inode's number of bytes. This needs to be done first
2976 * before updating the inode item.
2977 */
2978 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2979 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002980 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002981 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2982 0, 0, &cached_state);
2983
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002984 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002985 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002986 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002987 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002988 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002989 }
2990 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002991out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002992 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002993 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002994 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002995
Miao Xiea698d0752012-09-20 01:51:59 -06002996 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002997 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002998
Josef Bacik77cef2e2013-08-29 13:57:21 -04002999 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003000 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003001
Josef Bacikd61bec02021-05-19 09:38:27 -04003002 /*
3003 * If we failed to finish this ordered extent for any reason we
3004 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3005 * extent, and mark the inode with the error if it wasn't
3006 * already set. Any error during writeback would have already
3007 * set the mapping error, so we need to set it if we're the ones
3008 * marking this ordered extent as failed.
3009 */
3010 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3011 &ordered_extent->flags))
3012 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3013
Josef Bacik77cef2e2013-08-29 13:57:21 -04003014 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003015 unwritten_start += logical_len;
3016 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003017
3018 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003019 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003020
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003021 /*
3022 * If the ordered extent had an IOERR or something else went
3023 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003024 * back to the allocator. We only free the extent in the
3025 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003026 *
3027 * If we made it past insert_reserved_file_extent before we
3028 * errored out then we don't need to do this as the accounting
3029 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003030 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003031 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003032 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003033 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003034 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3035 /*
3036 * Discard the range before returning it back to the
3037 * free space pool
3038 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003039 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003040 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003041 ordered_extent->disk_bytenr,
3042 ordered_extent->disk_num_bytes,
3043 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003044 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003045 ordered_extent->disk_bytenr,
3046 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003047 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003048 }
3049
Josef Bacik5fd02042012-05-02 14:00:54 -04003050 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003051 * This needs to be done to make sure anybody waiting knows we are done
3052 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003053 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003054 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055
Chris Masone6dcd2d2008-07-17 12:53:50 -04003056 /* once for us */
3057 btrfs_put_ordered_extent(ordered_extent);
3058 /* once for the tree */
3059 btrfs_put_ordered_extent(ordered_extent);
3060
Josef Bacik5fd02042012-05-02 14:00:54 -04003061 return ret;
3062}
3063
3064static void finish_ordered_fn(struct btrfs_work *work)
3065{
3066 struct btrfs_ordered_extent *ordered_extent;
3067 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3068 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069}
3070
Nikolay Borisovc6297322018-11-08 10:18:08 +02003071void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3072 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003073{
Nikolay Borisov3347c482020-08-31 14:42:44 +03003074 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3075 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04003076 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003077 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003078
liubo1abe9b82011-03-24 11:18:59 +00003079 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3080
Chris Mason8b62b722009-09-02 16:53:46 -04003081 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03003082 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3083 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003084 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003085
Nikolay Borisov3347c482020-08-31 14:42:44 +03003086 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003087 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003088 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003089 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003090
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003091 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003092 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003093}
3094
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003095/*
3096 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003097 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003098 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003099 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003100 * @page: page where is the data to be verified
3101 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003102 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003103 *
3104 * The length of such check is always one sector size.
3105 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003106static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003107 u32 bio_offset, struct page *page, u32 pgoff,
3108 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003109{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3111 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003112 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003113 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003114 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003115 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003116 u8 *csum_expected;
3117 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003118
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003119 ASSERT(pgoff + len <= PAGE_SIZE);
3120
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003121 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3122 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003123
3124 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003125 shash->tfm = fs_info->csum_shash;
3126
Eric Biggersfd080012020-04-30 23:51:59 -07003127 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003128
3129 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003130 goto zeroit;
3131
3132 kunmap_atomic(kaddr);
3133 return 0;
3134zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003135 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3136 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003137 if (io_bio->device)
3138 btrfs_dev_stat_inc_and_print(io_bio->device,
3139 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003140 memset(kaddr + pgoff, 1, len);
3141 flush_dcache_page(page);
3142 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003143 return -EIO;
3144}
3145
Chris Masond352ac62008-09-29 15:18:18 -04003146/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003147 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003148 * if there's a match, we allow the bio to finish. If not, the code in
3149 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003150 *
3151 * @bio_offset: offset to the beginning of the bio (in bytes)
3152 * @start: file offset of the range start
3153 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003154 *
3155 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3156 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003157 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003158unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3159 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003160{
Chris Mason07157aa2007-08-30 08:50:51 -04003161 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003162 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003163 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003164 const u32 sectorsize = root->fs_info->sectorsize;
3165 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003166 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003167
Chris Masond20f7042008-12-08 16:58:54 -05003168 if (PageChecked(page)) {
3169 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003170 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003171 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003172
3173 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003174 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003175
Josef Bacik42437a62020-10-16 11:29:18 -04003176 if (!root->fs_info->csum_root)
3177 return 0;
3178
Yan Zheng17d217f2008-12-12 10:03:38 -05003179 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003180 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003181 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003182 return 0;
3183 }
3184
Qu Wenruof44cf412020-12-02 14:47:59 +08003185 ASSERT(page_offset(page) <= start &&
3186 end <= page_offset(page) + PAGE_SIZE - 1);
3187 for (pg_off = offset_in_page(start);
3188 pg_off < offset_in_page(end);
3189 pg_off += sectorsize, bio_offset += sectorsize) {
3190 int ret;
3191
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003192 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3193 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003194 if (ret < 0) {
3195 const int nr_bit = (pg_off - offset_in_page(start)) >>
3196 root->fs_info->sectorsize_bits;
3197
3198 result |= (1U << nr_bit);
3199 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003200 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003201 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003202}
Chris Masonb888db22007-08-27 16:49:44 -04003203
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003204/*
3205 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3206 *
3207 * @inode: The inode we want to perform iput on
3208 *
3209 * This function uses the generic vfs_inode::i_count to track whether we should
3210 * just decrement it (in case it's > 1) or if this is the last iput then link
3211 * the inode to the delayed iput machinery. Delayed iputs are processed at
3212 * transaction commit time/superblock commit/cleaner kthread.
3213 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003214void btrfs_add_delayed_iput(struct inode *inode)
3215{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003217 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218
3219 if (atomic_add_unless(&inode->i_count, -1, 1))
3220 return;
3221
Josef Bacik034f7842018-12-03 11:06:52 -05003222 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003224 ASSERT(list_empty(&binode->delayed_iput));
3225 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003226 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003227 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3228 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003229}
3230
Josef Bacik63611e72019-06-18 10:59:18 -04003231static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3232 struct btrfs_inode *inode)
3233{
3234 list_del_init(&inode->delayed_iput);
3235 spin_unlock(&fs_info->delayed_iput_lock);
3236 iput(&inode->vfs_inode);
3237 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3238 wake_up(&fs_info->delayed_iputs_wait);
3239 spin_lock(&fs_info->delayed_iput_lock);
3240}
3241
3242static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3243 struct btrfs_inode *inode)
3244{
3245 if (!list_empty(&inode->delayed_iput)) {
3246 spin_lock(&fs_info->delayed_iput_lock);
3247 if (!list_empty(&inode->delayed_iput))
3248 run_delayed_iput_locked(fs_info, inode);
3249 spin_unlock(&fs_info->delayed_iput_lock);
3250 }
3251}
3252
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003253void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003255
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003257 while (!list_empty(&fs_info->delayed_iputs)) {
3258 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259
David Sterba8089fe62015-11-19 14:15:51 +01003260 inode = list_first_entry(&fs_info->delayed_iputs,
3261 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003262 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003263 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264 }
David Sterba8089fe62015-11-19 14:15:51 +01003265 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266}
3267
Josef Bacik034f7842018-12-03 11:06:52 -05003268/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003269 * Wait for flushing all delayed iputs
3270 *
3271 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003272 *
3273 * This will wait on any delayed iputs that are currently running with KILLABLE
3274 * set. Once they are all done running we will return, unless we are killed in
3275 * which case we return EINTR. This helps in user operations like fallocate etc
3276 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003277 *
3278 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003279 */
3280int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3281{
3282 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3283 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3284 if (ret)
3285 return -EINTR;
3286 return 0;
3287}
3288
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003290 * This creates an orphan entry for the given inode in case something goes wrong
3291 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003292 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003293int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003294 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003295{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003297
Omar Sandoval27919062018-05-11 13:13:37 -07003298 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3299 if (ret && ret != -EEXIST) {
3300 btrfs_abort_transaction(trans, ret);
3301 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 }
3303
Yan, Zhengd68fc572010-05-16 10:49:58 -04003304 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003305}
3306
3307/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003308 * We have done the delete so we can go ahead and remove the orphan item for
3309 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003310 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003311static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003312 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003313{
Omar Sandoval27919062018-05-11 13:13:37 -07003314 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003315}
3316
3317/*
3318 * this cleans up any orphans that may be left on the list from the last use
3319 * of this root.
3320 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003321int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003322{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003323 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003324 struct btrfs_path *path;
3325 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003326 struct btrfs_key key, found_key;
3327 struct btrfs_trans_handle *trans;
3328 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003329 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003330 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003331
Yan, Zhengd68fc572010-05-16 10:49:58 -04003332 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003333 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003334
3335 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003336 if (!path) {
3337 ret = -ENOMEM;
3338 goto out;
3339 }
David Sterbae4058b52015-11-27 16:31:35 +01003340 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003341
3342 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003343 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003344 key.offset = (u64)-1;
3345
Josef Bacik7b128762008-07-24 12:17:14 -04003346 while (1) {
3347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003348 if (ret < 0)
3349 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003350
3351 /*
3352 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003353 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003354 * find the key and see if we have stuff that matches
3355 */
3356 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003357 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003358 if (path->slots[0] == 0)
3359 break;
3360 path->slots[0]--;
3361 }
3362
3363 /* pull out the item */
3364 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003365 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3366
3367 /* make sure the item matches what we want */
3368 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3369 break;
David Sterba962a2982014-06-04 18:41:45 +02003370 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003371 break;
3372
3373 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003374 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003375
3376 /*
3377 * this is where we are basically btrfs_lookup, without the
3378 * crossing root thing. we store the inode number in the
3379 * offset of the orphan item.
3380 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003381
3382 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 btrfs_err(fs_info,
3384 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003385 ret = -EINVAL;
3386 goto out;
3387 }
3388
3389 last_objectid = found_key.offset;
3390
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003391 found_key.objectid = found_key.offset;
3392 found_key.type = BTRFS_INODE_ITEM_KEY;
3393 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003394 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303395 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003396 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003397 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003398
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003399 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003400 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003401 int is_dead_root = 0;
3402
3403 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003404 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003405 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003406 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003407 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003408 * We need to distinguish those two, as the orphan item
3409 * for a root must not get deleted before the deletion
3410 * of the snapshot/subvolume's tree completes.
3411 *
3412 * btrfs_find_orphan_roots() ran before us, which has
3413 * found all deleted roots and loaded them into
3414 * fs_info->fs_roots_radix. So here we can find if an
3415 * orphan item corresponds to a deleted root by looking
3416 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003417 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003418
3419 spin_lock(&fs_info->fs_roots_radix_lock);
3420 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3421 (unsigned long)found_key.objectid);
3422 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3423 is_dead_root = 1;
3424 spin_unlock(&fs_info->fs_roots_radix_lock);
3425
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003426 if (is_dead_root) {
3427 /* prevent this orphan from being found again */
3428 key.offset = found_key.objectid - 1;
3429 continue;
3430 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003431
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003432 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003433
Josef Bacika8c9e572011-09-21 16:55:59 -04003434 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003435 * If we have an inode with links, there are a couple of
3436 * possibilities. Old kernels (before v3.12) used to create an
3437 * orphan item for truncate indicating that there were possibly
3438 * extent items past i_size that needed to be deleted. In v3.12,
3439 * truncate was changed to update i_size in sync with the extent
3440 * items, but the (useless) orphan item was still created. Since
3441 * v4.18, we don't create the orphan item for truncate at all.
3442 *
3443 * So, this item could mean that we need to do a truncate, but
3444 * only if this filesystem was last used on a pre-v3.12 kernel
3445 * and was not cleanly unmounted. The odds of that are quite
3446 * slim, and it's a pain to do the truncate now, so just delete
3447 * the orphan item.
3448 *
3449 * It's also possible that this orphan item was supposed to be
3450 * deleted but wasn't. The inode number may have been reused,
3451 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003452 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003453 if (ret == -ENOENT || inode->i_nlink) {
3454 if (!ret)
3455 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003456 trans = btrfs_start_transaction(root, 1);
3457 if (IS_ERR(trans)) {
3458 ret = PTR_ERR(trans);
3459 goto out;
3460 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003461 btrfs_debug(fs_info, "auto deleting %Lu",
3462 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003463 ret = btrfs_del_orphan_item(trans, root,
3464 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003465 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003466 if (ret)
3467 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003468 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 }
Josef Bacik7b128762008-07-24 12:17:14 -04003470
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003471 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003472
3473 /* this will do delete_inode and everything for us */
3474 iput(inode);
3475 }
Miao Xie3254c872011-11-10 20:45:05 -05003476 /* release the path since we're done with it */
3477 btrfs_release_path(path);
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3480
Omar Sandovala575cee2018-05-11 13:13:38 -07003481 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003482 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003484 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003485 }
Josef Bacik7b128762008-07-24 12:17:14 -04003486
3487 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003489
3490out:
3491 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003492 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003493 btrfs_free_path(path);
3494 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003495}
3496
Chris Masond352ac62008-09-29 15:18:18 -04003497/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003498 * very simple check to peek ahead in the leaf looking for xattrs. If we
3499 * don't find any xattrs, we know there can't be any acls.
3500 *
3501 * slot is the slot the inode is in, objectid is the objectid of the inode
3502 */
3503static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003504 int slot, u64 objectid,
3505 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003506{
3507 u32 nritems = btrfs_header_nritems(leaf);
3508 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003509 static u64 xattr_access = 0;
3510 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003511 int scanned = 0;
3512
Josef Bacikf23b5a52013-06-19 10:16:26 -04003513 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003514 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3515 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3516 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3517 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003518 }
3519
Chris Mason46a53cc2009-04-27 11:47:50 -04003520 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003521 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003522 while (slot < nritems) {
3523 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3524
3525 /* we found a different objectid, there must not be acls */
3526 if (found_key.objectid != objectid)
3527 return 0;
3528
3529 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003530 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003531 if (*first_xattr_slot == -1)
3532 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003533 if (found_key.offset == xattr_access ||
3534 found_key.offset == xattr_default)
3535 return 1;
3536 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003537
3538 /*
3539 * we found a key greater than an xattr key, there can't
3540 * be any acls later on
3541 */
3542 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3543 return 0;
3544
3545 slot++;
3546 scanned++;
3547
3548 /*
3549 * it goes inode, inode backrefs, xattrs, extents,
3550 * so if there are a ton of hard links to an inode there can
3551 * be a lot of backrefs. Don't waste time searching too hard,
3552 * this is just an optimization
3553 */
3554 if (scanned >= 8)
3555 break;
3556 }
3557 /* we hit the end of the leaf before we found an xattr or
3558 * something larger than an xattr. We have to assume the inode
3559 * has acls
3560 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003561 if (*first_xattr_slot == -1)
3562 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003563 return 1;
3564}
3565
3566/*
Chris Masond352ac62008-09-29 15:18:18 -04003567 * read an inode from the btree into the in-memory inode
3568 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003569static int btrfs_read_locked_inode(struct inode *inode,
3570 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003571{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003572 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003573 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003574 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 struct btrfs_inode_item *inode_item;
3576 struct btrfs_root *root = BTRFS_I(inode)->root;
3577 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003578 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003579 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003580 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003582 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003583 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003584
3585 ret = btrfs_fill_inode(inode, &rdev);
3586 if (!ret)
3587 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003588
Filipe Manana4222ea72018-10-24 10:13:03 +01003589 if (!path) {
3590 path = btrfs_alloc_path();
3591 if (!path)
3592 return -ENOMEM;
3593 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003594
Chris Mason39279cc2007-06-12 06:35:45 -04003595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003596
Chris Mason39279cc2007-06-12 06:35:45 -04003597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003598 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003599 if (path != in_path)
3600 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003601 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003602 }
Chris Mason39279cc2007-06-12 06:35:45 -04003603
Chris Mason5f39d392007-10-15 16:14:19 -04003604 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003605
3606 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003607 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003608
Chris Mason5f39d392007-10-15 16:14:19 -04003609 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3610 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003611 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003612 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003613 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3614 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003615 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003616 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3617 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003618
David Sterbaa937b972014-12-12 17:39:12 +01003619 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3620 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
David Sterbaa937b972014-12-12 17:39:12 +01003622 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3623 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003624
David Sterbaa937b972014-12-12 17:39:12 +01003625 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3626 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
chandan r9cc97d62012-07-04 12:48:07 +05303628 BTRFS_I(inode)->i_otime.tv_sec =
3629 btrfs_timespec_sec(leaf, &inode_item->otime);
3630 BTRFS_I(inode)->i_otime.tv_nsec =
3631 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003632
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003633 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003634 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003635 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3636
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003637 inode_set_iversion_queried(inode,
3638 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003639 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003640 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003641 rdev = btrfs_inode_rdev(leaf, inode_item);
3642
Josef Bacikaec74772008-07-24 12:12:38 -04003643 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003644 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003645
3646cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003647 /*
3648 * If we were modified in the current generation and evicted from memory
3649 * and then re-read we need to do a full sync since we don't have any
3650 * idea about which extents were modified before we were evicted from
3651 * cache.
3652 *
3653 * This is required for both inode re-read from disk and delayed inode
3654 * in delayed_nodes_tree.
3655 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003656 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003657 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3658 &BTRFS_I(inode)->runtime_flags);
3659
Filipe Mananabde6c242015-07-24 00:00:19 +01003660 /*
3661 * We don't persist the id of the transaction where an unlink operation
3662 * against the inode was last made. So here we assume the inode might
3663 * have been evicted, and therefore the exact value of last_unlink_trans
3664 * lost, and set it to last_trans to avoid metadata inconsistencies
3665 * between the inode and its parent if the inode is fsync'ed and the log
3666 * replayed. For example, in the scenario:
3667 *
3668 * touch mydir/foo
3669 * ln mydir/foo mydir/bar
3670 * sync
3671 * unlink mydir/bar
3672 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3673 * xfs_io -c fsync mydir/foo
3674 * <power failure>
3675 * mount fs, triggers fsync log replay
3676 *
3677 * We must make sure that when we fsync our inode foo we also log its
3678 * parent inode, otherwise after log replay the parent still has the
3679 * dentry with the "bar" name but our inode foo has a link count of 1
3680 * and doesn't have an inode ref with the name "bar" anymore.
3681 *
3682 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003683 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003684 * transaction commits on fsync if our inode is a directory, or if our
3685 * inode is not a directory, logging its parent unnecessarily.
3686 */
3687 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3688
Filipe Manana3ebac172020-07-15 12:30:43 +01003689 /*
3690 * Same logic as for last_unlink_trans. We don't persist the generation
3691 * of the last transaction where this inode was used for a reflink
3692 * operation, so after eviction and reloading the inode we must be
3693 * pessimistic and assume the last transaction that modified the inode.
3694 */
3695 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3696
Miao Xie67de1172013-12-26 13:07:06 +08003697 path->slots[0]++;
3698 if (inode->i_nlink != 1 ||
3699 path->slots[0] >= btrfs_header_nritems(leaf))
3700 goto cache_acl;
3701
3702 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003703 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003704 goto cache_acl;
3705
3706 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3707 if (location.type == BTRFS_INODE_REF_KEY) {
3708 struct btrfs_inode_ref *ref;
3709
3710 ref = (struct btrfs_inode_ref *)ptr;
3711 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3712 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3713 struct btrfs_inode_extref *extref;
3714
3715 extref = (struct btrfs_inode_extref *)ptr;
3716 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3717 extref);
3718 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003719cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003720 /*
3721 * try to precache a NULL acl entry for files that don't have
3722 * any xattrs or acls
3723 */
Li Zefan33345d012011-04-20 10:31:50 +08003724 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003725 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003726 if (first_xattr_slot != -1) {
3727 path->slots[0] = first_xattr_slot;
3728 ret = btrfs_load_inode_props(inode, path);
3729 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003730 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003731 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003732 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003733 root->root_key.objectid, ret);
3734 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003735 if (path != in_path)
3736 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003737
Al Viro72c04902009-06-24 16:58:48 -04003738 if (!maybe_acls)
3739 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003740
Chris Mason39279cc2007-06-12 06:35:45 -04003741 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003742 case S_IFREG:
3743 inode->i_mapping->a_ops = &btrfs_aops;
3744 inode->i_fop = &btrfs_file_operations;
3745 inode->i_op = &btrfs_file_inode_operations;
3746 break;
3747 case S_IFDIR:
3748 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003749 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003750 break;
3751 case S_IFLNK:
3752 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003753 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003754 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003756 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003757 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003758 init_special_inode(inode, inode->i_mode, rdev);
3759 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003761
David Sterba7b6a2212018-03-26 18:40:21 +02003762 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003763 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003764}
3765
Chris Masond352ac62008-09-29 15:18:18 -04003766/*
3767 * given a leaf and an inode, copy the inode fields into the leaf
3768 */
Chris Masone02119d2008-09-05 16:13:11 -04003769static void fill_inode_item(struct btrfs_trans_handle *trans,
3770 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003771 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003772 struct inode *inode)
3773{
Liu Bo51fab692012-12-27 09:01:21 +00003774 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003775
David Sterbac82f8232019-08-09 17:48:21 +02003776 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003777
David Sterbacc4c13d2020-04-29 02:15:56 +02003778 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3779 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3780 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3781 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3782 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
David Sterbacc4c13d2020-04-29 02:15:56 +02003784 btrfs_set_token_timespec_sec(&token, &item->atime,
3785 inode->i_atime.tv_sec);
3786 btrfs_set_token_timespec_nsec(&token, &item->atime,
3787 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbacc4c13d2020-04-29 02:15:56 +02003789 btrfs_set_token_timespec_sec(&token, &item->mtime,
3790 inode->i_mtime.tv_sec);
3791 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3792 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003793
David Sterbacc4c13d2020-04-29 02:15:56 +02003794 btrfs_set_token_timespec_sec(&token, &item->ctime,
3795 inode->i_ctime.tv_sec);
3796 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3797 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003798
David Sterbacc4c13d2020-04-29 02:15:56 +02003799 btrfs_set_token_timespec_sec(&token, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_sec);
3801 btrfs_set_token_timespec_nsec(&token, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303803
David Sterbacc4c13d2020-04-29 02:15:56 +02003804 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3805 btrfs_set_token_inode_generation(&token, item,
3806 BTRFS_I(inode)->generation);
3807 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3808 btrfs_set_token_inode_transid(&token, item, trans->transid);
3809 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3810 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3811 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003812}
3813
Chris Masond352ac62008-09-29 15:18:18 -04003814/*
3815 * copy everything in the in-memory inode into the btree.
3816 */
Chris Mason21151332011-11-10 20:39:08 -05003817static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003818 struct btrfs_root *root,
3819 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003820{
3821 struct btrfs_inode_item *inode_item;
3822 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003824 int ret;
3825
3826 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 if (!path)
3828 return -ENOMEM;
3829
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003830 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003831 if (ret) {
3832 if (ret > 0)
3833 ret = -ENOENT;
3834 goto failed;
3835 }
3836
Chris Mason5f39d392007-10-15 16:14:19 -04003837 leaf = path->nodes[0];
3838 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003839 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003840
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003841 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003842 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003843 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003844 ret = 0;
3845failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003846 btrfs_free_path(path);
3847 return ret;
3848}
3849
Chris Masond352ac62008-09-29 15:18:18 -04003850/*
Chris Mason21151332011-11-10 20:39:08 -05003851 * copy everything in the in-memory inode into the btree.
3852 */
3853noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003854 struct btrfs_root *root,
3855 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003857 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003858 int ret;
3859
3860 /*
3861 * If the inode is a free space inode, we can deadlock during commit
3862 * if we put it into the delayed code.
3863 *
3864 * The data relocation inode should also be directly updated
3865 * without delay
3866 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003867 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003868 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003869 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003870 btrfs_update_root_times(trans, root);
3871
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003872 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003873 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003874 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003875 return ret;
3876 }
3877
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003878 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003879}
3880
Nikolay Borisov729f7962020-11-02 16:49:06 +02003881int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003883{
3884 int ret;
3885
Nikolay Borisov729f7962020-11-02 16:49:06 +02003886 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003887 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003888 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003889 return ret;
3890}
3891
3892/*
Chris Masond352ac62008-09-29 15:18:18 -04003893 * unlink helper that gets used here in inode.c and in the tree logging
3894 * recovery code. It remove a link in a directory with a given name, and
3895 * also drops the back refs in the inode to the directory
3896 */
Al Viro92986792011-03-04 17:14:37 +00003897static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3898 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003899 struct btrfs_inode *dir,
3900 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003901 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003902{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003903 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003904 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003907 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003908 u64 ino = btrfs_ino(inode);
3909 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003910
3911 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003912 if (!path) {
3913 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003914 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003915 }
3916
Li Zefan33345d012011-04-20 10:31:50 +08003917 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003918 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003919 if (IS_ERR_OR_NULL(di)) {
3920 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 goto err;
3922 }
Chris Mason39279cc2007-06-12 06:35:45 -04003923 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003924 if (ret)
3925 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003926 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003927
Miao Xie67de1172013-12-26 13:07:06 +08003928 /*
3929 * If we don't have dir index, we have to get it by looking up
3930 * the inode ref, since we get the inode ref, remove it directly,
3931 * it is unnecessary to do delayed deletion.
3932 *
3933 * But if we have dir index, needn't search inode ref to get it.
3934 * Since the inode ref is close to the inode item, it is better
3935 * that we delay to delete it, and just do this deletion when
3936 * we update the inode item.
3937 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003938 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003939 ret = btrfs_delayed_delete_inode_ref(inode);
3940 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003941 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003942 goto skip_backref;
3943 }
3944 }
3945
Li Zefan33345d012011-04-20 10:31:50 +08003946 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3947 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003948 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003949 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003950 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003951 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003952 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003953 goto err;
3954 }
Miao Xie67de1172013-12-26 13:07:06 +08003955skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003956 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003957 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003958 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003959 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003960 }
Chris Mason39279cc2007-06-12 06:35:45 -04003961
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003962 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3963 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003965 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003966 goto err;
3967 }
Chris Masone02119d2008-09-05 16:13:11 -04003968
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003969 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3970 index);
Chris Mason6418c962010-10-30 07:34:24 -04003971 if (ret == -ENOENT)
3972 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003973 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003974 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003975
3976 /*
3977 * If we have a pending delayed iput we could end up with the final iput
3978 * being run in btrfs-cleaner context. If we have enough of these built
3979 * up we can end up burning a lot of time in btrfs-cleaner without any
3980 * way to throttle the unlinks. Since we're currently holding a ref on
3981 * the inode we can run the delayed iput here without any issues as the
3982 * final iput won't be done until after we drop the ref we're currently
3983 * holding.
3984 */
3985 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003986err:
3987 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003988 if (ret)
3989 goto out;
3990
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003991 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003992 inode_inc_iversion(&inode->vfs_inode);
3993 inode_inc_iversion(&dir->vfs_inode);
3994 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3995 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003996 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003997out:
Chris Mason39279cc2007-06-12 06:35:45 -04003998 return ret;
3999}
4000
Al Viro92986792011-03-04 17:14:37 +00004001int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004003 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004004 const char *name, int name_len)
4005{
4006 int ret;
4007 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4008 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004010 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004011 }
4012 return ret;
4013}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014
4015/*
4016 * helper to start transaction for unlink and rmdir.
4017 *
Josef Bacikd52be812013-05-29 14:54:47 -04004018 * unlink and rmdir are special in btrfs, they do not always free space, so
4019 * if we cannot make our reservations the normal way try and see if there is
4020 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4021 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022 */
Josef Bacikd52be812013-05-29 14:54:47 -04004023static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026
Josef Bacike70bea52011-10-11 14:18:24 -04004027 /*
4028 * 1 for the possible orphan item
4029 * 1 for the dir item
4030 * 1 for the dir index
4031 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004032 * 1 for the inode
4033 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004034 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035}
4036
Chris Mason39279cc2007-06-12 06:35:45 -04004037static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4038{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004039 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004041 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004042 int ret;
4043
Josef Bacikd52be812013-05-29 14:54:47 -04004044 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004046 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004047
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004048 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4049 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004050
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4052 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4053 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004054 if (ret)
4055 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004056
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004058 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004059 if (ret)
4060 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004061 }
Josef Bacik7b128762008-07-24 12:17:14 -04004062
Tsutomu Itohb5324022011-07-19 07:27:20 +00004063out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004064 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004065 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004066 return ret;
4067}
4068
Misono Tomohirof60a2362018-04-18 11:34:52 +09004069static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004070 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004071{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004072 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004073 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004074 struct btrfs_path *path;
4075 struct extent_buffer *leaf;
4076 struct btrfs_dir_item *di;
4077 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004078 const char *name = dentry->d_name.name;
4079 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004080 u64 index;
4081 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004082 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004083 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084
Josef Bacik045d3962019-12-18 17:20:27 -05004085 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4086 objectid = inode->root->root_key.objectid;
4087 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4088 objectid = inode->location.objectid;
4089 } else {
4090 WARN_ON(1);
4091 return -EINVAL;
4092 }
4093
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 path = btrfs_alloc_path();
4095 if (!path)
4096 return -ENOMEM;
4097
Li Zefan33345d012011-04-20 10:31:50 +08004098 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004101 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004102 goto out;
4103 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004104
4105 leaf = path->nodes[0];
4106 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4107 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4108 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004110 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 goto out;
4112 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004113 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114
Josef Bacikd49d3282019-12-18 17:20:28 -05004115 /*
4116 * This is a placeholder inode for a subvolume we didn't have a
4117 * reference to at the time of the snapshot creation. In the meantime
4118 * we could have renamed the real subvol link into our snapshot, so
4119 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4120 * Instead simply lookup the dir_index_item for this entry so we can
4121 * remove it. Otherwise we know we have a ref to the root and we can
4122 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4123 */
4124 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004125 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004126 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 if (IS_ERR_OR_NULL(di)) {
4128 if (!di)
4129 ret = -ENOENT;
4130 else
4131 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004132 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 goto out;
4134 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135
4136 leaf = path->nodes[0];
4137 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004138 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004139 btrfs_release_path(path);
4140 } else {
4141 ret = btrfs_del_root_ref(trans, objectid,
4142 root->root_key.objectid, dir_ino,
4143 &index, name, name_len);
4144 if (ret) {
4145 btrfs_abort_transaction(trans, ret);
4146 goto out;
4147 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149
Lu Fengqi9add2942018-08-01 11:32:26 +08004150 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004152 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 goto out;
4154 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004155
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004156 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004157 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004158 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004159 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004161 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004163 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004165}
4166
Misono Tomohiroec42f162018-04-18 11:34:13 +09004167/*
4168 * Helper to check if the subvolume references other subvolumes or if it's
4169 * default.
4170 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004171static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004172{
4173 struct btrfs_fs_info *fs_info = root->fs_info;
4174 struct btrfs_path *path;
4175 struct btrfs_dir_item *di;
4176 struct btrfs_key key;
4177 u64 dir_id;
4178 int ret;
4179
4180 path = btrfs_alloc_path();
4181 if (!path)
4182 return -ENOMEM;
4183
4184 /* Make sure this root isn't set as the default subvol */
4185 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4186 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4187 dir_id, "default", 7, 0);
4188 if (di && !IS_ERR(di)) {
4189 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4190 if (key.objectid == root->root_key.objectid) {
4191 ret = -EPERM;
4192 btrfs_err(fs_info,
4193 "deleting default subvolume %llu is not allowed",
4194 key.objectid);
4195 goto out;
4196 }
4197 btrfs_release_path(path);
4198 }
4199
4200 key.objectid = root->root_key.objectid;
4201 key.type = BTRFS_ROOT_REF_KEY;
4202 key.offset = (u64)-1;
4203
4204 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4205 if (ret < 0)
4206 goto out;
4207 BUG_ON(ret == 0);
4208
4209 ret = 0;
4210 if (path->slots[0] > 0) {
4211 path->slots[0]--;
4212 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4213 if (key.objectid == root->root_key.objectid &&
4214 key.type == BTRFS_ROOT_REF_KEY)
4215 ret = -ENOTEMPTY;
4216 }
4217out:
4218 btrfs_free_path(path);
4219 return ret;
4220}
4221
Nikolay Borisov20a68002018-04-27 14:36:24 +03004222/* Delete all dentries for inodes belonging to the root */
4223static void btrfs_prune_dentries(struct btrfs_root *root)
4224{
4225 struct btrfs_fs_info *fs_info = root->fs_info;
4226 struct rb_node *node;
4227 struct rb_node *prev;
4228 struct btrfs_inode *entry;
4229 struct inode *inode;
4230 u64 objectid = 0;
4231
4232 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4233 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4234
4235 spin_lock(&root->inode_lock);
4236again:
4237 node = root->inode_tree.rb_node;
4238 prev = NULL;
4239 while (node) {
4240 prev = node;
4241 entry = rb_entry(node, struct btrfs_inode, rb_node);
4242
David Sterba37508512018-06-29 10:56:40 +02004243 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004244 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004245 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004246 node = node->rb_right;
4247 else
4248 break;
4249 }
4250 if (!node) {
4251 while (prev) {
4252 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004253 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004254 node = prev;
4255 break;
4256 }
4257 prev = rb_next(prev);
4258 }
4259 }
4260 while (node) {
4261 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004262 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004263 inode = igrab(&entry->vfs_inode);
4264 if (inode) {
4265 spin_unlock(&root->inode_lock);
4266 if (atomic_read(&inode->i_count) > 1)
4267 d_prune_aliases(inode);
4268 /*
4269 * btrfs_drop_inode will have it removed from the inode
4270 * cache when its usage count hits zero.
4271 */
4272 iput(inode);
4273 cond_resched();
4274 spin_lock(&root->inode_lock);
4275 goto again;
4276 }
4277
4278 if (cond_resched_lock(&root->inode_lock))
4279 goto again;
4280
4281 node = rb_next(node);
4282 }
4283 spin_unlock(&root->inode_lock);
4284}
4285
Misono Tomohirof60a2362018-04-18 11:34:52 +09004286int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4287{
4288 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4289 struct btrfs_root *root = BTRFS_I(dir)->root;
4290 struct inode *inode = d_inode(dentry);
4291 struct btrfs_root *dest = BTRFS_I(inode)->root;
4292 struct btrfs_trans_handle *trans;
4293 struct btrfs_block_rsv block_rsv;
4294 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004295 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004296
4297 /*
4298 * Don't allow to delete a subvolume with send in progress. This is
4299 * inside the inode lock so the error handling that has to drop the bit
4300 * again is not run concurrently.
4301 */
4302 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004303 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004304 spin_unlock(&dest->root_item_lock);
4305 btrfs_warn(fs_info,
4306 "attempt to delete subvolume %llu during send",
4307 dest->root_key.objectid);
4308 return -EPERM;
4309 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004310 root_flags = btrfs_root_flags(&dest->root_item);
4311 btrfs_set_root_flags(&dest->root_item,
4312 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4313 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004314
4315 down_write(&fs_info->subvol_sem);
4316
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004317 ret = may_destroy_subvol(dest);
4318 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004319 goto out_up_write;
4320
4321 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4322 /*
4323 * One for dir inode,
4324 * two for dir entries,
4325 * two for root ref/backref.
4326 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004327 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4328 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004329 goto out_up_write;
4330
4331 trans = btrfs_start_transaction(root, 0);
4332 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004333 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004334 goto out_release;
4335 }
4336 trans->block_rsv = &block_rsv;
4337 trans->bytes_reserved = block_rsv.size;
4338
4339 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4340
Josef Bacik045d3962019-12-18 17:20:27 -05004341 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004342 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004343 btrfs_abort_transaction(trans, ret);
4344 goto out_end_trans;
4345 }
4346
Josef Bacik2731f512021-03-12 15:25:04 -05004347 ret = btrfs_record_root_in_trans(trans, dest);
4348 if (ret) {
4349 btrfs_abort_transaction(trans, ret);
4350 goto out_end_trans;
4351 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004352
4353 memset(&dest->root_item.drop_progress, 0,
4354 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004355 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004356 btrfs_set_root_refs(&dest->root_item, 0);
4357
4358 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4359 ret = btrfs_insert_orphan_item(trans,
4360 fs_info->tree_root,
4361 dest->root_key.objectid);
4362 if (ret) {
4363 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004364 goto out_end_trans;
4365 }
4366 }
4367
Lu Fengqid1957792018-05-29 15:01:54 +08004368 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004369 BTRFS_UUID_KEY_SUBVOL,
4370 dest->root_key.objectid);
4371 if (ret && ret != -ENOENT) {
4372 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004373 goto out_end_trans;
4374 }
4375 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004376 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004377 dest->root_item.received_uuid,
4378 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4379 dest->root_key.objectid);
4380 if (ret && ret != -ENOENT) {
4381 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004382 goto out_end_trans;
4383 }
4384 }
4385
Qu Wenruo082b6c92020-06-16 10:17:37 +08004386 free_anon_bdev(dest->anon_dev);
4387 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004388out_end_trans:
4389 trans->block_rsv = NULL;
4390 trans->bytes_reserved = 0;
4391 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004392 inode->i_flags |= S_DEAD;
4393out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004394 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004395out_up_write:
4396 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004397 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004398 spin_lock(&dest->root_item_lock);
4399 root_flags = btrfs_root_flags(&dest->root_item);
4400 btrfs_set_root_flags(&dest->root_item,
4401 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4402 spin_unlock(&dest->root_item_lock);
4403 } else {
4404 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004405 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004406 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004407 }
4408
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004409 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004410}
4411
Chris Mason39279cc2007-06-12 06:35:45 -04004412static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4413{
David Howells2b0143b2015-03-17 22:25:59 +00004414 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004415 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004418 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004419
David Sterbab3ae2442012-09-13 16:04:34 -06004420 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004421 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004422 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004423 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004424
Josef Bacikd52be812013-05-29 14:54:47 -04004425 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004426 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004427 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004428
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004429 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004430 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004431 goto out;
4432 }
4433
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004434 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004435 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004436 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004437
Filipe Manana44f714d2016-06-06 16:11:13 +01004438 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4439
Chris Mason39279cc2007-06-12 06:35:45 -04004440 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004441 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4442 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4443 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004444 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004445 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004446 /*
4447 * Propagate the last_unlink_trans value of the deleted dir to
4448 * its parent directory. This is to prevent an unrecoverable
4449 * log tree in the case we do something like this:
4450 * 1) create dir foo
4451 * 2) create snapshot under dir foo
4452 * 3) delete the snapshot
4453 * 4) rmdir foo
4454 * 5) mkdir foo
4455 * 6) fsync foo or some file inside foo
4456 */
4457 if (last_unlink_trans >= trans->transid)
4458 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4459 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004460out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004461 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004462 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004463
Chris Mason39279cc2007-06-12 06:35:45 -04004464 return err;
4465}
4466
Josef Bacikddfae632017-10-19 14:16:02 -04004467/*
4468 * Return this if we need to call truncate_block for the last bit of the
4469 * truncate.
4470 */
4471#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004472
Chris Mason323ac952008-10-01 19:05:46 -04004473/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004474 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004475 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004476 * @trans: A transaction handle.
4477 * @root: The root from which to remove items.
4478 * @inode: The inode whose items we want to remove.
4479 * @new_size: The new i_size for the inode. This is only applicable when
4480 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4481 * @min_type: The minimum key type to remove. All keys with a type
4482 * greater than this value are removed and all keys with
4483 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004484 * @extents_found: Output parameter that will contain the number of file
4485 * extent items that were removed or adjusted to the new
4486 * inode i_size. The caller is responsible for initializing
4487 * the counter. Also, it can be NULL if the caller does not
4488 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004489 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004490 * Remove all keys associated with the inode from the given root that have a key
4491 * with a type greater than or equals to @min_type. When @min_type has a value of
4492 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4493 * greater than or equals to @new_size. If a file extent item that starts before
4494 * @new_size and ends after it is found, its length is adjusted.
4495 *
4496 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4497 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004498 */
Yan, Zheng80825102009-11-12 09:35:36 +00004499int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4500 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004501 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004502 u64 new_size, u32 min_type,
4503 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004504{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004505 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004508 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004509 struct btrfs_key key;
4510 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004511 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004512 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004513 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004514 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004515 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004516 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004517 int found_extent;
4518 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004519 int pending_del_nr = 0;
4520 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004521 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004522 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004523 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004524 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004525 bool be_nice = false;
4526 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004527 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4528 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004529
4530 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004531
Chris Mason28ed1342014-12-17 09:41:04 -08004532 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004533 * For non-free space inodes and non-shareable roots, we want to back
4534 * off from time to time. This means all inodes in subvolume roots,
4535 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004536 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004537 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004538 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004539 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004540
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004541 path = btrfs_alloc_path();
4542 if (!path)
4543 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004544 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004545
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004546 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004547 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004548 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004549
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004550 /*
4551 * We want to drop from the next block forward in case this
4552 * new size is not block aligned since we will be keeping the
4553 * last block of the extent just the way it is.
4554 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004555 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004556 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004557 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004558 }
Yan, Zheng80825102009-11-12 09:35:36 +00004559
Miao Xie16cdcec2011-04-22 18:12:22 +08004560 /*
4561 * This function is also used to drop the items in the log tree before
4562 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004563 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004564 * items.
4565 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004566 if (min_type == 0 && root == inode->root)
4567 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004568
Li Zefan33345d012011-04-20 10:31:50 +08004569 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004570 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004571 key.type = (u8)-1;
4572
Chris Mason85e21ba2008-01-29 15:11:36 -05004573search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004574 /*
4575 * with a 16K leaf size and 128MB extents, you can actually queue
4576 * up a huge file in a single leaf. Most of the time that
4577 * bytes_deleted is > 0, it will be huge by the time we get here
4578 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004579 if (be_nice && bytes_deleted > SZ_32M &&
4580 btrfs_should_end_transaction(trans)) {
4581 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004582 goto out;
4583 }
Chris Masond3977122009-01-05 21:25:51 -05004584
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004585 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4586 if (ret < 0)
4587 goto out;
4588
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004590 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004591 /* there are no items in the tree for us to truncate, we're
4592 * done
4593 */
Yan, Zheng80825102009-11-12 09:35:36 +00004594 if (path->slots[0] == 0)
4595 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004596 path->slots[0]--;
4597 }
4598
Chris Masond3977122009-01-05 21:25:51 -05004599 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004600 u64 clear_start = 0, clear_len = 0;
4601
Chris Mason39279cc2007-06-12 06:35:45 -04004602 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004603 leaf = path->nodes[0];
4604 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004605 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004606
Li Zefan33345d012011-04-20 10:31:50 +08004607 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004608 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004609
Chris Mason85e21ba2008-01-29 15:11:36 -05004610 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004611 break;
4612
Chris Mason5f39d392007-10-15 16:14:19 -04004613 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004614 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004615 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004616 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004617 extent_type = btrfs_file_extent_type(leaf, fi);
4618 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004619 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004620 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004621
4622 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004623 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004624 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004625 item_end += btrfs_file_extent_ram_bytes(leaf,
4626 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004627
4628 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004629 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004630 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 }
Yan008630c2007-11-07 13:31:09 -05004632 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004633 }
Yan, Zheng80825102009-11-12 09:35:36 +00004634 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004635 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004636 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004637 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004638 break;
4639 if (found_key.offset >= new_size)
4640 del_item = 1;
4641 else
4642 del_item = 0;
4643 }
Chris Mason39279cc2007-06-12 06:35:45 -04004644 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004645 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004646 if (found_type != BTRFS_EXTENT_DATA_KEY)
4647 goto delete;
4648
Filipe Manana0d7d3162021-05-24 11:35:55 +01004649 if (extents_found != NULL)
4650 (*extents_found)++;
4651
Chris Mason179e29e2007-11-01 11:28:41 -04004652 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004653 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004654
4655 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004656 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004657 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004658 u64 orig_num_bytes =
4659 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004660 extent_num_bytes = ALIGN(new_size -
4661 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004662 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004663 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004664 btrfs_set_file_extent_num_bytes(leaf, fi,
4665 extent_num_bytes);
4666 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004667 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004668 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004669 &root->state) &&
4670 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004671 inode_sub_bytes(&inode->vfs_inode,
4672 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004673 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004674 } else {
Chris Masondb945352007-10-15 16:15:53 -04004675 extent_num_bytes =
4676 btrfs_file_extent_disk_num_bytes(leaf,
4677 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004678 extent_offset = found_key.offset -
4679 btrfs_file_extent_offset(leaf, fi);
4680
Chris Mason39279cc2007-06-12 06:35:45 -04004681 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004682 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004683 if (extent_start != 0) {
4684 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004685 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004686 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004687 inode_sub_bytes(&inode->vfs_inode,
4688 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004689 }
4690 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004691 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004692 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004693 /*
4694 * we can't truncate inline items that have had
4695 * special encodings
4696 */
4697 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004698 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004699 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4700 btrfs_file_extent_compression(leaf, fi) == 0) {
4701 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004702
Josef Bacikddfae632017-10-19 14:16:02 -04004703 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4704 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004705 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004706 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004707 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004708 * We have to bail so the last_size is set to
4709 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004710 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004711 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004712 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004713 } else {
4714 /*
4715 * Inline extents are special, we just treat
4716 * them as a full sector worth in the file
4717 * extent tree just for simplicity sake.
4718 */
4719 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004720 }
Josef Bacikddfae632017-10-19 14:16:02 -04004721
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004722 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004723 inode_sub_bytes(&inode->vfs_inode,
4724 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004725 }
Chris Mason179e29e2007-11-01 11:28:41 -04004726delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004727 /*
4728 * We use btrfs_truncate_inode_items() to clean up log trees for
4729 * multiple fsyncs, and in this case we don't want to clear the
4730 * file extent range because it's just the log.
4731 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004732 if (root == inode->root) {
4733 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004734 clear_start, clear_len);
4735 if (ret) {
4736 btrfs_abort_transaction(trans, ret);
4737 break;
4738 }
4739 }
4740
Josef Bacikddfae632017-10-19 14:16:02 -04004741 if (del_item)
4742 last_size = found_key.offset;
4743 else
4744 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004745 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004746 if (!pending_del_nr) {
4747 /* no pending yet, add ourselves */
4748 pending_del_slot = path->slots[0];
4749 pending_del_nr = 1;
4750 } else if (pending_del_nr &&
4751 path->slots[0] + 1 == pending_del_slot) {
4752 /* hop on the pending chunk */
4753 pending_del_nr++;
4754 pending_del_slot = path->slots[0];
4755 } else {
Chris Masond3977122009-01-05 21:25:51 -05004756 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004757 }
Chris Mason39279cc2007-06-12 06:35:45 -04004758 } else {
4759 break;
4760 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004761 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004762
Miao Xie27cdeb72014-04-02 19:51:05 +08004763 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004764 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004765 struct btrfs_ref ref = { 0 };
4766
Chris Mason28ed1342014-12-17 09:41:04 -08004767 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004768
4769 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4770 extent_start, extent_num_bytes, 0);
4771 ref.real_root = root->root_key.objectid;
4772 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4773 ino, extent_offset);
4774 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004775 if (ret) {
4776 btrfs_abort_transaction(trans, ret);
4777 break;
4778 }
Chris Mason28f75a02015-02-04 06:59:29 -08004779 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004780 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004781 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004782 }
Chris Mason39279cc2007-06-12 06:35:45 -04004783 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004784
Yan, Zheng80825102009-11-12 09:35:36 +00004785 if (found_type == BTRFS_INODE_ITEM_KEY)
4786 break;
4787
4788 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004789 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004790 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004791 if (pending_del_nr) {
4792 ret = btrfs_del_items(trans, root, path,
4793 pending_del_slot,
4794 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004796 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004797 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004798 }
Yan, Zheng80825102009-11-12 09:35:36 +00004799 pending_del_nr = 0;
4800 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004801 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004802
Chris Mason28f75a02015-02-04 06:59:29 -08004803 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004804 * We can generate a lot of delayed refs, so we need to
4805 * throttle every once and a while and make sure we're
4806 * adding enough space to keep up with the work we are
4807 * generating. Since we hold a transaction here we
4808 * can't flush, and we don't want to FLUSH_LIMIT because
4809 * we could have generated too many delayed refs to
4810 * actually allocate, so just bail if we're short and
4811 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004812 */
Josef Bacik28bad212018-12-03 10:20:38 -05004813 if (should_throttle) {
4814 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4815 BTRFS_RESERVE_NO_FLUSH);
4816 if (ret) {
4817 ret = -EAGAIN;
4818 break;
4819 }
Chris Mason28f75a02015-02-04 06:59:29 -08004820 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004821 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004822 } else {
4823 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004824 }
Chris Mason39279cc2007-06-12 06:35:45 -04004825 }
Yan, Zheng80825102009-11-12 09:35:36 +00004826out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004827 if (ret >= 0 && pending_del_nr) {
4828 int err;
4829
4830 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004831 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004832 if (err) {
4833 btrfs_abort_transaction(trans, err);
4834 ret = err;
4835 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004836 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004837 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4838 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004839 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004840 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004841 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4842 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4843 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004844 }
Chris Mason28ed1342014-12-17 09:41:04 -08004845
Chris Mason39279cc2007-06-12 06:35:45 -04004846 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004847 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004848}
4849
Chris Masona52d9a82007-08-27 16:49:44 -04004850/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004852 * @inode - inode that we're zeroing
4853 * @from - the offset to start zeroing
4854 * @len - the length to zero, 0 to zero the entire range respective to the
4855 * offset
4856 * @front - zero up to the offset instead of from the offset on
4857 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304858 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004859 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004860 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004861int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4862 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004863{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004864 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4865 struct address_space *mapping = inode->vfs_inode.i_mapping;
4866 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004868 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004869 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004870 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004872 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304873 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004874 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004875 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004876 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004877 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304878 u64 block_start;
4879 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004880
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004881 if (IS_ALIGNED(offset, blocksize) &&
4882 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004883 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304884
Josef Bacik8b62f872017-10-19 14:15:55 -04004885 block_start = round_down(from, blocksize);
4886 block_end = block_start + blocksize - 1;
4887
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004888 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4889 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004890 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004891 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004892 /* For nocow case, no need to reserve data space */
4893 only_release_metadata = true;
4894 } else {
4895 goto out;
4896 }
4897 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004898 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004899 if (ret < 0) {
4900 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004901 btrfs_free_reserved_data_space(inode, data_reserved,
4902 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004903 goto out;
4904 }
Chris Mason211c17f2008-05-15 09:13:45 -04004905again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004906 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004907 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004908 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4909 blocksize, true);
4910 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004911 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004912 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004913 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004914 ret = set_page_extent_mapped(page);
4915 if (ret < 0)
4916 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004917
Chris Masona52d9a82007-08-27 16:49:44 -04004918 if (!PageUptodate(page)) {
4919 ret = btrfs_readpage(NULL, page);
4920 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004921 if (page->mapping != mapping) {
4922 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004923 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004924 goto again;
4925 }
Chris Masona52d9a82007-08-27 16:49:44 -04004926 if (!PageUptodate(page)) {
4927 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004928 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004929 }
4930 }
Chris Mason211c17f2008-05-15 09:13:45 -04004931 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004932
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304933 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004934
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004935 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004936 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304937 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004938 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004939 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004940 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004941 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004942 btrfs_put_ordered_extent(ordered);
4943 goto again;
4944 }
4945
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004946 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004947 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4948 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004949
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004950 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004951 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004952 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304953 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004954 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004955 goto out_unlock;
4956 }
4957
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304958 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004959 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304960 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04004961 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07004962 memzero_page(page, (block_start - page_offset(page)),
4963 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004964 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07004965 memzero_page(page, (block_start - page_offset(page)) + offset,
4966 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004967 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004968 }
Chris Mason247e7432008-07-17 12:53:51 -04004969 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004970 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004971 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004972
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004973 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004974 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004975 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004976
Chris Mason89642222008-07-24 09:41:53 -04004977out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004978 if (ret) {
4979 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004980 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004981 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004982 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004983 block_start, blocksize, true);
4984 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004985 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004986 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004987 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004988out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004989 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004990 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004991 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004992 return ret;
4993}
4994
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004995static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004996 u64 offset, u64 len)
4997{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004998 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004999 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00005000 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04005001 int ret;
5002
5003 /*
5004 * Still need to make sure the inode looks like it's been updated so
5005 * that any holes get logged if we fsync.
5006 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005007 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005008 inode->last_trans = fs_info->generation;
5009 inode->last_sub_trans = root->log_transid;
5010 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04005011 return 0;
5012 }
5013
5014 /*
5015 * 1 - for the one we're dropping
5016 * 1 - for the one we're adding
5017 * 1 - for updating the inode.
5018 */
5019 trans = btrfs_start_transaction(root, 3);
5020 if (IS_ERR(trans))
5021 return PTR_ERR(trans);
5022
Filipe Manana5893dfb2020-11-04 11:07:32 +00005023 drop_args.start = offset;
5024 drop_args.end = offset + len;
5025 drop_args.drop_cache = true;
5026
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005027 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005028 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005029 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005030 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005031 return ret;
5032 }
5033
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005034 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005035 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005036 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005037 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005038 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005039 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5040 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005041 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005042 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005043 return ret;
5044}
5045
Josef Bacik695a0d02011-03-04 15:46:53 -05005046/*
5047 * This function puts in dummy file extents for the area we're creating a hole
5048 * for. So if we are truncating this file to a larger size we need to insert
5049 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5050 * the range between oldsize and size
5051 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005052int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005053{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005054 struct btrfs_root *root = inode->root;
5055 struct btrfs_fs_info *fs_info = root->fs_info;
5056 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005057 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005058 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005059 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005060 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5061 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005062 u64 last_byte;
5063 u64 cur_offset;
5064 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005065 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005066
Josef Bacika71754f2013-06-17 17:14:39 -04005067 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305068 * If our size started in the middle of a block we need to zero out the
5069 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005070 * expose stale data.
5071 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005072 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005073 if (err)
5074 return err;
5075
Yan Zheng9036c102008-10-30 14:19:41 -04005076 if (size <= hole_start)
5077 return 0;
5078
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005079 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5080 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005081 cur_offset = hole_start;
5082 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005083 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005084 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005085 if (IS_ERR(em)) {
5086 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005087 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005088 break;
5089 }
Yan Zheng9036c102008-10-30 14:19:41 -04005090 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005091 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005092 hole_size = last_byte - cur_offset;
5093
Yan, Zheng80825102009-11-12 09:35:36 +00005094 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005095 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005096
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005097 err = maybe_insert_hole(root, inode, cur_offset,
5098 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005099 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005100 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005101
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005102 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005103 cur_offset, hole_size);
5104 if (err)
5105 break;
5106
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005107 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005108 cur_offset + hole_size - 1, 0);
5109 hole_em = alloc_extent_map();
5110 if (!hole_em) {
5111 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005112 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005113 goto next;
5114 }
5115 hole_em->start = cur_offset;
5116 hole_em->len = hole_size;
5117 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005118
Josef Bacik5dc562c2012-08-17 13:14:17 -04005119 hole_em->block_start = EXTENT_MAP_HOLE;
5120 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005121 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005122 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005123 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005124 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005125
5126 while (1) {
5127 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005128 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005129 write_unlock(&em_tree->lock);
5130 if (err != -EEXIST)
5131 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005132 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005133 cur_offset +
5134 hole_size - 1, 0);
5135 }
5136 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005137 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005138 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005139 cur_offset, hole_size);
5140 if (err)
5141 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005142 }
Josef Bacik16e75492013-10-22 12:18:51 -04005143next:
Yan Zheng9036c102008-10-30 14:19:41 -04005144 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005145 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005146 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005147 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005148 break;
5149 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005150 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005151 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005152 return err;
5153}
5154
Eric Sandeen3972f262013-01-12 02:57:22 +00005155static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005156{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005157 struct btrfs_root *root = BTRFS_I(inode)->root;
5158 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005159 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005160 loff_t newsize = attr->ia_size;
5161 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005162 int ret;
5163
Eric Sandeen3972f262013-01-12 02:57:22 +00005164 /*
5165 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5166 * special case where we need to update the times despite not having
5167 * these flags set. For all other operations the VFS set these flags
5168 * explicitly if it wants a timestamp update.
5169 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005170 if (newsize != oldsize) {
5171 inode_inc_iversion(inode);
5172 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5173 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005174 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005175 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005176
Josef Bacika41ad392011-01-31 15:30:16 -05005177 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005178 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005179 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005180 * This is to ensure the snapshot captures a fully consistent
5181 * state of this file - if the snapshot captures this expanding
5182 * truncation, it must capture all writes that happened before
5183 * this truncation.
5184 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005185 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005186 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005187 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005188 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005189 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005190 }
Yan, Zheng80825102009-11-12 09:35:36 +00005191
Miao Xief4a2f4c2011-12-14 20:12:01 -05005192 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005193 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005194 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005195 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005196 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005197
5198 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005199 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305200 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005201 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005202 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005203 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005204 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5206
5207 if (btrfs_is_zoned(fs_info)) {
5208 ret = btrfs_wait_ordered_range(inode,
5209 ALIGN(newsize, fs_info->sectorsize),
5210 (u64)-1);
5211 if (ret)
5212 return ret;
5213 }
Yan, Zheng80825102009-11-12 09:35:36 +00005214
Josef Bacika41ad392011-01-31 15:30:16 -05005215 /*
5216 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005217 * zero. Make sure any new writes to the file get on disk
5218 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005219 */
5220 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005221 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005222 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005223
Josef Bacika41ad392011-01-31 15:30:16 -05005224 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005225
Miao Xie2e60a512013-02-08 07:01:08 +00005226 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005227
Filipe Manana213e8c52018-02-06 20:40:31 +00005228 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005229 if (ret && inode->i_nlink) {
5230 int err;
5231
5232 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005233 * Truncate failed, so fix up the in-memory size. We
5234 * adjusted disk_i_size down as we removed extents, so
5235 * wait for disk_i_size to be stable and then update the
5236 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005237 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005238 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005239 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005240 return err;
5241 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005242 }
Yan, Zheng80825102009-11-12 09:35:36 +00005243 }
5244
Josef Bacika41ad392011-01-31 15:30:16 -05005245 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005246}
5247
Christian Brauner549c7292021-01-21 14:19:43 +01005248static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5249 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
David Howells2b0143b2015-03-17 22:25:59 +00005251 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005252 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005253 int err;
5254
Li Zefanb83cc962010-12-20 16:04:08 +08005255 if (btrfs_root_readonly(root))
5256 return -EROFS;
5257
Christian Brauner2f221d62021-01-21 14:19:26 +01005258 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005259 if (err)
5260 return err;
5261
Chris Mason5a3f23d2009-03-31 13:27:11 -04005262 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005263 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005264 if (err)
5265 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005266 }
Yan Zheng9036c102008-10-30 14:19:41 -04005267
Christoph Hellwig10257742010-06-04 11:30:02 +02005268 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005269 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005270 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005271 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005272
Josef Bacik22c44fe2011-11-30 10:45:38 -05005273 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005274 err = posix_acl_chmod(&init_user_ns, inode,
5275 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005276 }
5277
Chris Mason39279cc2007-06-12 06:35:45 -04005278 return err;
5279}
Chris Mason61295eb2008-01-14 16:24:38 -05005280
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281/*
5282 * While truncating the inode pages during eviction, we get the VFS calling
5283 * btrfs_invalidatepage() against each page of the inode. This is slow because
5284 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5285 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5286 * extent_state structures over and over, wasting lots of time.
5287 *
5288 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5289 * those expensive operations on a per page basis and do only the ordered io
5290 * finishing, while we release here the extent_map and extent_state structures,
5291 * without the excessive merging and splitting.
5292 */
5293static void evict_inode_truncate_pages(struct inode *inode)
5294{
5295 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5296 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5297 struct rb_node *node;
5298
5299 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005300 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005301
5302 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005303 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005304 struct extent_map *em;
5305
Liu Bo07e1ce02018-08-23 03:51:52 +08005306 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005307 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005308 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5309 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005310 remove_extent_mapping(map_tree, em);
5311 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005312 if (need_resched()) {
5313 write_unlock(&map_tree->lock);
5314 cond_resched();
5315 write_lock(&map_tree->lock);
5316 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005317 }
5318 write_unlock(&map_tree->lock);
5319
Filipe Manana6ca07092015-05-26 00:55:42 +01005320 /*
5321 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005322 * We can have ongoing bios started by readahead that have
5323 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005324 * still in progress (unlocked the pages in the bio but did not yet
5325 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005326 * ranges can still be locked and eviction started because before
5327 * submitting those bios, which are executed by a separate task (work
5328 * queue kthread), inode references (inode->i_count) were not taken
5329 * (which would be dropped in the end io callback of each bio).
5330 * Therefore here we effectively end up waiting for those bios and
5331 * anyone else holding locked ranges without having bumped the inode's
5332 * reference count - if we don't do it, when they access the inode's
5333 * io_tree to unlock a range it may be too late, leading to an
5334 * use-after-free issue.
5335 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005336 spin_lock(&io_tree->lock);
5337 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5338 struct extent_state *state;
5339 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005340 u64 start;
5341 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005342 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005343
5344 node = rb_first(&io_tree->state);
5345 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005346 start = state->start;
5347 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005348 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005349 spin_unlock(&io_tree->lock);
5350
David Sterbaff13db42015-12-03 14:30:40 +01005351 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005352
5353 /*
5354 * If still has DELALLOC flag, the extent didn't reach disk,
5355 * and its reserved space won't be freed by delayed_ref.
5356 * So we need to free its reserved space here.
5357 * (Refer to comment in btrfs_invalidatepage, case 2)
5358 *
5359 * Note, end is the bytenr of last byte, so we need + 1 here.
5360 */
Filipe Manana421f0922018-10-12 13:02:48 +01005361 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005362 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5363 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005364
Filipe Manana6ca07092015-05-26 00:55:42 +01005365 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005366 EXTENT_LOCKED | EXTENT_DELALLOC |
5367 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5368 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005369
Filipe Manana7064dd52014-08-08 02:47:05 +01005370 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005371 spin_lock(&io_tree->lock);
5372 }
5373 spin_unlock(&io_tree->lock);
5374}
5375
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005376static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005377 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005378{
5379 struct btrfs_fs_info *fs_info = root->fs_info;
5380 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005381 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005382 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005383 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005384
Josef Bacikd3984c92019-08-01 18:19:37 -04005385 /*
5386 * Eviction should be taking place at some place safe because of our
5387 * delayed iputs. However the normal flushing code will run delayed
5388 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5389 *
5390 * We reserve the delayed_refs_extra here again because we can't use
5391 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5392 * above. We reserve our extra bit here because we generate a ton of
5393 * delayed refs activity by truncating.
5394 *
5395 * If we cannot make our reservation we'll attempt to steal from the
5396 * global reserve, because we really want to be able to free up space.
5397 */
5398 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5399 BTRFS_RESERVE_FLUSH_EVICT);
5400 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005401 /*
5402 * Try to steal from the global reserve if there is space for
5403 * it.
5404 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005405 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5406 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5407 btrfs_warn(fs_info,
5408 "could not allocate space for delete; will truncate on mount");
5409 return ERR_PTR(-ENOSPC);
5410 }
5411 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005412 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005413
5414 trans = btrfs_join_transaction(root);
5415 if (IS_ERR(trans))
5416 return trans;
5417
5418 if (delayed_refs_extra) {
5419 trans->block_rsv = &fs_info->trans_block_rsv;
5420 trans->bytes_reserved = delayed_refs_extra;
5421 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5422 delayed_refs_extra, 1);
5423 }
5424 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005425}
5426
Al Virobd555972010-06-07 11:35:40 -04005427void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005430 struct btrfs_trans_handle *trans;
5431 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005432 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005433 int ret;
5434
liubo1abe9b82011-03-24 11:18:59 +00005435 trace_btrfs_inode_evict(inode);
5436
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005437 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005438 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005439 return;
5440 }
5441
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005442 evict_inode_truncate_pages(inode);
5443
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005444 if (inode->i_nlink &&
5445 ((btrfs_root_refs(&root->root_item) != 0 &&
5446 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005447 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005448 goto no_delete;
5449
Omar Sandoval27919062018-05-11 13:13:37 -07005450 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005451 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005452
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005453 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005454
Omar Sandoval7b40b692018-05-11 13:13:33 -07005455 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005456 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005457
Yan, Zheng76dda932009-09-21 16:00:26 -04005458 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005459 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5460 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005461 goto no_delete;
5462 }
5463
Nikolay Borisovaa790212017-01-10 20:35:40 +02005464 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005465 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005466 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005467
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005468 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005469 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005470 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005471 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005472 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005473
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005474 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005475
Yan, Zheng80825102009-11-12 09:35:36 +00005476 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005477 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005478 if (IS_ERR(trans))
5479 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005480
5481 trans->block_rsv = rsv;
5482
Nikolay Borisov50743392020-11-02 16:48:55 +02005483 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005484 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005485 trans->block_rsv = &fs_info->trans_block_rsv;
5486 btrfs_end_transaction(trans);
5487 btrfs_btree_balance_dirty(fs_info);
5488 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5489 goto free_rsv;
5490 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005491 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005492 }
5493
Josef Bacik4ef31a42013-08-13 14:10:08 -04005494 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005495 * Errors here aren't a big deal, it just means we leave orphan items in
5496 * the tree. They will be cleaned up on the next mount. If the inode
5497 * number gets reused, cleanup deletes the orphan item without doing
5498 * anything, and unlink reuses the existing orphan item.
5499 *
5500 * If it turns out that we are dropping too many of these, we might want
5501 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005502 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005503 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005504 if (!IS_ERR(trans)) {
5505 trans->block_rsv = rsv;
5506 btrfs_orphan_del(trans, BTRFS_I(inode));
5507 trans->block_rsv = &fs_info->trans_block_rsv;
5508 btrfs_end_transaction(trans);
5509 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005510
Omar Sandoval27919062018-05-11 13:13:37 -07005511free_rsv:
5512 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005513no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005514 /*
5515 * If we didn't successfully delete, the orphan item will still be in
5516 * the tree and we'll retry on the next mount. Again, we might also want
5517 * to retry these periodically in the future.
5518 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005519 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005520 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005521}
5522
5523/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005524 * Return the key found in the dir entry in the location pointer, fill @type
5525 * with BTRFS_FT_*, and return 0.
5526 *
Su Yue005d6712018-03-05 17:13:37 +08005527 * If no dir entries were found, returns -ENOENT.
5528 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005529 */
5530static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005531 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005532{
5533 const char *name = dentry->d_name.name;
5534 int namelen = dentry->d_name.len;
5535 struct btrfs_dir_item *di;
5536 struct btrfs_path *path;
5537 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005538 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005539
5540 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005541 if (!path)
5542 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005543
David Sterbaf85b7372017-01-20 14:54:07 +01005544 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5545 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005546 if (IS_ERR_OR_NULL(di)) {
5547 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005548 goto out;
5549 }
Chris Masond3977122009-01-05 21:25:51 -05005550
Chris Mason5f39d392007-10-15 16:14:19 -04005551 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005552 if (location->type != BTRFS_INODE_ITEM_KEY &&
5553 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005554 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005555 btrfs_warn(root->fs_info,
5556"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5557 __func__, name, btrfs_ino(BTRFS_I(dir)),
5558 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005559 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005560 if (!ret)
5561 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005562out:
Chris Mason39279cc2007-06-12 06:35:45 -04005563 btrfs_free_path(path);
5564 return ret;
5565}
5566
5567/*
5568 * when we hit a tree root in a directory, the btrfs part of the inode
5569 * needs to be changed to reflect the root directory of the tree root. This
5570 * is kind of like crossing a mount point.
5571 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005572static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005573 struct inode *dir,
5574 struct dentry *dentry,
5575 struct btrfs_key *location,
5576 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005577{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005578 struct btrfs_path *path;
5579 struct btrfs_root *new_root;
5580 struct btrfs_root_ref *ref;
5581 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005582 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583 int ret;
5584 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005585
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005586 path = btrfs_alloc_path();
5587 if (!path) {
5588 err = -ENOMEM;
5589 goto out;
5590 }
Chris Mason39279cc2007-06-12 06:35:45 -04005591
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005592 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005593 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5594 key.type = BTRFS_ROOT_REF_KEY;
5595 key.offset = location->objectid;
5596
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005597 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005598 if (ret) {
5599 if (ret < 0)
5600 err = ret;
5601 goto out;
5602 }
Chris Mason39279cc2007-06-12 06:35:45 -04005603
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005604 leaf = path->nodes[0];
5605 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005606 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005607 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5608 goto out;
5609
5610 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5611 (unsigned long)(ref + 1),
5612 dentry->d_name.len);
5613 if (ret)
5614 goto out;
5615
David Sterbab3b4aa72011-04-21 01:20:15 +02005616 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005617
David Sterba56e93572020-05-15 19:35:55 +02005618 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 if (IS_ERR(new_root)) {
5620 err = PTR_ERR(new_root);
5621 goto out;
5622 }
5623
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005624 *sub_root = new_root;
5625 location->objectid = btrfs_root_dirid(&new_root->root_item);
5626 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005627 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005628 err = 0;
5629out:
5630 btrfs_free_path(path);
5631 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005632}
5633
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634static void inode_tree_add(struct inode *inode)
5635{
5636 struct btrfs_root *root = BTRFS_I(inode)->root;
5637 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005638 struct rb_node **p;
5639 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005640 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005641 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642
Al Viro1d3382cb2010-10-23 15:19:20 -04005643 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005645 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005647 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005648 while (*p) {
5649 parent = *p;
5650 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5651
David Sterba37508512018-06-29 10:56:40 +02005652 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005653 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005654 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005655 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005656 else {
5657 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005658 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005659 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005660 RB_CLEAR_NODE(parent);
5661 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005662 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005663 }
5664 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005665 rb_link_node(new, parent, p);
5666 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667 spin_unlock(&root->inode_lock);
5668}
5669
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005670static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005671{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005672 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005673 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005674
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005675 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005676 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5677 rb_erase(&inode->rb_node, &root->inode_tree);
5678 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005679 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005681 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005682
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005683 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005684 spin_lock(&root->inode_lock);
5685 empty = RB_EMPTY_ROOT(&root->inode_tree);
5686 spin_unlock(&root->inode_lock);
5687 if (empty)
5688 btrfs_add_dead_root(root);
5689 }
5690}
5691
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005692
Chris Masone02119d2008-09-05 16:13:11 -04005693static int btrfs_init_locked_inode(struct inode *inode, void *p)
5694{
5695 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005696
5697 inode->i_ino = args->ino;
5698 BTRFS_I(inode)->location.objectid = args->ino;
5699 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5700 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005701 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5702 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005703 return 0;
5704}
5705
5706static int btrfs_find_actor(struct inode *inode, void *opaque)
5707{
5708 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005709
5710 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005711 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005712}
5713
David Sterba0202e832020-05-15 19:35:59 +02005714static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005716{
5717 struct inode *inode;
5718 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005719 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005720
David Sterba0202e832020-05-15 19:35:59 +02005721 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005722 args.root = root;
5723
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005724 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005725 btrfs_init_locked_inode,
5726 (void *)&args);
5727 return inode;
5728}
5729
David Sterba4c66e0d2019-10-03 19:09:35 +02005730/*
David Sterba0202e832020-05-15 19:35:59 +02005731 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005732 * Path can be preallocated to prevent recursing back to iget through
5733 * allocator. NULL is also valid but may require an additional allocation
5734 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305735 */
David Sterba0202e832020-05-15 19:35:59 +02005736struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005737 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305738{
5739 struct inode *inode;
5740
David Sterba0202e832020-05-15 19:35:59 +02005741 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305742 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005743 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305744
5745 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005746 int ret;
5747
Filipe Manana4222ea72018-10-24 10:13:03 +01005748 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005749 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005750 inode_tree_add(inode);
5751 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005752 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005753 iget_failed(inode);
5754 /*
5755 * ret > 0 can come from btrfs_search_slot called by
5756 * btrfs_read_locked_inode, this means the inode item
5757 * was not found.
5758 */
5759 if (ret > 0)
5760 ret = -ENOENT;
5761 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005762 }
5763 }
5764
Balaji Rao1a54ef82008-07-21 02:01:04 +05305765 return inode;
5766}
5767
David Sterba0202e832020-05-15 19:35:59 +02005768struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005769{
David Sterba0202e832020-05-15 19:35:59 +02005770 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005771}
5772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773static struct inode *new_simple_dir(struct super_block *s,
5774 struct btrfs_key *key,
5775 struct btrfs_root *root)
5776{
5777 struct inode *inode = new_inode(s);
5778
5779 if (!inode)
5780 return ERR_PTR(-ENOMEM);
5781
Josef Bacik5c8fd992020-02-14 16:11:43 -05005782 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005783 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005784 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005785
5786 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005787 /*
5788 * We only need lookup, the rest is read-only and there's no inode
5789 * associated with the dentry
5790 */
5791 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005792 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793 inode->i_fop = &simple_dir_operations;
5794 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005795 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305796 inode->i_atime = inode->i_mtime;
5797 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005798 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799
5800 return inode;
5801}
5802
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005803static inline u8 btrfs_inode_type(struct inode *inode)
5804{
5805 /*
5806 * Compile-time asserts that generic FT_* types still match
5807 * BTRFS_FT_* types
5808 */
5809 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5810 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5811 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5812 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5813 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5814 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5815 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5816 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5817
5818 return fs_umode_to_ftype(inode->i_mode);
5819}
5820
Chris Mason3de45862008-11-17 21:02:50 -05005821struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005824 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005825 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005826 struct btrfs_root *sub_root = root;
5827 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005828 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005829 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005830
5831 if (dentry->d_name.len > BTRFS_NAME_LEN)
5832 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005833
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005834 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005835 if (ret < 0)
5836 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005837
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005838 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005839 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005840 if (IS_ERR(inode))
5841 return inode;
5842
5843 /* Do extra check against inode mode with di_type */
5844 if (btrfs_inode_type(inode) != di_type) {
5845 btrfs_crit(fs_info,
5846"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5847 inode->i_mode, btrfs_inode_type(inode),
5848 di_type);
5849 iput(inode);
5850 return ERR_PTR(-EUCLEAN);
5851 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005852 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005854
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005855 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005856 &location, &sub_root);
5857 if (ret < 0) {
5858 if (ret != -ENOENT)
5859 inode = ERR_PTR(ret);
5860 else
5861 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5862 } else {
David Sterba0202e832020-05-15 19:35:59 +02005863 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005864 }
Josef Bacik87270022020-01-24 09:32:31 -05005865 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005866 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005867
Julia Lawall34d19ba2011-01-24 19:55:19 +00005868 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005869 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005870 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005871 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005872 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005873 if (ret) {
5874 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005875 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005876 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005877 }
5878
Chris Mason3de45862008-11-17 21:02:50 -05005879 return inode;
5880}
5881
Nick Pigginfe15ce42011-01-07 17:49:23 +11005882static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005883{
5884 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005885 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005886
Li Zefan848cce02012-02-21 17:04:28 +08005887 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005888 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005889
Li Zefan848cce02012-02-21 17:04:28 +08005890 if (inode) {
5891 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005892 if (btrfs_root_refs(&root->root_item) == 0)
5893 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005894
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005895 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005896 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005897 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005898 return 0;
5899}
5900
Chris Mason3de45862008-11-17 21:02:50 -05005901static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005902 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005903{
Al Viro3837d202018-10-10 16:38:27 -04005904 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005905
Al Viro3837d202018-10-10 16:38:27 -04005906 if (inode == ERR_PTR(-ENOENT))
5907 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005908 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005909}
5910
Josef Bacik23b5ec72017-07-24 15:14:25 -04005911/*
5912 * All this infrastructure exists because dir_emit can fault, and we are holding
5913 * the tree lock when doing readdir. For now just allocate a buffer and copy
5914 * our information into that, and then dir_emit from the buffer. This is
5915 * similar to what NFS does, only we don't keep the buffer around in pagecache
5916 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5917 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5918 * tree lock.
5919 */
5920static int btrfs_opendir(struct inode *inode, struct file *file)
5921{
5922 struct btrfs_file_private *private;
5923
5924 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5925 if (!private)
5926 return -ENOMEM;
5927 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5928 if (!private->filldir_buf) {
5929 kfree(private);
5930 return -ENOMEM;
5931 }
5932 file->private_data = private;
5933 return 0;
5934}
5935
5936struct dir_entry {
5937 u64 ino;
5938 u64 offset;
5939 unsigned type;
5940 int name_len;
5941};
5942
5943static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5944{
5945 while (entries--) {
5946 struct dir_entry *entry = addr;
5947 char *name = (char *)(entry + 1);
5948
David Sterba92d32172018-04-16 21:10:14 +02005949 ctx->pos = get_unaligned(&entry->offset);
5950 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5951 get_unaligned(&entry->ino),
5952 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005953 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005954 addr += sizeof(struct dir_entry) +
5955 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005956 ctx->pos++;
5957 }
5958 return 0;
5959}
5960
Al Viro9cdda8d2013-05-22 16:48:09 -04005961static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005962{
Al Viro9cdda8d2013-05-22 16:48:09 -04005963 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005966 struct btrfs_dir_item *di;
5967 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005968 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005969 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005971 struct list_head ins_list;
5972 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005974 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005976 char *name_ptr;
5977 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978 int entries = 0;
5979 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005980 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005982
Al Viro9cdda8d2013-05-22 16:48:09 -04005983 if (!dir_emit_dots(file, ctx))
5984 return 0;
5985
David Woodhouse49593bf2008-08-17 17:08:36 +01005986 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005987 if (!path)
5988 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005989
Josef Bacik23b5ec72017-07-24 15:14:25 -04005990 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005991 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005992
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005993 INIT_LIST_HEAD(&ins_list);
5994 INIT_LIST_HEAD(&del_list);
5995 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005996
Josef Bacik23b5ec72017-07-24 15:14:25 -04005997again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005998 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005999 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006000 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006001
Chris Mason39279cc2007-06-12 06:35:45 -04006002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6003 if (ret < 0)
6004 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006005
6006 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006007 struct dir_entry *entry;
6008
Chris Mason5f39d392007-10-15 16:14:19 -04006009 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006010 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006011 if (slot >= btrfs_header_nritems(leaf)) {
6012 ret = btrfs_next_leaf(root, path);
6013 if (ret < 0)
6014 goto err;
6015 else if (ret > 0)
6016 break;
6017 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006018 }
Chris Mason3de45862008-11-17 21:02:50 -05006019
Chris Mason5f39d392007-10-15 16:14:19 -04006020 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6021
6022 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006023 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006024 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006025 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006026 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006027 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006028 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006029 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006031 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006032 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6033 PAGE_SIZE) {
6034 btrfs_release_path(path);
6035 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6036 if (ret)
6037 goto nopos;
6038 addr = private->filldir_buf;
6039 entries = 0;
6040 total_len = 0;
6041 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006042 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006043
6044 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006045 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006046 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006047 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6048 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006049 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006050 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006051 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006052 put_unaligned(location.objectid, &entry->ino);
6053 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006054 entries++;
6055 addr += sizeof(struct dir_entry) + name_len;
6056 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006057next:
6058 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006059 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006060 btrfs_release_path(path);
6061
6062 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6063 if (ret)
6064 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006065
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006066 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006067 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006068 goto nopos;
6069
Zach Browndb62efb2013-07-11 16:19:42 -07006070 /*
6071 * Stop new entries from being returned after we return the last
6072 * entry.
6073 *
6074 * New directory entries are assigned a strictly increasing
6075 * offset. This means that new entries created during readdir
6076 * are *guaranteed* to be seen in the future by that readdir.
6077 * This has broken buggy programs which operate on names as
6078 * they're returned by readdir. Until we re-use freed offsets
6079 * we have this hack to stop new entries from being returned
6080 * under the assumption that they'll never reach this huge
6081 * offset.
6082 *
6083 * This is being careful not to overflow 32bit loff_t unless the
6084 * last entry requires it because doing so has broken 32bit apps
6085 * in the past.
6086 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006087 if (ctx->pos >= INT_MAX)
6088 ctx->pos = LLONG_MAX;
6089 else
6090 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006091nopos:
6092 ret = 0;
6093err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006094 if (put)
6095 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006096 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006097 return ret;
6098}
6099
Chris Mason39279cc2007-06-12 06:35:45 -04006100/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006101 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006102 * inode changes. But, it is most likely to find the inode in cache.
6103 * FIXME, needs more benchmarking...there are no reasons other than performance
6104 * to keep or drop this code.
6105 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006106static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006107{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006108 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006109 struct btrfs_root *root = BTRFS_I(inode)->root;
6110 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006111 int ret;
6112
Josef Bacik72ac3c02012-05-23 14:13:11 -04006113 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006114 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006115
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006116 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006117 if (IS_ERR(trans))
6118 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006119
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006120 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006121 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006122 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006123 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006124 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125 if (IS_ERR(trans))
6126 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006127
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006128 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006129 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006130 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006131 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006132 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006133
6134 return ret;
6135}
6136
6137/*
6138 * This is a copy of file_update_time. We need this so we can return error on
6139 * ENOSPC for updating the inode in the case of file write and mmap writes.
6140 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006141static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006142 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006143{
Alexander Block2bc5565282012-06-15 09:49:33 +02006144 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006145 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006146
6147 if (btrfs_root_readonly(root))
6148 return -EROFS;
6149
Josef Bacike41f9412012-03-26 09:46:47 -04006150 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006151 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006152 if (flags & S_CTIME)
6153 inode->i_ctime = *now;
6154 if (flags & S_MTIME)
6155 inode->i_mtime = *now;
6156 if (flags & S_ATIME)
6157 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006158 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006159}
6160
Chris Masond352ac62008-09-29 15:18:18 -04006161/*
6162 * find the highest existing sequence number in a directory
6163 * and then set the in-memory index_cnt variable to reflect
6164 * free sequence numbers
6165 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006167{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006168 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006169 struct btrfs_key key, found_key;
6170 struct btrfs_path *path;
6171 struct extent_buffer *leaf;
6172 int ret;
6173
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006174 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006175 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006176 key.offset = (u64)-1;
6177
6178 path = btrfs_alloc_path();
6179 if (!path)
6180 return -ENOMEM;
6181
6182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6183 if (ret < 0)
6184 goto out;
6185 /* FIXME: we should be able to handle this */
6186 if (ret == 0)
6187 goto out;
6188 ret = 0;
6189
6190 /*
6191 * MAGIC NUMBER EXPLANATION:
6192 * since we search a directory based on f_pos we have to start at 2
6193 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6194 * else has to start at 2
6195 */
6196 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006197 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006198 goto out;
6199 }
6200
6201 path->slots[0]--;
6202
6203 leaf = path->nodes[0];
6204 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6205
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006206 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006207 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006208 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006209 goto out;
6210 }
6211
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006212 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006213out:
6214 btrfs_free_path(path);
6215 return ret;
6216}
6217
Chris Masond352ac62008-09-29 15:18:18 -04006218/*
6219 * helper to find a free sequence number in a given directory. This current
6220 * code is very simple, later versions will do smarter things in the btree
6221 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006222int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006223{
6224 int ret = 0;
6225
Nikolay Borisov877574e2017-02-20 13:50:33 +02006226 if (dir->index_cnt == (u64)-1) {
6227 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006228 if (ret) {
6229 ret = btrfs_set_inode_index_count(dir);
6230 if (ret)
6231 return ret;
6232 }
Josef Bacikaec74772008-07-24 12:12:38 -04006233 }
6234
Nikolay Borisov877574e2017-02-20 13:50:33 +02006235 *index = dir->index_cnt;
6236 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006237
6238 return ret;
6239}
6240
Chris Masonb0d5d102014-09-08 13:08:51 -07006241static int btrfs_insert_inode_locked(struct inode *inode)
6242{
6243 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006244
6245 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006246 args.root = BTRFS_I(inode)->root;
6247
6248 return insert_inode_locked4(inode,
6249 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6250 btrfs_find_actor, &args);
6251}
6252
Anand Jain19aee8d2017-07-18 17:37:05 +08006253/*
6254 * Inherit flags from the parent inode.
6255 *
6256 * Currently only the compression flags and the cow flags are inherited.
6257 */
6258static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6259{
6260 unsigned int flags;
6261
6262 if (!dir)
6263 return;
6264
6265 flags = BTRFS_I(dir)->flags;
6266
6267 if (flags & BTRFS_INODE_NOCOMPRESS) {
6268 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6269 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6270 } else if (flags & BTRFS_INODE_COMPRESS) {
6271 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6273 }
6274
6275 if (flags & BTRFS_INODE_NODATACOW) {
6276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6277 if (S_ISREG(inode->i_mode))
6278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6279 }
6280
David Sterba7b6a2212018-03-26 18:40:21 +02006281 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006282}
6283
Chris Mason39279cc2007-06-12 06:35:45 -04006284static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6285 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006286 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006287 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006288 u64 ref_objectid, u64 objectid,
6289 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006292 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006293 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006294 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006295 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006296 struct btrfs_inode_ref *ref;
6297 struct btrfs_key key[2];
6298 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006300 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006301 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006303
Chris Mason5f39d392007-10-15 16:14:19 -04006304 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006305 if (!path)
6306 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006307
Josef Bacik11a19a92019-09-09 10:12:04 -04006308 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006309 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006310 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006311 if (!inode) {
6312 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006313 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006314 }
Chris Mason39279cc2007-06-12 06:35:45 -04006315
Li Zefan581bb052011-04-20 10:06:11 +08006316 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006317 * O_TMPFILE, set link count to 0, so that after this point,
6318 * we fill in an inode item with the correct link count.
6319 */
6320 if (!name)
6321 set_nlink(inode, 0);
6322
6323 /*
Li Zefan581bb052011-04-20 10:06:11 +08006324 * we have to initialize this early, so we can reclaim the inode
6325 * number if we fail afterwards in this function.
6326 */
6327 inode->i_ino = objectid;
6328
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006329 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006330 trace_btrfs_inode_request(dir);
6331
Nikolay Borisov877574e2017-02-20 13:50:33 +02006332 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006333 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006334 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006335 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006336 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006337 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006338 } else if (dir) {
6339 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006340 }
6341 /*
6342 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006343 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006344 * number
6345 */
6346 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006347 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006348 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006349 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006350 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006351
Josef Bacik5dc562c2012-08-17 13:14:17 -04006352 /*
6353 * We could have gotten an inode number from somebody who was fsynced
6354 * and then removed in this same transaction, so let's just set full
6355 * sync since it will be a full sync anyway and this will blow away the
6356 * old info in the log.
6357 */
6358 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6359
Chris Mason9c583092008-01-29 15:15:18 -05006360 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006361 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006362 key[0].offset = 0;
6363
Chris Mason9c583092008-01-29 15:15:18 -05006364 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006365
6366 if (name) {
6367 /*
6368 * Start new inodes with an inode_ref. This is slightly more
6369 * efficient for small numbers of hard links since they will
6370 * be packed into one item. Extended refs will kick in if we
6371 * add more hard links than can fit in the ref item.
6372 */
6373 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006374 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006375 key[1].offset = ref_objectid;
6376
6377 sizes[1] = name_len + sizeof(*ref);
6378 }
Chris Mason9c583092008-01-29 15:15:18 -05006379
Chris Masonb0d5d102014-09-08 13:08:51 -07006380 location = &BTRFS_I(inode)->location;
6381 location->objectid = objectid;
6382 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006383 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006384
6385 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006386 if (ret < 0) {
6387 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006388 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006389 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006390
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006391 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006392 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006393 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006394
Christian Brauner21cb47b2021-01-21 14:19:25 +01006395 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006396 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306397
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006398 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306399 inode->i_atime = inode->i_mtime;
6400 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006401 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306402
Chris Mason5f39d392007-10-15 16:14:19 -04006403 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6404 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006405 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006406 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006407 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006408
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006409 if (name) {
6410 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6411 struct btrfs_inode_ref);
6412 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6413 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6414 ptr = (unsigned long)(ref + 1);
6415 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6416 }
Chris Mason9c583092008-01-29 15:15:18 -05006417
Chris Mason5f39d392007-10-15 16:14:19 -04006418 btrfs_mark_buffer_dirty(path->nodes[0]);
6419 btrfs_free_path(path);
6420
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006421 btrfs_inherit_iflags(inode, dir);
6422
Al Viro569254b2011-07-24 17:08:40 -04006423 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006424 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006425 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006426 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006427 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6428 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006429 }
6430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006431 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006432
6433 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006434 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006435
Alexander Block8ea05e32012-07-25 17:35:53 +02006436 btrfs_update_root_times(trans, root);
6437
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006438 ret = btrfs_inode_inherit_props(trans, inode, dir);
6439 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006440 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006441 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006442 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006443
Chris Mason39279cc2007-06-12 06:35:45 -04006444 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006445
6446fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006447 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006448fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006449 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006450 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006451 btrfs_free_path(path);
6452 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006453}
6454
Chris Masond352ac62008-09-29 15:18:18 -04006455/*
6456 * utility function to add 'inode' into 'parent_inode' with
6457 * a give name and a given sequence number.
6458 * if 'add_backref' is true, also insert a backref from the
6459 * inode to the parent directory.
6460 */
Chris Masone02119d2008-09-05 16:13:11 -04006461int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006462 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006463 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006464{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006465 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006466 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006467 struct btrfs_root *root = parent_inode->root;
6468 u64 ino = btrfs_ino(inode);
6469 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006470
Li Zefan33345d012011-04-20 10:31:50 +08006471 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006472 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006473 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006474 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006475 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006476 key.offset = 0;
6477 }
Chris Mason39279cc2007-06-12 06:35:45 -04006478
Li Zefan33345d012011-04-20 10:31:50 +08006479 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006480 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006481 root->root_key.objectid, parent_ino,
6482 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006483 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006484 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6485 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006486 }
6487
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006488 /* Nothing to clean up yet */
6489 if (ret)
6490 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006491
Lu Fengqi684572d2018-08-04 21:10:57 +08006492 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006493 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006494 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006495 goto fail_dir_item;
6496 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006499 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006502 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006503 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006504 /*
6505 * If we are replaying a log tree, we do not want to update the mtime
6506 * and ctime of the parent directory with the current time, since the
6507 * log replay procedure is responsible for setting them to their correct
6508 * values (the ones it had when the fsync was done).
6509 */
6510 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6511 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6512
6513 parent_inode->vfs_inode.i_mtime = now;
6514 parent_inode->vfs_inode.i_ctime = now;
6515 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006516 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006517 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006518 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006519 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006520
6521fail_dir_item:
6522 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6523 u64 local_index;
6524 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006525 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006526 root->root_key.objectid, parent_ino,
6527 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006528 if (err)
6529 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006530 } else if (add_backref) {
6531 u64 local_index;
6532 int err;
6533
6534 err = btrfs_del_inode_ref(trans, root, name, name_len,
6535 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006536 if (err)
6537 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006538 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006539
6540 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006541 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006542}
6543
6544static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006545 struct btrfs_inode *dir, struct dentry *dentry,
6546 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006547{
Josef Bacika1b075d2010-11-19 20:36:11 +00006548 int err = btrfs_add_link(trans, dir, inode,
6549 dentry->d_name.name, dentry->d_name.len,
6550 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006551 if (err > 0)
6552 err = -EEXIST;
6553 return err;
6554}
6555
Christian Brauner549c7292021-01-21 14:19:43 +01006556static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6557 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006558{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006559 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006560 struct btrfs_trans_handle *trans;
6561 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006562 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006563 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006564 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006565 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006566
Josef Bacik9ed74f22009-09-11 16:12:44 -04006567 /*
6568 * 2 for inode item and ref
6569 * 2 for dir items
6570 * 1 for xattr if selinux is on
6571 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006572 trans = btrfs_start_transaction(root, 5);
6573 if (IS_ERR(trans))
6574 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006575
Nikolay Borisov543068a2020-12-07 17:32:33 +02006576 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006577 if (err)
6578 goto out_unlock;
6579
Josef Bacikaec74772008-07-24 12:12:38 -04006580 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006581 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6582 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006583 if (IS_ERR(inode)) {
6584 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006585 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006586 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006587 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006588
Casey Schauflerad19db72011-12-15 10:09:07 -05006589 /*
6590 * If the active LSM wants to access the inode during
6591 * d_instantiate it needs these. Smack checks to see
6592 * if the filesystem supports xattrs by looking at the
6593 * ops vector.
6594 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006595 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006596 init_special_inode(inode, inode->i_mode, rdev);
6597
6598 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006599 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006600 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006601
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006602 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6603 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006604 if (err)
6605 goto out_unlock;
6606
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006607 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006608 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006609
Josef Bacik618e21d2007-07-11 10:18:17 -04006610out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006611 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006612 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006613 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006614 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006615 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006616 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006617 return err;
6618}
6619
Christian Brauner549c7292021-01-21 14:19:43 +01006620static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6621 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006622{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006623 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 struct btrfs_trans_handle *trans;
6625 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006626 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006627 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006628 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006629 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006630
Josef Bacik9ed74f22009-09-11 16:12:44 -04006631 /*
6632 * 2 for inode item and ref
6633 * 2 for dir items
6634 * 1 for xattr if selinux is on
6635 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006636 trans = btrfs_start_transaction(root, 5);
6637 if (IS_ERR(trans))
6638 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006639
Nikolay Borisov543068a2020-12-07 17:32:33 +02006640 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006641 if (err)
6642 goto out_unlock;
6643
Josef Bacikaec74772008-07-24 12:12:38 -04006644 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006645 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6646 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006647 if (IS_ERR(inode)) {
6648 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006649 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006650 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006651 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006652 /*
6653 * If the active LSM wants to access the inode during
6654 * d_instantiate it needs these. Smack checks to see
6655 * if the filesystem supports xattrs by looking at the
6656 * ops vector.
6657 */
6658 inode->i_fop = &btrfs_file_operations;
6659 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006660 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006661
6662 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6663 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006664 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006665
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006666 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006668 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006669
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006670 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6671 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006672 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006673 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006674
Al Viro1e2e5472018-05-04 08:23:01 -04006675 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006676
Chris Mason39279cc2007-06-12 06:35:45 -04006677out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006678 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006679 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006680 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006681 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006682 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006683 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 return err;
6685}
6686
6687static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6688 struct dentry *dentry)
6689{
Filipe Manana271dba452016-01-05 16:24:05 +00006690 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006691 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006692 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006694 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006695 int err;
6696 int drop_inode = 0;
6697
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006698 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006699 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006700 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006701
Mark Fashehf1863732012-08-08 11:32:27 -07006702 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006703 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006704
Nikolay Borisov877574e2017-02-20 13:50:33 +02006705 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006706 if (err)
6707 goto fail;
6708
Yan, Zhenga22285a2010-05-16 10:48:46 -04006709 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006710 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006711 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006712 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006713 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006715 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 if (IS_ERR(trans)) {
6717 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006718 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006719 goto fail;
6720 }
Chris Mason5f39d392007-10-15 16:14:19 -04006721
Miao Xie67de1172013-12-26 13:07:06 +08006722 /* There are several dir indexes for this inode, clear the cache. */
6723 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006724 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006725 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006726 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006727 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006728 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006729
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006730 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6731 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006736 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006737
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006738 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006739 if (err)
6740 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006741 if (inode->i_nlink == 1) {
6742 /*
6743 * If new hard link count is 1, it's a file created
6744 * with open(2) O_TMPFILE flag.
6745 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006746 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006747 if (err)
6748 goto fail;
6749 }
Al Viro08c422c2011-12-23 07:58:13 -05006750 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006751 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006752 }
Chris Mason39279cc2007-06-12 06:35:45 -04006753
Chris Mason1832a6d2007-12-21 16:27:21 -05006754fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006755 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006756 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006757 if (drop_inode) {
6758 inode_dec_link_count(inode);
6759 iput(inode);
6760 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006761 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006762 return err;
6763}
6764
Christian Brauner549c7292021-01-21 14:19:43 +01006765static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6766 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006767{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006768 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006769 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006770 struct btrfs_trans_handle *trans;
6771 struct btrfs_root *root = BTRFS_I(dir)->root;
6772 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006773 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006774 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006775
Josef Bacik9ed74f22009-09-11 16:12:44 -04006776 /*
6777 * 2 items for inode and ref
6778 * 2 items for dir items
6779 * 1 for xattr if selinux is on
6780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006781 trans = btrfs_start_transaction(root, 5);
6782 if (IS_ERR(trans))
6783 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006784
Nikolay Borisov543068a2020-12-07 17:32:33 +02006785 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006786 if (err)
6787 goto out_fail;
6788
Josef Bacikaec74772008-07-24 12:12:38 -04006789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006790 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6791 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 if (IS_ERR(inode)) {
6793 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006794 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006795 goto out_fail;
6796 }
Chris Mason5f39d392007-10-15 16:14:19 -04006797
Chris Masonb0d5d102014-09-08 13:08:51 -07006798 /* these must be set before we unlock the inode */
6799 inode->i_op = &btrfs_dir_inode_operations;
6800 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006801
Eric Paris2a7dba32011-02-01 11:05:39 -05006802 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006803 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006804 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006805
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006806 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006807 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006808 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006809 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006810
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006811 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6812 dentry->d_name.name,
6813 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006814 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006815 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006816
Al Viro1e2e5472018-05-04 08:23:01 -04006817 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006818
6819out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006820 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006821 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006822 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006823 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006824 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006825 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 return err;
6827}
6828
Chris Masonc8b97812008-10-29 14:49:59 -04006829static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006830 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006831 size_t pg_offset, u64 extent_offset,
6832 struct btrfs_file_extent_item *item)
6833{
6834 int ret;
6835 struct extent_buffer *leaf = path->nodes[0];
6836 char *tmp;
6837 size_t max_size;
6838 unsigned long inline_size;
6839 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006840 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006841
6842 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006843 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006844 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6845 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006846 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006847 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006848 if (!tmp)
6849 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006850 ptr = btrfs_file_extent_inline_start(item);
6851
6852 read_extent_buffer(leaf, tmp, ptr, inline_size);
6853
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006854 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006855 ret = btrfs_decompress(compress_type, tmp, page,
6856 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006857
6858 /*
6859 * decompression code contains a memset to fill in any space between the end
6860 * of the uncompressed data and the end of max_size in case the decompressed
6861 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6862 * the end of an inline extent and the beginning of the next block, so we
6863 * cover that region here.
6864 */
6865
Ira Weinyd048b9c2021-05-04 18:40:07 -07006866 if (max_size + pg_offset < PAGE_SIZE)
6867 memzero_page(page, pg_offset + max_size,
6868 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006869 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006870 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006871}
6872
Omar Sandoval39b07b52019-12-02 17:34:23 -08006873/**
6874 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6875 * @inode: file to search in
6876 * @page: page to read extent data into if the extent is inline
6877 * @pg_offset: offset into @page to copy to
6878 * @start: file offset
6879 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006880 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006881 * This returns the first &struct extent_map which overlaps with the given
6882 * range, reading it from the B-tree and caching it if necessary. Note that
6883 * there may be more extents which overlap the given range after the returned
6884 * extent_map.
6885 *
6886 * If @page is not NULL and the extent is inline, this also reads the extent
6887 * data directly into the page and marks the extent up to date in the io_tree.
6888 *
6889 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006890 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006891struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006892 struct page *page, size_t pg_offset,
6893 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006894{
David Sterba3ffbd682018-06-29 10:56:42 +02006895 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006896 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006897 u64 extent_start = 0;
6898 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006899 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006900 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006901 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006902 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006903 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006904 struct extent_buffer *leaf;
6905 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006907 struct extent_map_tree *em_tree = &inode->extent_tree;
6908 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006909
Chris Mason890871b2009-09-02 16:24:52 -04006910 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006911 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006912 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006913
Chris Masona52d9a82007-08-27 16:49:44 -04006914 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006915 if (em->start > start || em->start + em->len <= start)
6916 free_extent_map(em);
6917 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006918 free_extent_map(em);
6919 else
6920 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006921 }
David Sterba172ddd62011-04-21 00:48:27 +02006922 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006923 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006924 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006925 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
Chris Masond1310b22008-01-24 16:13:08 -05006927 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006928 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006929 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006930 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006931
Liu Bobee6ec82018-08-17 05:05:28 +08006932 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006933 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006934 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006935 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006936 }
6937
Liu Bobee6ec82018-08-17 05:05:28 +08006938 /* Chances are we'll be called again, so go ahead and do readahead */
6939 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006940
6941 /*
6942 * The same explanation in load_free_space_cache applies here as well,
6943 * we only read when we're loading the free space cache, and at that
6944 * point the commit_root has everything we need.
6945 */
6946 if (btrfs_is_free_space_inode(inode)) {
6947 path->search_commit_root = 1;
6948 path->skip_locking = 1;
6949 }
Josef Bacik51899412020-08-20 11:46:01 -04006950
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006951 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006952 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006953 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006954 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006955 if (path->slots[0] == 0)
6956 goto not_found;
6957 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006958 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006959 }
6960
Chris Mason5f39d392007-10-15 16:14:19 -04006961 leaf = path->nodes[0];
6962 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006963 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006965 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006966 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006967 /*
6968 * If we backup past the first extent we want to move forward
6969 * and see if there is an extent in front of us, otherwise we'll
6970 * say there is a hole for our whole search range which can
6971 * cause problems.
6972 */
6973 extent_end = start;
6974 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006975 }
6976
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006977 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006978 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006979 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006980 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6981 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006982 /* Only regular file could have regular/prealloc extent */
6983 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006984 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006985 btrfs_crit(fs_info,
6986 "regular/prealloc extent found for non-regular inode %llu",
6987 btrfs_ino(inode));
6988 goto out;
6989 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006990 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6991 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006993 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6994 path->slots[0],
6995 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006996 }
Josef Bacik25a50342013-10-14 12:08:38 -04006997next:
Yan Zheng9036c102008-10-30 14:19:41 -04006998 if (start >= extent_end) {
6999 path->slots[0]++;
7000 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7001 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007002 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007003 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007004 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007005 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007006
Yan Zheng9036c102008-10-30 14:19:41 -04007007 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007008 }
Yan Zheng9036c102008-10-30 14:19:41 -04007009 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7010 if (found_key.objectid != objectid ||
7011 found_key.type != BTRFS_EXTENT_DATA_KEY)
7012 goto not_found;
7013 if (start + len <= found_key.offset)
7014 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007015 if (start > found_key.offset)
7016 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007017
7018 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007019 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007020 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007021 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007022 em->block_start = EXTENT_MAP_HOLE;
7023 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007024 }
7025
Omar Sandoval39b07b52019-12-02 17:34:23 -08007026 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007027
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007028 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7029 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007030 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007031 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007032 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007033 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007034 size_t size;
7035 size_t extent_offset;
7036 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007037
Omar Sandoval39b07b52019-12-02 17:34:23 -08007038 if (!page)
Yan689f9342007-10-29 11:41:07 -04007039 goto out;
Yan689f9342007-10-29 11:41:07 -04007040
Qu Wenruoe41ca582018-06-06 15:41:49 +08007041 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007042 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007043 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7044 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007045 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007046 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007047 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007048 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007049 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007050
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007051 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007052 if (btrfs_file_extent_compression(leaf, item) !=
7053 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007054 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007055 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007056 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007057 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007058 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007059 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007060 read_extent_buffer(leaf, map + pg_offset, ptr,
7061 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007062 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007063 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007064 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007065 copy_size);
7066 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007067 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007068 }
Chris Mason179e29e2007-11-01 11:28:41 -04007069 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007070 }
Chris Masond1310b22008-01-24 16:13:08 -05007071 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007072 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007073 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007074 }
7075not_found:
7076 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007077 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007078 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007079 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007080insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007081 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007082 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007083 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007084 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007085 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7086 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007087 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007088 goto out;
7089 }
Chris Masond1310b22008-01-24 16:13:08 -05007090
Chris Mason890871b2009-09-02 16:24:52 -04007091 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007092 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007093 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007094out:
Liu Boc6414282018-08-23 07:36:17 +08007095 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007096
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007097 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007098
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007099 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007100 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007101 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007102 }
7103 return em;
7104}
7105
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007106struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007107 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007108{
7109 struct extent_map *em;
7110 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007111 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007112 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007113 u64 delalloc_len;
7114 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007115 int err = 0;
7116
Omar Sandoval39b07b52019-12-02 17:34:23 -08007117 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007118 if (IS_ERR(em))
7119 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007120 /*
7121 * If our em maps to:
7122 * - a hole or
7123 * - a pre-alloc extent,
7124 * there might actually be delalloc bytes behind it.
7125 */
7126 if (em->block_start != EXTENT_MAP_HOLE &&
7127 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7128 return em;
7129 else
7130 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007131
7132 /* check to see if we've wrapped (len == -1 or similar) */
7133 end = start + len;
7134 if (end < start)
7135 end = (u64)-1;
7136 else
7137 end -= 1;
7138
7139 em = NULL;
7140
7141 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007142 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007143 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007144 delalloc_end = delalloc_start + delalloc_len;
7145 if (delalloc_end < delalloc_start)
7146 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007147
7148 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007149 * We didn't find anything useful, return the original results from
7150 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007151 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007152 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007153 em = hole_em;
7154 hole_em = NULL;
7155 goto out;
7156 }
7157
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007158 /*
7159 * Adjust the delalloc_start to make sure it doesn't go backwards from
7160 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007161 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007162 delalloc_start = max(start, delalloc_start);
7163 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007164
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007165 if (delalloc_len > 0) {
7166 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007167 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007168 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007169
David Sterba172ddd62011-04-21 00:48:27 +02007170 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007171 if (!em) {
7172 err = -ENOMEM;
7173 goto out;
7174 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007175
7176 ASSERT(hole_em);
7177 /*
7178 * When btrfs_get_extent can't find anything it returns one
7179 * huge hole
7180 *
7181 * Make sure what it found really fits our range, and adjust to
7182 * make sure it is based on the start from the caller
7183 */
7184 if (hole_end <= start || hole_em->start > end) {
7185 free_extent_map(hole_em);
7186 hole_em = NULL;
7187 } else {
7188 hole_start = max(hole_em->start, start);
7189 hole_len = hole_end - hole_start;
7190 }
7191
7192 if (hole_em && delalloc_start > hole_start) {
7193 /*
7194 * Our hole starts before our delalloc, so we have to
7195 * return just the parts of the hole that go until the
7196 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007197 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007198 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007199 em->start = hole_start;
7200 em->orig_start = hole_start;
7201 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007202 * Don't adjust block start at all, it is fixed at
7203 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007204 */
7205 em->block_start = hole_em->block_start;
7206 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007207 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7208 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007209 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007210 /*
7211 * Hole is out of passed range or it starts after
7212 * delalloc range
7213 */
7214 em->start = delalloc_start;
7215 em->len = delalloc_len;
7216 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007217 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007218 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007219 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007220 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007221 return hole_em;
7222 }
7223out:
7224
7225 free_extent_map(hole_em);
7226 if (err) {
7227 free_extent_map(em);
7228 return ERR_PTR(err);
7229 }
7230 return em;
7231}
7232
Nikolay Borisov64f54182020-06-03 08:55:31 +03007233static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007234 const u64 start,
7235 const u64 len,
7236 const u64 orig_start,
7237 const u64 block_start,
7238 const u64 block_len,
7239 const u64 orig_block_len,
7240 const u64 ram_bytes,
7241 const int type)
7242{
7243 struct extent_map *em = NULL;
7244 int ret;
7245
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007246 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007247 em = create_io_em(inode, start, len, orig_start, block_start,
7248 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007249 BTRFS_COMPRESS_NONE, /* compress_type */
7250 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007251 if (IS_ERR(em))
7252 goto out;
7253 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007254 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7255 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007256 if (ret) {
7257 if (em) {
7258 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007259 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007260 }
7261 em = ERR_PTR(ret);
7262 }
7263 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007264
7265 return em;
7266}
7267
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007268static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269 u64 start, u64 len)
7270{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007271 struct btrfs_root *root = inode->root;
7272 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007273 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 struct btrfs_key ins;
7275 u64 alloc_hint;
7276 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007278 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007279 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007280 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007281 if (ret)
7282 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007284 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007285 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007286 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007287 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007288 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007289 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7290 1);
Josef Bacik00361582013-08-14 14:02:47 -04007291
Josef Bacik4b46fce2010-05-23 11:00:55 -04007292 return em;
7293}
7294
Anand Jainf4639632021-02-10 21:25:16 -08007295static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007296{
7297 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007298 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007299
7300 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7301 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007302 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007303 if (block_group)
7304 btrfs_put_block_group(block_group);
7305 return readonly;
7306}
7307
Chris Mason46bfbb52010-05-26 11:04:10 -04007308/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007309 * Check if we can do nocow write into the range [@offset, @offset + @len)
7310 *
7311 * @offset: File offset
7312 * @len: The length to write, will be updated to the nocow writeable
7313 * range
7314 * @orig_start: (optional) Return the original file offset of the file extent
7315 * @orig_len: (optional) Return the original on-disk length of the file extent
7316 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007317 * @strict: if true, omit optimizations that might force us into unnecessary
7318 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007319 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007320 * Return:
7321 * >0 and update @len if we can do nocow write
7322 * 0 if we can't do nocow write
7323 * <0 if error happened
7324 *
7325 * NOTE: This only checks the file extents, caller is responsible to wait for
7326 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007327 */
Josef Bacik00361582013-08-14 14:02:47 -04007328noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007329 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007330 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007331{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007333 struct btrfs_path *path;
7334 int ret;
7335 struct extent_buffer *leaf;
7336 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007337 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007338 struct btrfs_file_extent_item *fi;
7339 struct btrfs_key key;
7340 u64 disk_bytenr;
7341 u64 backref_offset;
7342 u64 extent_end;
7343 u64 num_bytes;
7344 int slot;
7345 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007346 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007347
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 path = btrfs_alloc_path();
7349 if (!path)
7350 return -ENOMEM;
7351
David Sterbaf85b7372017-01-20 14:54:07 +01007352 ret = btrfs_lookup_file_extent(NULL, root, path,
7353 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007354 if (ret < 0)
7355 goto out;
7356
7357 slot = path->slots[0];
7358 if (ret == 1) {
7359 if (slot == 0) {
7360 /* can't find the item, must cow */
7361 ret = 0;
7362 goto out;
7363 }
7364 slot--;
7365 }
7366 ret = 0;
7367 leaf = path->nodes[0];
7368 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007369 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007370 key.type != BTRFS_EXTENT_DATA_KEY) {
7371 /* not our file or wrong item type, must cow */
7372 goto out;
7373 }
7374
7375 if (key.offset > offset) {
7376 /* Wrong offset, must cow */
7377 goto out;
7378 }
7379
7380 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7381 found_type = btrfs_file_extent_type(leaf, fi);
7382 if (found_type != BTRFS_FILE_EXTENT_REG &&
7383 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7384 /* not a regular extent, must cow */
7385 goto out;
7386 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007387
7388 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7389 goto out;
7390
Miao Xiee77751a2013-12-27 21:11:50 +08007391 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7392 if (extent_end <= offset)
7393 goto out;
7394
Chris Mason46bfbb52010-05-26 11:04:10 -04007395 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007396 if (disk_bytenr == 0)
7397 goto out;
7398
7399 if (btrfs_file_extent_compression(leaf, fi) ||
7400 btrfs_file_extent_encryption(leaf, fi) ||
7401 btrfs_file_extent_other_encoding(leaf, fi))
7402 goto out;
7403
Ethan Lien78d42952018-05-17 14:58:29 +08007404 /*
7405 * Do the same check as in btrfs_cross_ref_exist but without the
7406 * unnecessary search.
7407 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007408 if (!strict &&
7409 (btrfs_file_extent_generation(leaf, fi) <=
7410 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007411 goto out;
7412
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 backref_offset = btrfs_file_extent_offset(leaf, fi);
7414
Josef Bacik7ee9e442013-06-21 16:37:03 -04007415 if (orig_start) {
7416 *orig_start = key.offset - backref_offset;
7417 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7418 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7419 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007421 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007423
7424 num_bytes = min(offset + *len, extent_end) - offset;
7425 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7426 u64 range_end;
7427
Jeff Mahoneyda170662016-06-15 09:22:56 -04007428 range_end = round_up(offset + num_bytes,
7429 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007430 ret = test_range_bit(io_tree, offset, range_end,
7431 EXTENT_DELALLOC, 0, NULL);
7432 if (ret) {
7433 ret = -EAGAIN;
7434 goto out;
7435 }
7436 }
7437
Josef Bacik1bda19e2013-10-18 12:10:36 -04007438 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007439
7440 /*
7441 * look for other files referencing this extent, if we
7442 * find any we must cow
7443 */
Josef Bacik00361582013-08-14 14:02:47 -04007444
Liu Boe4c3b2d2017-01-30 12:25:28 -08007445 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007446 key.offset - backref_offset, disk_bytenr,
7447 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007448 if (ret) {
7449 ret = 0;
7450 goto out;
7451 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007452
7453 /*
7454 * adjust disk_bytenr and num_bytes to cover just the bytes
7455 * in this extent we are about to write. If there
7456 * are any csums in that range we have to cow in order
7457 * to keep the csums correct
7458 */
7459 disk_bytenr += backref_offset;
7460 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007461 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7462 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007463 /*
7464 * all of the above have passed, it is safe to overwrite this extent
7465 * without cow
7466 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007467 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 ret = 1;
7469out:
7470 btrfs_free_path(path);
7471 return ret;
7472}
7473
Josef Bacikeb838e72012-07-31 16:28:48 -04007474static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007475 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007476{
7477 struct btrfs_ordered_extent *ordered;
7478 int ret = 0;
7479
7480 while (1) {
7481 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007482 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007483 /*
7484 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007485 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007486 * extents in this range.
7487 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007488 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007489 lockend - lockstart + 1);
7490
7491 /*
7492 * We need to make sure there are no buffered pages in this
7493 * range either, we could have raced between the invalidate in
7494 * generic_file_direct_write and locking the extent. The
7495 * invalidate needs to happen so that reads after a write do not
7496 * get stale data.
7497 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007498 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007499 (!writing || !filemap_range_has_page(inode->i_mapping,
7500 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007501 break;
7502
7503 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007504 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007505
7506 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007507 /*
7508 * If we are doing a DIO read and the ordered extent we
7509 * found is for a buffered write, we can not wait for it
7510 * to complete and retry, because if we do so we can
7511 * deadlock with concurrent buffered writes on page
7512 * locks. This happens only if our DIO read covers more
7513 * than one extent map, if at this point has already
7514 * created an ordered extent for a previous extent map
7515 * and locked its range in the inode's io tree, and a
7516 * concurrent write against that previous extent map's
7517 * range and this range started (we unlock the ranges
7518 * in the io tree only when the bios complete and
7519 * buffered writes always lock pages before attempting
7520 * to lock range in the io tree).
7521 */
7522 if (writing ||
7523 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007524 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007525 else
7526 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007527 btrfs_put_ordered_extent(ordered);
7528 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007529 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007530 * We could trigger writeback for this range (and wait
7531 * for it to complete) and then invalidate the pages for
7532 * this range (through invalidate_inode_pages2_range()),
7533 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007534 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007535 * triggered a readahead) on a page lock due to an
7536 * ordered dio extent we created before but did not have
7537 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007538 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007539 * ordered extent to complete while holding a lock on
7540 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007542 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 }
7544
Filipe Mananaade77022016-02-18 14:28:55 +00007545 if (ret)
7546 break;
7547
Josef Bacikeb838e72012-07-31 16:28:48 -04007548 cond_resched();
7549 }
7550
7551 return ret;
7552}
7553
Liu Bo6f9994d2017-01-31 07:50:22 -08007554/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007555static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7556 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007557 u64 block_len, u64 orig_block_len,
7558 u64 ram_bytes, int compress_type,
7559 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007560{
7561 struct extent_map_tree *em_tree;
7562 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007563 int ret;
7564
Liu Bo6f9994d2017-01-31 07:50:22 -08007565 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7566 type == BTRFS_ORDERED_COMPRESSED ||
7567 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007568 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007569
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007570 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007571 em = alloc_extent_map();
7572 if (!em)
7573 return ERR_PTR(-ENOMEM);
7574
7575 em->start = start;
7576 em->orig_start = orig_start;
7577 em->len = len;
7578 em->block_len = block_len;
7579 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007580 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007581 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007582 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007583 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007584 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007585 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007586 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007587 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7588 em->compress_type = compress_type;
7589 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007590
7591 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007592 btrfs_drop_extent_cache(inode, em->start,
7593 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007594 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007595 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007596 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007597 /*
7598 * The caller has taken lock_extent(), who could race with us
7599 * to add em?
7600 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007601 } while (ret == -EEXIST);
7602
7603 if (ret) {
7604 free_extent_map(em);
7605 return ERR_PTR(ret);
7606 }
7607
Liu Bo6f9994d2017-01-31 07:50:22 -08007608 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 return em;
7610}
7611
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007612
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007613static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007614 struct inode *inode,
7615 struct btrfs_dio_data *dio_data,
7616 u64 start, u64 len)
7617{
7618 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7619 struct extent_map *em = *map;
7620 int ret = 0;
7621
7622 /*
7623 * We don't allocate a new extent in the following cases
7624 *
7625 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7626 * existing extent.
7627 * 2) The extent is marked as PREALLOC. We're good to go here and can
7628 * just use the extent.
7629 *
7630 */
7631 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7632 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7633 em->block_start != EXTENT_MAP_HOLE)) {
7634 int type;
7635 u64 block_start, orig_start, orig_block_len, ram_bytes;
7636
7637 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7638 type = BTRFS_ORDERED_PREALLOC;
7639 else
7640 type = BTRFS_ORDERED_NOCOW;
7641 len = min(len, em->len - (start - em->start));
7642 block_start = em->block_start + (start - em->start);
7643
7644 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007645 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007646 btrfs_inc_nocow_writers(fs_info, block_start)) {
7647 struct extent_map *em2;
7648
Nikolay Borisov64f54182020-06-03 08:55:31 +03007649 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007650 orig_start, block_start,
7651 len, orig_block_len,
7652 ram_bytes, type);
7653 btrfs_dec_nocow_writers(fs_info, block_start);
7654 if (type == BTRFS_ORDERED_PREALLOC) {
7655 free_extent_map(em);
7656 *map = em = em2;
7657 }
7658
7659 if (em2 && IS_ERR(em2)) {
7660 ret = PTR_ERR(em2);
7661 goto out;
7662 }
7663 /*
7664 * For inode marked NODATACOW or extent marked PREALLOC,
7665 * use the existing or preallocated extent, so does not
7666 * need to adjust btrfs_space_info's bytes_may_use.
7667 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007668 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007669 goto skip_cow;
7670 }
7671 }
7672
7673 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007674 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007675 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007676 if (IS_ERR(em)) {
7677 ret = PTR_ERR(em);
7678 goto out;
7679 }
7680
7681 len = min(len, em->len - (start - em->start));
7682
7683skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007684 /*
7685 * Need to update the i_size under the extent lock so buffered
7686 * readers will get the updated i_size when we unlock.
7687 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007688 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007689 i_size_write(inode, start + len);
7690
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007691 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007692out:
7693 return ret;
7694}
7695
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007696static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7697 loff_t length, unsigned int flags, struct iomap *iomap,
7698 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007702 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307703 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007704 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007705 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007706 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007707 u64 len = length;
7708 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007709
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007710 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007711 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007712
Josef Bacikc3298612012-08-03 16:49:19 -04007713 lockstart = start;
7714 lockend = start + len - 1;
7715
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007716 /*
7717 * The generic stuff only does filemap_write_and_wait_range, which
7718 * isn't enough if we've written compressed pages to this area, so we
7719 * need to flush the dirty pages again to make absolutely sure that any
7720 * outstanding dirty pages are on disk.
7721 */
7722 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7723 &BTRFS_I(inode)->runtime_flags)) {
7724 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7725 start + length - 1);
7726 if (ret)
7727 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007728 }
7729
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007730 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7731 if (!dio_data)
7732 return -ENOMEM;
7733
7734 dio_data->length = length;
7735 if (write) {
7736 dio_data->reserve = round_up(length, fs_info->sectorsize);
7737 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7738 &dio_data->data_reserved,
7739 start, dio_data->reserve);
7740 if (ret) {
7741 extent_changeset_free(dio_data->data_reserved);
7742 kfree(dio_data);
7743 return ret;
7744 }
7745 }
7746 iomap->private = dio_data;
7747
7748
Josef Bacikeb838e72012-07-31 16:28:48 -04007749 /*
7750 * If this errors out it's because we couldn't invalidate pagecache for
7751 * this range and we need to fallback to buffered.
7752 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007753 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007754 ret = -ENOTBLK;
7755 goto err;
7756 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007757
Omar Sandoval39b07b52019-12-02 17:34:23 -08007758 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007759 if (IS_ERR(em)) {
7760 ret = PTR_ERR(em);
7761 goto unlock_err;
7762 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763
7764 /*
7765 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7766 * io. INLINE is special, and we could probably kludge it in here, but
7767 * it's still buffered so for safety lets just fall back to the generic
7768 * buffered path.
7769 *
7770 * For COMPRESSED we _have_ to read the entire extent in so we can
7771 * decompress it, so there will be buffering required no matter what we
7772 * do, so go ahead and fallback to buffered.
7773 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007774 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775 * to buffered IO. Don't blame me, this is the price we pay for using
7776 * the generic code.
7777 */
7778 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7779 em->block_start == EXTENT_MAP_INLINE) {
7780 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007781 ret = -ENOTBLK;
7782 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 }
7784
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007785 len = min(len, em->len - (start - em->start));
7786 if (write) {
7787 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7788 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007789 if (ret < 0)
7790 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007791 unlock_extents = true;
7792 /* Recalc len in case the new em is smaller than requested */
7793 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007794 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007795 /*
7796 * We need to unlock only the end area that we aren't using.
7797 * The rest is going to be unlocked by the endio routine.
7798 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007799 lockstart = start + len;
7800 if (lockstart < lockend)
7801 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802 }
7803
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007804 if (unlock_extents)
7805 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7806 lockstart, lockend, &cached_state);
7807 else
7808 free_extent_state(cached_state);
7809
7810 /*
7811 * Translate extent map information to iomap.
7812 * We trim the extents (and move the addr) even though iomap code does
7813 * that, since we have locked only the parts we are performing I/O in.
7814 */
7815 if ((em->block_start == EXTENT_MAP_HOLE) ||
7816 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7817 iomap->addr = IOMAP_NULL_ADDR;
7818 iomap->type = IOMAP_HOLE;
7819 } else {
7820 iomap->addr = em->block_start + (start - em->start);
7821 iomap->type = IOMAP_MAPPED;
7822 }
7823 iomap->offset = start;
7824 iomap->bdev = fs_info->fs_devices->latest_bdev;
7825 iomap->length = len;
7826
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007827 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007828 iomap->flags |= IOMAP_F_ZONE_APPEND;
7829
Josef Bacik4b46fce2010-05-23 11:00:55 -04007830 free_extent_map(em);
7831
7832 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007833
7834unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007835 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7836 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007837err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007838 if (dio_data) {
7839 btrfs_delalloc_release_space(BTRFS_I(inode),
7840 dio_data->data_reserved, start,
7841 dio_data->reserve, true);
7842 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7843 extent_changeset_free(dio_data->data_reserved);
7844 kfree(dio_data);
7845 }
7846 return ret;
7847}
7848
7849static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7850 ssize_t written, unsigned int flags, struct iomap *iomap)
7851{
7852 int ret = 0;
7853 struct btrfs_dio_data *dio_data = iomap->private;
7854 size_t submitted = dio_data->submitted;
7855 const bool write = !!(flags & IOMAP_WRITE);
7856
7857 if (!write && (iomap->type == IOMAP_HOLE)) {
7858 /* If reading from a hole, unlock and return */
7859 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7860 goto out;
7861 }
7862
7863 if (submitted < length) {
7864 pos += submitted;
7865 length -= submitted;
7866 if (write)
7867 __endio_write_update_ordered(BTRFS_I(inode), pos,
7868 length, false);
7869 else
7870 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7871 pos + length - 1);
7872 ret = -ENOTBLK;
7873 }
7874
7875 if (write) {
7876 if (dio_data->reserve)
7877 btrfs_delalloc_release_space(BTRFS_I(inode),
7878 dio_data->data_reserved, pos,
7879 dio_data->reserve, true);
7880 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7881 extent_changeset_free(dio_data->data_reserved);
7882 }
7883out:
7884 kfree(dio_data);
7885 iomap->private = NULL;
7886
Josef Bacikeb838e72012-07-31 16:28:48 -04007887 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888}
7889
Omar Sandoval769b4f22020-04-16 14:46:22 -07007890static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7891{
7892 /*
7893 * This implies a barrier so that stores to dio_bio->bi_status before
7894 * this and loads of dio_bio->bi_status after this are fully ordered.
7895 */
7896 if (!refcount_dec_and_test(&dip->refs))
7897 return;
7898
Naohiro Aotacfe94442021-02-04 19:21:59 +09007899 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007900 __endio_write_update_ordered(BTRFS_I(dip->inode),
7901 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007902 dip->bytes,
7903 !dip->dio_bio->bi_status);
7904 } else {
7905 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7906 dip->logical_offset,
7907 dip->logical_offset + dip->bytes - 1);
7908 }
7909
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007910 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007911 kfree(dip);
7912}
7913
Omar Sandoval77d5d682020-04-16 14:46:25 -07007914static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7915 int mirror_num,
7916 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007917{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007918 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007919 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007920 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007921
Mike Christie37226b22016-06-05 14:31:52 -05007922 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007923
Omar Sandoval5c047a62020-04-16 14:46:24 -07007924 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007925 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007926 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007927
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007928 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007929 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007930 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007931 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007932 return ret;
7933}
7934
Omar Sandoval769b4f22020-04-16 14:46:22 -07007935static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7936 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007937 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007938{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007939 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7940 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007941 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7943 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007944 struct bio_vec bvec;
7945 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007946 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007947 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007948 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007949
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007950 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7951 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952
Liu Bo17347ce2017-05-15 15:33:27 -07007953 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7954 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007955 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007956 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007957 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007958 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007959 bio_offset, bvec.bv_page,
7960 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007961 clean_io_failure(fs_info, failure_tree, io_tree,
7962 start, bvec.bv_page,
7963 btrfs_ino(BTRFS_I(inode)),
7964 pgoff);
7965 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08007966 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007967
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007968 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08007969 ret = btrfs_repair_one_sector(inode,
7970 &io_bio->bio,
7971 start - io_bio->logical,
7972 bvec.bv_page, pgoff,
7973 start, io_bio->mirror_num,
7974 submit_dio_repair_bio);
7975 if (ret)
7976 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007977 }
7978 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007979 ASSERT(bio_offset + sectorsize > bio_offset);
7980 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007983 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007984 return err;
7985}
7986
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007987static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007988 const u64 offset, const u64 bytes,
7989 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007990{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007991 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007992 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007993 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007994 u64 ordered_offset = offset;
7995 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007996 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007997
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007998 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007999 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008000 else
Qu Wenruo524272602017-03-08 10:25:52 +08008001 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08008002
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008003 while (ordered_offset < offset + bytes) {
8004 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008005 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03008006 &ordered_offset,
8007 ordered_bytes,
8008 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07008009 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8010 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008011 btrfs_queue_work(wq, &ordered->work);
8012 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08008013
8014 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008015 if (ordered_offset == last_offset)
8016 return;
8017 /*
8018 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008019 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008020 */
8021 if (ordered_offset < offset + bytes) {
8022 ordered_bytes = offset + bytes - ordered_offset;
8023 ordered = NULL;
8024 }
Chris Mason163cf092010-11-28 19:56:33 -05008025 }
Filipe Manana14543772015-11-24 16:23:54 +00008026}
8027
Qu Wenruo8896a082020-10-21 14:24:53 +08008028static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008029 struct bio *bio,
8030 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008031{
Qu Wenruo1941b642020-12-02 14:47:57 +08008032 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008033}
8034
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008035static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008036{
8037 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008038 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008039
Miao Xie8b110e32014-09-12 18:44:03 +08008040 if (err)
8041 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008042 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008043 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008044 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008045 bio->bi_iter.bi_size, err);
8046
Omar Sandoval769b4f22020-04-16 14:46:22 -07008047 if (bio_op(bio) == REQ_OP_READ) {
8048 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008049 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008050 }
8051
Omar Sandoval769b4f22020-04-16 14:46:22 -07008052 if (err)
8053 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008054
Naohiro Aota544d24f2021-02-04 19:22:06 +09008055 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8056
Miao Xiee65e1532010-11-22 03:04:43 +00008057 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008058 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008059}
8060
David Sterbad0ee3932018-03-08 14:35:48 +01008061static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8062 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008063{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008064 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008065 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008066 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008067 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008068
Liu Bo4c274bc2017-11-01 17:19:27 -06008069 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008070 if (async_submit)
8071 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8072
Josef Bacik5fd02042012-05-02 14:00:54 -04008073 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008074 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008075 if (ret)
8076 goto err;
8077 }
Miao Xiee65e1532010-11-22 03:04:43 +00008078
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008079 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008080 goto map;
8081
8082 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008083 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008084 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008085 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008086 } else if (write) {
8087 /*
8088 * If we aren't doing async submit, calculate the csum of the
8089 * bio now.
8090 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008091 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008092 if (ret)
8093 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008094 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008095 u64 csum_offset;
8096
8097 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008098 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008099 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008100 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008101 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008102map:
Chris Mason08635ba2019-07-10 12:28:14 -07008103 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008104err:
Miao Xiee65e1532010-11-22 03:04:43 +00008105 return ret;
8106}
8107
Omar Sandovalc36cac22020-04-16 14:46:13 -07008108/*
8109 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8110 * or ordered extents whether or not we submit any bios.
8111 */
8112static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8113 struct inode *inode,
8114 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008115{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008116 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008117 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8118 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008119 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008120
Omar Sandoval85879572020-04-16 14:46:21 -07008121 dip_size = sizeof(*dip);
8122 if (!write && csum) {
8123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008124 size_t nblocks;
8125
David Sterba265fdfa2020-07-01 21:19:09 +02008126 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008127 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008128 }
8129
8130 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008131 if (!dip)
8132 return NULL;
8133
Omar Sandovalc36cac22020-04-16 14:46:13 -07008134 dip->inode = inode;
8135 dip->logical_offset = file_offset;
8136 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008137 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008138 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008139 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008140 return dip;
8141}
8142
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008143static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8144 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008145{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008146 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008147 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008148 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8149 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008150 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008151 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008152 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008153 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008154 u64 submit_len;
8155 int clone_offset = 0;
8156 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008157 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308158 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008159 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008160 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008161 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008162 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008163
Omar Sandovalc36cac22020-04-16 14:46:13 -07008164 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8165 if (!dip) {
8166 if (!write) {
8167 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8168 file_offset + dio_bio->bi_iter.bi_size - 1);
8169 }
8170 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008171 bio_endio(dio_bio);
8172 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008173 }
8174
Josef Bacik334c16d2020-10-16 11:29:14 -04008175 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008176 /*
8177 * Load the csums up front to reduce csum tree searches and
8178 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008179 *
8180 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008181 */
Qu Wenruo62751932020-12-02 14:48:06 +08008182 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008183 if (status != BLK_STS_OK)
8184 goto out_err;
8185 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008186
Omar Sandoval769b4f22020-04-16 14:46:22 -07008187 start_sector = dio_bio->bi_iter.bi_sector;
8188 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008189
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008190 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008191 logical = start_sector << 9;
8192 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8193 if (IS_ERR(em)) {
8194 status = errno_to_blk_status(PTR_ERR(em));
8195 em = NULL;
8196 goto out_err_em;
8197 }
8198 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008199 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008200 if (ret) {
8201 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008202 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008203 }
8204 ASSERT(geom.len <= INT_MAX);
8205
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008206 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008207
Liu Bo725130b2017-05-16 09:51:39 -07008208 /*
8209 * This will never fail as it's passing GPF_NOFS and
8210 * the allocation is backed by btrfs_bioset.
8211 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008212 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008213 bio->bi_private = dip;
8214 bio->bi_end_io = btrfs_end_dio_bio;
8215 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008216
Naohiro Aota544d24f2021-02-04 19:22:06 +09008217 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8218 status = extract_ordered_extent(BTRFS_I(inode), bio,
8219 file_offset);
8220 if (status) {
8221 bio_put(bio);
8222 goto out_err;
8223 }
8224 }
8225
Liu Bo725130b2017-05-16 09:51:39 -07008226 ASSERT(submit_len >= clone_len);
8227 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008228
Liu Bo725130b2017-05-16 09:51:39 -07008229 /*
8230 * Increase the count before we submit the bio so we know
8231 * the end IO handler won't happen before we increase the
8232 * count. Otherwise, the dip might get freed before we're
8233 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008234 *
8235 * We transfer the initial reference to the last bio, so we
8236 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008237 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008238 if (submit_len > 0) {
8239 refcount_inc(&dip->refs);
8240 /*
8241 * If we are submitting more than one bio, submit them
8242 * all asynchronously. The exception is RAID 5 or 6, as
8243 * asynchronous checksums make it difficult to collect
8244 * full stripe writes.
8245 */
8246 if (!raid56)
8247 async_submit = 1;
8248 }
Miao Xiee65e1532010-11-22 03:04:43 +00008249
David Sterbad0ee3932018-03-08 14:35:48 +01008250 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008251 async_submit);
8252 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008253 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008254 if (submit_len > 0)
8255 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008256 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008257 }
Liu Bo725130b2017-05-16 09:51:39 -07008258
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008259 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008260 clone_offset += clone_len;
8261 start_sector += clone_len >> 9;
8262 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008263
8264 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008265 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008266 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008267
Michal Rostecki42034312021-01-27 14:57:27 +01008268out_err_em:
8269 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008270out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008271 dip->dio_bio->bi_status = status;
8272 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008273
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008274 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008275}
8276
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008277const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008278 .iomap_begin = btrfs_dio_iomap_begin,
8279 .iomap_end = btrfs_dio_iomap_end,
8280};
8281
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008282const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008283 .submit_io = btrfs_submit_direct,
8284};
8285
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008286static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008287 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008288{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008289 int ret;
8290
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008291 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008292 if (ret)
8293 return ret;
8294
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008295 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008296}
8297
Chris Mason9ebefb182007-06-15 13:50:00 -04008298int btrfs_readpage(struct file *file, struct page *page)
8299{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008300 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8301 u64 start = page_offset(page);
8302 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008303 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008304 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008305 int ret;
8306
Nikolay Borisov0f208812020-09-14 14:39:16 +03008307 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8308
8309 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008310 if (bio)
8311 ret = submit_one_bio(bio, 0, bio_flags);
8312 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008313}
Chris Mason1832a6d2007-12-21 16:27:21 -05008314
Chris Mason39279cc2007-06-12 06:35:45 -04008315static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8316{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008317 struct inode *inode = page->mapping->host;
8318 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008319
8320 if (current->flags & PF_MEMALLOC) {
8321 redirty_page_for_writepage(wbc, page);
8322 unlock_page(page);
8323 return 0;
8324 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008325
8326 /*
8327 * If we are under memory pressure we will call this directly from the
8328 * VM, we need to make sure we have the inode referenced for the ordered
8329 * extent. If not just return like we didn't do anything.
8330 */
8331 if (!igrab(inode)) {
8332 redirty_page_for_writepage(wbc, page);
8333 return AOP_WRITEPAGE_ACTIVATE;
8334 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008335 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008336 btrfs_add_delayed_iput(inode);
8337 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008338}
Chris Mason39279cc2007-06-12 06:35:45 -04008339
Eric Sandeen48a3b632013-04-25 20:41:01 +00008340static int btrfs_writepages(struct address_space *mapping,
8341 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008342{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008343 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008344}
8345
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008346static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008347{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008348 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008349}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008350
Chris Masone6dcd2d2008-07-17 12:53:50 -04008351static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008352{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008353 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008354 if (ret == 1)
Qu Wenruo32443de2021-01-26 16:34:00 +08008355 clear_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008356 return ret;
8357}
Chris Mason39279cc2007-06-12 06:35:45 -04008358
Chris Masone6dcd2d2008-07-17 12:53:50 -04008359static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8360{
Chris Mason98509cf2008-09-11 15:51:43 -04008361 if (PageWriteback(page) || PageDirty(page))
8362 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008363 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008364}
8365
Roman Gushchinf8e66082020-03-04 16:57:35 -08008366#ifdef CONFIG_MIGRATION
8367static int btrfs_migratepage(struct address_space *mapping,
8368 struct page *newpage, struct page *page,
8369 enum migrate_mode mode)
8370{
8371 int ret;
8372
8373 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8374 if (ret != MIGRATEPAGE_SUCCESS)
8375 return ret;
8376
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008377 if (page_has_private(page))
8378 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008379
8380 if (PagePrivate2(page)) {
8381 ClearPagePrivate2(page);
8382 SetPagePrivate2(newpage);
8383 }
8384
8385 if (mode != MIGRATE_SYNC_NO_COPY)
8386 migrate_page_copy(newpage, page);
8387 else
8388 migrate_page_states(newpage, page);
8389 return MIGRATEPAGE_SUCCESS;
8390}
8391#endif
8392
Lukas Czernerd47992f2013-05-21 23:17:23 -04008393static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8394 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008395{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008396 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8397 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008398 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008399 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008400 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008401 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308402 u64 start;
8403 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008404 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008405 bool found_ordered = false;
8406 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008407
Chris Mason8b62b722009-09-02 16:53:46 -04008408 /*
8409 * we have the page locked, so new writeback can't start,
8410 * and the dirty bit won't be cleared while we are here.
8411 *
8412 * Wait for IO on this page so that we can safely clear
8413 * the PagePrivate2 bit and do ordered accounting
8414 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008415 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008416
Chris Masone6dcd2d2008-07-17 12:53:50 -04008417 if (offset) {
8418 btrfs_releasepage(page, GFP_NOFS);
8419 return;
8420 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008421
8422 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008423 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008424
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308425 start = page_start;
Qu Wenruo951c80f2021-01-27 14:38:48 +08008426again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008427 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008428 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008429 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008430 end = min(page_end,
8431 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008432 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008433 * IO on this page will never be started, so we need to account
8434 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8435 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008436 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008437 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308438 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008439 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008440 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008441 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008442 /*
8443 * whoever cleared the private bit is responsible
8444 * for the finish_ordered_io
8445 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008446 if (TestClearPagePrivate2(page)) {
Qu Wenruod239bcb2021-03-25 15:14:35 +08008447 spin_lock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008448 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Qu Wenruoac5804e2021-03-25 15:14:34 +08008449 ordered->truncated_len = min(ordered->truncated_len,
8450 start - ordered->file_offset);
Qu Wenruod239bcb2021-03-25 15:14:35 +08008451 spin_unlock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008452
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008453 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8454 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008455 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008456 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008457 completed_ordered = true;
8458 }
Chris Mason8b62b722009-09-02 16:53:46 -04008459 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008460 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008461 if (!inode_evicting) {
8462 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308463 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008464 &cached_state);
8465 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308466
8467 start = end + 1;
8468 if (start < page_end)
8469 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008470 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008471
Qu Wenruob9d0b382015-09-29 10:35:16 +08008472 /*
8473 * Qgroup reserved space handler
8474 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008475 * 1) Already written to disk or ordered extent already submitted
8476 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8477 * Qgroup will be handled by its qgroup_record then.
8478 * btrfs_qgroup_free_data() call will do nothing here.
8479 *
8480 * 2) Not written to disk yet
8481 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8482 * bit of its io_tree, and free the qgroup reserved data space.
8483 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008484 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008485 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008486 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008487 bool delete = true;
8488
8489 /*
8490 * If there's an ordered extent for this range and we have not
8491 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8492 * in the range for the ordered extent completion. We must also
8493 * not delete the range, otherwise we would lose that bit (and
8494 * any other bits set in the range). Make sure EXTENT_UPTODATE
8495 * is cleared if we don't delete, otherwise it can lead to
8496 * corruptions if the i_size is extented later.
8497 */
8498 if (found_ordered && !completed_ordered)
8499 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008500 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008501 EXTENT_DELALLOC | EXTENT_UPTODATE |
8502 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8503 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008504
8505 __btrfs_releasepage(page, GFP_NOFS);
8506 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008507
Chris Mason4a096752008-07-21 10:29:44 -04008508 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008509 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008510}
8511
Chris Mason9ebefb182007-06-15 13:50:00 -04008512/*
8513 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8514 * called from a page fault handler when a page is first dirtied. Hence we must
8515 * be careful to check for EOF conditions here. We set the page up correctly
8516 * for a written page which means we get ENOSPC checking when writing into
8517 * holes and correct delalloc and unwritten extent mapping on filesystems that
8518 * support these features.
8519 *
8520 * We are not allowed to take the i_mutex here so we have to play games to
8521 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008522 * truncate_setsize() writes the inode size before removing pages, once we have
8523 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008524 * beyond EOF, then the page is guaranteed safe against truncation until we
8525 * unlock the page.
8526 */
Souptick Joardera528a242018-06-06 19:54:44 +05308527vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008528{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008529 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008530 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008532 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8533 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008534 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008535 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008536 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008537 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308538 vm_fault_t ret;
8539 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008540 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308541 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008542 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008543 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308544 u64 end;
8545
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008546 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008547
Jan Karab2b5ef52012-06-12 16:20:45 +02008548 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008549 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008550 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308551 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008552
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308553 /*
8554 * Reserving delalloc space after obtaining the page lock can lead to
8555 * deadlock. For example, if a dirty page is locked by this function
8556 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8557 * dirty page write out, then the btrfs_writepage() function could
8558 * end up waiting indefinitely to get a lock on the page currently
8559 * being processed by btrfs_page_mkwrite() function.
8560 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008561 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8562 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308563 if (!ret2) {
8564 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008565 reserved = 1;
8566 }
Souptick Joardera528a242018-06-06 19:54:44 +05308567 if (ret2) {
8568 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008569 if (reserved)
8570 goto out;
8571 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008572 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008573
Nick Piggin56a76f82009-03-31 15:23:23 -07008574 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008575again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008576 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008577 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008578 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008579
Chris Mason9ebefb182007-06-15 13:50:00 -04008580 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008581 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008582 /* page got truncated out from underneath us */
8583 goto out_unlock;
8584 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008585 wait_on_page_writeback(page);
8586
David Sterbaff13db42015-12-03 14:30:40 +01008587 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008588 ret2 = set_page_extent_mapped(page);
8589 if (ret2 < 0) {
8590 ret = vmf_error(ret2);
8591 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8592 goto out_unlock;
8593 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008594
Chris Masoneb84ae02008-07-17 13:53:27 -04008595 /*
8596 * we can't set the delalloc bits if there are pending ordered
8597 * extents. Drop our locks and wait for them to finish
8598 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008599 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8600 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008601 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008602 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008603 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008604 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008605 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008606 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008607 btrfs_put_ordered_extent(ordered);
8608 goto again;
8609 }
8610
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008611 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008612 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008613 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008614 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308615 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008616 btrfs_delalloc_release_space(BTRFS_I(inode),
8617 data_reserved, page_start,
8618 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308619 }
8620 }
8621
Josef Bacikfbf19082009-10-01 17:10:23 -04008622 /*
Liu Bo54160342016-12-13 12:15:19 -08008623 * page_mkwrite gets called when the page is firstly dirtied after it's
8624 * faulted in, but write(2) could also dirty a page and set delalloc
8625 * bits, thus in this case for space account reason, we still need to
8626 * clear any delalloc bits within this page range since we have to
8627 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008628 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308629 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008630 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8631 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008632
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008633 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008634 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308635 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008636 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008637 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008638 ret = VM_FAULT_SIGBUS;
8639 goto out_unlock;
8640 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008641
8642 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008643 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008644 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008645 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008646 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008647
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008648 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008649 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008650 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008651 }
Chris Mason247e7432008-07-17 12:53:51 -04008652 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008653 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008654 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008655
Filipe Mananabc0939f2021-02-23 12:08:48 +00008656 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008657
David Sterbae43bbe52017-12-12 21:43:52 +01008658 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008659 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008660
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008661 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8662 sb_end_pagefault(inode->i_sb);
8663 extent_changeset_free(data_reserved);
8664 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008665
8666out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008667 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008668 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008669out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008670 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008671 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008672 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008673out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008674 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008675 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008676 return ret;
8677}
8678
Filipe Manana213e8c52018-02-06 20:40:31 +00008679static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008680{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008681 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008682 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008683 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008684 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008685 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008686 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008687 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008688 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008689
Filipe Manana213e8c52018-02-06 20:40:31 +00008690 if (!skip_writeback) {
8691 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8692 (u64)-1);
8693 if (ret)
8694 return ret;
8695 }
Chris Mason39279cc2007-06-12 06:35:45 -04008696
Josef Bacikfcb80c22011-05-03 10:40:22 -04008697 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008698 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8699 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008700 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008701 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008702 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008703 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008704 * be free'd up by the truncate operation, but also have some slack
8705 * space reserved in case it uses space during the truncate (thank you
8706 * very much snapshotting).
8707 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008708 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008709 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008710 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008711 * doesn't end up using space reserved for updating the inode. We also
8712 * need to be able to stop the transaction and start a new one, which
8713 * means we need to be able to update the inode several times, and we
8714 * have no idea of knowing how many times that will be, so we can't just
8715 * reserve 1 item for the entirety of the operation, so that has to be
8716 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008717 *
8718 * So that leaves us with
8719 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008720 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008721 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008722 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008723 * updating the inode.
8724 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008725 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008726 if (!rsv)
8727 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008728 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008729 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008730
Josef Bacik907cbce2011-08-08 13:46:15 -04008731 /*
Josef Bacik07127182011-08-19 10:29:59 -04008732 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008733 * 1 for updating the inode.
8734 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008735 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008736 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008737 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008738 goto out;
8739 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008740
Josef Bacik907cbce2011-08-08 13:46:15 -04008741 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008742 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008743 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008744 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008745
Josef Bacikca7e70f2012-08-27 17:48:15 -04008746 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008747
Yan, Zheng80825102009-11-12 09:35:36 +00008748 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008749 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008750 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008751 BTRFS_EXTENT_DATA_KEY,
8752 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008753 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008754 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008755 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008756
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008757 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008758 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008759 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008760
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008761 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008762 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008763
8764 trans = btrfs_start_transaction(root, 2);
8765 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008766 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008767 trans = NULL;
8768 break;
8769 }
8770
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008771 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008772 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008773 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008774 BUG_ON(ret); /* shouldn't happen */
8775 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008776 }
8777
Josef Bacikddfae632017-10-19 14:16:02 -04008778 /*
8779 * We can't call btrfs_truncate_block inside a trans handle as we could
8780 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8781 * we've truncated everything except the last little bit, and can do
8782 * btrfs_truncate_block and then update the disk_i_size.
8783 */
8784 if (ret == NEED_TRUNCATE_BLOCK) {
8785 btrfs_end_transaction(trans);
8786 btrfs_btree_balance_dirty(fs_info);
8787
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008788 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008789 if (ret)
8790 goto out;
8791 trans = btrfs_start_transaction(root, 1);
8792 if (IS_ERR(trans)) {
8793 ret = PTR_ERR(trans);
8794 goto out;
8795 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008796 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008797 }
8798
Chris Mason917c16b2011-11-08 14:49:59 -05008799 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008800 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008801
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008802 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008803 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008804 if (ret2 && !ret)
8805 ret = ret2;
8806
8807 ret2 = btrfs_end_transaction(trans);
8808 if (ret2 && !ret)
8809 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008810 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008811 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008812out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008813 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008814 /*
8815 * So if we truncate and then write and fsync we normally would just
8816 * write the extents that changed, which is a problem if we need to
8817 * first truncate that entire inode. So set this flag so we write out
8818 * all of the extents in the inode to the sync log so we're completely
8819 * safe.
8820 *
8821 * If no extents were dropped or trimmed we don't need to force the next
8822 * fsync to truncate all the inode's items from the log and re-log them
8823 * all. This means the truncate operation did not change the file size,
8824 * or changed it to a smaller size but there was only an implicit hole
8825 * between the old i_size and the new i_size, and there were no prealloc
8826 * extents beyond i_size to drop.
8827 */
8828 if (extents_found > 0)
8829 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008830
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008831 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008832}
8833
Sven Wegener3b963622008-06-09 21:57:42 -04008834/*
Chris Masond352ac62008-09-29 15:18:18 -04008835 * create a new subvolume directory/inode (helper for the ioctl).
8836 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008837int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008838 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008839 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008840{
Chris Mason39279cc2007-06-12 06:35:45 -04008841 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008842 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008843 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008844 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008845
Nikolay Borisov23125102020-12-07 17:32:36 +02008846 err = btrfs_get_free_objectid(new_root, &ino);
8847 if (err < 0)
8848 return err;
8849
8850 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008851 S_IFDIR | (~current_umask() & S_IRWXUGO),
8852 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008853 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008854 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008855 inode->i_op = &btrfs_dir_inode_operations;
8856 inode->i_fop = &btrfs_dir_file_operations;
8857
Miklos Szeredibfe86842011-10-28 14:13:29 +02008858 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008859 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008860 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008861
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008862 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8863 if (err)
8864 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008865 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008866 new_root->root_key.objectid, err);
8867
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008868 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008869
Yan, Zheng76dda932009-09-21 16:00:26 -04008870 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008871 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008872}
8873
Chris Mason39279cc2007-06-12 06:35:45 -04008874struct inode *btrfs_alloc_inode(struct super_block *sb)
8875{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008876 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008877 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008878 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008879
David Sterba712e36c2017-10-31 17:08:27 +01008880 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008881 if (!ei)
8882 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008883
8884 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008885 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008886 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008887 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008888 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008889 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008890 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008891 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008892 ei->disk_i_size = 0;
8893 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008894 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008895 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008896 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008897 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008898 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008899 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008900
Josef Bacik9e0baf62011-07-15 15:16:44 +00008901 spin_lock_init(&ei->lock);
8902 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008903 if (sb->s_magic != BTRFS_TEST_MAGIC)
8904 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8905 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008906 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008907 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008908 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008909
Miao Xie16cdcec2011-04-22 18:12:22 +08008910 ei->delayed_node = NULL;
8911
chandan r9cc97d62012-07-04 12:48:07 +05308912 ei->i_otime.tv_sec = 0;
8913 ei->i_otime.tv_nsec = 0;
8914
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008915 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008916 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008917 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8918 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8919 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008920 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8921 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008922 ei->io_tree.track_uptodate = true;
8923 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008924 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008925 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008927 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008928 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008929 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05008930 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008931
8932 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008933}
8934
Josef Bacikaaedb552013-10-11 14:44:09 -04008935#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8936void btrfs_test_destroy_inode(struct inode *inode)
8937{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008938 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008939 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8940}
8941#endif
8942
Al Viro26602ca2019-04-10 15:14:41 -04008943void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008944{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008945 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8946}
8947
Nikolay Borisov633cc812020-09-18 12:15:49 +03008948void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008949{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008950 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008951 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8952 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008953
Nikolay Borisov633cc812020-09-18 12:15:49 +03008954 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8955 WARN_ON(vfs_inode->i_data.nrpages);
8956 WARN_ON(inode->block_rsv.reserved);
8957 WARN_ON(inode->block_rsv.size);
8958 WARN_ON(inode->outstanding_extents);
8959 WARN_ON(inode->delalloc_bytes);
8960 WARN_ON(inode->new_delalloc_bytes);
8961 WARN_ON(inode->csum_bytes);
8962 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008963
Chris Mason5a3f23d2009-03-31 13:27:11 -04008964 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008965 * This can happen where we create an inode, but somebody else also
8966 * created the same inode and we need to destroy the one we already
8967 * created.
8968 */
8969 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008970 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008971
Chris Masond3977122009-01-05 21:25:51 -05008972 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008973 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 if (!ordered)
8975 break;
8976 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008977 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008978 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008979 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008980 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008981 btrfs_put_ordered_extent(ordered);
8982 btrfs_put_ordered_extent(ordered);
8983 }
8984 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008985 btrfs_qgroup_check_reserved_leak(inode);
8986 inode_tree_del(inode);
8987 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8988 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8989 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008990}
8991
Al Viro45321ac2010-06-07 13:43:19 -04008992int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008993{
8994 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008995
Naohiro Aota6379ef92013-06-06 09:56:34 +00008996 if (root == NULL)
8997 return 1;
8998
Liu Bofa6ac872013-02-20 14:10:23 +00008999 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009000 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009001 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009002 else
Al Viro45321ac2010-06-07 13:43:19 -04009003 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009004}
9005
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009006static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009007{
9008 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9009
9010 inode_init_once(&ei->vfs_inode);
9011}
9012
David Sterbae67c7182018-02-19 17:24:18 +01009013void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009014{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009015 /*
9016 * Make sure all delayed rcu free inodes are flushed before we
9017 * destroy cache.
9018 */
9019 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009020 kmem_cache_destroy(btrfs_inode_cachep);
9021 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009022 kmem_cache_destroy(btrfs_path_cachep);
9023 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009024 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009025}
9026
Liu Bof5c29bd2017-11-02 17:21:50 -06009027int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009028{
David Sterba837e1972012-09-07 03:00:48 -06009029 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009030 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009031 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9032 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009033 if (!btrfs_inode_cachep)
9034 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009035
David Sterba837e1972012-09-07 03:00:48 -06009036 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009037 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009038 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009039 if (!btrfs_trans_handle_cachep)
9040 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009041
David Sterba837e1972012-09-07 03:00:48 -06009042 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009043 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009044 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009045 if (!btrfs_path_cachep)
9046 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009047
David Sterba837e1972012-09-07 03:00:48 -06009048 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009049 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009050 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009051 if (!btrfs_free_space_cachep)
9052 goto fail;
9053
Christophe Leroy3acd4852019-08-21 15:05:55 +00009054 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9055 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009056 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009057 if (!btrfs_free_space_bitmap_cachep)
9058 goto fail;
9059
Chris Mason39279cc2007-06-12 06:35:45 -04009060 return 0;
9061fail:
9062 btrfs_destroy_cachep();
9063 return -ENOMEM;
9064}
9065
Christian Brauner549c7292021-01-21 14:19:43 +01009066static int btrfs_getattr(struct user_namespace *mnt_userns,
9067 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009068 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009069{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009070 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009071 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009072 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009073 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009074 u32 bi_flags = BTRFS_I(inode)->flags;
9075
9076 stat->result_mask |= STATX_BTIME;
9077 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9078 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9079 if (bi_flags & BTRFS_INODE_APPEND)
9080 stat->attributes |= STATX_ATTR_APPEND;
9081 if (bi_flags & BTRFS_INODE_COMPRESS)
9082 stat->attributes |= STATX_ATTR_COMPRESSED;
9083 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9084 stat->attributes |= STATX_ATTR_IMMUTABLE;
9085 if (bi_flags & BTRFS_INODE_NODUMP)
9086 stat->attributes |= STATX_ATTR_NODUMP;
9087
9088 stat->attributes_mask |= (STATX_ATTR_APPEND |
9089 STATX_ATTR_COMPRESSED |
9090 STATX_ATTR_IMMUTABLE |
9091 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009092
Christian Brauner0d56a452021-01-21 14:19:30 +01009093 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009094 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009095
9096 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009097 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009098 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009099 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009100 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009101 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009102 return 0;
9103}
9104
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009105static int btrfs_rename_exchange(struct inode *old_dir,
9106 struct dentry *old_dentry,
9107 struct inode *new_dir,
9108 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009109{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009110 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009111 struct btrfs_trans_handle *trans;
9112 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009113 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009114 struct inode *new_inode = new_dentry->d_inode;
9115 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009116 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009117 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9118 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009119 u64 old_idx = 0;
9120 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009121 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009122 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009123 bool root_log_pinned = false;
9124 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009125 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009126
9127 /* we only allow rename subvolume link between subvolumes */
9128 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9129 return -EXDEV;
9130
9131 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009132 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9133 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009134 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009135
9136 /*
9137 * We want to reserve the absolute worst case amount of items. So if
9138 * both inodes are subvols and we need to unlink them then that would
9139 * require 4 item modifications, but if they are both normal inodes it
9140 * would require 5 item modifications, so we'll assume their normal
9141 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9142 * should cover the worst case number of items we'll modify.
9143 */
9144 trans = btrfs_start_transaction(root, 12);
9145 if (IS_ERR(trans)) {
9146 ret = PTR_ERR(trans);
9147 goto out_notrans;
9148 }
9149
Josef Bacik00aa8e82021-03-12 15:25:02 -05009150 if (dest != root) {
9151 ret = btrfs_record_root_in_trans(trans, dest);
9152 if (ret)
9153 goto out_fail;
9154 }
Josef Bacik3e174092019-11-15 15:43:06 -05009155
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009156 /*
9157 * We need to find a free sequence number both in the source and
9158 * in the destination directory for the exchange.
9159 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009160 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009161 if (ret)
9162 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009163 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009164 if (ret)
9165 goto out_fail;
9166
9167 BTRFS_I(old_inode)->dir_index = 0ULL;
9168 BTRFS_I(new_inode)->dir_index = 0ULL;
9169
9170 /* Reference for the source. */
9171 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9172 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009173 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009174 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009175 btrfs_pin_log_trans(root);
9176 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009177 ret = btrfs_insert_inode_ref(trans, dest,
9178 new_dentry->d_name.name,
9179 new_dentry->d_name.len,
9180 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009181 btrfs_ino(BTRFS_I(new_dir)),
9182 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009183 if (ret)
9184 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009185 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009186 }
9187
9188 /* And now for the dest. */
9189 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9190 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009191 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009192 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009193 btrfs_pin_log_trans(dest);
9194 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009195 ret = btrfs_insert_inode_ref(trans, root,
9196 old_dentry->d_name.name,
9197 old_dentry->d_name.len,
9198 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009199 btrfs_ino(BTRFS_I(old_dir)),
9200 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009201 if (ret) {
9202 if (need_abort)
9203 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009204 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009205 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009206 }
9207
9208 /* Update inode version and ctime/mtime. */
9209 inode_inc_iversion(old_dir);
9210 inode_inc_iversion(new_dir);
9211 inode_inc_iversion(old_inode);
9212 inode_inc_iversion(new_inode);
9213 old_dir->i_ctime = old_dir->i_mtime = ctime;
9214 new_dir->i_ctime = new_dir->i_mtime = ctime;
9215 old_inode->i_ctime = ctime;
9216 new_inode->i_ctime = ctime;
9217
9218 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009219 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9220 BTRFS_I(old_inode), 1);
9221 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9222 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009223 }
9224
9225 /* src is a subvolume */
9226 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009227 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009228 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009229 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9230 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009231 old_dentry->d_name.name,
9232 old_dentry->d_name.len);
9233 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009234 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009235 }
9236 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009237 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009238 goto out_fail;
9239 }
9240
9241 /* dest is a subvolume */
9242 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009243 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009244 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009245 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9246 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009247 new_dentry->d_name.name,
9248 new_dentry->d_name.len);
9249 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009250 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009251 }
9252 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009253 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009254 goto out_fail;
9255 }
9256
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009257 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009258 new_dentry->d_name.name,
9259 new_dentry->d_name.len, 0, old_idx);
9260 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009261 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009262 goto out_fail;
9263 }
9264
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009265 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009266 old_dentry->d_name.name,
9267 old_dentry->d_name.len, 0, new_idx);
9268 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009269 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009270 goto out_fail;
9271 }
9272
9273 if (old_inode->i_nlink == 1)
9274 BTRFS_I(old_inode)->dir_index = old_idx;
9275 if (new_inode->i_nlink == 1)
9276 BTRFS_I(new_inode)->dir_index = new_idx;
9277
Filipe Manana86e8aa02016-05-05 02:02:27 +01009278 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009279 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9280 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009281 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009282 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009283 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009284 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009285 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9286 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009287 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009288 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009289 }
9290out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009291 /*
9292 * If we have pinned a log and an error happened, we unpin tasks
9293 * trying to sync the log and force them to fallback to a transaction
9294 * commit if the log currently contains any of the inodes involved in
9295 * this rename operation (to ensure we do not persist a log with an
9296 * inconsistent state for any of these inodes or leading to any
9297 * inconsistencies when replayed). If the transaction was aborted, the
9298 * abortion reason is propagated to userspace when attempting to commit
9299 * the transaction. If the log does not contain any of these inodes, we
9300 * allow the tasks to sync it.
9301 */
9302 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009303 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9304 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9305 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009306 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009307 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009308 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009309
9310 if (root_log_pinned) {
9311 btrfs_end_log_trans(root);
9312 root_log_pinned = false;
9313 }
9314 if (dest_log_pinned) {
9315 btrfs_end_log_trans(dest);
9316 dest_log_pinned = false;
9317 }
9318 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009319 ret2 = btrfs_end_transaction(trans);
9320 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009321out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009322 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9323 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009324 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009325
9326 return ret;
9327}
9328
9329static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9330 struct btrfs_root *root,
9331 struct inode *dir,
9332 struct dentry *dentry)
9333{
9334 int ret;
9335 struct inode *inode;
9336 u64 objectid;
9337 u64 index;
9338
Nikolay Borisov543068a2020-12-07 17:32:33 +02009339 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009340 if (ret)
9341 return ret;
9342
9343 inode = btrfs_new_inode(trans, root, dir,
9344 dentry->d_name.name,
9345 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009346 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009347 objectid,
9348 S_IFCHR | WHITEOUT_MODE,
9349 &index);
9350
9351 if (IS_ERR(inode)) {
9352 ret = PTR_ERR(inode);
9353 return ret;
9354 }
9355
9356 inode->i_op = &btrfs_special_inode_operations;
9357 init_special_inode(inode, inode->i_mode,
9358 WHITEOUT_DEV);
9359
9360 ret = btrfs_init_inode_security(trans, inode, dir,
9361 &dentry->d_name);
9362 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009363 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009364
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009365 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9366 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009367 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009368 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009369
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009370 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009371out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009372 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009373 if (ret)
9374 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009375 iput(inode);
9376
Filipe Mananac9901612016-05-05 01:41:57 +01009377 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009378}
9379
Chris Mason39279cc2007-06-12 06:35:45 -04009380static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009381 struct inode *new_dir, struct dentry *new_dentry,
9382 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009384 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009385 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009386 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009387 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9388 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009389 struct inode *new_inode = d_inode(new_dentry);
9390 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009391 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009392 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009393 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009394 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009395 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009396
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009397 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009398 return -EPERM;
9399
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009400 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009401 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009402 return -EXDEV;
9403
Li Zefan33345d012011-04-20 10:31:50 +08009404 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009405 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009406 return -ENOTEMPTY;
9407
Chris Mason39279cc2007-06-12 06:35:45 -04009408 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009409 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009410 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009411
9412
9413 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009414 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009415 new_dentry->d_name.name,
9416 new_dentry->d_name.len);
9417
9418 if (ret) {
9419 if (ret == -EEXIST) {
9420 /* we shouldn't get
9421 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309422 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009423 return ret;
9424 }
9425 } else {
9426 /* maybe -EOVERFLOW */
9427 return ret;
9428 }
9429 }
9430 ret = 0;
9431
Chris Mason5a3f23d2009-03-31 13:27:11 -04009432 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009433 * we're using rename to replace one file with another. Start IO on it
9434 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009435 */
Chris Mason8d875f92014-08-12 10:47:42 -07009436 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009437 filemap_flush(old_inode->i_mapping);
9438
Yan, Zheng76dda932009-09-21 16:00:26 -04009439 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009440 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009441 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009442 /*
9443 * We want to reserve the absolute worst case amount of items. So if
9444 * both inodes are subvols and we need to unlink them then that would
9445 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009447 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9448 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009449 * If our rename has the whiteout flag, we need more 5 units for the
9450 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9451 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009452 */
Filipe Manana5062af32016-05-05 10:26:26 +01009453 trans_num_items = 11;
9454 if (flags & RENAME_WHITEOUT)
9455 trans_num_items += 5;
9456 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009457 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009458 ret = PTR_ERR(trans);
9459 goto out_notrans;
9460 }
Chris Mason5f39d392007-10-15 16:14:19 -04009461
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009462 if (dest != root) {
9463 ret = btrfs_record_root_in_trans(trans, dest);
9464 if (ret)
9465 goto out_fail;
9466 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009467
Nikolay Borisov877574e2017-02-20 13:50:33 +02009468 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009469 if (ret)
9470 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009471
Miao Xie67de1172013-12-26 13:07:06 +08009472 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009473 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009474 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009475 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009476 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009477 btrfs_pin_log_trans(root);
9478 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009479 ret = btrfs_insert_inode_ref(trans, dest,
9480 new_dentry->d_name.name,
9481 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009482 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009483 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009484 if (ret)
9485 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009486 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009487
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009488 inode_inc_iversion(old_dir);
9489 inode_inc_iversion(new_dir);
9490 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009491 old_dir->i_ctime = old_dir->i_mtime =
9492 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009493 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009494
Chris Mason12fcfd22009-03-24 10:24:20 -04009495 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009496 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9497 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009498
Li Zefan33345d012011-04-20 10:31:50 +08009499 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009500 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009501 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009502 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9503 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009504 old_dentry->d_name.name,
9505 old_dentry->d_name.len);
9506 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009507 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009508 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009509 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009510 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009511 goto out_fail;
9512 }
Chris Mason39279cc2007-06-12 06:35:45 -04009513
9514 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009515 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009516 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009517 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009518 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009519 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009520 BUG_ON(new_inode->i_nlink == 0);
9521 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009522 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9523 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009524 new_dentry->d_name.name,
9525 new_dentry->d_name.len);
9526 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009527 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009528 ret = btrfs_orphan_add(trans,
9529 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009530 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009531 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009532 goto out_fail;
9533 }
Chris Mason39279cc2007-06-12 06:35:45 -04009534 }
Josef Bacikaec74772008-07-24 12:12:38 -04009535
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009536 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009537 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009538 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009539 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009540 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009541 goto out_fail;
9542 }
Chris Mason39279cc2007-06-12 06:35:45 -04009543
Miao Xie67de1172013-12-26 13:07:06 +08009544 if (old_inode->i_nlink == 1)
9545 BTRFS_I(old_inode)->dir_index = index;
9546
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009547 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009548 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9549 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009550 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009551 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009552 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553
9554 if (flags & RENAME_WHITEOUT) {
9555 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9556 old_dentry);
9557
9558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009559 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 goto out_fail;
9561 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009562 }
Chris Mason39279cc2007-06-12 06:35:45 -04009563out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009564 /*
9565 * If we have pinned the log and an error happened, we unpin tasks
9566 * trying to sync the log and force them to fallback to a transaction
9567 * commit if the log currently contains any of the inodes involved in
9568 * this rename operation (to ensure we do not persist a log with an
9569 * inconsistent state for any of these inodes or leading to any
9570 * inconsistencies when replayed). If the transaction was aborted, the
9571 * abortion reason is propagated to userspace when attempting to commit
9572 * the transaction. If the log does not contain any of these inodes, we
9573 * allow the tasks to sync it.
9574 */
9575 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009576 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9577 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9578 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009579 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009580 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009581 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009582
9583 btrfs_end_log_trans(root);
9584 log_pinned = false;
9585 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009586 ret2 = btrfs_end_transaction(trans);
9587 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009588out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009589 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009590 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009591
Chris Mason39279cc2007-06-12 06:35:45 -04009592 return ret;
9593}
9594
Christian Brauner549c7292021-01-21 14:19:43 +01009595static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9596 struct dentry *old_dentry, struct inode *new_dir,
9597 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009598{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009600 return -EINVAL;
9601
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 if (flags & RENAME_EXCHANGE)
9603 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9604 new_dentry);
9605
9606 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009607}
9608
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009609struct btrfs_delalloc_work {
9610 struct inode *inode;
9611 struct completion completion;
9612 struct list_head list;
9613 struct btrfs_work work;
9614};
9615
Miao Xie8ccf6f192012-10-25 09:28:04 +00009616static void btrfs_run_delalloc_work(struct btrfs_work *work)
9617{
9618 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009619 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009620
9621 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9622 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009623 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009624 filemap_flush(inode->i_mapping);
9625 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9626 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009627 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009628
Nikolay Borisov076da912018-04-23 10:54:16 +03009629 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009630 complete(&delalloc_work->completion);
9631}
9632
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009633static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009634{
9635 struct btrfs_delalloc_work *work;
9636
David Sterba100d5702015-12-08 14:39:32 +01009637 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009638 if (!work)
9639 return NULL;
9640
9641 init_completion(&work->completion);
9642 INIT_LIST_HEAD(&work->list);
9643 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009644 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009645
9646 return work;
9647}
9648
Chris Masond352ac62008-09-29 15:18:18 -04009649/*
9650 * some fairly slow code that needs optimization. This walks the list
9651 * of all the inodes with pending delalloc and forces them to disk.
9652 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009653static int start_delalloc_inodes(struct btrfs_root *root,
9654 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009655 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009656{
Chris Masonea8c2812008-08-04 23:17:27 -04009657 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009658 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009659 struct btrfs_delalloc_work *work, *next;
9660 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009661 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009662 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009663 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009664
Miao Xie8ccf6f192012-10-25 09:28:04 +00009665 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009666 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009667
Miao Xie573bfb72014-03-06 13:55:03 +08009668 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009669 spin_lock(&root->delalloc_lock);
9670 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009671 while (!list_empty(&splice)) {
9672 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009673 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009674
Miao Xieeb73c1b2013-05-15 07:48:22 +00009675 list_move_tail(&binode->delalloc_inodes,
9676 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009677
9678 if (in_reclaim_context &&
9679 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9680 continue;
9681
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009682 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009683 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009684 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009685 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009686 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009687 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009688
Ethan Lien3cd24c62018-11-01 14:49:03 +08009689 if (snapshot)
9690 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9691 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009692 if (full_flush) {
9693 work = btrfs_alloc_delalloc_work(inode);
9694 if (!work) {
9695 iput(inode);
9696 ret = -ENOMEM;
9697 goto out;
9698 }
9699 list_add_tail(&work->list, &works);
9700 btrfs_queue_work(root->fs_info->flush_workers,
9701 &work->work);
9702 } else {
9703 ret = sync_inode(inode, wbc);
9704 if (!ret &&
9705 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9706 &BTRFS_I(inode)->runtime_flags))
9707 ret = sync_inode(inode, wbc);
9708 btrfs_add_delayed_iput(inode);
9709 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009710 goto out;
9711 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009712 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009713 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009714 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009715 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009716
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009717out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009718 list_for_each_entry_safe(work, next, &works, list) {
9719 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009720 wait_for_completion(&work->completion);
9721 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009722 }
9723
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009724 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009725 spin_lock(&root->delalloc_lock);
9726 list_splice_tail(&splice, &root->delalloc_inodes);
9727 spin_unlock(&root->delalloc_lock);
9728 }
Miao Xie573bfb72014-03-06 13:55:03 +08009729 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009730 return ret;
9731}
9732
Filipe Mananaf9baa502021-04-22 12:08:05 +01009733int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009734{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009735 struct writeback_control wbc = {
9736 .nr_to_write = LONG_MAX,
9737 .sync_mode = WB_SYNC_NONE,
9738 .range_start = 0,
9739 .range_end = LLONG_MAX,
9740 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009741 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009742
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009743 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009744 return -EROFS;
9745
Filipe Mananaf9baa502021-04-22 12:08:05 +01009746 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009747}
9748
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009749int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009750 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009751{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009752 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009753 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009754 .sync_mode = WB_SYNC_NONE,
9755 .range_start = 0,
9756 .range_end = LLONG_MAX,
9757 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009758 struct btrfs_root *root;
9759 struct list_head splice;
9760 int ret;
9761
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009762 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009763 return -EROFS;
9764
9765 INIT_LIST_HEAD(&splice);
9766
Miao Xie573bfb72014-03-06 13:55:03 +08009767 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009768 spin_lock(&fs_info->delalloc_root_lock);
9769 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009770 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009771 /*
9772 * Reset nr_to_write here so we know that we're doing a full
9773 * flush.
9774 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009775 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009776 wbc.nr_to_write = LONG_MAX;
9777
Miao Xieeb73c1b2013-05-15 07:48:22 +00009778 root = list_first_entry(&splice, struct btrfs_root,
9779 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009780 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009781 BUG_ON(!root);
9782 list_move_tail(&root->delalloc_root,
9783 &fs_info->delalloc_roots);
9784 spin_unlock(&fs_info->delalloc_root_lock);
9785
Josef Bacike076ab2a2021-01-07 17:08:30 -05009786 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009787 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009788 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009789 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009790 spin_lock(&fs_info->delalloc_root_lock);
9791 }
9792 spin_unlock(&fs_info->delalloc_root_lock);
9793
Miao Xie6c255e62014-03-06 13:55:01 +08009794 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009795out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009796 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009797 spin_lock(&fs_info->delalloc_root_lock);
9798 list_splice_tail(&splice, &fs_info->delalloc_roots);
9799 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009800 }
Miao Xie573bfb72014-03-06 13:55:03 +08009801 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009802 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009803}
9804
Christian Brauner549c7292021-01-21 14:19:43 +01009805static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9806 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009808 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009809 struct btrfs_trans_handle *trans;
9810 struct btrfs_root *root = BTRFS_I(dir)->root;
9811 struct btrfs_path *path;
9812 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009813 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009814 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009815 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309816 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009817 int name_len;
9818 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009819 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009820 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009821 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009822
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009823 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009824 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009825 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009826
Josef Bacik9ed74f22009-09-11 16:12:44 -04009827 /*
9828 * 2 items for inode item and ref
9829 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009830 * 1 item for updating parent inode item
9831 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009832 * 1 item for xattr if selinux is on
9833 */
Filipe Manana9269d122015-12-31 18:16:29 +00009834 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009835 if (IS_ERR(trans))
9836 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009837
Nikolay Borisov543068a2020-12-07 17:32:33 +02009838 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009839 if (err)
9840 goto out_unlock;
9841
Josef Bacikaec74772008-07-24 12:12:38 -04009842 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009843 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9844 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009845 if (IS_ERR(inode)) {
9846 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009847 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009848 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009849 }
Chris Mason39279cc2007-06-12 06:35:45 -04009850
Casey Schauflerad19db72011-12-15 10:09:07 -05009851 /*
9852 * If the active LSM wants to access the inode during
9853 * d_instantiate it needs these. Smack checks to see
9854 * if the filesystem supports xattrs by looking at the
9855 * ops vector.
9856 */
9857 inode->i_fop = &btrfs_file_operations;
9858 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009859 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009860
9861 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9862 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009863 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009864
Chris Mason39279cc2007-06-12 06:35:45 -04009865 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009866 if (!path) {
9867 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009868 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009869 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009870 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009871 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009872 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009873 datasize = btrfs_file_extent_calc_inline_size(name_len);
9874 err = btrfs_insert_empty_item(trans, root, path, &key,
9875 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009876 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009877 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009878 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009879 }
Chris Mason5f39d392007-10-15 16:14:19 -04009880 leaf = path->nodes[0];
9881 ei = btrfs_item_ptr(leaf, path->slots[0],
9882 struct btrfs_file_extent_item);
9883 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9884 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009885 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009886 btrfs_set_file_extent_encryption(leaf, ei, 0);
9887 btrfs_set_file_extent_compression(leaf, ei, 0);
9888 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9889 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9890
Chris Mason39279cc2007-06-12 06:35:45 -04009891 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009892 write_extent_buffer(leaf, symname, ptr, name_len);
9893 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009894 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009895
Chris Mason39279cc2007-06-12 06:35:45 -04009896 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009897 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009898 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009899 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009900 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009901 /*
9902 * Last step, add directory indexes for our symlink inode. This is the
9903 * last step to avoid extra cleanup of these indexes if an error happens
9904 * elsewhere above.
9905 */
9906 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009907 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9908 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009909 if (err)
9910 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009911
Al Viro1e2e5472018-05-04 08:23:01 -04009912 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009913
9914out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009915 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009916 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009917 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009918 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009919 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009920 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009921 return err;
9922}
Chris Mason16432982008-04-10 10:23:21 -04009923
Filipe Manana8fccebf2020-09-08 11:27:20 +01009924static struct btrfs_trans_handle *insert_prealloc_file_extent(
9925 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009926 struct btrfs_inode *inode,
9927 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009928 u64 file_offset)
9929{
9930 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009931 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009932 struct btrfs_trans_handle *trans = trans_in;
9933 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009934 u64 start = ins->objectid;
9935 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009936 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009937 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009938
9939 memset(&stack_fi, 0, sizeof(stack_fi));
9940
9941 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9942 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9943 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9944 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9945 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9946 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9947 /* Encryption and other encoding is reserved and all 0 */
9948
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009949 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9950 if (qgroup_released < 0)
9951 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009952
9953 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009954 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009955 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009956 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009957 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009958 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009959 return trans;
9960 }
9961
9962 extent_info.disk_offset = start;
9963 extent_info.disk_len = len;
9964 extent_info.data_offset = 0;
9965 extent_info.data_len = len;
9966 extent_info.file_offset = file_offset;
9967 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009968 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009969 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009970 extent_info.insertions = 0;
9971
9972 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009973 if (!path) {
9974 ret = -ENOMEM;
9975 goto free_qgroup;
9976 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009977
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009978 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009979 file_offset + len - 1, &extent_info,
9980 &trans);
9981 btrfs_free_path(path);
9982 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009983 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009984 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009985
9986free_qgroup:
9987 /*
9988 * We have released qgroup data range at the beginning of the function,
9989 * and normally qgroup_released bytes will be freed when committing
9990 * transaction.
9991 * But if we error out early, we have to free what we have released
9992 * or we leak qgroup data reservation.
9993 */
9994 btrfs_qgroup_free_refroot(inode->root->fs_info,
9995 inode->root->root_key.objectid, qgroup_released,
9996 BTRFS_QGROUP_RSV_DATA);
9997 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009998}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009999
Josef Bacik0af3d002010-06-21 14:48:16 -040010000static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10001 u64 start, u64 num_bytes, u64 min_size,
10002 loff_t actual_len, u64 *alloc_hint,
10003 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010004{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010005 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010006 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10007 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010008 struct btrfs_root *root = BTRFS_I(inode)->root;
10009 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010010 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010011 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010012 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010013 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010014 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010015 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010016 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010017 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010018
Josef Bacik0af3d002010-06-21 14:48:16 -040010019 if (trans)
10020 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010021 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010022 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010023 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010024 /*
10025 * If we are severely fragmented we could end up with really
10026 * small allocations, so if the allocator is returning small
10027 * chunks lets make its job easier by only searching for those
10028 * sized chunks.
10029 */
10030 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010031 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10032 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010033 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010034 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010035
10036 /*
10037 * We've reserved this space, and thus converted it from
10038 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10039 * from here on out we will only need to clear our reservation
10040 * for the remaining unreserved area, so advance our
10041 * clear_offset by our extent size.
10042 */
10043 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010044
Josef Bacik0b670dc2015-09-23 17:11:16 -040010045 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010046 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10047 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010048 /*
10049 * Now that we inserted the prealloc extent we can finally
10050 * decrement the number of reservations in the block group.
10051 * If we did it before, we could race with relocation and have
10052 * relocation miss the reserved extent, making it fail later.
10053 */
10054 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010055 if (IS_ERR(trans)) {
10056 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010057 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010058 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010059 break;
10060 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010061
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010062 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010063 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010064
Josef Bacik5dc562c2012-08-17 13:14:17 -040010065 em = alloc_extent_map();
10066 if (!em) {
10067 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10068 &BTRFS_I(inode)->runtime_flags);
10069 goto next;
10070 }
10071
10072 em->start = cur_offset;
10073 em->orig_start = cur_offset;
10074 em->len = ins.offset;
10075 em->block_start = ins.objectid;
10076 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010077 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010078 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010079 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10080 em->generation = trans->transid;
10081
10082 while (1) {
10083 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010084 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010085 write_unlock(&em_tree->lock);
10086 if (ret != -EEXIST)
10087 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010088 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010089 cur_offset + ins.offset - 1,
10090 0);
10091 }
10092 free_extent_map(em);
10093next:
Yan Zhengd899e052008-10-30 14:25:28 -040010094 num_bytes -= ins.offset;
10095 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010096 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010097
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010098 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010099 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010100 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010101 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010102 (actual_len > inode->i_size) &&
10103 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010104 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010105 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010106 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010107 i_size = cur_offset;
10108 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010109 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010110 }
10111
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010112 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010113
10114 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010115 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010116 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010117 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010118 break;
10119 }
Yan Zhengd899e052008-10-30 14:25:28 -040010120
Filipe Manana8fccebf2020-09-08 11:27:20 +010010121 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010122 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010123 trans = NULL;
10124 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010125 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010126 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010127 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010128 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010129 return ret;
10130}
10131
Josef Bacik0af3d002010-06-21 14:48:16 -040010132int btrfs_prealloc_file_range(struct inode *inode, int mode,
10133 u64 start, u64 num_bytes, u64 min_size,
10134 loff_t actual_len, u64 *alloc_hint)
10135{
10136 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10137 min_size, actual_len, alloc_hint,
10138 NULL);
10139}
10140
10141int btrfs_prealloc_file_range_trans(struct inode *inode,
10142 struct btrfs_trans_handle *trans, int mode,
10143 u64 start, u64 num_bytes, u64 min_size,
10144 loff_t actual_len, u64 *alloc_hint)
10145{
10146 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10147 min_size, actual_len, alloc_hint, trans);
10148}
10149
Chris Masone6dcd2d2008-07-17 12:53:50 -040010150static int btrfs_set_page_dirty(struct page *page)
10151{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010152 return __set_page_dirty_nobuffers(page);
10153}
10154
Christian Brauner549c7292021-01-21 14:19:43 +010010155static int btrfs_permission(struct user_namespace *mnt_userns,
10156 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010157{
Li Zefanb83cc962010-12-20 16:04:08 +080010158 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010159 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010160
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010161 if (mask & MAY_WRITE &&
10162 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10163 if (btrfs_root_readonly(root))
10164 return -EROFS;
10165 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10166 return -EACCES;
10167 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010168 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010169}
Chris Mason39279cc2007-06-12 06:35:45 -040010170
Christian Brauner549c7292021-01-21 14:19:43 +010010171static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10172 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010173{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010174 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010175 struct btrfs_trans_handle *trans;
10176 struct btrfs_root *root = BTRFS_I(dir)->root;
10177 struct inode *inode = NULL;
10178 u64 objectid;
10179 u64 index;
10180 int ret = 0;
10181
10182 /*
10183 * 5 units required for adding orphan entry
10184 */
10185 trans = btrfs_start_transaction(root, 5);
10186 if (IS_ERR(trans))
10187 return PTR_ERR(trans);
10188
Nikolay Borisov543068a2020-12-07 17:32:33 +020010189 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010190 if (ret)
10191 goto out;
10192
10193 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010194 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010195 if (IS_ERR(inode)) {
10196 ret = PTR_ERR(inode);
10197 inode = NULL;
10198 goto out;
10199 }
10200
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010201 inode->i_fop = &btrfs_file_operations;
10202 inode->i_op = &btrfs_file_inode_operations;
10203
10204 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010205
Chris Masonb0d5d102014-09-08 13:08:51 -070010206 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10207 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010208 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010209
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010210 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010211 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010212 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010213 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010214 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010215 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010216
Filipe Manana5762b5c2014-08-01 00:10:32 +010010217 /*
10218 * We set number of links to 0 in btrfs_new_inode(), and here we set
10219 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10220 * through:
10221 *
10222 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10223 */
10224 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010225 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010226 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010227 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010228out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010229 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010230 if (ret && inode)
10231 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010232 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010233 return ret;
10234}
10235
David Sterba5cdc84b2018-07-18 20:32:52 +020010236void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010237{
David Sterba5cdc84b2018-07-18 20:32:52 +020010238 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010239 unsigned long index = start >> PAGE_SHIFT;
10240 unsigned long end_index = end >> PAGE_SHIFT;
10241 struct page *page;
10242
10243 while (index <= end_index) {
10244 page = find_get_page(inode->i_mapping, index);
10245 ASSERT(page); /* Pages should be in the extent_io_tree */
10246 set_page_writeback(page);
10247 put_page(page);
10248 index++;
10249 }
10250}
10251
Omar Sandovaled46ff32016-11-03 10:28:14 -070010252#ifdef CONFIG_SWAP
10253/*
10254 * Add an entry indicating a block group or device which is pinned by a
10255 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10256 * negative errno on failure.
10257 */
10258static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10259 bool is_block_group)
10260{
10261 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10262 struct btrfs_swapfile_pin *sp, *entry;
10263 struct rb_node **p;
10264 struct rb_node *parent = NULL;
10265
10266 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10267 if (!sp)
10268 return -ENOMEM;
10269 sp->ptr = ptr;
10270 sp->inode = inode;
10271 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010272 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010273
10274 spin_lock(&fs_info->swapfile_pins_lock);
10275 p = &fs_info->swapfile_pins.rb_node;
10276 while (*p) {
10277 parent = *p;
10278 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10279 if (sp->ptr < entry->ptr ||
10280 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10281 p = &(*p)->rb_left;
10282 } else if (sp->ptr > entry->ptr ||
10283 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10284 p = &(*p)->rb_right;
10285 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010286 if (is_block_group)
10287 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010288 spin_unlock(&fs_info->swapfile_pins_lock);
10289 kfree(sp);
10290 return 1;
10291 }
10292 }
10293 rb_link_node(&sp->node, parent, p);
10294 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10295 spin_unlock(&fs_info->swapfile_pins_lock);
10296 return 0;
10297}
10298
10299/* Free all of the entries pinned by this swapfile. */
10300static void btrfs_free_swapfile_pins(struct inode *inode)
10301{
10302 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10303 struct btrfs_swapfile_pin *sp;
10304 struct rb_node *node, *next;
10305
10306 spin_lock(&fs_info->swapfile_pins_lock);
10307 node = rb_first(&fs_info->swapfile_pins);
10308 while (node) {
10309 next = rb_next(node);
10310 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10311 if (sp->inode == inode) {
10312 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010313 if (sp->is_block_group) {
10314 btrfs_dec_block_group_swap_extents(sp->ptr,
10315 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010316 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010317 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010318 kfree(sp);
10319 }
10320 node = next;
10321 }
10322 spin_unlock(&fs_info->swapfile_pins_lock);
10323}
10324
10325struct btrfs_swap_info {
10326 u64 start;
10327 u64 block_start;
10328 u64 block_len;
10329 u64 lowest_ppage;
10330 u64 highest_ppage;
10331 unsigned long nr_pages;
10332 int nr_extents;
10333};
10334
10335static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10336 struct btrfs_swap_info *bsi)
10337{
10338 unsigned long nr_pages;
10339 u64 first_ppage, first_ppage_reported, next_ppage;
10340 int ret;
10341
10342 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10343 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10344 PAGE_SIZE) >> PAGE_SHIFT;
10345
10346 if (first_ppage >= next_ppage)
10347 return 0;
10348 nr_pages = next_ppage - first_ppage;
10349
10350 first_ppage_reported = first_ppage;
10351 if (bsi->start == 0)
10352 first_ppage_reported++;
10353 if (bsi->lowest_ppage > first_ppage_reported)
10354 bsi->lowest_ppage = first_ppage_reported;
10355 if (bsi->highest_ppage < (next_ppage - 1))
10356 bsi->highest_ppage = next_ppage - 1;
10357
10358 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10359 if (ret < 0)
10360 return ret;
10361 bsi->nr_extents += ret;
10362 bsi->nr_pages += nr_pages;
10363 return 0;
10364}
10365
10366static void btrfs_swap_deactivate(struct file *file)
10367{
10368 struct inode *inode = file_inode(file);
10369
10370 btrfs_free_swapfile_pins(inode);
10371 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10372}
10373
10374static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10375 sector_t *span)
10376{
10377 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010378 struct btrfs_root *root = BTRFS_I(inode)->root;
10379 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010380 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10381 struct extent_state *cached_state = NULL;
10382 struct extent_map *em = NULL;
10383 struct btrfs_device *device = NULL;
10384 struct btrfs_swap_info bsi = {
10385 .lowest_ppage = (sector_t)-1ULL,
10386 };
10387 int ret = 0;
10388 u64 isize;
10389 u64 start;
10390
10391 /*
10392 * If the swap file was just created, make sure delalloc is done. If the
10393 * file changes again after this, the user is doing something stupid and
10394 * we don't really care.
10395 */
10396 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10397 if (ret)
10398 return ret;
10399
10400 /*
10401 * The inode is locked, so these flags won't change after we check them.
10402 */
10403 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10404 btrfs_warn(fs_info, "swapfile must not be compressed");
10405 return -EINVAL;
10406 }
10407 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10408 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10409 return -EINVAL;
10410 }
10411 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10412 btrfs_warn(fs_info, "swapfile must not be checksummed");
10413 return -EINVAL;
10414 }
10415
10416 /*
10417 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010418 * under us. The exclop protection makes sure they aren't running/won't
10419 * run concurrently while we are mapping the swap extents, and
10420 * fs_info->swapfile_pins prevents them from running while the swap
10421 * file is active and moving the extents. Note that this also prevents
10422 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010423 * really worth the trouble to allow it.
10424 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010425 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010426 btrfs_warn(fs_info,
10427 "cannot activate swapfile while exclusive operation is running");
10428 return -EBUSY;
10429 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010430
10431 /*
10432 * Prevent snapshot creation while we are activating the swap file.
10433 * We do not want to race with snapshot creation. If snapshot creation
10434 * already started before we bumped nr_swapfiles from 0 to 1 and
10435 * completes before the first write into the swap file after it is
10436 * activated, than that write would fallback to COW.
10437 */
10438 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10439 btrfs_exclop_finish(fs_info);
10440 btrfs_warn(fs_info,
10441 "cannot activate swapfile because snapshot creation is in progress");
10442 return -EINVAL;
10443 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010444 /*
10445 * Snapshots can create extents which require COW even if NODATACOW is
10446 * set. We use this counter to prevent snapshots. We must increment it
10447 * before walking the extents because we don't want a concurrent
10448 * snapshot to run after we've already checked the extents.
10449 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010450 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010451
10452 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10453
10454 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10455 start = 0;
10456 while (start < isize) {
10457 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010458 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010459 u64 len = isize - start;
10460
Omar Sandoval39b07b52019-12-02 17:34:23 -080010461 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010462 if (IS_ERR(em)) {
10463 ret = PTR_ERR(em);
10464 goto out;
10465 }
10466
10467 if (em->block_start == EXTENT_MAP_HOLE) {
10468 btrfs_warn(fs_info, "swapfile must not have holes");
10469 ret = -EINVAL;
10470 goto out;
10471 }
10472 if (em->block_start == EXTENT_MAP_INLINE) {
10473 /*
10474 * It's unlikely we'll ever actually find ourselves
10475 * here, as a file small enough to fit inline won't be
10476 * big enough to store more than the swap header, but in
10477 * case something changes in the future, let's catch it
10478 * here rather than later.
10479 */
10480 btrfs_warn(fs_info, "swapfile must not be inline");
10481 ret = -EINVAL;
10482 goto out;
10483 }
10484 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10485 btrfs_warn(fs_info, "swapfile must not be compressed");
10486 ret = -EINVAL;
10487 goto out;
10488 }
10489
10490 logical_block_start = em->block_start + (start - em->start);
10491 len = min(len, em->len - (start - em->start));
10492 free_extent_map(em);
10493 em = NULL;
10494
Boris Burkova84d5d42020-08-18 11:00:05 -070010495 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010496 if (ret < 0) {
10497 goto out;
10498 } else if (ret) {
10499 ret = 0;
10500 } else {
10501 btrfs_warn(fs_info,
10502 "swapfile must not be copy-on-write");
10503 ret = -EINVAL;
10504 goto out;
10505 }
10506
10507 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10508 if (IS_ERR(em)) {
10509 ret = PTR_ERR(em);
10510 goto out;
10511 }
10512
10513 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10514 btrfs_warn(fs_info,
10515 "swapfile must have single data profile");
10516 ret = -EINVAL;
10517 goto out;
10518 }
10519
10520 if (device == NULL) {
10521 device = em->map_lookup->stripes[0].dev;
10522 ret = btrfs_add_swapfile_pin(inode, device, false);
10523 if (ret == 1)
10524 ret = 0;
10525 else if (ret)
10526 goto out;
10527 } else if (device != em->map_lookup->stripes[0].dev) {
10528 btrfs_warn(fs_info, "swapfile must be on one device");
10529 ret = -EINVAL;
10530 goto out;
10531 }
10532
10533 physical_block_start = (em->map_lookup->stripes[0].physical +
10534 (logical_block_start - em->start));
10535 len = min(len, em->len - (logical_block_start - em->start));
10536 free_extent_map(em);
10537 em = NULL;
10538
10539 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10540 if (!bg) {
10541 btrfs_warn(fs_info,
10542 "could not find block group containing swapfile");
10543 ret = -EINVAL;
10544 goto out;
10545 }
10546
Filipe Manana195a49e2021-02-05 12:55:37 +000010547 if (!btrfs_inc_block_group_swap_extents(bg)) {
10548 btrfs_warn(fs_info,
10549 "block group for swapfile at %llu is read-only%s",
10550 bg->start,
10551 atomic_read(&fs_info->scrubs_running) ?
10552 " (scrub running)" : "");
10553 btrfs_put_block_group(bg);
10554 ret = -EINVAL;
10555 goto out;
10556 }
10557
Omar Sandovaled46ff32016-11-03 10:28:14 -070010558 ret = btrfs_add_swapfile_pin(inode, bg, true);
10559 if (ret) {
10560 btrfs_put_block_group(bg);
10561 if (ret == 1)
10562 ret = 0;
10563 else
10564 goto out;
10565 }
10566
10567 if (bsi.block_len &&
10568 bsi.block_start + bsi.block_len == physical_block_start) {
10569 bsi.block_len += len;
10570 } else {
10571 if (bsi.block_len) {
10572 ret = btrfs_add_swap_extent(sis, &bsi);
10573 if (ret)
10574 goto out;
10575 }
10576 bsi.start = start;
10577 bsi.block_start = physical_block_start;
10578 bsi.block_len = len;
10579 }
10580
10581 start += len;
10582 }
10583
10584 if (bsi.block_len)
10585 ret = btrfs_add_swap_extent(sis, &bsi);
10586
10587out:
10588 if (!IS_ERR_OR_NULL(em))
10589 free_extent_map(em);
10590
10591 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10592
10593 if (ret)
10594 btrfs_swap_deactivate(file);
10595
Filipe Mananadd0734f2021-02-05 12:55:38 +000010596 btrfs_drew_write_unlock(&root->snapshot_lock);
10597
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010598 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010599
10600 if (ret)
10601 return ret;
10602
10603 if (device)
10604 sis->bdev = device->bdev;
10605 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10606 sis->max = bsi.nr_pages;
10607 sis->pages = bsi.nr_pages - 1;
10608 sis->highest_bit = bsi.nr_pages - 1;
10609 return bsi.nr_extents;
10610}
10611#else
10612static void btrfs_swap_deactivate(struct file *file)
10613{
10614}
10615
10616static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10617 sector_t *span)
10618{
10619 return -EOPNOTSUPP;
10620}
10621#endif
10622
Filipe Manana2766ff62020-11-04 11:07:34 +000010623/*
10624 * Update the number of bytes used in the VFS' inode. When we replace extents in
10625 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10626 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10627 * always get a correct value.
10628 */
10629void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10630 const u64 add_bytes,
10631 const u64 del_bytes)
10632{
10633 if (add_bytes == del_bytes)
10634 return;
10635
10636 spin_lock(&inode->lock);
10637 if (del_bytes > 0)
10638 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10639 if (add_bytes > 0)
10640 inode_add_bytes(&inode->vfs_inode, add_bytes);
10641 spin_unlock(&inode->lock);
10642}
10643
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010644static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010645 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010646 .lookup = btrfs_lookup,
10647 .create = btrfs_create,
10648 .unlink = btrfs_unlink,
10649 .link = btrfs_link,
10650 .mkdir = btrfs_mkdir,
10651 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010652 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010653 .symlink = btrfs_symlink,
10654 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010655 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010656 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010657 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010658 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010659 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010660 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010661 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010662 .fileattr_get = btrfs_fileattr_get,
10663 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010664};
Yan, Zheng76dda932009-09-21 16:00:26 -040010665
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010666static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010667 .llseek = generic_file_llseek,
10668 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010669 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010670 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010671 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010672#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010673 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010674#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010675 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010676 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010677};
10678
Chris Mason35054392009-01-21 13:11:13 -050010679/*
10680 * btrfs doesn't support the bmap operation because swapfiles
10681 * use bmap to make a mapping of extents in the file. They assume
10682 * these extents won't change over the life of the file and they
10683 * use the bmap result to do IO directly to the drive.
10684 *
10685 * the btrfs bmap call would return logical addresses that aren't
10686 * suitable for IO and they also will change frequently as COW
10687 * operations happen. So, swapfile + btrfs == corruption.
10688 *
10689 * For now we're avoiding this by dropping bmap.
10690 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010691static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010692 .readpage = btrfs_readpage,
10693 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010694 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010695 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010696 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010697 .invalidatepage = btrfs_invalidatepage,
10698 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010699#ifdef CONFIG_MIGRATION
10700 .migratepage = btrfs_migratepage,
10701#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010702 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010703 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010704 .swap_activate = btrfs_swap_activate,
10705 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010706};
10707
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010708static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010709 .getattr = btrfs_getattr,
10710 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010711 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010712 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010713 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010714 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010715 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010716 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010717 .fileattr_get = btrfs_fileattr_get,
10718 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010719};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010720static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010721 .getattr = btrfs_getattr,
10722 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010723 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010724 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010725 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010726 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010727 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010728};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010729static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010730 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010731 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010732 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010733 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010734 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010735 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010736};
Yan, Zheng76dda932009-09-21 16:00:26 -040010737
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010738const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010739 .d_delete = btrfs_dentry_delete,
10740};