blob: 9a9158b19205d4fbc81f8e14defbbf194668c00a [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;
Qu Wenruo38a39ac72021-04-08 20:32:27 +0800949 btrfs_writepage_endio_finish_ordered(inode, p, start,
950 end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200951
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100952 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300953 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100954 PAGE_END_WRITEBACK |
955 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100956 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100957 }
Chris Mason771ed682008-11-06 22:02:51 -0500958 alloc_hint = ins.objectid + ins.offset;
959 kfree(async_extent);
960 cond_resched();
961 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100962 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500963out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400964 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400965 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300967 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500968 async_extent->start +
969 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400970 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100971 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400972 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800973 PAGE_UNLOCK | PAGE_START_WRITEBACK |
974 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100975 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100976 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500977 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500978}
979
Nikolay Borisov43c69842020-06-03 08:55:02 +0300980static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400981 u64 num_bytes)
982{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300983 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400984 struct extent_map *em;
985 u64 alloc_hint = 0;
986
987 read_lock(&em_tree->lock);
988 em = search_extent_mapping(em_tree, start, num_bytes);
989 if (em) {
990 /*
991 * if block start isn't an actual block number then find the
992 * first block in this inode and use that as a hint. If that
993 * block is also bogus then just don't worry about it.
994 */
995 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
996 free_extent_map(em);
997 em = search_extent_mapping(em_tree, 0, 0);
998 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
999 alloc_hint = em->block_start;
1000 if (em)
1001 free_extent_map(em);
1002 } else {
1003 alloc_hint = em->block_start;
1004 free_extent_map(em);
1005 }
1006 }
1007 read_unlock(&em_tree->lock);
1008
1009 return alloc_hint;
1010}
1011
Chris Mason771ed682008-11-06 22:02:51 -05001012/*
1013 * when extent_io.c finds a delayed allocation range in the file,
1014 * the call backs end up in this code. The basic idea is to
1015 * allocate extents on disk for the range, and create ordered data structs
1016 * in ram to track those extents.
1017 *
1018 * locked_page is the page that writepage had locked already. We use
1019 * it to make sure we don't do extra locks or unlocks.
1020 *
1021 * *page_started is set to one if we unlock locked_page and do everything
1022 * required to start IO on it. It may be clean and already done with
1023 * IO when we return.
1024 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001025static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001026 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001027 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001028 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001029{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001030 struct btrfs_root *root = inode->root;
1031 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001032 u64 alloc_hint = 0;
1033 u64 num_bytes;
1034 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001035 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001036 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001037 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001038 struct btrfs_key ins;
1039 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001040 unsigned clear_bits;
1041 unsigned long page_ops;
1042 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001043 int ret = 0;
1044
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001045 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001046 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001047 ret = -EINVAL;
1048 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001049 }
Chris Mason771ed682008-11-06 22:02:51 -05001050
Qu Wenruofda28322013-02-26 08:10:22 +00001051 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001052 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001053 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001054
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001055 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001056
Chris Mason771ed682008-11-06 22:02:51 -05001057 if (start == 0) {
1058 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001059 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001060 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001061 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001062 /*
1063 * We use DO_ACCOUNTING here because we need the
1064 * delalloc_release_metadata to be run _after_ we drop
1065 * our outstanding extent for clearing delalloc for this
1066 * range.
1067 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001068 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001069 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001070 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1071 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001072 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001073 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001074 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001075 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001076 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001079 }
1080 }
Chris Masonc8b97812008-10-29 14:49:59 -04001081
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001082 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1083 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001084
Filipe Manana432cd2a2020-06-08 13:32:55 +01001085 /*
1086 * Relocation relies on the relocated extents to have exactly the same
1087 * size as the original extents. Normally writeback for relocation data
1088 * extents follows a NOCOW path because relocation preallocates the
1089 * extents. However, due to an operation such as scrub turning a block
1090 * group to RO mode, it may fallback to COW mode, so we must make sure
1091 * an extent allocated during COW has exactly the requested size and can
1092 * not be split into smaller extents, otherwise relocation breaks and
1093 * fails during the stage where it updates the bytenr of file extent
1094 * items.
1095 */
1096 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1097 min_alloc_size = num_bytes;
1098 else
1099 min_alloc_size = fs_info->sectorsize;
1100
Anand Jain3752d222018-02-15 12:29:38 +08001101 while (num_bytes > 0) {
1102 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001103 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001104 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001105 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001106 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001107 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001108 cur_alloc_size = ins.offset;
1109 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001110
Chris Mason771ed682008-11-06 22:02:51 -05001111 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001112 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001113 start, /* orig_start */
1114 ins.objectid, /* block_start */
1115 ins.offset, /* block_len */
1116 ins.offset, /* orig_block_len */
1117 ram_size, /* ram_bytes */
1118 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001119 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001120 if (IS_ERR(em)) {
1121 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001122 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001123 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001124 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001125
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001126 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001127 ram_size, cur_alloc_size,
1128 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001129 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001130 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001131
Yan Zheng17d217f2008-12-12 10:03:38 -05001132 if (root->root_key.objectid ==
1133 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001134 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001135 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001136 /*
1137 * Only drop cache here, and process as normal.
1138 *
1139 * We must not allow extent_clear_unlock_delalloc()
1140 * at out_unlock label to free meta of this ordered
1141 * extent, as its meta should be freed by
1142 * btrfs_finish_ordered_io().
1143 *
1144 * So we must continue until @start is increased to
1145 * skip current ordered extent.
1146 */
Josef Bacik00361582013-08-14 14:02:47 -04001147 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001148 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001149 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001150 }
1151
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001153
Chris Masonc8b97812008-10-29 14:49:59 -04001154 /* we're not doing compressed IO, don't unlock the first
1155 * page (which the caller expects to stay locked), don't
1156 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001157 *
1158 * Do set the Private2 bit so we know this page was properly
1159 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001160 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001161 page_ops = unlock ? PAGE_UNLOCK : 0;
1162 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001163
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001164 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001165 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001166 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001167 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001168 if (num_bytes < cur_alloc_size)
1169 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001170 else
Anand Jain3752d222018-02-15 12:29:38 +08001171 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001172 alloc_hint = ins.objectid + ins.offset;
1173 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001174 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001175
1176 /*
1177 * btrfs_reloc_clone_csums() error, since start is increased
1178 * extent_clear_unlock_delalloc() at out_unlock label won't
1179 * free metadata of current ordered extent, we're OK to exit.
1180 */
1181 if (ret)
1182 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001183 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001185 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001186
Filipe Mananad9f85962014-08-25 10:43:00 +01001187out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001188 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001189out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001190 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001191 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001192out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001193 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1194 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001195 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001196 /*
1197 * If we reserved an extent for our delalloc range (or a subrange) and
1198 * failed to create the respective ordered extent, then it means that
1199 * when we reserved the extent we decremented the extent's size from
1200 * the data space_info's bytes_may_use counter and incremented the
1201 * space_info's bytes_reserved counter by the same amount. We must make
1202 * sure extent_clear_unlock_delalloc() does not try to decrement again
1203 * the data space_info's bytes_may_use counter, therefore we do not pass
1204 * it the flag EXTENT_CLEAR_DATA_RESV.
1205 */
1206 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001207 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001208 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001209 locked_page,
1210 clear_bits,
1211 page_ops);
1212 start += cur_alloc_size;
1213 if (start >= end)
1214 goto out;
1215 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001216 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001217 clear_bits | EXTENT_CLEAR_DATA_RESV,
1218 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001219 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001220}
Chris Masonc8b97812008-10-29 14:49:59 -04001221
Chris Mason771ed682008-11-06 22:02:51 -05001222/*
1223 * work queue call back to started compression on a file and pages
1224 */
1225static noinline void async_cow_start(struct btrfs_work *work)
1226{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001227 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001228 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001229
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001231
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001232 compressed_extents = compress_file_range(async_chunk);
1233 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001234 btrfs_add_delayed_iput(async_chunk->inode);
1235 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001236 }
Chris Mason771ed682008-11-06 22:02:51 -05001237}
1238
1239/*
1240 * work queue call back to submit previously compressed pages
1241 */
1242static noinline void async_cow_submit(struct btrfs_work *work)
1243{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001244 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1245 work);
1246 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001247 unsigned long nr_pages;
1248
Nikolay Borisovb5326272019-03-12 17:20:25 +02001249 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001250 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001251
David Sterba093258e2018-02-26 16:15:17 +01001252 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001253 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001254 5 * SZ_1M)
1255 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001256
Nikolay Borisov4546d172019-01-03 10:50:03 +02001257 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001258 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001259 * in which case we don't have anything to submit, yet we need to
1260 * always adjust ->async_delalloc_pages as its paired with the init
1261 * happening in cow_file_range_async
1262 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001263 if (async_chunk->inode)
1264 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001265}
Chris Masonc8b97812008-10-29 14:49:59 -04001266
Chris Mason771ed682008-11-06 22:02:51 -05001267static noinline void async_cow_free(struct btrfs_work *work)
1268{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001269 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001270
Nikolay Borisovb5326272019-03-12 17:20:25 +02001271 async_chunk = container_of(work, struct async_chunk, work);
1272 if (async_chunk->inode)
1273 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001274 if (async_chunk->blkcg_css)
1275 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001276 /*
1277 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001278 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001279 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001280 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001281 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001282}
1283
Nikolay Borisov751b6432020-06-03 08:55:22 +03001284static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001285 struct writeback_control *wbc,
1286 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001287 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001288 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001289{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001290 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001291 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001292 struct async_cow *ctx;
1293 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001294 unsigned long nr_pages;
1295 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001296 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1297 int i;
1298 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001299 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001300 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001301
Nikolay Borisov751b6432020-06-03 08:55:22 +03001302 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001303
Nikolay Borisov751b6432020-06-03 08:55:22 +03001304 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001305 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1306 num_chunks = 1;
1307 should_compress = false;
1308 } else {
1309 should_compress = true;
1310 }
1311
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001312 nofs_flag = memalloc_nofs_save();
1313 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1314 memalloc_nofs_restore(nofs_flag);
1315
Nikolay Borisov97db1202019-03-12 17:20:24 +02001316 if (!ctx) {
1317 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1318 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1319 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001320 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1321 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001322
Nikolay Borisov751b6432020-06-03 08:55:22 +03001323 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1324 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001325 return -ENOMEM;
1326 }
1327
1328 async_chunk = ctx->chunks;
1329 atomic_set(&ctx->num_chunks, num_chunks);
1330
1331 for (i = 0; i < num_chunks; i++) {
1332 if (should_compress)
1333 cur_end = min(end, start + SZ_512K - 1);
1334 else
1335 cur_end = end;
1336
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001337 /*
1338 * igrab is called higher up in the call chain, take only the
1339 * lightweight reference for the callback lifetime
1340 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001341 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001343 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001344 async_chunk[i].start = start;
1345 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001346 async_chunk[i].write_flags = write_flags;
1347 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001348
Chris Mason1d53c9e2019-07-10 12:28:16 -07001349 /*
1350 * The locked_page comes all the way from writepage and its
1351 * the original page we were actually given. As we spread
1352 * this large delalloc region across multiple async_chunk
1353 * structs, only the first struct needs a pointer to locked_page
1354 *
1355 * This way we don't need racey decisions about who is supposed
1356 * to unlock it.
1357 */
1358 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001359 /*
1360 * Depending on the compressibility, the pages might or
1361 * might not go through async. We want all of them to
1362 * be accounted against wbc once. Let's do it here
1363 * before the paths diverge. wbc accounting is used
1364 * only for foreign writeback detection and doesn't
1365 * need full accuracy. Just account the whole thing
1366 * against the first page.
1367 */
1368 wbc_account_cgroup_owner(wbc, locked_page,
1369 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001370 async_chunk[i].locked_page = locked_page;
1371 locked_page = NULL;
1372 } else {
1373 async_chunk[i].locked_page = NULL;
1374 }
1375
Chris Masonec39f762019-07-10 12:28:17 -07001376 if (blkcg_css != blkcg_root_css) {
1377 css_get(blkcg_css);
1378 async_chunk[i].blkcg_css = blkcg_css;
1379 } else {
1380 async_chunk[i].blkcg_css = NULL;
1381 }
1382
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001383 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1384 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001385
Nikolay Borisov97db1202019-03-12 17:20:24 +02001386 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001387 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001388
Nikolay Borisov97db1202019-03-12 17:20:24 +02001389 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001390
Chris Mason771ed682008-11-06 22:02:51 -05001391 *nr_written += nr_pages;
1392 start = cur_end + 1;
1393 }
1394 *page_started = 1;
1395 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001396}
1397
Naohiro Aota42c01102021-02-04 19:22:07 +09001398static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1399 struct page *locked_page, u64 start,
1400 u64 end, int *page_started,
1401 unsigned long *nr_written)
1402{
1403 int ret;
1404
1405 ret = cow_file_range(inode, locked_page, start, end, page_started,
1406 nr_written, 0);
1407 if (ret)
1408 return ret;
1409
1410 if (*page_started)
1411 return 0;
1412
1413 __set_page_dirty_nobuffers(locked_page);
1414 account_page_redirty(locked_page);
1415 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1416 *page_started = 1;
1417
1418 return 0;
1419}
1420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001421static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001422 u64 bytenr, u64 num_bytes)
1423{
1424 int ret;
1425 struct btrfs_ordered_sum *sums;
1426 LIST_HEAD(list);
1427
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001429 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001430 if (ret == 0 && list_empty(&list))
1431 return 0;
1432
1433 while (!list_empty(&list)) {
1434 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1435 list_del(&sums->list);
1436 kfree(sums);
1437 }
Liu Bo58113752018-01-31 17:09:13 -07001438 if (ret < 0)
1439 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001440 return 1;
1441}
1442
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001443static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001444 const u64 start, const u64 end,
1445 int *page_started, unsigned long *nr_written)
1446{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001447 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1448 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001449 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001450 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001451 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001452 u64 range_start = start;
1453 u64 count;
1454
1455 /*
1456 * If EXTENT_NORESERVE is set it means that when the buffered write was
1457 * made we had not enough available data space and therefore we did not
1458 * reserve data space for it, since we though we could do NOCOW for the
1459 * respective file range (either there is prealloc extent or the inode
1460 * has the NOCOW bit set).
1461 *
1462 * However when we need to fallback to COW mode (because for example the
1463 * block group for the corresponding extent was turned to RO mode by a
1464 * scrub or relocation) we need to do the following:
1465 *
1466 * 1) We increment the bytes_may_use counter of the data space info.
1467 * If COW succeeds, it allocates a new data extent and after doing
1468 * that it decrements the space info's bytes_may_use counter and
1469 * increments its bytes_reserved counter by the same amount (we do
1470 * this at btrfs_add_reserved_bytes()). So we need to increment the
1471 * bytes_may_use counter to compensate (when space is reserved at
1472 * buffered write time, the bytes_may_use counter is incremented);
1473 *
1474 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1475 * that if the COW path fails for any reason, it decrements (through
1476 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1477 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001478 *
1479 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001480 * space cache inode or an inode of the data relocation tree, we must
1481 * also increment bytes_may_use of the data space_info for the same
1482 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001483 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001484 * group that contains that extent to RO mode and therefore force COW
1485 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001486 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001487 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001488 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001489 if (count > 0 || is_space_ino || is_reloc_ino) {
1490 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001491 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001492 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1493
Filipe Manana6bd335b2020-06-08 13:33:05 +01001494 if (is_space_ino || is_reloc_ino)
1495 bytes = range_bytes;
1496
Filipe Manana467dc472020-05-27 11:16:07 +01001497 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001498 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001499 spin_unlock(&sinfo->lock);
1500
Filipe Manana2166e5e2020-05-27 11:16:19 +01001501 if (count > 0)
1502 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1503 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001504 }
1505
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001506 return cow_file_range(inode, locked_page, start, end, page_started,
1507 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001508}
1509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
1511 * when nowcow writeback call back. This checks for snapshots or COW copies
1512 * of the extents that exist in the file, and COWs the file as required.
1513 *
1514 * If no cow copies or snapshots exist, we write directly to the existing
1515 * blocks on disk
1516 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001517static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001518 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001519 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001520 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001521 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001522{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001523 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1524 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001525 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001526 u64 cow_start = (u64)-1;
1527 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001528 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001529 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001530 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1531 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001532 bool nocow = false;
1533 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001534 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001535
1536 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001538 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001540 EXTENT_DO_ACCOUNTING |
1541 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001542 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001544 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001545 }
Li Zefan82d59022011-04-20 10:33:24 +08001546
Yan Zheng80ff3852008-10-30 14:20:02 -04001547 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001548 struct btrfs_key found_key;
1549 struct btrfs_file_extent_item *fi;
1550 struct extent_buffer *leaf;
1551 u64 extent_end;
1552 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001553 u64 num_bytes = 0;
1554 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001555 u64 ram_bytes;
1556 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001557
1558 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001559
Liu Boe4c3b2d2017-01-30 12:25:28 -08001560 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001561 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001562 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001564
1565 /*
1566 * If there is no extent for our range when doing the initial
1567 * search, then go back to the previous slot as it will be the
1568 * one containing the search offset
1569 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1571 leaf = path->nodes[0];
1572 btrfs_item_key_to_cpu(leaf, &found_key,
1573 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001574 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001575 found_key.type == BTRFS_EXTENT_DATA_KEY)
1576 path->slots[0]--;
1577 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001578 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001579next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001581 leaf = path->nodes[0];
1582 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1583 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001584 if (ret < 0) {
1585 if (cow_start != (u64)-1)
1586 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001587 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001588 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001589 if (ret > 0)
1590 break;
1591 leaf = path->nodes[0];
1592 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001593
Yan Zheng80ff3852008-10-30 14:20:02 -04001594 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001595
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001596 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001597 if (found_key.objectid > ino)
1598 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001599 /*
1600 * Keep searching until we find an EXTENT_ITEM or there are no
1601 * more extents for this inode
1602 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001603 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1604 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1605 path->slots[0]++;
1606 goto next_slot;
1607 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001608
1609 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001610 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 found_key.offset > end)
1612 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001613
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001614 /*
1615 * If the found extent starts after requested offset, then
1616 * adjust extent_end to be right before this extent begins
1617 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001618 if (found_key.offset > cur_offset) {
1619 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001620 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001621 goto out_check;
1622 }
Chris Masonc31f8832008-01-08 15:46:31 -05001623
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001624 /*
1625 * Found extent which begins before our range and potentially
1626 * intersect it
1627 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001628 fi = btrfs_item_ptr(leaf, path->slots[0],
1629 struct btrfs_file_extent_item);
1630 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001631
Josef Bacikcc95bef2013-04-04 14:31:27 -04001632 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001633 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1634 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001635 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001637 extent_end = found_key.offset +
1638 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001639 disk_num_bytes =
1640 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001641 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001642 * If the extent we got ends before our current offset,
1643 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001644 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001645 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001646 path->slots[0]++;
1647 goto next_slot;
1648 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001649 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001650 if (disk_bytenr == 0)
1651 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001652 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001653 if (btrfs_file_extent_compression(leaf, fi) ||
1654 btrfs_file_extent_encryption(leaf, fi) ||
1655 btrfs_file_extent_other_encoding(leaf, fi))
1656 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001657 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001658 * If extent is created before the last volume's snapshot
1659 * this implies the extent is shared, hence we can't do
1660 * nocow. This is the same check as in
1661 * btrfs_cross_ref_exist but without calling
1662 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001663 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001664 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001665 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001666 btrfs_root_last_snapshot(&root->root_item))
1667 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001668 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1669 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001670
1671 /*
1672 * The following checks can be expensive, as they need to
1673 * take other locks and do btree or rbtree searches, so
1674 * release the path to avoid blocking other tasks for too
1675 * long.
1676 */
1677 btrfs_release_path(path);
1678
Liu Bo58113752018-01-31 17:09:13 -07001679 ret = btrfs_cross_ref_exist(root, ino,
1680 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001681 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001682 if (ret) {
1683 /*
1684 * ret could be -EIO if the above fails to read
1685 * metadata.
1686 */
1687 if (ret < 0) {
1688 if (cow_start != (u64)-1)
1689 cur_offset = cow_start;
1690 goto error;
1691 }
1692
Nikolay Borisov3e024842019-08-21 10:42:03 +03001693 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001694 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001695 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001697 disk_bytenr += cur_offset - found_key.offset;
1698 num_bytes = min(end + 1, extent_end) - cur_offset;
1699 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001700 * If there are pending snapshots for this root, we
1701 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001702 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001703 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001704 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001705 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001706 * force cow if csum exists in the range.
1707 * this ensure that csum for a given extent are
1708 * either valid or do not exist.
1709 */
Liu Bo58113752018-01-31 17:09:13 -07001710 ret = csum_exist_in_range(fs_info, disk_bytenr,
1711 num_bytes);
1712 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001713 /*
1714 * ret could be -EIO if the above fails to read
1715 * metadata.
1716 */
1717 if (ret < 0) {
1718 if (cow_start != (u64)-1)
1719 cur_offset = cow_start;
1720 goto error;
1721 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001722 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001723 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001724 }
Filipe Manana20903032021-02-05 12:55:36 +00001725 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001726 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001727 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001728 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001729 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001730 extent_end = found_key.offset + ram_bytes;
1731 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001732 /* Skip extents outside of our requested range */
1733 if (extent_end <= start) {
1734 path->slots[0]++;
1735 goto next_slot;
1736 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001737 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001738 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001739 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001740 }
1741out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001742 /*
1743 * If nocow is false then record the beginning of the range
1744 * that needs to be COWed
1745 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001746 if (!nocow) {
1747 if (cow_start == (u64)-1)
1748 cow_start = cur_offset;
1749 cur_offset = extent_end;
1750 if (cur_offset > end)
1751 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001752 if (!path->nodes[0])
1753 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001754 path->slots[0]++;
1755 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001756 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001757
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001758 /*
1759 * COW range from cow_start to found_key.offset - 1. As the key
1760 * will contain the beginning of the first extent that can be
1761 * NOCOW, following one which needs to be COW'ed
1762 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001763 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001764 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001765 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001766 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001767 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001768 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001769 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001770 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001771
Yan Zhengd899e052008-10-30 14:25:28 -04001772 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001773 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001774 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001775
Nikolay Borisov968322c2020-06-03 08:55:21 +03001776 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001777 orig_start,
1778 disk_bytenr, /* block_start */
1779 num_bytes, /* block_len */
1780 disk_num_bytes, /* orig_block_len */
1781 ram_bytes, BTRFS_COMPRESS_NONE,
1782 BTRFS_ORDERED_PREALLOC);
1783 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001784 ret = PTR_ERR(em);
1785 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001786 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001787 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001788 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001789 disk_bytenr, num_bytes,
1790 num_bytes,
1791 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001792 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001793 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001794 cur_offset + num_bytes - 1,
1795 0);
1796 goto error;
1797 }
Yan Zhengd899e052008-10-30 14:25:28 -04001798 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001799 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001800 disk_bytenr, num_bytes,
1801 num_bytes,
1802 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001803 if (ret)
1804 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001805 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001806
Filipe Mananaf78c4362016-05-09 13:15:41 +01001807 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001808 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001809 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001810
Yan, Zhengefa56462010-05-16 10:49:59 -04001811 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001812 BTRFS_DATA_RELOC_TREE_OBJECTID)
1813 /*
1814 * Error handled later, as we must prevent
1815 * extent_clear_unlock_delalloc() in error handler
1816 * from freeing metadata of created ordered extent.
1817 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001818 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001819 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001820
Nikolay Borisov968322c2020-06-03 08:55:21 +03001821 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001822 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001823 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001824 EXTENT_DELALLOC |
1825 EXTENT_CLEAR_DATA_RESV,
1826 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1827
Yan Zheng80ff3852008-10-30 14:20:02 -04001828 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001829
1830 /*
1831 * btrfs_reloc_clone_csums() error, now we're OK to call error
1832 * handler, as metadata for created ordered extent will only
1833 * be freed by btrfs_finish_ordered_io().
1834 */
1835 if (ret)
1836 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001837 if (cur_offset > end)
1838 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001839 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001840 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001841
Robbie Ko506481b2018-10-30 18:04:04 +08001842 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001843 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001844
Yan Zheng80ff3852008-10-30 14:20:02 -04001845 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001846 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001847 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1848 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001849 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001850 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001851 }
1852
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001853error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001854 if (nocow)
1855 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1856
Josef Bacik17ca04a2012-05-31 15:58:55 -04001857 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001858 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001859 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001860 EXTENT_DELALLOC | EXTENT_DEFRAG |
1861 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001862 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001863 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001864 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001865 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001866}
1867
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001868static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001869{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001870 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1871 if (inode->defrag_bytes &&
1872 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1873 0, NULL))
1874 return false;
1875 return true;
1876 }
1877 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001878}
1879
Chris Masond352ac62008-09-29 15:18:18 -04001880/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001881 * Function to process delayed allocation (create CoW) for ranges which are
1882 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001883 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001884int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001885 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1886 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001887{
Chris Masonbe20aa92007-12-17 20:14:01 -05001888 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001889 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001890
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001891 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001892 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001893 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001894 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001895 } else if (!inode_can_compress(inode) ||
1896 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001897 if (zoned)
1898 ret = run_delalloc_zoned(inode, locked_page, start, end,
1899 page_started, nr_written);
1900 else
1901 ret = cow_file_range(inode, locked_page, start, end,
1902 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001903 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001904 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1905 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001906 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001907 }
Qu Wenruo524272602017-03-08 10:25:52 +08001908 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001909 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001910 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001911 return ret;
1912}
1913
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001914void btrfs_split_delalloc_extent(struct inode *inode,
1915 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001916{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001917 u64 size;
1918
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001919 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001920 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001921 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001922
Josef Bacikdcab6a32015-02-11 15:08:59 -05001923 size = orig->end - orig->start + 1;
1924 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001925 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001926 u64 new_size;
1927
1928 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001929 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001930 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001931 */
1932 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001933 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001934 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001935 num_extents += count_max_extents(new_size);
1936 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001937 return;
1938 }
1939
Josef Bacik9e0baf62011-07-15 15:16:44 +00001940 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001941 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001942 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001943}
1944
1945/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001946 * Handle merged delayed allocation extents so we can keep track of new extents
1947 * that are just merged onto old extents, such as when we are doing sequential
1948 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001949 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001950void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1951 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001952{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001953 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001954 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001955
Josef Bacik9ed74f22009-09-11 16:12:44 -04001956 /* not delalloc, ignore it */
1957 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001958 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001959
Josef Bacik8461a3d2015-03-13 15:12:08 -04001960 if (new->start > other->start)
1961 new_size = new->end - other->start + 1;
1962 else
1963 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001964
1965 /* we're not bigger than the max, unreserve the space and go */
1966 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1967 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001968 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001969 spin_unlock(&BTRFS_I(inode)->lock);
1970 return;
1971 }
1972
1973 /*
Josef Bacikba117212015-03-13 15:01:24 -04001974 * We have to add up either side to figure out how many extents were
1975 * accounted for before we merged into one big extent. If the number of
1976 * extents we accounted for is <= the amount we need for the new range
1977 * then we can return, otherwise drop. Think of it like this
1978 *
1979 * [ 4k][MAX_SIZE]
1980 *
1981 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1982 * need 2 outstanding extents, on one side we have 1 and the other side
1983 * we have 1 so they are == and we can return. But in this case
1984 *
1985 * [MAX_SIZE+4k][MAX_SIZE+4k]
1986 *
1987 * Each range on their own accounts for 2 extents, but merged together
1988 * they are only 3 extents worth of accounting, so we need to drop in
1989 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001990 */
Josef Bacikba117212015-03-13 15:01:24 -04001991 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001992 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001993 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001994 num_extents += count_max_extents(old_size);
1995 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001996 return;
1997
Josef Bacik9e0baf62011-07-15 15:16:44 +00001998 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001999 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002000 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002001}
2002
Miao Xieeb73c1b2013-05-15 07:48:22 +00002003static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2004 struct inode *inode)
2005{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2007
Miao Xieeb73c1b2013-05-15 07:48:22 +00002008 spin_lock(&root->delalloc_lock);
2009 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2010 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2011 &root->delalloc_inodes);
2012 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2013 &BTRFS_I(inode)->runtime_flags);
2014 root->nr_delalloc_inodes++;
2015 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002017 BUG_ON(!list_empty(&root->delalloc_root));
2018 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 &fs_info->delalloc_roots);
2020 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002021 }
2022 }
2023 spin_unlock(&root->delalloc_lock);
2024}
2025
Nikolay Borisov2b877332018-04-27 12:21:51 +03002026
2027void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2028 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002029{
David Sterba3ffbd682018-06-29 10:56:42 +02002030 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002031
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002032 if (!list_empty(&inode->delalloc_inodes)) {
2033 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002034 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002035 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002036 root->nr_delalloc_inodes--;
2037 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002038 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002039 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002040 BUG_ON(list_empty(&root->delalloc_root));
2041 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002042 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002043 }
2044 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002045}
2046
2047static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2048 struct btrfs_inode *inode)
2049{
2050 spin_lock(&root->delalloc_lock);
2051 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002052 spin_unlock(&root->delalloc_lock);
2053}
2054
Chris Masond352ac62008-09-29 15:18:18 -04002055/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002056 * Properly track delayed allocation bytes in the inode and to maintain the
2057 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002058 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002059void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2060 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002061{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2063
Wang Shilong47059d92014-07-03 18:22:07 +08002064 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2065 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002066 /*
2067 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002068 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002069 * bit, which is only set or cleared with irqs on
2070 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002071 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002072 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002073 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002074 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002075 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002076
Josef Bacik8b62f872017-10-19 14:15:55 -04002077 spin_lock(&BTRFS_I(inode)->lock);
2078 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2079 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002080
Josef Bacik6a3891c2015-03-16 17:38:52 -04002081 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002082 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002083 return;
2084
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002085 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2086 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002087 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002088 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002089 if (*bits & EXTENT_DEFRAG)
2090 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002091 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002092 &BTRFS_I(inode)->runtime_flags))
2093 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002094 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002095 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002096
2097 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2098 (*bits & EXTENT_DELALLOC_NEW)) {
2099 spin_lock(&BTRFS_I(inode)->lock);
2100 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2101 state->start;
2102 spin_unlock(&BTRFS_I(inode)->lock);
2103 }
Chris Mason291d6732008-01-29 15:55:23 -05002104}
2105
Chris Masond352ac62008-09-29 15:18:18 -04002106/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002107 * Once a range is no longer delalloc this function ensures that proper
2108 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002109 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002110void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2111 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002112{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002113 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2114 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002115 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002116 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002117
Filipe Manana4a4b9642017-07-27 19:52:55 +01002118 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2119 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002120 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002121 spin_unlock(&inode->lock);
2122 }
Wang Shilong47059d92014-07-03 18:22:07 +08002123
Chris Mason75eff682008-12-15 15:54:40 -05002124 /*
2125 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002126 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002127 * bit, which is only set or cleared with irqs on
2128 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002129 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002130 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002131 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002132
Josef Bacik8b62f872017-10-19 14:15:55 -04002133 spin_lock(&inode->lock);
2134 btrfs_mod_outstanding_extents(inode, -num_extents);
2135 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002136
Josef Bacikb6d08f02013-09-27 14:57:43 -04002137 /*
2138 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002139 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002140 * error.
2141 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002142 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002143 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002144 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002145
Josef Bacik6a3891c2015-03-16 17:38:52 -04002146 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002147 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002148 return;
2149
Filipe Mananaa315e682017-03-06 23:04:20 +00002150 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2151 do_list && !(state->state & EXTENT_NORESERVE) &&
2152 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002153 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002154
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002155 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2156 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002157 spin_lock(&inode->lock);
2158 inode->delalloc_bytes -= len;
2159 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002160 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002161 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002162 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002163 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002164 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002165
2166 if ((state->state & EXTENT_DELALLOC_NEW) &&
2167 (*bits & EXTENT_DELALLOC_NEW)) {
2168 spin_lock(&inode->lock);
2169 ASSERT(inode->new_delalloc_bytes >= len);
2170 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002171 if (*bits & EXTENT_ADD_INODE_BYTES)
2172 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002173 spin_unlock(&inode->lock);
2174 }
Chris Mason291d6732008-01-29 15:55:23 -05002175}
2176
Chris Masond352ac62008-09-29 15:18:18 -04002177/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002178 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2179 * in a chunk's stripe. This function ensures that bios do not span a
2180 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002181 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002182 * @page - The page we are about to add to the bio
2183 * @size - size we want to add to the bio
2184 * @bio - bio we want to ensure is smaller than a stripe
2185 * @bio_flags - flags of the bio
2186 *
2187 * return 1 if page cannot be added to the bio
2188 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002189 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002190 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002191int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2192 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002194 struct inode *inode = page->mapping->host;
2195 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002196 u64 logical = bio->bi_iter.bi_sector << 9;
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002197 u32 bio_len = bio->bi_iter.bi_size;
Michal Rostecki42034312021-01-27 14:57:27 +01002198 struct extent_map *em;
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
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002205 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
Michal Rostecki42034312021-01-27 14:57:27 +01002206 if (IS_ERR(em))
2207 return PTR_ERR(em);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002208 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002209 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002210 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002211
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002212 if (geom.len < bio_len + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002213 ret = 1;
2214out:
2215 free_extent_map(em);
2216 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002217}
2218
Chris Masond352ac62008-09-29 15:18:18 -04002219/*
2220 * in order to insert checksums into the metadata in large chunks,
2221 * we wait until bio submission time. All the pages in the bio are
2222 * checksummed and sums are attached onto the ordered extent record.
2223 *
2224 * At IO completion time the cums attached on the ordered extent record
2225 * are inserted into the btree
2226 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002227static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002228 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002229{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002230 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002231}
Chris Masone0156402008-04-16 11:15:20 -04002232
Naohiro Aotad22002f2021-02-04 19:22:00 +09002233static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2234 struct bio *bio, loff_t file_offset)
2235{
2236 struct btrfs_ordered_extent *ordered;
2237 struct extent_map *em = NULL, *em_new = NULL;
2238 struct extent_map_tree *em_tree = &inode->extent_tree;
2239 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2240 u64 len = bio->bi_iter.bi_size;
2241 u64 end = start + len;
2242 u64 ordered_end;
2243 u64 pre, post;
2244 int ret = 0;
2245
2246 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2247 if (WARN_ON_ONCE(!ordered))
2248 return BLK_STS_IOERR;
2249
2250 /* No need to split */
2251 if (ordered->disk_num_bytes == len)
2252 goto out;
2253
2254 /* We cannot split once end_bio'd ordered extent */
2255 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2256 ret = -EINVAL;
2257 goto out;
2258 }
2259
2260 /* We cannot split a compressed ordered extent */
2261 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2262 ret = -EINVAL;
2263 goto out;
2264 }
2265
2266 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2267 /* bio must be in one ordered extent */
2268 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2269 ret = -EINVAL;
2270 goto out;
2271 }
2272
2273 /* Checksum list should be empty */
2274 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2275 ret = -EINVAL;
2276 goto out;
2277 }
2278
2279 pre = start - ordered->disk_bytenr;
2280 post = ordered_end - end;
2281
2282 ret = btrfs_split_ordered_extent(ordered, pre, post);
2283 if (ret)
2284 goto out;
2285
2286 read_lock(&em_tree->lock);
2287 em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
2288 if (!em) {
2289 read_unlock(&em_tree->lock);
2290 ret = -EIO;
2291 goto out;
2292 }
2293 read_unlock(&em_tree->lock);
2294
2295 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2296 /*
2297 * We cannot reuse em_new here but have to create a new one, as
2298 * unpin_extent_cache() expects the start of the extent map to be the
2299 * logical offset of the file, which does not hold true anymore after
2300 * splitting.
2301 */
2302 em_new = create_io_em(inode, em->start + pre, len,
2303 em->start + pre, em->block_start + pre, len,
2304 len, len, BTRFS_COMPRESS_NONE,
2305 BTRFS_ORDERED_REGULAR);
2306 if (IS_ERR(em_new)) {
2307 ret = PTR_ERR(em_new);
2308 goto out;
2309 }
2310 free_extent_map(em_new);
2311
2312out:
2313 free_extent_map(em);
2314 btrfs_put_ordered_extent(ordered);
2315
2316 return errno_to_blk_status(ret);
2317}
2318
Chris Mason4a69a412008-11-06 22:03:00 -05002319/*
Chris Masoncad321a2008-12-17 14:51:42 -05002320 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002321 * on write, or reading the csums from the tree before a read.
2322 *
2323 * Rules about async/sync submit,
2324 * a) read: sync submit
2325 *
2326 * b) write without checksum: sync submit
2327 *
2328 * c) write with checksum:
2329 * c-1) if bio is issued by fsync: sync submit
2330 * (sync_writers != 0)
2331 *
2332 * c-2) if root is reloc root: sync submit
2333 * (only in case of buffered IO)
2334 *
2335 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002336 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002337blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2338 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002339
Chris Mason44b8bd72008-04-16 11:14:51 -04002340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002341 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002342 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302343 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002344 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002345 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002346 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002347
Josef Bacik42437a62020-10-16 11:29:18 -04002348 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2349 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002350
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002351 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302352 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002353
Naohiro Aotad22002f2021-02-04 19:22:00 +09002354 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2355 struct page *page = bio_first_bvec_all(bio)->bv_page;
2356 loff_t file_offset = page_offset(page);
2357
2358 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2359 if (ret)
2360 goto out;
2361 }
2362
Naohiro Aotacfe94442021-02-04 19:21:59 +09002363 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002364 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002365 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002366 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002367
Chris Masond20f7042008-12-08 16:58:54 -05002368 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002369 ret = btrfs_submit_compressed_read(inode, bio,
2370 mirror_num,
2371 bio_flags);
2372 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002373 } else {
2374 /*
2375 * Lookup bio sums does extra checks around whether we
2376 * need to csum or not, which is why we ignore skip_sum
2377 * here.
2378 */
Qu Wenruo62751932020-12-02 14:48:06 +08002379 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002380 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002381 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002382 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002383 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002384 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002385 /* csum items have already been cloned */
2386 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2387 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002388 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002389 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2390 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002391 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002392 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002393 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002394 if (ret)
2395 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002396 }
2397
Chris Mason0b86a832008-03-24 15:01:56 -04002398mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002399 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002400
2401out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002402 if (ret) {
2403 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002404 bio_endio(bio);
2405 }
Stefan Behrens61891922012-11-05 18:51:52 +01002406 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002407}
Chris Mason6885f302008-02-20 16:11:05 -05002408
Chris Masond352ac62008-09-29 15:18:18 -04002409/*
2410 * given a list of ordered sums record them in the inode. This happens
2411 * at IO completion time based on sums calculated at bio submission time.
2412 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002413static int add_pending_csums(struct btrfs_trans_handle *trans,
2414 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002415{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002416 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002417 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002418
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002419 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002420 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002421 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002422 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002423 if (ret)
2424 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002425 }
2426 return 0;
2427}
2428
Filipe Mananac3347302020-11-04 11:07:31 +00002429static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2430 const u64 start,
2431 const u64 len,
2432 struct extent_state **cached_state)
2433{
2434 u64 search_start = start;
2435 const u64 end = start + len - 1;
2436
2437 while (search_start < end) {
2438 const u64 search_len = end - search_start + 1;
2439 struct extent_map *em;
2440 u64 em_len;
2441 int ret = 0;
2442
2443 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2444 if (IS_ERR(em))
2445 return PTR_ERR(em);
2446
2447 if (em->block_start != EXTENT_MAP_HOLE)
2448 goto next;
2449
2450 em_len = em->len;
2451 if (em->start < search_start)
2452 em_len -= search_start - em->start;
2453 if (em_len > search_len)
2454 em_len = search_len;
2455
2456 ret = set_extent_bit(&inode->io_tree, search_start,
2457 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002458 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2459 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002460next:
2461 search_start = extent_map_end(em);
2462 free_extent_map(em);
2463 if (ret)
2464 return ret;
2465 }
2466 return 0;
2467}
2468
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002469int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002470 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002471 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002472{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002473 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002474
2475 if (start >= i_size_read(&inode->vfs_inode) &&
2476 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2477 /*
2478 * There can't be any extents following eof in this case so just
2479 * set the delalloc new bit for the range directly.
2480 */
2481 extra_bits |= EXTENT_DELALLOC_NEW;
2482 } else {
2483 int ret;
2484
2485 ret = btrfs_find_new_delalloc_bytes(inode, start,
2486 end + 1 - start,
2487 cached_state);
2488 if (ret)
2489 return ret;
2490 }
2491
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002492 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2493 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002494}
2495
Chris Masond352ac62008-09-29 15:18:18 -04002496/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002497struct btrfs_writepage_fixup {
2498 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002499 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002500 struct btrfs_work work;
2501};
2502
Christoph Hellwigb2950862008-12-02 09:54:17 -05002503static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002504{
2505 struct btrfs_writepage_fixup *fixup;
2506 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002507 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002508 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002509 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002510 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002511 u64 page_start;
2512 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002513 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002514 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002515
2516 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2517 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002518 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002519 page_start = page_offset(page);
2520 page_end = page_offset(page) + PAGE_SIZE - 1;
2521
2522 /*
2523 * This is similar to page_mkwrite, we need to reserve the space before
2524 * we take the page lock.
2525 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002526 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2527 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002528again:
Chris Mason247e7432008-07-17 12:53:51 -04002529 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002530
Chris Mason25f3c502020-01-21 11:51:42 -05002531 /*
2532 * Before we queued this fixup, we took a reference on the page.
2533 * page->mapping may go NULL, but it shouldn't be moved to a different
2534 * address space.
2535 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002536 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2537 /*
2538 * Unfortunately this is a little tricky, either
2539 *
2540 * 1) We got here and our page had already been dealt with and
2541 * we reserved our space, thus ret == 0, so we need to just
2542 * drop our space reservation and bail. This can happen the
2543 * first time we come into the fixup worker, or could happen
2544 * while waiting for the ordered extent.
2545 * 2) Our page was already dealt with, but we happened to get an
2546 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2547 * this case we obviously don't have anything to release, but
2548 * because the page was already dealt with we don't want to
2549 * mark the page with an error, so make sure we're resetting
2550 * ret to 0. This is why we have this check _before_ the ret
2551 * check, because we do not want to have a surprise ENOSPC
2552 * when the page was already properly dealt with.
2553 */
2554 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002555 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2556 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002557 page_start, PAGE_SIZE,
2558 true);
2559 }
2560 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002561 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002562 }
Chris Mason25f3c502020-01-21 11:51:42 -05002563
2564 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002565 * We can't mess with the page state unless it is locked, so now that
2566 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002567 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002568 if (ret)
2569 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002570
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002571 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002572
2573 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002574 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002575 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002576
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002577 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002578 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002579 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2580 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002581 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002582 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002583 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002584 goto again;
2585 }
Chris Mason247e7432008-07-17 12:53:51 -04002586
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002587 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002588 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002589 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002590 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002591
Chris Mason25f3c502020-01-21 11:51:42 -05002592 /*
2593 * Everything went as planned, we're now the owner of a dirty page with
2594 * delayed allocation bits set and space reserved for our COW
2595 * destination.
2596 *
2597 * The page was dirty when we started, nothing should have cleaned it.
2598 */
2599 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002600 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002601out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002602 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002603 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002604 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2605 PAGE_SIZE, true);
2606 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002607 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002608out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002609 if (ret) {
2610 /*
2611 * We hit ENOSPC or other errors. Update the mapping and page
2612 * to reflect the errors and clean the page.
2613 */
2614 mapping_set_error(page->mapping, ret);
2615 end_extent_writepage(page, ret, page_start, page_end);
2616 clear_page_dirty_for_io(page);
2617 SetPageError(page);
2618 }
2619 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002620 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002621 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002622 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002623 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002624 /*
2625 * As a precaution, do a delayed iput in case it would be the last iput
2626 * that could need flushing space. Recursing back to fixup worker would
2627 * deadlock.
2628 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002629 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002630}
2631
2632/*
2633 * There are a few paths in the higher layers of the kernel that directly
2634 * set the page dirty bit without asking the filesystem if it is a
2635 * good idea. This causes problems because we want to make sure COW
2636 * properly happens and the data=ordered rules are followed.
2637 *
Chris Masonc8b97812008-10-29 14:49:59 -04002638 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002639 * hasn't been properly setup for IO. We kick off an async process
2640 * to fix it up. The async helper will wait for ordered extents, set
2641 * the delalloc bit and make it safe to write the page.
2642 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002643int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002644{
2645 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002646 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002647 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002648
Chris Mason8b62b722009-09-02 16:53:46 -04002649 /* this page is properly in the ordered list */
Qu Wenruo87b4d862021-01-22 14:00:52 +08002650 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002651 return 0;
2652
Chris Mason25f3c502020-01-21 11:51:42 -05002653 /*
2654 * PageChecked is set below when we create a fixup worker for this page,
2655 * don't try to create another one if we're already PageChecked()
2656 *
2657 * The extent_io writepage code will redirty the page if we send back
2658 * EAGAIN.
2659 */
Chris Mason247e7432008-07-17 12:53:51 -04002660 if (PageChecked(page))
2661 return -EAGAIN;
2662
2663 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2664 if (!fixup)
2665 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002666
Josef Bacikf4b13632020-01-21 14:34:52 -05002667 /*
2668 * We are already holding a reference to this inode from
2669 * write_cache_pages. We need to hold it because the space reservation
2670 * takes place outside of the page lock, and we can't trust
2671 * page->mapping outside of the page lock.
2672 */
2673 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002674 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002675 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002676 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002677 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002678 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002679 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002680
2681 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002682}
2683
Yan Zhengd899e052008-10-30 14:25:28 -04002684static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002685 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002686 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002687 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002688 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002689{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002690 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002691 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002692 struct btrfs_path *path;
2693 struct extent_buffer *leaf;
2694 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002695 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2696 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2697 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2698 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002699 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002700 int ret;
2701
2702 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002703 if (!path)
2704 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002705
Chris Masona1ed8352009-09-11 12:27:37 -04002706 /*
2707 * we may be replacing one extent in the tree with another.
2708 * The new extent is pinned in the extent map, and we don't want
2709 * to drop it from the cache until it is completely in the btree.
2710 *
2711 * So, tell btrfs_drop_extents to leave this extent in the cache.
2712 * the caller is expected to unpin it and allow it to be merged
2713 * with the others.
2714 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002715 drop_args.path = path;
2716 drop_args.start = file_pos;
2717 drop_args.end = file_pos + num_bytes;
2718 drop_args.replace_extent = true;
2719 drop_args.extent_item_size = sizeof(*stack_fi);
2720 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002721 if (ret)
2722 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002723
Filipe Manana5893dfb2020-11-04 11:07:32 +00002724 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002725 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002726 ins.offset = file_pos;
2727 ins.type = BTRFS_EXTENT_DATA_KEY;
2728
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002729 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002730 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002731 if (ret)
2732 goto out;
2733 }
Yan Zhengd899e052008-10-30 14:25:28 -04002734 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002735 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2736 write_extent_buffer(leaf, stack_fi,
2737 btrfs_item_ptr_offset(leaf, path->slots[0]),
2738 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002739
Yan Zhengd899e052008-10-30 14:25:28 -04002740 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002741 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002742
Filipe Manana2766ff62020-11-04 11:07:34 +00002743 /*
2744 * If we dropped an inline extent here, we know the range where it is
2745 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2746 * number of bytes only for that range contaning the inline extent.
2747 * The remaining of the range will be processed when clearning the
2748 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2749 */
2750 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2751 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2752
2753 inline_size = drop_args.bytes_found - inline_size;
2754 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2755 drop_args.bytes_found -= inline_size;
2756 num_bytes -= sectorsize;
2757 }
2758
2759 if (update_inode_bytes)
2760 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002761
2762 ins.objectid = disk_bytenr;
2763 ins.offset = disk_num_bytes;
2764 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002765
Nikolay Borisovc553f942020-06-03 08:55:19 +03002766 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002767 if (ret)
2768 goto out;
2769
Nikolay Borisovc553f942020-06-03 08:55:19 +03002770 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002771 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002772out:
Yan Zhengd899e052008-10-30 14:25:28 -04002773 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002774
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002775 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002776}
2777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002778static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002779 u64 start, u64 len)
2780{
David Sterba32da53862019-10-29 19:20:18 +01002781 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002782
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002783 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002784 ASSERT(cache);
2785
2786 spin_lock(&cache->lock);
2787 cache->delalloc_bytes -= len;
2788 spin_unlock(&cache->lock);
2789
2790 btrfs_put_block_group(cache);
2791}
2792
Qu Wenruo203f44c52020-06-10 09:04:40 +08002793static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002794 struct btrfs_ordered_extent *oe)
2795{
2796 struct btrfs_file_extent_item stack_fi;
2797 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002798 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002799
2800 memset(&stack_fi, 0, sizeof(stack_fi));
2801 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2802 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2803 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2804 oe->disk_num_bytes);
2805 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2806 logical_len = oe->truncated_len;
2807 else
2808 logical_len = oe->num_bytes;
2809 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2810 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2811 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2812 /* Encryption and other encoding is reserved and all 0 */
2813
Filipe Manana2766ff62020-11-04 11:07:34 +00002814 /*
2815 * For delalloc, when completing an ordered extent we update the inode's
2816 * bytes when clearing the range in the inode's io tree, so pass false
2817 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2818 * except if the ordered extent was truncated.
2819 */
2820 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2821 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2822
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002823 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2824 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002825 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002826}
2827
2828/*
2829 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002830 * an ordered extent if the range of bytes in the file it covers are
2831 * fully written.
2832 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002833static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002834{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002835 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2836 struct btrfs_root *root = inode->root;
2837 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002838 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002839 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002840 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002841 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002842 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002843 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002844 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002845 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002846 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002847 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002848 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002849
Omar Sandovalbffe6332019-12-02 17:34:19 -08002850 start = ordered_extent->file_offset;
2851 end = start + ordered_extent->num_bytes - 1;
2852
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002853 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2854 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2855 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002856 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002858 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002859
Josef Bacik5fd02042012-05-02 14:00:54 -04002860 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2861 ret = -EIO;
2862 goto out;
2863 }
2864
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002865 if (ordered_extent->disk)
2866 btrfs_rewrite_logical_zoned(ordered_extent);
2867
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002868 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002869
Josef Bacik77cef2e2013-08-29 13:57:21 -04002870 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2871 truncated = true;
2872 logical_len = ordered_extent->truncated_len;
2873 /* Truncated the entire extent, don't bother adding */
2874 if (!logical_len)
2875 goto out;
2876 }
2877
Yan, Zhengc2167752009-11-12 09:34:21 +00002878 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002879 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002880
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002881 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002882 if (freespace_inode)
2883 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002884 else
2885 trans = btrfs_join_transaction(root);
2886 if (IS_ERR(trans)) {
2887 ret = PTR_ERR(trans);
2888 trans = NULL;
2889 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002890 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002891 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002892 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002893 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002894 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002895 goto out;
2896 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002897
Filipe Manana2766ff62020-11-04 11:07:34 +00002898 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002899 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900
Nikolay Borisov8d510122019-10-08 20:43:06 +03002901 if (freespace_inode)
2902 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002903 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002904 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002905 if (IS_ERR(trans)) {
2906 ret = PTR_ERR(trans);
2907 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002908 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 }
Chris Masona79b7d42014-05-22 16:18:52 -07002910
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002911 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002912
Chris Masonc8b97812008-10-29 14:49:59 -04002913 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002914 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002915 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002916 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002917 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002918 ordered_extent->file_offset,
2919 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002920 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002921 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002923 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002924 if (!ret) {
2925 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002926 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002927 ordered_extent->disk_bytenr,
2928 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002929 }
Yan Zhengd899e052008-10-30 14:25:28 -04002930 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002931 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002932 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002933 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002934 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002935 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002936 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002937
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002938 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002939 if (ret) {
2940 btrfs_abort_transaction(trans, ret);
2941 goto out;
2942 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002943
Filipe Manana2766ff62020-11-04 11:07:34 +00002944 /*
2945 * If this is a new delalloc range, clear its new delalloc flag to
2946 * update the inode's number of bytes. This needs to be done first
2947 * before updating the inode item.
2948 */
2949 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2950 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002951 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002952 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2953 0, 0, &cached_state);
2954
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002955 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002956 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002957 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002958 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002959 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002960 }
2961 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002962out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002963 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002964 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002965 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002966
Miao Xiea698d0752012-09-20 01:51:59 -06002967 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002968 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002969
Josef Bacik77cef2e2013-08-29 13:57:21 -04002970 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002971 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002972
Josef Bacikd61bec02021-05-19 09:38:27 -04002973 /*
2974 * If we failed to finish this ordered extent for any reason we
2975 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
2976 * extent, and mark the inode with the error if it wasn't
2977 * already set. Any error during writeback would have already
2978 * set the mapping error, so we need to set it if we're the ones
2979 * marking this ordered extent as failed.
2980 */
2981 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
2982 &ordered_extent->flags))
2983 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
2984
Josef Bacik77cef2e2013-08-29 13:57:21 -04002985 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002986 unwritten_start += logical_len;
2987 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002988
2989 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002990 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002992 /*
2993 * If the ordered extent had an IOERR or something else went
2994 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 * back to the allocator. We only free the extent in the
2996 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002997 *
2998 * If we made it past insert_reserved_file_extent before we
2999 * errored out then we don't need to do this as the accounting
3000 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003001 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003002 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003003 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003004 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003005 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3006 /*
3007 * Discard the range before returning it back to the
3008 * free space pool
3009 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003010 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003011 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003012 ordered_extent->disk_bytenr,
3013 ordered_extent->disk_num_bytes,
3014 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003015 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003016 ordered_extent->disk_bytenr,
3017 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003018 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003019 }
3020
Josef Bacik5fd02042012-05-02 14:00:54 -04003021 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003022 * This needs to be done to make sure anybody waiting knows we are done
3023 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003024 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003025 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003026
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027 /* once for us */
3028 btrfs_put_ordered_extent(ordered_extent);
3029 /* once for the tree */
3030 btrfs_put_ordered_extent(ordered_extent);
3031
Josef Bacik5fd02042012-05-02 14:00:54 -04003032 return ret;
3033}
3034
3035static void finish_ordered_fn(struct btrfs_work *work)
3036{
3037 struct btrfs_ordered_extent *ordered_extent;
3038 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3039 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003040}
3041
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003042void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3043 struct page *page, u64 start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003044 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003045{
Nikolay Borisov3347c482020-08-31 14:42:44 +03003046 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04003047 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003048 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003049
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003050 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003051
Chris Mason8b62b722009-09-02 16:53:46 -04003052 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03003053 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3054 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003055 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003056
Nikolay Borisov3347c482020-08-31 14:42:44 +03003057 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003058 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003059 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003060 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003061
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003062 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003063 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003064}
3065
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003066/*
3067 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003068 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003069 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003070 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003071 * @page: page where is the data to be verified
3072 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003073 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003074 *
3075 * The length of such check is always one sector size.
3076 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003077static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003078 u32 bio_offset, struct page *page, u32 pgoff,
3079 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003080{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003081 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3082 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003083 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003084 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003085 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003086 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003087 u8 *csum_expected;
3088 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003089
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003090 ASSERT(pgoff + len <= PAGE_SIZE);
3091
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003092 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3093 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003094
3095 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003096 shash->tfm = fs_info->csum_shash;
3097
Eric Biggersfd080012020-04-30 23:51:59 -07003098 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003099
3100 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003101 goto zeroit;
3102
3103 kunmap_atomic(kaddr);
3104 return 0;
3105zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003106 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3107 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003108 if (io_bio->device)
3109 btrfs_dev_stat_inc_and_print(io_bio->device,
3110 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003111 memset(kaddr + pgoff, 1, len);
3112 flush_dcache_page(page);
3113 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003114 return -EIO;
3115}
3116
Chris Masond352ac62008-09-29 15:18:18 -04003117/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003118 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003119 * if there's a match, we allow the bio to finish. If not, the code in
3120 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003121 *
3122 * @bio_offset: offset to the beginning of the bio (in bytes)
3123 * @start: file offset of the range start
3124 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003125 *
3126 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3127 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003128 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003129unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3130 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003131{
Chris Mason07157aa2007-08-30 08:50:51 -04003132 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003133 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003134 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003135 const u32 sectorsize = root->fs_info->sectorsize;
3136 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003137 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003138
Chris Masond20f7042008-12-08 16:58:54 -05003139 if (PageChecked(page)) {
3140 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003141 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003142 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003143
3144 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003145 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003146
Josef Bacik42437a62020-10-16 11:29:18 -04003147 if (!root->fs_info->csum_root)
3148 return 0;
3149
Yan Zheng17d217f2008-12-12 10:03:38 -05003150 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003151 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003152 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003153 return 0;
3154 }
3155
Qu Wenruof44cf412020-12-02 14:47:59 +08003156 ASSERT(page_offset(page) <= start &&
3157 end <= page_offset(page) + PAGE_SIZE - 1);
3158 for (pg_off = offset_in_page(start);
3159 pg_off < offset_in_page(end);
3160 pg_off += sectorsize, bio_offset += sectorsize) {
3161 int ret;
3162
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003163 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3164 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003165 if (ret < 0) {
3166 const int nr_bit = (pg_off - offset_in_page(start)) >>
3167 root->fs_info->sectorsize_bits;
3168
3169 result |= (1U << nr_bit);
3170 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003171 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003172 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003173}
Chris Masonb888db22007-08-27 16:49:44 -04003174
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003175/*
3176 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3177 *
3178 * @inode: The inode we want to perform iput on
3179 *
3180 * This function uses the generic vfs_inode::i_count to track whether we should
3181 * just decrement it (in case it's > 1) or if this is the last iput then link
3182 * the inode to the delayed iput machinery. Delayed iputs are processed at
3183 * transaction commit time/superblock commit/cleaner kthread.
3184 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003185void btrfs_add_delayed_iput(struct inode *inode)
3186{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003188 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003189
3190 if (atomic_add_unless(&inode->i_count, -1, 1))
3191 return;
3192
Josef Bacik034f7842018-12-03 11:06:52 -05003193 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003195 ASSERT(list_empty(&binode->delayed_iput));
3196 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003197 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003198 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3199 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003200}
3201
Josef Bacik63611e72019-06-18 10:59:18 -04003202static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3203 struct btrfs_inode *inode)
3204{
3205 list_del_init(&inode->delayed_iput);
3206 spin_unlock(&fs_info->delayed_iput_lock);
3207 iput(&inode->vfs_inode);
3208 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3209 wake_up(&fs_info->delayed_iputs_wait);
3210 spin_lock(&fs_info->delayed_iput_lock);
3211}
3212
3213static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3214 struct btrfs_inode *inode)
3215{
3216 if (!list_empty(&inode->delayed_iput)) {
3217 spin_lock(&fs_info->delayed_iput_lock);
3218 if (!list_empty(&inode->delayed_iput))
3219 run_delayed_iput_locked(fs_info, inode);
3220 spin_unlock(&fs_info->delayed_iput_lock);
3221 }
3222}
3223
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003224void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003225{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003226
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003227 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003228 while (!list_empty(&fs_info->delayed_iputs)) {
3229 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003230
David Sterba8089fe62015-11-19 14:15:51 +01003231 inode = list_first_entry(&fs_info->delayed_iputs,
3232 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003233 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003234 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235 }
David Sterba8089fe62015-11-19 14:15:51 +01003236 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003237}
3238
Josef Bacik034f7842018-12-03 11:06:52 -05003239/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003240 * Wait for flushing all delayed iputs
3241 *
3242 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003243 *
3244 * This will wait on any delayed iputs that are currently running with KILLABLE
3245 * set. Once they are all done running we will return, unless we are killed in
3246 * which case we return EINTR. This helps in user operations like fallocate etc
3247 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003248 *
3249 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003250 */
3251int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3252{
3253 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3254 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3255 if (ret)
3256 return -EINTR;
3257 return 0;
3258}
3259
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003261 * This creates an orphan entry for the given inode in case something goes wrong
3262 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003263 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003264int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003265 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003266{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003268
Omar Sandoval27919062018-05-11 13:13:37 -07003269 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3270 if (ret && ret != -EEXIST) {
3271 btrfs_abort_transaction(trans, ret);
3272 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 }
3274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276}
3277
3278/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003279 * We have done the delete so we can go ahead and remove the orphan item for
3280 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003281 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003282static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003283 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003284{
Omar Sandoval27919062018-05-11 13:13:37 -07003285 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003286}
3287
3288/*
3289 * this cleans up any orphans that may be left on the list from the last use
3290 * of this root.
3291 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003292int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003295 struct btrfs_path *path;
3296 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003297 struct btrfs_key key, found_key;
3298 struct btrfs_trans_handle *trans;
3299 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003300 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003301 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003302
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003304 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003305
3306 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003307 if (!path) {
3308 ret = -ENOMEM;
3309 goto out;
3310 }
David Sterbae4058b52015-11-27 16:31:35 +01003311 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003312
3313 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003314 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003315 key.offset = (u64)-1;
3316
Josef Bacik7b128762008-07-24 12:17:14 -04003317 while (1) {
3318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 if (ret < 0)
3320 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003321
3322 /*
3323 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003324 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003325 * find the key and see if we have stuff that matches
3326 */
3327 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003329 if (path->slots[0] == 0)
3330 break;
3331 path->slots[0]--;
3332 }
3333
3334 /* pull out the item */
3335 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003336 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3337
3338 /* make sure the item matches what we want */
3339 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3340 break;
David Sterba962a2982014-06-04 18:41:45 +02003341 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003342 break;
3343
3344 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003345 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003346
3347 /*
3348 * this is where we are basically btrfs_lookup, without the
3349 * crossing root thing. we store the inode number in the
3350 * offset of the orphan item.
3351 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003352
3353 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003354 btrfs_err(fs_info,
3355 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003356 ret = -EINVAL;
3357 goto out;
3358 }
3359
3360 last_objectid = found_key.offset;
3361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 found_key.objectid = found_key.offset;
3363 found_key.type = BTRFS_INODE_ITEM_KEY;
3364 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003365 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303366 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003367 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003368 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003369
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003370 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003371 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003372 int is_dead_root = 0;
3373
3374 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003375 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003376 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003377 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003378 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003379 * We need to distinguish those two, as the orphan item
3380 * for a root must not get deleted before the deletion
3381 * of the snapshot/subvolume's tree completes.
3382 *
3383 * btrfs_find_orphan_roots() ran before us, which has
3384 * found all deleted roots and loaded them into
3385 * fs_info->fs_roots_radix. So here we can find if an
3386 * orphan item corresponds to a deleted root by looking
3387 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003388 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003389
3390 spin_lock(&fs_info->fs_roots_radix_lock);
3391 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3392 (unsigned long)found_key.objectid);
3393 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3394 is_dead_root = 1;
3395 spin_unlock(&fs_info->fs_roots_radix_lock);
3396
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003397 if (is_dead_root) {
3398 /* prevent this orphan from being found again */
3399 key.offset = found_key.objectid - 1;
3400 continue;
3401 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003402
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003403 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003404
Josef Bacika8c9e572011-09-21 16:55:59 -04003405 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003406 * If we have an inode with links, there are a couple of
3407 * possibilities. Old kernels (before v3.12) used to create an
3408 * orphan item for truncate indicating that there were possibly
3409 * extent items past i_size that needed to be deleted. In v3.12,
3410 * truncate was changed to update i_size in sync with the extent
3411 * items, but the (useless) orphan item was still created. Since
3412 * v4.18, we don't create the orphan item for truncate at all.
3413 *
3414 * So, this item could mean that we need to do a truncate, but
3415 * only if this filesystem was last used on a pre-v3.12 kernel
3416 * and was not cleanly unmounted. The odds of that are quite
3417 * slim, and it's a pain to do the truncate now, so just delete
3418 * the orphan item.
3419 *
3420 * It's also possible that this orphan item was supposed to be
3421 * deleted but wasn't. The inode number may have been reused,
3422 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003423 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003424 if (ret == -ENOENT || inode->i_nlink) {
3425 if (!ret)
3426 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003427 trans = btrfs_start_transaction(root, 1);
3428 if (IS_ERR(trans)) {
3429 ret = PTR_ERR(trans);
3430 goto out;
3431 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003432 btrfs_debug(fs_info, "auto deleting %Lu",
3433 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003434 ret = btrfs_del_orphan_item(trans, root,
3435 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003436 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003437 if (ret)
3438 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003439 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003440 }
Josef Bacik7b128762008-07-24 12:17:14 -04003441
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003442 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003443
3444 /* this will do delete_inode and everything for us */
3445 iput(inode);
3446 }
Miao Xie3254c872011-11-10 20:45:05 -05003447 /* release the path since we're done with it */
3448 btrfs_release_path(path);
3449
Yan, Zhengd68fc572010-05-16 10:49:58 -04003450 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3451
Omar Sandovala575cee2018-05-11 13:13:38 -07003452 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003453 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003454 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003455 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 }
Josef Bacik7b128762008-07-24 12:17:14 -04003457
3458 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460
3461out:
3462 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003463 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464 btrfs_free_path(path);
3465 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003466}
3467
Chris Masond352ac62008-09-29 15:18:18 -04003468/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003469 * very simple check to peek ahead in the leaf looking for xattrs. If we
3470 * don't find any xattrs, we know there can't be any acls.
3471 *
3472 * slot is the slot the inode is in, objectid is the objectid of the inode
3473 */
3474static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003475 int slot, u64 objectid,
3476 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003477{
3478 u32 nritems = btrfs_header_nritems(leaf);
3479 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003480 static u64 xattr_access = 0;
3481 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003482 int scanned = 0;
3483
Josef Bacikf23b5a52013-06-19 10:16:26 -04003484 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003485 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3486 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3487 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3488 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003489 }
3490
Chris Mason46a53cc2009-04-27 11:47:50 -04003491 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003492 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003493 while (slot < nritems) {
3494 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3495
3496 /* we found a different objectid, there must not be acls */
3497 if (found_key.objectid != objectid)
3498 return 0;
3499
3500 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003501 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003502 if (*first_xattr_slot == -1)
3503 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003504 if (found_key.offset == xattr_access ||
3505 found_key.offset == xattr_default)
3506 return 1;
3507 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003508
3509 /*
3510 * we found a key greater than an xattr key, there can't
3511 * be any acls later on
3512 */
3513 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3514 return 0;
3515
3516 slot++;
3517 scanned++;
3518
3519 /*
3520 * it goes inode, inode backrefs, xattrs, extents,
3521 * so if there are a ton of hard links to an inode there can
3522 * be a lot of backrefs. Don't waste time searching too hard,
3523 * this is just an optimization
3524 */
3525 if (scanned >= 8)
3526 break;
3527 }
3528 /* we hit the end of the leaf before we found an xattr or
3529 * something larger than an xattr. We have to assume the inode
3530 * has acls
3531 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003532 if (*first_xattr_slot == -1)
3533 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003534 return 1;
3535}
3536
3537/*
Chris Masond352ac62008-09-29 15:18:18 -04003538 * read an inode from the btree into the in-memory inode
3539 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003540static int btrfs_read_locked_inode(struct inode *inode,
3541 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003544 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003545 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 struct btrfs_inode_item *inode_item;
3547 struct btrfs_root *root = BTRFS_I(inode)->root;
3548 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003549 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003550 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003551 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003552 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003553 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003554 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003555
3556 ret = btrfs_fill_inode(inode, &rdev);
3557 if (!ret)
3558 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003559
Filipe Manana4222ea72018-10-24 10:13:03 +01003560 if (!path) {
3561 path = btrfs_alloc_path();
3562 if (!path)
3563 return -ENOMEM;
3564 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003565
Chris Mason39279cc2007-06-12 06:35:45 -04003566 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003567
Chris Mason39279cc2007-06-12 06:35:45 -04003568 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003569 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003570 if (path != in_path)
3571 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003572 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003573 }
Chris Mason39279cc2007-06-12 06:35:45 -04003574
Chris Mason5f39d392007-10-15 16:14:19 -04003575 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003576
3577 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003578 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003579
Chris Mason5f39d392007-10-15 16:14:19 -04003580 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3581 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003582 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003583 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003584 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3585 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003586 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003587 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3588 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003589
David Sterbaa937b972014-12-12 17:39:12 +01003590 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3591 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003592
David Sterbaa937b972014-12-12 17:39:12 +01003593 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3594 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003595
David Sterbaa937b972014-12-12 17:39:12 +01003596 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3597 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003598
chandan r9cc97d62012-07-04 12:48:07 +05303599 BTRFS_I(inode)->i_otime.tv_sec =
3600 btrfs_timespec_sec(leaf, &inode_item->otime);
3601 BTRFS_I(inode)->i_otime.tv_nsec =
3602 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003603
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003604 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003605 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003606 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3607
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003608 inode_set_iversion_queried(inode,
3609 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003610 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003611 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003612 rdev = btrfs_inode_rdev(leaf, inode_item);
3613
Josef Bacikaec74772008-07-24 12:12:38 -04003614 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003615 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003616
3617cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003618 /*
3619 * If we were modified in the current generation and evicted from memory
3620 * and then re-read we need to do a full sync since we don't have any
3621 * idea about which extents were modified before we were evicted from
3622 * cache.
3623 *
3624 * This is required for both inode re-read from disk and delayed inode
3625 * in delayed_nodes_tree.
3626 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003627 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003628 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3629 &BTRFS_I(inode)->runtime_flags);
3630
Filipe Mananabde6c242015-07-24 00:00:19 +01003631 /*
3632 * We don't persist the id of the transaction where an unlink operation
3633 * against the inode was last made. So here we assume the inode might
3634 * have been evicted, and therefore the exact value of last_unlink_trans
3635 * lost, and set it to last_trans to avoid metadata inconsistencies
3636 * between the inode and its parent if the inode is fsync'ed and the log
3637 * replayed. For example, in the scenario:
3638 *
3639 * touch mydir/foo
3640 * ln mydir/foo mydir/bar
3641 * sync
3642 * unlink mydir/bar
3643 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3644 * xfs_io -c fsync mydir/foo
3645 * <power failure>
3646 * mount fs, triggers fsync log replay
3647 *
3648 * We must make sure that when we fsync our inode foo we also log its
3649 * parent inode, otherwise after log replay the parent still has the
3650 * dentry with the "bar" name but our inode foo has a link count of 1
3651 * and doesn't have an inode ref with the name "bar" anymore.
3652 *
3653 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003654 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003655 * transaction commits on fsync if our inode is a directory, or if our
3656 * inode is not a directory, logging its parent unnecessarily.
3657 */
3658 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3659
Filipe Manana3ebac172020-07-15 12:30:43 +01003660 /*
3661 * Same logic as for last_unlink_trans. We don't persist the generation
3662 * of the last transaction where this inode was used for a reflink
3663 * operation, so after eviction and reloading the inode we must be
3664 * pessimistic and assume the last transaction that modified the inode.
3665 */
3666 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3667
Miao Xie67de1172013-12-26 13:07:06 +08003668 path->slots[0]++;
3669 if (inode->i_nlink != 1 ||
3670 path->slots[0] >= btrfs_header_nritems(leaf))
3671 goto cache_acl;
3672
3673 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003674 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003675 goto cache_acl;
3676
3677 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3678 if (location.type == BTRFS_INODE_REF_KEY) {
3679 struct btrfs_inode_ref *ref;
3680
3681 ref = (struct btrfs_inode_ref *)ptr;
3682 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3683 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3684 struct btrfs_inode_extref *extref;
3685
3686 extref = (struct btrfs_inode_extref *)ptr;
3687 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3688 extref);
3689 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003690cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003691 /*
3692 * try to precache a NULL acl entry for files that don't have
3693 * any xattrs or acls
3694 */
Li Zefan33345d012011-04-20 10:31:50 +08003695 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003696 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697 if (first_xattr_slot != -1) {
3698 path->slots[0] = first_xattr_slot;
3699 ret = btrfs_load_inode_props(inode, path);
3700 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003701 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003702 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003703 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003704 root->root_key.objectid, ret);
3705 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003706 if (path != in_path)
3707 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003708
Al Viro72c04902009-06-24 16:58:48 -04003709 if (!maybe_acls)
3710 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003711
Chris Mason39279cc2007-06-12 06:35:45 -04003712 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003713 case S_IFREG:
3714 inode->i_mapping->a_ops = &btrfs_aops;
3715 inode->i_fop = &btrfs_file_operations;
3716 inode->i_op = &btrfs_file_inode_operations;
3717 break;
3718 case S_IFDIR:
3719 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003720 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003721 break;
3722 case S_IFLNK:
3723 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003724 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003725 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003726 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003727 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003728 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003729 init_special_inode(inode, inode->i_mode, rdev);
3730 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003732
David Sterba7b6a2212018-03-26 18:40:21 +02003733 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003734 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003735}
3736
Chris Masond352ac62008-09-29 15:18:18 -04003737/*
3738 * given a leaf and an inode, copy the inode fields into the leaf
3739 */
Chris Masone02119d2008-09-05 16:13:11 -04003740static void fill_inode_item(struct btrfs_trans_handle *trans,
3741 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003742 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003743 struct inode *inode)
3744{
Liu Bo51fab692012-12-27 09:01:21 +00003745 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003746
David Sterbac82f8232019-08-09 17:48:21 +02003747 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
David Sterbacc4c13d2020-04-29 02:15:56 +02003749 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3750 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3751 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3752 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3753 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
David Sterbacc4c13d2020-04-29 02:15:56 +02003755 btrfs_set_token_timespec_sec(&token, &item->atime,
3756 inode->i_atime.tv_sec);
3757 btrfs_set_token_timespec_nsec(&token, &item->atime,
3758 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
David Sterbacc4c13d2020-04-29 02:15:56 +02003760 btrfs_set_token_timespec_sec(&token, &item->mtime,
3761 inode->i_mtime.tv_sec);
3762 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3763 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003764
David Sterbacc4c13d2020-04-29 02:15:56 +02003765 btrfs_set_token_timespec_sec(&token, &item->ctime,
3766 inode->i_ctime.tv_sec);
3767 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3768 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003769
David Sterbacc4c13d2020-04-29 02:15:56 +02003770 btrfs_set_token_timespec_sec(&token, &item->otime,
3771 BTRFS_I(inode)->i_otime.tv_sec);
3772 btrfs_set_token_timespec_nsec(&token, &item->otime,
3773 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303774
David Sterbacc4c13d2020-04-29 02:15:56 +02003775 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3776 btrfs_set_token_inode_generation(&token, item,
3777 BTRFS_I(inode)->generation);
3778 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3779 btrfs_set_token_inode_transid(&token, item, trans->transid);
3780 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3781 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3782 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003783}
3784
Chris Masond352ac62008-09-29 15:18:18 -04003785/*
3786 * copy everything in the in-memory inode into the btree.
3787 */
Chris Mason21151332011-11-10 20:39:08 -05003788static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003789 struct btrfs_root *root,
3790 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003791{
3792 struct btrfs_inode_item *inode_item;
3793 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003794 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003795 int ret;
3796
3797 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003798 if (!path)
3799 return -ENOMEM;
3800
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003801 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003802 if (ret) {
3803 if (ret > 0)
3804 ret = -ENOENT;
3805 goto failed;
3806 }
3807
Chris Mason5f39d392007-10-15 16:14:19 -04003808 leaf = path->nodes[0];
3809 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003810 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003811
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003812 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003813 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003814 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003815 ret = 0;
3816failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003817 btrfs_free_path(path);
3818 return ret;
3819}
3820
Chris Masond352ac62008-09-29 15:18:18 -04003821/*
Chris Mason21151332011-11-10 20:39:08 -05003822 * copy everything in the in-memory inode into the btree.
3823 */
3824noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003825 struct btrfs_root *root,
3826 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003827{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003828 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003829 int ret;
3830
3831 /*
3832 * If the inode is a free space inode, we can deadlock during commit
3833 * if we put it into the delayed code.
3834 *
3835 * The data relocation inode should also be directly updated
3836 * without delay
3837 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003838 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003839 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003840 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003841 btrfs_update_root_times(trans, root);
3842
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003843 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003844 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003845 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003846 return ret;
3847 }
3848
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003849 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003850}
3851
Nikolay Borisov729f7962020-11-02 16:49:06 +02003852int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3853 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003854{
3855 int ret;
3856
Nikolay Borisov729f7962020-11-02 16:49:06 +02003857 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003858 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003859 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003860 return ret;
3861}
3862
3863/*
Chris Masond352ac62008-09-29 15:18:18 -04003864 * unlink helper that gets used here in inode.c and in the tree logging
3865 * recovery code. It remove a link in a directory with a given name, and
3866 * also drops the back refs in the inode to the directory
3867 */
Al Viro92986792011-03-04 17:14:37 +00003868static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3869 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003870 struct btrfs_inode *dir,
3871 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003872 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003873{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003874 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003876 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003878 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003879 u64 ino = btrfs_ino(inode);
3880 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003881
3882 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003883 if (!path) {
3884 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003885 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003886 }
3887
Li Zefan33345d012011-04-20 10:31:50 +08003888 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003889 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003890 if (IS_ERR_OR_NULL(di)) {
3891 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 goto err;
3893 }
Chris Mason39279cc2007-06-12 06:35:45 -04003894 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003895 if (ret)
3896 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003897 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003898
Miao Xie67de1172013-12-26 13:07:06 +08003899 /*
3900 * If we don't have dir index, we have to get it by looking up
3901 * the inode ref, since we get the inode ref, remove it directly,
3902 * it is unnecessary to do delayed deletion.
3903 *
3904 * But if we have dir index, needn't search inode ref to get it.
3905 * Since the inode ref is close to the inode item, it is better
3906 * that we delay to delete it, and just do this deletion when
3907 * we update the inode item.
3908 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003909 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003910 ret = btrfs_delayed_delete_inode_ref(inode);
3911 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003912 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003913 goto skip_backref;
3914 }
3915 }
3916
Li Zefan33345d012011-04-20 10:31:50 +08003917 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3918 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003919 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003920 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003921 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003922 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003923 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003924 goto err;
3925 }
Miao Xie67de1172013-12-26 13:07:06 +08003926skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003927 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003929 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003930 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 }
Chris Mason39279cc2007-06-12 06:35:45 -04003932
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003933 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3934 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003935 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003936 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003937 goto err;
3938 }
Chris Masone02119d2008-09-05 16:13:11 -04003939
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003940 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3941 index);
Chris Mason6418c962010-10-30 07:34:24 -04003942 if (ret == -ENOENT)
3943 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003944 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003945 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003946
3947 /*
3948 * If we have a pending delayed iput we could end up with the final iput
3949 * being run in btrfs-cleaner context. If we have enough of these built
3950 * up we can end up burning a lot of time in btrfs-cleaner without any
3951 * way to throttle the unlinks. Since we're currently holding a ref on
3952 * the inode we can run the delayed iput here without any issues as the
3953 * final iput won't be done until after we drop the ref we're currently
3954 * holding.
3955 */
3956 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003957err:
3958 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003959 if (ret)
3960 goto out;
3961
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003962 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003963 inode_inc_iversion(&inode->vfs_inode);
3964 inode_inc_iversion(&dir->vfs_inode);
3965 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3966 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003967 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003968out:
Chris Mason39279cc2007-06-12 06:35:45 -04003969 return ret;
3970}
3971
Al Viro92986792011-03-04 17:14:37 +00003972int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3973 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003974 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003975 const char *name, int name_len)
3976{
3977 int ret;
3978 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3979 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003980 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003981 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00003982 }
3983 return ret;
3984}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985
3986/*
3987 * helper to start transaction for unlink and rmdir.
3988 *
Josef Bacikd52be812013-05-29 14:54:47 -04003989 * unlink and rmdir are special in btrfs, they do not always free space, so
3990 * if we cannot make our reservations the normal way try and see if there is
3991 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3992 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003993 */
Josef Bacikd52be812013-05-29 14:54:47 -04003994static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003995{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003996 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003997
Josef Bacike70bea52011-10-11 14:18:24 -04003998 /*
3999 * 1 for the possible orphan item
4000 * 1 for the dir item
4001 * 1 for the dir index
4002 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004003 * 1 for the inode
4004 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004005 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004006}
4007
Chris Mason39279cc2007-06-12 06:35:45 -04004008static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4009{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004011 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004012 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004013 int ret;
4014
Josef Bacikd52be812013-05-29 14:54:47 -04004015 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004017 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004018
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004019 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4020 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004021
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004022 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4023 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4024 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004025 if (ret)
4026 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004027
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004029 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004030 if (ret)
4031 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 }
Josef Bacik7b128762008-07-24 12:17:14 -04004033
Tsutomu Itohb5324022011-07-19 07:27:20 +00004034out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004035 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004036 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004037 return ret;
4038}
4039
Misono Tomohirof60a2362018-04-18 11:34:52 +09004040static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004041 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004042{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004043 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004044 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004045 struct btrfs_path *path;
4046 struct extent_buffer *leaf;
4047 struct btrfs_dir_item *di;
4048 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004049 const char *name = dentry->d_name.name;
4050 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004051 u64 index;
4052 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004053 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004054 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004055
Josef Bacik045d3962019-12-18 17:20:27 -05004056 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4057 objectid = inode->root->root_key.objectid;
4058 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4059 objectid = inode->location.objectid;
4060 } else {
4061 WARN_ON(1);
4062 return -EINVAL;
4063 }
4064
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004065 path = btrfs_alloc_path();
4066 if (!path)
4067 return -ENOMEM;
4068
Li Zefan33345d012011-04-20 10:31:50 +08004069 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004071 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004072 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004073 goto out;
4074 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075
4076 leaf = path->nodes[0];
4077 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4078 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4079 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004080 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004081 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 goto out;
4083 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004084 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085
Josef Bacikd49d3282019-12-18 17:20:28 -05004086 /*
4087 * This is a placeholder inode for a subvolume we didn't have a
4088 * reference to at the time of the snapshot creation. In the meantime
4089 * we could have renamed the real subvol link into our snapshot, so
4090 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4091 * Instead simply lookup the dir_index_item for this entry so we can
4092 * remove it. Otherwise we know we have a ref to the root and we can
4093 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4094 */
4095 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004096 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (IS_ERR_OR_NULL(di)) {
4099 if (!di)
4100 ret = -ENOENT;
4101 else
4102 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004103 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 goto out;
4105 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106
4107 leaf = path->nodes[0];
4108 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004110 btrfs_release_path(path);
4111 } else {
4112 ret = btrfs_del_root_ref(trans, objectid,
4113 root->root_key.objectid, dir_ino,
4114 &index, name, name_len);
4115 if (ret) {
4116 btrfs_abort_transaction(trans, ret);
4117 goto out;
4118 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120
Lu Fengqi9add2942018-08-01 11:32:26 +08004121 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124 goto out;
4125 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004126
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004127 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004128 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004129 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004130 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004132 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004134 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136}
4137
Misono Tomohiroec42f162018-04-18 11:34:13 +09004138/*
4139 * Helper to check if the subvolume references other subvolumes or if it's
4140 * default.
4141 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004142static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004143{
4144 struct btrfs_fs_info *fs_info = root->fs_info;
4145 struct btrfs_path *path;
4146 struct btrfs_dir_item *di;
4147 struct btrfs_key key;
4148 u64 dir_id;
4149 int ret;
4150
4151 path = btrfs_alloc_path();
4152 if (!path)
4153 return -ENOMEM;
4154
4155 /* Make sure this root isn't set as the default subvol */
4156 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4157 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4158 dir_id, "default", 7, 0);
4159 if (di && !IS_ERR(di)) {
4160 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4161 if (key.objectid == root->root_key.objectid) {
4162 ret = -EPERM;
4163 btrfs_err(fs_info,
4164 "deleting default subvolume %llu is not allowed",
4165 key.objectid);
4166 goto out;
4167 }
4168 btrfs_release_path(path);
4169 }
4170
4171 key.objectid = root->root_key.objectid;
4172 key.type = BTRFS_ROOT_REF_KEY;
4173 key.offset = (u64)-1;
4174
4175 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4176 if (ret < 0)
4177 goto out;
4178 BUG_ON(ret == 0);
4179
4180 ret = 0;
4181 if (path->slots[0] > 0) {
4182 path->slots[0]--;
4183 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4184 if (key.objectid == root->root_key.objectid &&
4185 key.type == BTRFS_ROOT_REF_KEY)
4186 ret = -ENOTEMPTY;
4187 }
4188out:
4189 btrfs_free_path(path);
4190 return ret;
4191}
4192
Nikolay Borisov20a68002018-04-27 14:36:24 +03004193/* Delete all dentries for inodes belonging to the root */
4194static void btrfs_prune_dentries(struct btrfs_root *root)
4195{
4196 struct btrfs_fs_info *fs_info = root->fs_info;
4197 struct rb_node *node;
4198 struct rb_node *prev;
4199 struct btrfs_inode *entry;
4200 struct inode *inode;
4201 u64 objectid = 0;
4202
4203 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4204 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4205
4206 spin_lock(&root->inode_lock);
4207again:
4208 node = root->inode_tree.rb_node;
4209 prev = NULL;
4210 while (node) {
4211 prev = node;
4212 entry = rb_entry(node, struct btrfs_inode, rb_node);
4213
David Sterba37508512018-06-29 10:56:40 +02004214 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004215 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004216 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004217 node = node->rb_right;
4218 else
4219 break;
4220 }
4221 if (!node) {
4222 while (prev) {
4223 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004224 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004225 node = prev;
4226 break;
4227 }
4228 prev = rb_next(prev);
4229 }
4230 }
4231 while (node) {
4232 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004233 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004234 inode = igrab(&entry->vfs_inode);
4235 if (inode) {
4236 spin_unlock(&root->inode_lock);
4237 if (atomic_read(&inode->i_count) > 1)
4238 d_prune_aliases(inode);
4239 /*
4240 * btrfs_drop_inode will have it removed from the inode
4241 * cache when its usage count hits zero.
4242 */
4243 iput(inode);
4244 cond_resched();
4245 spin_lock(&root->inode_lock);
4246 goto again;
4247 }
4248
4249 if (cond_resched_lock(&root->inode_lock))
4250 goto again;
4251
4252 node = rb_next(node);
4253 }
4254 spin_unlock(&root->inode_lock);
4255}
4256
Misono Tomohirof60a2362018-04-18 11:34:52 +09004257int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4258{
4259 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4260 struct btrfs_root *root = BTRFS_I(dir)->root;
4261 struct inode *inode = d_inode(dentry);
4262 struct btrfs_root *dest = BTRFS_I(inode)->root;
4263 struct btrfs_trans_handle *trans;
4264 struct btrfs_block_rsv block_rsv;
4265 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004266 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004267
4268 /*
4269 * Don't allow to delete a subvolume with send in progress. This is
4270 * inside the inode lock so the error handling that has to drop the bit
4271 * again is not run concurrently.
4272 */
4273 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004274 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004275 spin_unlock(&dest->root_item_lock);
4276 btrfs_warn(fs_info,
4277 "attempt to delete subvolume %llu during send",
4278 dest->root_key.objectid);
4279 return -EPERM;
4280 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004281 root_flags = btrfs_root_flags(&dest->root_item);
4282 btrfs_set_root_flags(&dest->root_item,
4283 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4284 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004285
4286 down_write(&fs_info->subvol_sem);
4287
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004288 ret = may_destroy_subvol(dest);
4289 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004290 goto out_up_write;
4291
4292 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4293 /*
4294 * One for dir inode,
4295 * two for dir entries,
4296 * two for root ref/backref.
4297 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004298 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4299 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004300 goto out_up_write;
4301
4302 trans = btrfs_start_transaction(root, 0);
4303 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004304 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004305 goto out_release;
4306 }
4307 trans->block_rsv = &block_rsv;
4308 trans->bytes_reserved = block_rsv.size;
4309
4310 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4311
Josef Bacik045d3962019-12-18 17:20:27 -05004312 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004313 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004314 btrfs_abort_transaction(trans, ret);
4315 goto out_end_trans;
4316 }
4317
Josef Bacik2731f512021-03-12 15:25:04 -05004318 ret = btrfs_record_root_in_trans(trans, dest);
4319 if (ret) {
4320 btrfs_abort_transaction(trans, ret);
4321 goto out_end_trans;
4322 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004323
4324 memset(&dest->root_item.drop_progress, 0,
4325 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004326 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004327 btrfs_set_root_refs(&dest->root_item, 0);
4328
4329 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4330 ret = btrfs_insert_orphan_item(trans,
4331 fs_info->tree_root,
4332 dest->root_key.objectid);
4333 if (ret) {
4334 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004335 goto out_end_trans;
4336 }
4337 }
4338
Lu Fengqid1957792018-05-29 15:01:54 +08004339 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004340 BTRFS_UUID_KEY_SUBVOL,
4341 dest->root_key.objectid);
4342 if (ret && ret != -ENOENT) {
4343 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004344 goto out_end_trans;
4345 }
4346 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004347 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004348 dest->root_item.received_uuid,
4349 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4350 dest->root_key.objectid);
4351 if (ret && ret != -ENOENT) {
4352 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004353 goto out_end_trans;
4354 }
4355 }
4356
Qu Wenruo082b6c92020-06-16 10:17:37 +08004357 free_anon_bdev(dest->anon_dev);
4358 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004359out_end_trans:
4360 trans->block_rsv = NULL;
4361 trans->bytes_reserved = 0;
4362 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004363 inode->i_flags |= S_DEAD;
4364out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004365 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004366out_up_write:
4367 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004368 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004369 spin_lock(&dest->root_item_lock);
4370 root_flags = btrfs_root_flags(&dest->root_item);
4371 btrfs_set_root_flags(&dest->root_item,
4372 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4373 spin_unlock(&dest->root_item_lock);
4374 } else {
4375 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004376 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004377 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004378 }
4379
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004380 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004381}
4382
Chris Mason39279cc2007-06-12 06:35:45 -04004383static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4384{
David Howells2b0143b2015-03-17 22:25:59 +00004385 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004386 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004389 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004390
David Sterbab3ae2442012-09-13 16:04:34 -06004391 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004392 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004393 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004394 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004395
Josef Bacikd52be812013-05-29 14:54:47 -04004396 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004397 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004398 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004399
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004400 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004401 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004402 goto out;
4403 }
4404
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004405 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004406 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004407 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004408
Filipe Manana44f714d2016-06-06 16:11:13 +01004409 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4410
Chris Mason39279cc2007-06-12 06:35:45 -04004411 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004412 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4413 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4414 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004415 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004416 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004417 /*
4418 * Propagate the last_unlink_trans value of the deleted dir to
4419 * its parent directory. This is to prevent an unrecoverable
4420 * log tree in the case we do something like this:
4421 * 1) create dir foo
4422 * 2) create snapshot under dir foo
4423 * 3) delete the snapshot
4424 * 4) rmdir foo
4425 * 5) mkdir foo
4426 * 6) fsync foo or some file inside foo
4427 */
4428 if (last_unlink_trans >= trans->transid)
4429 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4430 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004431out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004432 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004433 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004434
Chris Mason39279cc2007-06-12 06:35:45 -04004435 return err;
4436}
4437
Josef Bacikddfae632017-10-19 14:16:02 -04004438/*
4439 * Return this if we need to call truncate_block for the last bit of the
4440 * truncate.
4441 */
4442#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004443
Chris Mason323ac952008-10-01 19:05:46 -04004444/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004445 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004446 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004447 * @trans: A transaction handle.
4448 * @root: The root from which to remove items.
4449 * @inode: The inode whose items we want to remove.
4450 * @new_size: The new i_size for the inode. This is only applicable when
4451 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4452 * @min_type: The minimum key type to remove. All keys with a type
4453 * greater than this value are removed and all keys with
4454 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004455 * @extents_found: Output parameter that will contain the number of file
4456 * extent items that were removed or adjusted to the new
4457 * inode i_size. The caller is responsible for initializing
4458 * the counter. Also, it can be NULL if the caller does not
4459 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004460 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004461 * Remove all keys associated with the inode from the given root that have a key
4462 * with a type greater than or equals to @min_type. When @min_type has a value of
4463 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4464 * greater than or equals to @new_size. If a file extent item that starts before
4465 * @new_size and ends after it is found, its length is adjusted.
4466 *
4467 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4468 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004469 */
Yan, Zheng80825102009-11-12 09:35:36 +00004470int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4471 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004472 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004473 u64 new_size, u32 min_type,
4474 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004475{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004476 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004480 struct btrfs_key key;
4481 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004483 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004484 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004485 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004486 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004487 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004488 int found_extent;
4489 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 int pending_del_nr = 0;
4491 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004492 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004493 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004494 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004495 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004496 bool be_nice = false;
4497 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004498 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4499 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004500
4501 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004502
Chris Mason28ed1342014-12-17 09:41:04 -08004503 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004504 * For non-free space inodes and non-shareable roots, we want to back
4505 * off from time to time. This means all inodes in subvolume roots,
4506 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004507 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004508 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004509 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004510 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004511
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004512 path = btrfs_alloc_path();
4513 if (!path)
4514 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004515 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004516
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004517 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004518 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004519 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004520
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004521 /*
4522 * We want to drop from the next block forward in case this
4523 * new size is not block aligned since we will be keeping the
4524 * last block of the extent just the way it is.
4525 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004526 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004527 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004528 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004529 }
Yan, Zheng80825102009-11-12 09:35:36 +00004530
Miao Xie16cdcec2011-04-22 18:12:22 +08004531 /*
4532 * This function is also used to drop the items in the log tree before
4533 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004534 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004535 * items.
4536 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004537 if (min_type == 0 && root == inode->root)
4538 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004539
Li Zefan33345d012011-04-20 10:31:50 +08004540 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004542 key.type = (u8)-1;
4543
Chris Mason85e21ba2008-01-29 15:11:36 -05004544search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004545 /*
4546 * with a 16K leaf size and 128MB extents, you can actually queue
4547 * up a huge file in a single leaf. Most of the time that
4548 * bytes_deleted is > 0, it will be huge by the time we get here
4549 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004550 if (be_nice && bytes_deleted > SZ_32M &&
4551 btrfs_should_end_transaction(trans)) {
4552 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004553 goto out;
4554 }
Chris Masond3977122009-01-05 21:25:51 -05004555
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004556 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4557 if (ret < 0)
4558 goto out;
4559
Chris Mason85e21ba2008-01-29 15:11:36 -05004560 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004561 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004562 /* there are no items in the tree for us to truncate, we're
4563 * done
4564 */
Yan, Zheng80825102009-11-12 09:35:36 +00004565 if (path->slots[0] == 0)
4566 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004567 path->slots[0]--;
4568 }
4569
Chris Masond3977122009-01-05 21:25:51 -05004570 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004571 u64 clear_start = 0, clear_len = 0;
4572
Chris Mason39279cc2007-06-12 06:35:45 -04004573 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004574 leaf = path->nodes[0];
4575 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004576 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004577
Li Zefan33345d012011-04-20 10:31:50 +08004578 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004579 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004580
Chris Mason85e21ba2008-01-29 15:11:36 -05004581 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004582 break;
4583
Chris Mason5f39d392007-10-15 16:14:19 -04004584 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004585 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004586 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004587 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004588 extent_type = btrfs_file_extent_type(leaf, fi);
4589 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004590 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004591 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004592
4593 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004594 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004595 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004596 item_end += btrfs_file_extent_ram_bytes(leaf,
4597 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004598
4599 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004600 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004601 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004602 }
Yan008630c2007-11-07 13:31:09 -05004603 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004604 }
Yan, Zheng80825102009-11-12 09:35:36 +00004605 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004606 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004607 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004608 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004609 break;
4610 if (found_key.offset >= new_size)
4611 del_item = 1;
4612 else
4613 del_item = 0;
4614 }
Chris Mason39279cc2007-06-12 06:35:45 -04004615 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004616 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004617 if (found_type != BTRFS_EXTENT_DATA_KEY)
4618 goto delete;
4619
Filipe Manana0d7d3162021-05-24 11:35:55 +01004620 if (extents_found != NULL)
4621 (*extents_found)++;
4622
Chris Mason179e29e2007-11-01 11:28:41 -04004623 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004624 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004625
4626 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004627 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004628 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004629 u64 orig_num_bytes =
4630 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004631 extent_num_bytes = ALIGN(new_size -
4632 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004633 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004634 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004635 btrfs_set_file_extent_num_bytes(leaf, fi,
4636 extent_num_bytes);
4637 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004638 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004639 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004640 &root->state) &&
4641 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004642 inode_sub_bytes(&inode->vfs_inode,
4643 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004644 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004645 } else {
Chris Masondb945352007-10-15 16:15:53 -04004646 extent_num_bytes =
4647 btrfs_file_extent_disk_num_bytes(leaf,
4648 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004649 extent_offset = found_key.offset -
4650 btrfs_file_extent_offset(leaf, fi);
4651
Chris Mason39279cc2007-06-12 06:35:45 -04004652 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004653 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004654 if (extent_start != 0) {
4655 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004656 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004657 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004658 inode_sub_bytes(&inode->vfs_inode,
4659 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004660 }
4661 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004662 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004663 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004664 /*
4665 * we can't truncate inline items that have had
4666 * special encodings
4667 */
4668 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004669 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004670 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4671 btrfs_file_extent_compression(leaf, fi) == 0) {
4672 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004673
Josef Bacikddfae632017-10-19 14:16:02 -04004674 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4675 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004676 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004677 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004678 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004679 * We have to bail so the last_size is set to
4680 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004681 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004682 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004683 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004684 } else {
4685 /*
4686 * Inline extents are special, we just treat
4687 * them as a full sector worth in the file
4688 * extent tree just for simplicity sake.
4689 */
4690 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004691 }
Josef Bacikddfae632017-10-19 14:16:02 -04004692
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004693 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004694 inode_sub_bytes(&inode->vfs_inode,
4695 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004696 }
Chris Mason179e29e2007-11-01 11:28:41 -04004697delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004698 /*
4699 * We use btrfs_truncate_inode_items() to clean up log trees for
4700 * multiple fsyncs, and in this case we don't want to clear the
4701 * file extent range because it's just the log.
4702 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004703 if (root == inode->root) {
4704 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004705 clear_start, clear_len);
4706 if (ret) {
4707 btrfs_abort_transaction(trans, ret);
4708 break;
4709 }
4710 }
4711
Josef Bacikddfae632017-10-19 14:16:02 -04004712 if (del_item)
4713 last_size = found_key.offset;
4714 else
4715 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004717 if (!pending_del_nr) {
4718 /* no pending yet, add ourselves */
4719 pending_del_slot = path->slots[0];
4720 pending_del_nr = 1;
4721 } else if (pending_del_nr &&
4722 path->slots[0] + 1 == pending_del_slot) {
4723 /* hop on the pending chunk */
4724 pending_del_nr++;
4725 pending_del_slot = path->slots[0];
4726 } else {
Chris Masond3977122009-01-05 21:25:51 -05004727 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 }
Chris Mason39279cc2007-06-12 06:35:45 -04004729 } else {
4730 break;
4731 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004732 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004733
Miao Xie27cdeb72014-04-02 19:51:05 +08004734 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004735 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004736 struct btrfs_ref ref = { 0 };
4737
Chris Mason28ed1342014-12-17 09:41:04 -08004738 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004739
4740 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4741 extent_start, extent_num_bytes, 0);
4742 ref.real_root = root->root_key.objectid;
4743 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4744 ino, extent_offset);
4745 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004746 if (ret) {
4747 btrfs_abort_transaction(trans, ret);
4748 break;
4749 }
Chris Mason28f75a02015-02-04 06:59:29 -08004750 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004751 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004752 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004753 }
Chris Mason39279cc2007-06-12 06:35:45 -04004754 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004755
Yan, Zheng80825102009-11-12 09:35:36 +00004756 if (found_type == BTRFS_INODE_ITEM_KEY)
4757 break;
4758
4759 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004760 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004761 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004762 if (pending_del_nr) {
4763 ret = btrfs_del_items(trans, root, path,
4764 pending_del_slot,
4765 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004766 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004767 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004768 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004769 }
Yan, Zheng80825102009-11-12 09:35:36 +00004770 pending_del_nr = 0;
4771 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004772 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004773
Chris Mason28f75a02015-02-04 06:59:29 -08004774 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004775 * We can generate a lot of delayed refs, so we need to
4776 * throttle every once and a while and make sure we're
4777 * adding enough space to keep up with the work we are
4778 * generating. Since we hold a transaction here we
4779 * can't flush, and we don't want to FLUSH_LIMIT because
4780 * we could have generated too many delayed refs to
4781 * actually allocate, so just bail if we're short and
4782 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004783 */
Josef Bacik28bad212018-12-03 10:20:38 -05004784 if (should_throttle) {
4785 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4786 BTRFS_RESERVE_NO_FLUSH);
4787 if (ret) {
4788 ret = -EAGAIN;
4789 break;
4790 }
Chris Mason28f75a02015-02-04 06:59:29 -08004791 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004792 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004793 } else {
4794 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004795 }
Chris Mason39279cc2007-06-12 06:35:45 -04004796 }
Yan, Zheng80825102009-11-12 09:35:36 +00004797out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004798 if (ret >= 0 && pending_del_nr) {
4799 int err;
4800
4801 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004802 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004803 if (err) {
4804 btrfs_abort_transaction(trans, err);
4805 ret = err;
4806 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004807 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004808 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4809 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004810 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004811 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004812 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4813 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4814 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004815 }
Chris Mason28ed1342014-12-17 09:41:04 -08004816
Chris Mason39279cc2007-06-12 06:35:45 -04004817 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004818 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004819}
4820
Chris Masona52d9a82007-08-27 16:49:44 -04004821/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004823 * @inode - inode that we're zeroing
4824 * @from - the offset to start zeroing
4825 * @len - the length to zero, 0 to zero the entire range respective to the
4826 * offset
4827 * @front - zero up to the offset instead of from the offset on
4828 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004830 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004831 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004832int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4833 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004834{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004835 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4836 struct address_space *mapping = inode->vfs_inode.i_mapping;
4837 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004838 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004839 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004840 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004841 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004843 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004845 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004846 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004847 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004848 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 u64 block_start;
4850 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004851
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004852 if (IS_ALIGNED(offset, blocksize) &&
4853 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004854 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855
Josef Bacik8b62f872017-10-19 14:15:55 -04004856 block_start = round_down(from, blocksize);
4857 block_end = block_start + blocksize - 1;
4858
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004859 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4860 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004861 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004862 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004863 /* For nocow case, no need to reserve data space */
4864 only_release_metadata = true;
4865 } else {
4866 goto out;
4867 }
4868 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004869 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004870 if (ret < 0) {
4871 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004872 btrfs_free_reserved_data_space(inode, data_reserved,
4873 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004874 goto out;
4875 }
Chris Mason211c17f2008-05-15 09:13:45 -04004876again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004877 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004878 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004879 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4880 blocksize, true);
4881 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004882 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004883 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004884 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004885 ret = set_page_extent_mapped(page);
4886 if (ret < 0)
4887 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004888
Chris Masona52d9a82007-08-27 16:49:44 -04004889 if (!PageUptodate(page)) {
4890 ret = btrfs_readpage(NULL, page);
4891 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004892 if (page->mapping != mapping) {
4893 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004894 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004895 goto again;
4896 }
Chris Masona52d9a82007-08-27 16:49:44 -04004897 if (!PageUptodate(page)) {
4898 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004899 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004900 }
4901 }
Chris Mason211c17f2008-05-15 09:13:45 -04004902 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004903
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304904 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004905
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004906 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004907 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304908 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004909 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004910 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004911 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004912 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004913 btrfs_put_ordered_extent(ordered);
4914 goto again;
4915 }
4916
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004917 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004918 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4919 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004920
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004921 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004922 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004923 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304924 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004925 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004926 goto out_unlock;
4927 }
4928
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304929 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004930 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304931 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04004932 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07004933 memzero_page(page, (block_start - page_offset(page)),
4934 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004935 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07004936 memzero_page(page, (block_start - page_offset(page)) + offset,
4937 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004938 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004939 }
Chris Mason247e7432008-07-17 12:53:51 -04004940 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004941 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004942 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004943
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004944 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004945 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004946 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004947
Chris Mason89642222008-07-24 09:41:53 -04004948out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004949 if (ret) {
4950 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004951 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004952 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004953 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004954 block_start, blocksize, true);
4955 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004956 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004957 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004958 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004959out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004960 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004961 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004962 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004963 return ret;
4964}
4965
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004966static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004967 u64 offset, u64 len)
4968{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004969 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004970 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004971 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004972 int ret;
4973
4974 /*
4975 * Still need to make sure the inode looks like it's been updated so
4976 * that any holes get logged if we fsync.
4977 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004978 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004979 inode->last_trans = fs_info->generation;
4980 inode->last_sub_trans = root->log_transid;
4981 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004982 return 0;
4983 }
4984
4985 /*
4986 * 1 - for the one we're dropping
4987 * 1 - for the one we're adding
4988 * 1 - for updating the inode.
4989 */
4990 trans = btrfs_start_transaction(root, 3);
4991 if (IS_ERR(trans))
4992 return PTR_ERR(trans);
4993
Filipe Manana5893dfb2020-11-04 11:07:32 +00004994 drop_args.start = offset;
4995 drop_args.end = offset + len;
4996 drop_args.drop_cache = true;
4997
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004998 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004999 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005000 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005001 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005002 return ret;
5003 }
5004
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005005 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005006 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005007 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005008 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005009 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005010 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5011 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005012 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005013 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005014 return ret;
5015}
5016
Josef Bacik695a0d02011-03-04 15:46:53 -05005017/*
5018 * This function puts in dummy file extents for the area we're creating a hole
5019 * for. So if we are truncating this file to a larger size we need to insert
5020 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5021 * the range between oldsize and size
5022 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005023int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005024{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005025 struct btrfs_root *root = inode->root;
5026 struct btrfs_fs_info *fs_info = root->fs_info;
5027 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005028 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005029 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005030 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5032 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005033 u64 last_byte;
5034 u64 cur_offset;
5035 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005036 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005037
Josef Bacika71754f2013-06-17 17:14:39 -04005038 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305039 * If our size started in the middle of a block we need to zero out the
5040 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005041 * expose stale data.
5042 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005043 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005044 if (err)
5045 return err;
5046
Yan Zheng9036c102008-10-30 14:19:41 -04005047 if (size <= hole_start)
5048 return 0;
5049
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005050 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5051 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005052 cur_offset = hole_start;
5053 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005054 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005055 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005056 if (IS_ERR(em)) {
5057 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005058 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005059 break;
5060 }
Yan Zheng9036c102008-10-30 14:19:41 -04005061 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005062 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005063 hole_size = last_byte - cur_offset;
5064
Yan, Zheng80825102009-11-12 09:35:36 +00005065 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005066 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005067
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005068 err = maybe_insert_hole(root, inode, cur_offset,
5069 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005070 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005071 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005072
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005073 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005074 cur_offset, hole_size);
5075 if (err)
5076 break;
5077
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005078 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005079 cur_offset + hole_size - 1, 0);
5080 hole_em = alloc_extent_map();
5081 if (!hole_em) {
5082 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005083 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005084 goto next;
5085 }
5086 hole_em->start = cur_offset;
5087 hole_em->len = hole_size;
5088 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005089
Josef Bacik5dc562c2012-08-17 13:14:17 -04005090 hole_em->block_start = EXTENT_MAP_HOLE;
5091 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005092 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005093 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005094 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005095 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005096
5097 while (1) {
5098 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005099 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005100 write_unlock(&em_tree->lock);
5101 if (err != -EEXIST)
5102 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005103 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005104 cur_offset +
5105 hole_size - 1, 0);
5106 }
5107 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005108 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005109 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005110 cur_offset, hole_size);
5111 if (err)
5112 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005113 }
Josef Bacik16e75492013-10-22 12:18:51 -04005114next:
Yan Zheng9036c102008-10-30 14:19:41 -04005115 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005116 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005117 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005118 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005119 break;
5120 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005121 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005122 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005123 return err;
5124}
5125
Eric Sandeen3972f262013-01-12 02:57:22 +00005126static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005127{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005128 struct btrfs_root *root = BTRFS_I(inode)->root;
5129 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005130 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005131 loff_t newsize = attr->ia_size;
5132 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005133 int ret;
5134
Eric Sandeen3972f262013-01-12 02:57:22 +00005135 /*
5136 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5137 * special case where we need to update the times despite not having
5138 * these flags set. For all other operations the VFS set these flags
5139 * explicitly if it wants a timestamp update.
5140 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005141 if (newsize != oldsize) {
5142 inode_inc_iversion(inode);
5143 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5144 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005145 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005146 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005147
Josef Bacika41ad392011-01-31 15:30:16 -05005148 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005149 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005150 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005151 * This is to ensure the snapshot captures a fully consistent
5152 * state of this file - if the snapshot captures this expanding
5153 * truncation, it must capture all writes that happened before
5154 * this truncation.
5155 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005156 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005157 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005158 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005159 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005160 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005161 }
Yan, Zheng80825102009-11-12 09:35:36 +00005162
Miao Xief4a2f4c2011-12-14 20:12:01 -05005163 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005164 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005165 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005166 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005167 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005168
5169 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005170 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305171 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005172 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005173 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005174 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005175 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5177
5178 if (btrfs_is_zoned(fs_info)) {
5179 ret = btrfs_wait_ordered_range(inode,
5180 ALIGN(newsize, fs_info->sectorsize),
5181 (u64)-1);
5182 if (ret)
5183 return ret;
5184 }
Yan, Zheng80825102009-11-12 09:35:36 +00005185
Josef Bacika41ad392011-01-31 15:30:16 -05005186 /*
5187 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005188 * zero. Make sure any new writes to the file get on disk
5189 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005190 */
5191 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005192 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005193 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005194
Josef Bacika41ad392011-01-31 15:30:16 -05005195 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005196
Miao Xie2e60a512013-02-08 07:01:08 +00005197 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005198
Filipe Manana213e8c52018-02-06 20:40:31 +00005199 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005200 if (ret && inode->i_nlink) {
5201 int err;
5202
5203 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005204 * Truncate failed, so fix up the in-memory size. We
5205 * adjusted disk_i_size down as we removed extents, so
5206 * wait for disk_i_size to be stable and then update the
5207 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005208 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005209 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005210 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005211 return err;
5212 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005213 }
Yan, Zheng80825102009-11-12 09:35:36 +00005214 }
5215
Josef Bacika41ad392011-01-31 15:30:16 -05005216 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005217}
5218
Christian Brauner549c7292021-01-21 14:19:43 +01005219static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5220 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005221{
David Howells2b0143b2015-03-17 22:25:59 +00005222 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005223 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005224 int err;
5225
Li Zefanb83cc962010-12-20 16:04:08 +08005226 if (btrfs_root_readonly(root))
5227 return -EROFS;
5228
Christian Brauner2f221d62021-01-21 14:19:26 +01005229 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005230 if (err)
5231 return err;
5232
Chris Mason5a3f23d2009-03-31 13:27:11 -04005233 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005234 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005235 if (err)
5236 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005237 }
Yan Zheng9036c102008-10-30 14:19:41 -04005238
Christoph Hellwig10257742010-06-04 11:30:02 +02005239 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005240 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005241 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005242 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005243
Josef Bacik22c44fe2011-11-30 10:45:38 -05005244 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005245 err = posix_acl_chmod(&init_user_ns, inode,
5246 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005247 }
5248
Chris Mason39279cc2007-06-12 06:35:45 -04005249 return err;
5250}
Chris Mason61295eb2008-01-14 16:24:38 -05005251
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005252/*
5253 * While truncating the inode pages during eviction, we get the VFS calling
5254 * btrfs_invalidatepage() against each page of the inode. This is slow because
5255 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5256 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5257 * extent_state structures over and over, wasting lots of time.
5258 *
5259 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5260 * those expensive operations on a per page basis and do only the ordered io
5261 * finishing, while we release here the extent_map and extent_state structures,
5262 * without the excessive merging and splitting.
5263 */
5264static void evict_inode_truncate_pages(struct inode *inode)
5265{
5266 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5267 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5268 struct rb_node *node;
5269
5270 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005271 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005272
5273 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005274 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 struct extent_map *em;
5276
Liu Bo07e1ce02018-08-23 03:51:52 +08005277 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005279 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5280 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281 remove_extent_mapping(map_tree, em);
5282 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005283 if (need_resched()) {
5284 write_unlock(&map_tree->lock);
5285 cond_resched();
5286 write_lock(&map_tree->lock);
5287 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005288 }
5289 write_unlock(&map_tree->lock);
5290
Filipe Manana6ca07092015-05-26 00:55:42 +01005291 /*
5292 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005293 * We can have ongoing bios started by readahead that have
5294 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005295 * still in progress (unlocked the pages in the bio but did not yet
5296 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005297 * ranges can still be locked and eviction started because before
5298 * submitting those bios, which are executed by a separate task (work
5299 * queue kthread), inode references (inode->i_count) were not taken
5300 * (which would be dropped in the end io callback of each bio).
5301 * Therefore here we effectively end up waiting for those bios and
5302 * anyone else holding locked ranges without having bumped the inode's
5303 * reference count - if we don't do it, when they access the inode's
5304 * io_tree to unlock a range it may be too late, leading to an
5305 * use-after-free issue.
5306 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005307 spin_lock(&io_tree->lock);
5308 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5309 struct extent_state *state;
5310 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005311 u64 start;
5312 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005313 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005314
5315 node = rb_first(&io_tree->state);
5316 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005317 start = state->start;
5318 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005319 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005320 spin_unlock(&io_tree->lock);
5321
David Sterbaff13db42015-12-03 14:30:40 +01005322 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005323
5324 /*
5325 * If still has DELALLOC flag, the extent didn't reach disk,
5326 * and its reserved space won't be freed by delayed_ref.
5327 * So we need to free its reserved space here.
5328 * (Refer to comment in btrfs_invalidatepage, case 2)
5329 *
5330 * Note, end is the bytenr of last byte, so we need + 1 here.
5331 */
Filipe Manana421f0922018-10-12 13:02:48 +01005332 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005333 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5334 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005335
Filipe Manana6ca07092015-05-26 00:55:42 +01005336 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005337 EXTENT_LOCKED | EXTENT_DELALLOC |
5338 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5339 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005340
Filipe Manana7064dd52014-08-08 02:47:05 +01005341 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005342 spin_lock(&io_tree->lock);
5343 }
5344 spin_unlock(&io_tree->lock);
5345}
5346
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005347static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005348 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005349{
5350 struct btrfs_fs_info *fs_info = root->fs_info;
5351 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005352 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005353 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005354 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005355
Josef Bacikd3984c92019-08-01 18:19:37 -04005356 /*
5357 * Eviction should be taking place at some place safe because of our
5358 * delayed iputs. However the normal flushing code will run delayed
5359 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5360 *
5361 * We reserve the delayed_refs_extra here again because we can't use
5362 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5363 * above. We reserve our extra bit here because we generate a ton of
5364 * delayed refs activity by truncating.
5365 *
5366 * If we cannot make our reservation we'll attempt to steal from the
5367 * global reserve, because we really want to be able to free up space.
5368 */
5369 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5370 BTRFS_RESERVE_FLUSH_EVICT);
5371 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005372 /*
5373 * Try to steal from the global reserve if there is space for
5374 * it.
5375 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005376 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5377 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5378 btrfs_warn(fs_info,
5379 "could not allocate space for delete; will truncate on mount");
5380 return ERR_PTR(-ENOSPC);
5381 }
5382 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005383 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005384
5385 trans = btrfs_join_transaction(root);
5386 if (IS_ERR(trans))
5387 return trans;
5388
5389 if (delayed_refs_extra) {
5390 trans->block_rsv = &fs_info->trans_block_rsv;
5391 trans->bytes_reserved = delayed_refs_extra;
5392 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5393 delayed_refs_extra, 1);
5394 }
5395 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005396}
5397
Al Virobd555972010-06-07 11:35:40 -04005398void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005399{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005400 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005401 struct btrfs_trans_handle *trans;
5402 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005403 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005404 int ret;
5405
liubo1abe9b82011-03-24 11:18:59 +00005406 trace_btrfs_inode_evict(inode);
5407
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005408 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005409 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005410 return;
5411 }
5412
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005413 evict_inode_truncate_pages(inode);
5414
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005415 if (inode->i_nlink &&
5416 ((btrfs_root_refs(&root->root_item) != 0 &&
5417 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005418 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005419 goto no_delete;
5420
Omar Sandoval27919062018-05-11 13:13:37 -07005421 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005422 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005423
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005424 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005425
Omar Sandoval7b40b692018-05-11 13:13:33 -07005426 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005427 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005428
Yan, Zheng76dda932009-09-21 16:00:26 -04005429 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005430 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5431 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005432 goto no_delete;
5433 }
5434
Nikolay Borisovaa790212017-01-10 20:35:40 +02005435 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005436 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005437 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005438
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005439 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005440 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005441 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005442 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005443 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005444
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005445 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005446
Yan, Zheng80825102009-11-12 09:35:36 +00005447 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005448 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005449 if (IS_ERR(trans))
5450 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005451
5452 trans->block_rsv = rsv;
5453
Nikolay Borisov50743392020-11-02 16:48:55 +02005454 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005455 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005456 trans->block_rsv = &fs_info->trans_block_rsv;
5457 btrfs_end_transaction(trans);
5458 btrfs_btree_balance_dirty(fs_info);
5459 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5460 goto free_rsv;
5461 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005462 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005463 }
5464
Josef Bacik4ef31a42013-08-13 14:10:08 -04005465 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005466 * Errors here aren't a big deal, it just means we leave orphan items in
5467 * the tree. They will be cleaned up on the next mount. If the inode
5468 * number gets reused, cleanup deletes the orphan item without doing
5469 * anything, and unlink reuses the existing orphan item.
5470 *
5471 * If it turns out that we are dropping too many of these, we might want
5472 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005473 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005474 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005475 if (!IS_ERR(trans)) {
5476 trans->block_rsv = rsv;
5477 btrfs_orphan_del(trans, BTRFS_I(inode));
5478 trans->block_rsv = &fs_info->trans_block_rsv;
5479 btrfs_end_transaction(trans);
5480 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005481
Omar Sandoval27919062018-05-11 13:13:37 -07005482free_rsv:
5483 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005484no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005485 /*
5486 * If we didn't successfully delete, the orphan item will still be in
5487 * the tree and we'll retry on the next mount. Again, we might also want
5488 * to retry these periodically in the future.
5489 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005490 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005491 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005492}
5493
5494/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005495 * Return the key found in the dir entry in the location pointer, fill @type
5496 * with BTRFS_FT_*, and return 0.
5497 *
Su Yue005d6712018-03-05 17:13:37 +08005498 * If no dir entries were found, returns -ENOENT.
5499 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005500 */
5501static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005502 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005503{
5504 const char *name = dentry->d_name.name;
5505 int namelen = dentry->d_name.len;
5506 struct btrfs_dir_item *di;
5507 struct btrfs_path *path;
5508 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005509 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005510
5511 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005512 if (!path)
5513 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005514
David Sterbaf85b7372017-01-20 14:54:07 +01005515 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5516 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005517 if (IS_ERR_OR_NULL(di)) {
5518 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005519 goto out;
5520 }
Chris Masond3977122009-01-05 21:25:51 -05005521
Chris Mason5f39d392007-10-15 16:14:19 -04005522 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005523 if (location->type != BTRFS_INODE_ITEM_KEY &&
5524 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005525 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005526 btrfs_warn(root->fs_info,
5527"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5528 __func__, name, btrfs_ino(BTRFS_I(dir)),
5529 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005530 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005531 if (!ret)
5532 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005533out:
Chris Mason39279cc2007-06-12 06:35:45 -04005534 btrfs_free_path(path);
5535 return ret;
5536}
5537
5538/*
5539 * when we hit a tree root in a directory, the btrfs part of the inode
5540 * needs to be changed to reflect the root directory of the tree root. This
5541 * is kind of like crossing a mount point.
5542 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005543static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 struct inode *dir,
5545 struct dentry *dentry,
5546 struct btrfs_key *location,
5547 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005549 struct btrfs_path *path;
5550 struct btrfs_root *new_root;
5551 struct btrfs_root_ref *ref;
5552 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005553 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005554 int ret;
5555 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005556
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557 path = btrfs_alloc_path();
5558 if (!path) {
5559 err = -ENOMEM;
5560 goto out;
5561 }
Chris Mason39279cc2007-06-12 06:35:45 -04005562
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005563 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005564 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5565 key.type = BTRFS_ROOT_REF_KEY;
5566 key.offset = location->objectid;
5567
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005568 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569 if (ret) {
5570 if (ret < 0)
5571 err = ret;
5572 goto out;
5573 }
Chris Mason39279cc2007-06-12 06:35:45 -04005574
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005575 leaf = path->nodes[0];
5576 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005577 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005578 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5579 goto out;
5580
5581 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5582 (unsigned long)(ref + 1),
5583 dentry->d_name.len);
5584 if (ret)
5585 goto out;
5586
David Sterbab3b4aa72011-04-21 01:20:15 +02005587 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005588
David Sterba56e93572020-05-15 19:35:55 +02005589 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005590 if (IS_ERR(new_root)) {
5591 err = PTR_ERR(new_root);
5592 goto out;
5593 }
5594
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005595 *sub_root = new_root;
5596 location->objectid = btrfs_root_dirid(&new_root->root_item);
5597 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005598 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599 err = 0;
5600out:
5601 btrfs_free_path(path);
5602 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005603}
5604
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605static void inode_tree_add(struct inode *inode)
5606{
5607 struct btrfs_root *root = BTRFS_I(inode)->root;
5608 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005609 struct rb_node **p;
5610 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005611 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005612 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613
Al Viro1d3382cb2010-10-23 15:19:20 -04005614 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005616 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005618 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 while (*p) {
5620 parent = *p;
5621 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5622
David Sterba37508512018-06-29 10:56:40 +02005623 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005624 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005625 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005626 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005627 else {
5628 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005629 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005630 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005631 RB_CLEAR_NODE(parent);
5632 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005633 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634 }
5635 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005636 rb_link_node(new, parent, p);
5637 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 spin_unlock(&root->inode_lock);
5639}
5640
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005641static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005643 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005644 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005645
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005646 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005647 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5648 rb_erase(&inode->rb_node, &root->inode_tree);
5649 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005650 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005651 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005652 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005653
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005654 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 spin_lock(&root->inode_lock);
5656 empty = RB_EMPTY_ROOT(&root->inode_tree);
5657 spin_unlock(&root->inode_lock);
5658 if (empty)
5659 btrfs_add_dead_root(root);
5660 }
5661}
5662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005663
Chris Masone02119d2008-09-05 16:13:11 -04005664static int btrfs_init_locked_inode(struct inode *inode, void *p)
5665{
5666 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005667
5668 inode->i_ino = args->ino;
5669 BTRFS_I(inode)->location.objectid = args->ino;
5670 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5671 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005672 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5673 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005674 return 0;
5675}
5676
5677static int btrfs_find_actor(struct inode *inode, void *opaque)
5678{
5679 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005680
5681 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005682 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005683}
5684
David Sterba0202e832020-05-15 19:35:59 +02005685static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005686 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005687{
5688 struct inode *inode;
5689 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005690 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005691
David Sterba0202e832020-05-15 19:35:59 +02005692 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005693 args.root = root;
5694
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005695 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005696 btrfs_init_locked_inode,
5697 (void *)&args);
5698 return inode;
5699}
5700
David Sterba4c66e0d2019-10-03 19:09:35 +02005701/*
David Sterba0202e832020-05-15 19:35:59 +02005702 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005703 * Path can be preallocated to prevent recursing back to iget through
5704 * allocator. NULL is also valid but may require an additional allocation
5705 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305706 */
David Sterba0202e832020-05-15 19:35:59 +02005707struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005708 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305709{
5710 struct inode *inode;
5711
David Sterba0202e832020-05-15 19:35:59 +02005712 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305713 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005714 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305715
5716 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005717 int ret;
5718
Filipe Manana4222ea72018-10-24 10:13:03 +01005719 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005720 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005721 inode_tree_add(inode);
5722 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005723 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005724 iget_failed(inode);
5725 /*
5726 * ret > 0 can come from btrfs_search_slot called by
5727 * btrfs_read_locked_inode, this means the inode item
5728 * was not found.
5729 */
5730 if (ret > 0)
5731 ret = -ENOENT;
5732 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005733 }
5734 }
5735
Balaji Rao1a54ef82008-07-21 02:01:04 +05305736 return inode;
5737}
5738
David Sterba0202e832020-05-15 19:35:59 +02005739struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005740{
David Sterba0202e832020-05-15 19:35:59 +02005741 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005742}
5743
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744static struct inode *new_simple_dir(struct super_block *s,
5745 struct btrfs_key *key,
5746 struct btrfs_root *root)
5747{
5748 struct inode *inode = new_inode(s);
5749
5750 if (!inode)
5751 return ERR_PTR(-ENOMEM);
5752
Josef Bacik5c8fd992020-02-14 16:11:43 -05005753 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005755 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
5757 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005758 /*
5759 * We only need lookup, the rest is read-only and there's no inode
5760 * associated with the dentry
5761 */
5762 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005763 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 inode->i_fop = &simple_dir_operations;
5765 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005766 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305767 inode->i_atime = inode->i_mtime;
5768 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005769 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005770
5771 return inode;
5772}
5773
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005774static inline u8 btrfs_inode_type(struct inode *inode)
5775{
5776 /*
5777 * Compile-time asserts that generic FT_* types still match
5778 * BTRFS_FT_* types
5779 */
5780 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5781 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5782 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5783 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5784 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5785 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5786 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5787 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5788
5789 return fs_umode_to_ftype(inode->i_mode);
5790}
5791
Chris Mason3de45862008-11-17 21:02:50 -05005792struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005793{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005794 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005795 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005797 struct btrfs_root *sub_root = root;
5798 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005799 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005800 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005801
5802 if (dentry->d_name.len > BTRFS_NAME_LEN)
5803 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005804
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005805 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 if (ret < 0)
5807 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005808
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005809 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005810 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005811 if (IS_ERR(inode))
5812 return inode;
5813
5814 /* Do extra check against inode mode with di_type */
5815 if (btrfs_inode_type(inode) != di_type) {
5816 btrfs_crit(fs_info,
5817"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5818 inode->i_mode, btrfs_inode_type(inode),
5819 di_type);
5820 iput(inode);
5821 return ERR_PTR(-EUCLEAN);
5822 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005823 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005826 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005827 &location, &sub_root);
5828 if (ret < 0) {
5829 if (ret != -ENOENT)
5830 inode = ERR_PTR(ret);
5831 else
5832 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5833 } else {
David Sterba0202e832020-05-15 19:35:59 +02005834 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005835 }
Josef Bacik87270022020-01-24 09:32:31 -05005836 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005837 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005838
Julia Lawall34d19ba2011-01-24 19:55:19 +00005839 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005840 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005841 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005842 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005843 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005844 if (ret) {
5845 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005846 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005847 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005848 }
5849
Chris Mason3de45862008-11-17 21:02:50 -05005850 return inode;
5851}
5852
Nick Pigginfe15ce42011-01-07 17:49:23 +11005853static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005854{
5855 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005856 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005857
Li Zefan848cce02012-02-21 17:04:28 +08005858 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005859 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005860
Li Zefan848cce02012-02-21 17:04:28 +08005861 if (inode) {
5862 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005863 if (btrfs_root_refs(&root->root_item) == 0)
5864 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005865
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005866 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005867 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005868 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005869 return 0;
5870}
5871
Chris Mason3de45862008-11-17 21:02:50 -05005872static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005873 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005874{
Al Viro3837d202018-10-10 16:38:27 -04005875 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005876
Al Viro3837d202018-10-10 16:38:27 -04005877 if (inode == ERR_PTR(-ENOENT))
5878 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005879 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005880}
5881
Josef Bacik23b5ec72017-07-24 15:14:25 -04005882/*
5883 * All this infrastructure exists because dir_emit can fault, and we are holding
5884 * the tree lock when doing readdir. For now just allocate a buffer and copy
5885 * our information into that, and then dir_emit from the buffer. This is
5886 * similar to what NFS does, only we don't keep the buffer around in pagecache
5887 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5888 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5889 * tree lock.
5890 */
5891static int btrfs_opendir(struct inode *inode, struct file *file)
5892{
5893 struct btrfs_file_private *private;
5894
5895 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5896 if (!private)
5897 return -ENOMEM;
5898 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5899 if (!private->filldir_buf) {
5900 kfree(private);
5901 return -ENOMEM;
5902 }
5903 file->private_data = private;
5904 return 0;
5905}
5906
5907struct dir_entry {
5908 u64 ino;
5909 u64 offset;
5910 unsigned type;
5911 int name_len;
5912};
5913
5914static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5915{
5916 while (entries--) {
5917 struct dir_entry *entry = addr;
5918 char *name = (char *)(entry + 1);
5919
David Sterba92d32172018-04-16 21:10:14 +02005920 ctx->pos = get_unaligned(&entry->offset);
5921 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5922 get_unaligned(&entry->ino),
5923 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005924 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005925 addr += sizeof(struct dir_entry) +
5926 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005927 ctx->pos++;
5928 }
5929 return 0;
5930}
5931
Al Viro9cdda8d2013-05-22 16:48:09 -04005932static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005933{
Al Viro9cdda8d2013-05-22 16:48:09 -04005934 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 struct btrfs_dir_item *di;
5938 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005939 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005940 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005941 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005942 struct list_head ins_list;
5943 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005944 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005945 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005947 char *name_ptr;
5948 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005949 int entries = 0;
5950 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005951 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005952 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Al Viro9cdda8d2013-05-22 16:48:09 -04005954 if (!dir_emit_dots(file, ctx))
5955 return 0;
5956
David Woodhouse49593bf2008-08-17 17:08:36 +01005957 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005958 if (!path)
5959 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005960
Josef Bacik23b5ec72017-07-24 15:14:25 -04005961 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005962 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005963
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005964 INIT_LIST_HEAD(&ins_list);
5965 INIT_LIST_HEAD(&del_list);
5966 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005967
Josef Bacik23b5ec72017-07-24 15:14:25 -04005968again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005970 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005971 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005972
Chris Mason39279cc2007-06-12 06:35:45 -04005973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5974 if (ret < 0)
5975 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005976
5977 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005978 struct dir_entry *entry;
5979
Chris Mason5f39d392007-10-15 16:14:19 -04005980 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005981 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005982 if (slot >= btrfs_header_nritems(leaf)) {
5983 ret = btrfs_next_leaf(root, path);
5984 if (ret < 0)
5985 goto err;
5986 else if (ret > 0)
5987 break;
5988 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005989 }
Chris Mason3de45862008-11-17 21:02:50 -05005990
Chris Mason5f39d392007-10-15 16:14:19 -04005991 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5992
5993 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005994 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005995 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005996 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005997 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005998 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005999 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006000 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006001 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006003 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6004 PAGE_SIZE) {
6005 btrfs_release_path(path);
6006 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6007 if (ret)
6008 goto nopos;
6009 addr = private->filldir_buf;
6010 entries = 0;
6011 total_len = 0;
6012 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014
6015 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006016 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006017 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006018 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6019 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006020 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006021 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006022 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006023 put_unaligned(location.objectid, &entry->ino);
6024 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006025 entries++;
6026 addr += sizeof(struct dir_entry) + name_len;
6027 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006028next:
6029 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006031 btrfs_release_path(path);
6032
6033 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6034 if (ret)
6035 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006036
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006037 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006038 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006039 goto nopos;
6040
Zach Browndb62efb2013-07-11 16:19:42 -07006041 /*
6042 * Stop new entries from being returned after we return the last
6043 * entry.
6044 *
6045 * New directory entries are assigned a strictly increasing
6046 * offset. This means that new entries created during readdir
6047 * are *guaranteed* to be seen in the future by that readdir.
6048 * This has broken buggy programs which operate on names as
6049 * they're returned by readdir. Until we re-use freed offsets
6050 * we have this hack to stop new entries from being returned
6051 * under the assumption that they'll never reach this huge
6052 * offset.
6053 *
6054 * This is being careful not to overflow 32bit loff_t unless the
6055 * last entry requires it because doing so has broken 32bit apps
6056 * in the past.
6057 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006058 if (ctx->pos >= INT_MAX)
6059 ctx->pos = LLONG_MAX;
6060 else
6061 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006062nopos:
6063 ret = 0;
6064err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006065 if (put)
6066 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006067 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 return ret;
6069}
6070
Chris Mason39279cc2007-06-12 06:35:45 -04006071/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006072 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006073 * inode changes. But, it is most likely to find the inode in cache.
6074 * FIXME, needs more benchmarking...there are no reasons other than performance
6075 * to keep or drop this code.
6076 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006077static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006078{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006080 struct btrfs_root *root = BTRFS_I(inode)->root;
6081 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006082 int ret;
6083
Josef Bacik72ac3c02012-05-23 14:13:11 -04006084 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006085 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006086
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006087 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006088 if (IS_ERR(trans))
6089 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006090
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006091 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006092 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006093 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006094 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006095 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006096 if (IS_ERR(trans))
6097 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006098
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006099 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006100 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006101 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006102 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006103 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006104
6105 return ret;
6106}
6107
6108/*
6109 * This is a copy of file_update_time. We need this so we can return error on
6110 * ENOSPC for updating the inode in the case of file write and mmap writes.
6111 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006112static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006113 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006114{
Alexander Block2bc5565282012-06-15 09:49:33 +02006115 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006116 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006117
6118 if (btrfs_root_readonly(root))
6119 return -EROFS;
6120
Josef Bacike41f9412012-03-26 09:46:47 -04006121 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006122 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006123 if (flags & S_CTIME)
6124 inode->i_ctime = *now;
6125 if (flags & S_MTIME)
6126 inode->i_mtime = *now;
6127 if (flags & S_ATIME)
6128 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006129 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006130}
6131
Chris Masond352ac62008-09-29 15:18:18 -04006132/*
6133 * find the highest existing sequence number in a directory
6134 * and then set the in-memory index_cnt variable to reflect
6135 * free sequence numbers
6136 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006137static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006138{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006139 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006140 struct btrfs_key key, found_key;
6141 struct btrfs_path *path;
6142 struct extent_buffer *leaf;
6143 int ret;
6144
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006146 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006147 key.offset = (u64)-1;
6148
6149 path = btrfs_alloc_path();
6150 if (!path)
6151 return -ENOMEM;
6152
6153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6154 if (ret < 0)
6155 goto out;
6156 /* FIXME: we should be able to handle this */
6157 if (ret == 0)
6158 goto out;
6159 ret = 0;
6160
6161 /*
6162 * MAGIC NUMBER EXPLANATION:
6163 * since we search a directory based on f_pos we have to start at 2
6164 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6165 * else has to start at 2
6166 */
6167 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006168 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006169 goto out;
6170 }
6171
6172 path->slots[0]--;
6173
6174 leaf = path->nodes[0];
6175 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6176
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006177 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006178 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006179 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006180 goto out;
6181 }
6182
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006183 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006184out:
6185 btrfs_free_path(path);
6186 return ret;
6187}
6188
Chris Masond352ac62008-09-29 15:18:18 -04006189/*
6190 * helper to find a free sequence number in a given directory. This current
6191 * code is very simple, later versions will do smarter things in the btree
6192 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006193int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006194{
6195 int ret = 0;
6196
Nikolay Borisov877574e2017-02-20 13:50:33 +02006197 if (dir->index_cnt == (u64)-1) {
6198 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006199 if (ret) {
6200 ret = btrfs_set_inode_index_count(dir);
6201 if (ret)
6202 return ret;
6203 }
Josef Bacikaec74772008-07-24 12:12:38 -04006204 }
6205
Nikolay Borisov877574e2017-02-20 13:50:33 +02006206 *index = dir->index_cnt;
6207 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006208
6209 return ret;
6210}
6211
Chris Masonb0d5d102014-09-08 13:08:51 -07006212static int btrfs_insert_inode_locked(struct inode *inode)
6213{
6214 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006215
6216 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006217 args.root = BTRFS_I(inode)->root;
6218
6219 return insert_inode_locked4(inode,
6220 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6221 btrfs_find_actor, &args);
6222}
6223
Anand Jain19aee8d2017-07-18 17:37:05 +08006224/*
6225 * Inherit flags from the parent inode.
6226 *
6227 * Currently only the compression flags and the cow flags are inherited.
6228 */
6229static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6230{
6231 unsigned int flags;
6232
6233 if (!dir)
6234 return;
6235
6236 flags = BTRFS_I(dir)->flags;
6237
6238 if (flags & BTRFS_INODE_NOCOMPRESS) {
6239 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6240 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6241 } else if (flags & BTRFS_INODE_COMPRESS) {
6242 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6243 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6244 }
6245
6246 if (flags & BTRFS_INODE_NODATACOW) {
6247 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6248 if (S_ISREG(inode->i_mode))
6249 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6250 }
6251
David Sterba7b6a2212018-03-26 18:40:21 +02006252 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006253}
6254
Chris Mason39279cc2007-06-12 06:35:45 -04006255static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6256 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006257 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006258 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006259 u64 ref_objectid, u64 objectid,
6260 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006262 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006263 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006264 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006265 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006266 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006267 struct btrfs_inode_ref *ref;
6268 struct btrfs_key key[2];
6269 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006270 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006271 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006272 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006273 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006274
Chris Mason5f39d392007-10-15 16:14:19 -04006275 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006276 if (!path)
6277 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006278
Josef Bacik11a19a92019-09-09 10:12:04 -04006279 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006280 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006281 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006282 if (!inode) {
6283 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006284 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006285 }
Chris Mason39279cc2007-06-12 06:35:45 -04006286
Li Zefan581bb052011-04-20 10:06:11 +08006287 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006288 * O_TMPFILE, set link count to 0, so that after this point,
6289 * we fill in an inode item with the correct link count.
6290 */
6291 if (!name)
6292 set_nlink(inode, 0);
6293
6294 /*
Li Zefan581bb052011-04-20 10:06:11 +08006295 * we have to initialize this early, so we can reclaim the inode
6296 * number if we fail afterwards in this function.
6297 */
6298 inode->i_ino = objectid;
6299
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006300 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006301 trace_btrfs_inode_request(dir);
6302
Nikolay Borisov877574e2017-02-20 13:50:33 +02006303 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006304 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006305 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006306 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006307 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006308 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006309 } else if (dir) {
6310 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006311 }
6312 /*
6313 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006314 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006315 * number
6316 */
6317 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006318 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006319 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006320 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006321 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006322
Josef Bacik5dc562c2012-08-17 13:14:17 -04006323 /*
6324 * We could have gotten an inode number from somebody who was fsynced
6325 * and then removed in this same transaction, so let's just set full
6326 * sync since it will be a full sync anyway and this will blow away the
6327 * old info in the log.
6328 */
6329 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6330
Chris Mason9c583092008-01-29 15:15:18 -05006331 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006332 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006333 key[0].offset = 0;
6334
Chris Mason9c583092008-01-29 15:15:18 -05006335 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006336
6337 if (name) {
6338 /*
6339 * Start new inodes with an inode_ref. This is slightly more
6340 * efficient for small numbers of hard links since they will
6341 * be packed into one item. Extended refs will kick in if we
6342 * add more hard links than can fit in the ref item.
6343 */
6344 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006345 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006346 key[1].offset = ref_objectid;
6347
6348 sizes[1] = name_len + sizeof(*ref);
6349 }
Chris Mason9c583092008-01-29 15:15:18 -05006350
Chris Masonb0d5d102014-09-08 13:08:51 -07006351 location = &BTRFS_I(inode)->location;
6352 location->objectid = objectid;
6353 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006354 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006355
6356 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006357 if (ret < 0) {
6358 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006359 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006360 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006361
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006362 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006363 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006364 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006365
Christian Brauner21cb47b2021-01-21 14:19:25 +01006366 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006367 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306368
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006369 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306370 inode->i_atime = inode->i_mtime;
6371 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006372 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306373
Chris Mason5f39d392007-10-15 16:14:19 -04006374 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6375 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006376 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006377 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006378 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006379
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006380 if (name) {
6381 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6382 struct btrfs_inode_ref);
6383 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6384 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6385 ptr = (unsigned long)(ref + 1);
6386 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6387 }
Chris Mason9c583092008-01-29 15:15:18 -05006388
Chris Mason5f39d392007-10-15 16:14:19 -04006389 btrfs_mark_buffer_dirty(path->nodes[0]);
6390 btrfs_free_path(path);
6391
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006392 btrfs_inherit_iflags(inode, dir);
6393
Al Viro569254b2011-07-24 17:08:40 -04006394 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006396 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006397 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006398 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6399 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006400 }
6401
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006402 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006403
6404 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006405 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006406
Alexander Block8ea05e32012-07-25 17:35:53 +02006407 btrfs_update_root_times(trans, root);
6408
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006409 ret = btrfs_inode_inherit_props(trans, inode, dir);
6410 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006412 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006413 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006414
Chris Mason39279cc2007-06-12 06:35:45 -04006415 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006416
6417fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006418 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006419fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006420 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006421 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006422 btrfs_free_path(path);
6423 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006424}
6425
Chris Masond352ac62008-09-29 15:18:18 -04006426/*
6427 * utility function to add 'inode' into 'parent_inode' with
6428 * a give name and a given sequence number.
6429 * if 'add_backref' is true, also insert a backref from the
6430 * inode to the parent directory.
6431 */
Chris Masone02119d2008-09-05 16:13:11 -04006432int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006433 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006434 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006435{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006436 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006437 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006438 struct btrfs_root *root = parent_inode->root;
6439 u64 ino = btrfs_ino(inode);
6440 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006441
Li Zefan33345d012011-04-20 10:31:50 +08006442 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006444 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006445 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006446 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006447 key.offset = 0;
6448 }
Chris Mason39279cc2007-06-12 06:35:45 -04006449
Li Zefan33345d012011-04-20 10:31:50 +08006450 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006451 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006452 root->root_key.objectid, parent_ino,
6453 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006455 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6456 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006457 }
6458
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 /* Nothing to clean up yet */
6460 if (ret)
6461 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462
Lu Fengqi684572d2018-08-04 21:10:57 +08006463 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006464 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006465 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006466 goto fail_dir_item;
6467 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006468 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006469 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006470 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006471
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006472 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006473 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006474 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006475 /*
6476 * If we are replaying a log tree, we do not want to update the mtime
6477 * and ctime of the parent directory with the current time, since the
6478 * log replay procedure is responsible for setting them to their correct
6479 * values (the ones it had when the fsync was done).
6480 */
6481 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6482 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6483
6484 parent_inode->vfs_inode.i_mtime = now;
6485 parent_inode->vfs_inode.i_ctime = now;
6486 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006487 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006488 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006489 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006490 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006491
6492fail_dir_item:
6493 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6494 u64 local_index;
6495 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006496 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006497 root->root_key.objectid, parent_ino,
6498 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006499 if (err)
6500 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006501 } else if (add_backref) {
6502 u64 local_index;
6503 int err;
6504
6505 err = btrfs_del_inode_ref(trans, root, name, name_len,
6506 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006507 if (err)
6508 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006509 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006510
6511 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006512 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006513}
6514
6515static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006516 struct btrfs_inode *dir, struct dentry *dentry,
6517 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006518{
Josef Bacika1b075d2010-11-19 20:36:11 +00006519 int err = btrfs_add_link(trans, dir, inode,
6520 dentry->d_name.name, dentry->d_name.len,
6521 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006522 if (err > 0)
6523 err = -EEXIST;
6524 return err;
6525}
6526
Christian Brauner549c7292021-01-21 14:19:43 +01006527static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6528 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006529{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006530 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006531 struct btrfs_trans_handle *trans;
6532 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006533 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006534 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006535 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006536 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006537
Josef Bacik9ed74f22009-09-11 16:12:44 -04006538 /*
6539 * 2 for inode item and ref
6540 * 2 for dir items
6541 * 1 for xattr if selinux is on
6542 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006543 trans = btrfs_start_transaction(root, 5);
6544 if (IS_ERR(trans))
6545 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006546
Nikolay Borisov543068a2020-12-07 17:32:33 +02006547 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006548 if (err)
6549 goto out_unlock;
6550
Josef Bacikaec74772008-07-24 12:12:38 -04006551 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006552 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6553 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006554 if (IS_ERR(inode)) {
6555 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006556 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006557 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006558 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006559
Casey Schauflerad19db72011-12-15 10:09:07 -05006560 /*
6561 * If the active LSM wants to access the inode during
6562 * d_instantiate it needs these. Smack checks to see
6563 * if the filesystem supports xattrs by looking at the
6564 * ops vector.
6565 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006566 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006567 init_special_inode(inode, inode->i_mode, rdev);
6568
6569 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006570 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006571 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006572
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006573 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6574 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006575 if (err)
6576 goto out_unlock;
6577
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006578 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006579 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006580
Josef Bacik618e21d2007-07-11 10:18:17 -04006581out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006582 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006583 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006584 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006585 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006586 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006587 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006588 return err;
6589}
6590
Christian Brauner549c7292021-01-21 14:19:43 +01006591static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6592 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006593{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006594 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006595 struct btrfs_trans_handle *trans;
6596 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006597 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006598 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006599 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006600 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006601
Josef Bacik9ed74f22009-09-11 16:12:44 -04006602 /*
6603 * 2 for inode item and ref
6604 * 2 for dir items
6605 * 1 for xattr if selinux is on
6606 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006607 trans = btrfs_start_transaction(root, 5);
6608 if (IS_ERR(trans))
6609 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006610
Nikolay Borisov543068a2020-12-07 17:32:33 +02006611 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006612 if (err)
6613 goto out_unlock;
6614
Josef Bacikaec74772008-07-24 12:12:38 -04006615 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006616 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6617 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006618 if (IS_ERR(inode)) {
6619 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006620 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006621 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006622 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006623 /*
6624 * If the active LSM wants to access the inode during
6625 * d_instantiate it needs these. Smack checks to see
6626 * if the filesystem supports xattrs by looking at the
6627 * ops vector.
6628 */
6629 inode->i_fop = &btrfs_file_operations;
6630 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006632
6633 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6634 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006635 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006636
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006637 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006638 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006639 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006640
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006641 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6642 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006644 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006645
Al Viro1e2e5472018-05-04 08:23:01 -04006646 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006647
Chris Mason39279cc2007-06-12 06:35:45 -04006648out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006649 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006650 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006651 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006652 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006653 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006654 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006655 return err;
6656}
6657
6658static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6659 struct dentry *dentry)
6660{
Filipe Manana271dba452016-01-05 16:24:05 +00006661 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006662 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006663 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006665 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006666 int err;
6667 int drop_inode = 0;
6668
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006669 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006670 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006671 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006672
Mark Fashehf1863732012-08-08 11:32:27 -07006673 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006674 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006675
Nikolay Borisov877574e2017-02-20 13:50:33 +02006676 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006677 if (err)
6678 goto fail;
6679
Yan, Zhenga22285a2010-05-16 10:48:46 -04006680 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006681 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006683 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006684 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006685 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006686 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006687 if (IS_ERR(trans)) {
6688 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006689 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006690 goto fail;
6691 }
Chris Mason5f39d392007-10-15 16:14:19 -04006692
Miao Xie67de1172013-12-26 13:07:06 +08006693 /* There are several dir indexes for this inode, clear the cache. */
6694 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006695 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006696 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006697 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006698 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006699 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006700
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006701 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6702 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006703
Yan, Zhenga5719522009-09-24 09:17:31 -04006704 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006705 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006706 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006707 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006708
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006709 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006710 if (err)
6711 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006712 if (inode->i_nlink == 1) {
6713 /*
6714 * If new hard link count is 1, it's a file created
6715 * with open(2) O_TMPFILE flag.
6716 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006717 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006718 if (err)
6719 goto fail;
6720 }
Al Viro08c422c2011-12-23 07:58:13 -05006721 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006722 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006723 }
Chris Mason39279cc2007-06-12 06:35:45 -04006724
Chris Mason1832a6d2007-12-21 16:27:21 -05006725fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006726 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006727 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006728 if (drop_inode) {
6729 inode_dec_link_count(inode);
6730 iput(inode);
6731 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006732 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006733 return err;
6734}
6735
Christian Brauner549c7292021-01-21 14:19:43 +01006736static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6737 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006738{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006739 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006740 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006741 struct btrfs_trans_handle *trans;
6742 struct btrfs_root *root = BTRFS_I(dir)->root;
6743 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006744 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006745 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006746
Josef Bacik9ed74f22009-09-11 16:12:44 -04006747 /*
6748 * 2 items for inode and ref
6749 * 2 items for dir items
6750 * 1 for xattr if selinux is on
6751 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006752 trans = btrfs_start_transaction(root, 5);
6753 if (IS_ERR(trans))
6754 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006755
Nikolay Borisov543068a2020-12-07 17:32:33 +02006756 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006757 if (err)
6758 goto out_fail;
6759
Josef Bacikaec74772008-07-24 12:12:38 -04006760 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006761 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6762 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006763 if (IS_ERR(inode)) {
6764 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006765 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006766 goto out_fail;
6767 }
Chris Mason5f39d392007-10-15 16:14:19 -04006768
Chris Masonb0d5d102014-09-08 13:08:51 -07006769 /* these must be set before we unlock the inode */
6770 inode->i_op = &btrfs_dir_inode_operations;
6771 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006772
Eric Paris2a7dba32011-02-01 11:05:39 -05006773 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006774 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006775 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006776
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006777 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006778 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006779 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006780 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006781
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006782 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6783 dentry->d_name.name,
6784 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006785 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006786 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006787
Al Viro1e2e5472018-05-04 08:23:01 -04006788 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006789
6790out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006791 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006792 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006793 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006794 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006795 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006796 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006797 return err;
6798}
6799
Chris Masonc8b97812008-10-29 14:49:59 -04006800static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006801 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006802 size_t pg_offset, u64 extent_offset,
6803 struct btrfs_file_extent_item *item)
6804{
6805 int ret;
6806 struct extent_buffer *leaf = path->nodes[0];
6807 char *tmp;
6808 size_t max_size;
6809 unsigned long inline_size;
6810 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006811 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006812
6813 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006814 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006815 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6816 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006817 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006818 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006819 if (!tmp)
6820 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006821 ptr = btrfs_file_extent_inline_start(item);
6822
6823 read_extent_buffer(leaf, tmp, ptr, inline_size);
6824
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006825 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006826 ret = btrfs_decompress(compress_type, tmp, page,
6827 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006828
6829 /*
6830 * decompression code contains a memset to fill in any space between the end
6831 * of the uncompressed data and the end of max_size in case the decompressed
6832 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6833 * the end of an inline extent and the beginning of the next block, so we
6834 * cover that region here.
6835 */
6836
Ira Weinyd048b9c2021-05-04 18:40:07 -07006837 if (max_size + pg_offset < PAGE_SIZE)
6838 memzero_page(page, pg_offset + max_size,
6839 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006840 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006841 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006842}
6843
Omar Sandoval39b07b52019-12-02 17:34:23 -08006844/**
6845 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6846 * @inode: file to search in
6847 * @page: page to read extent data into if the extent is inline
6848 * @pg_offset: offset into @page to copy to
6849 * @start: file offset
6850 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006851 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006852 * This returns the first &struct extent_map which overlaps with the given
6853 * range, reading it from the B-tree and caching it if necessary. Note that
6854 * there may be more extents which overlap the given range after the returned
6855 * extent_map.
6856 *
6857 * If @page is not NULL and the extent is inline, this also reads the extent
6858 * data directly into the page and marks the extent up to date in the io_tree.
6859 *
6860 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006861 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006862struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006863 struct page *page, size_t pg_offset,
6864 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006865{
David Sterba3ffbd682018-06-29 10:56:42 +02006866 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006867 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006868 u64 extent_start = 0;
6869 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006870 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006871 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006872 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006873 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006874 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006875 struct extent_buffer *leaf;
6876 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006877 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006878 struct extent_map_tree *em_tree = &inode->extent_tree;
6879 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006880
Chris Mason890871b2009-09-02 16:24:52 -04006881 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006882 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006883 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006884
Chris Masona52d9a82007-08-27 16:49:44 -04006885 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006886 if (em->start > start || em->start + em->len <= start)
6887 free_extent_map(em);
6888 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006889 free_extent_map(em);
6890 else
6891 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006892 }
David Sterba172ddd62011-04-21 00:48:27 +02006893 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006894 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006895 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006896 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006897 }
Chris Masond1310b22008-01-24 16:13:08 -05006898 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006899 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006900 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006901 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006902
Liu Bobee6ec82018-08-17 05:05:28 +08006903 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006904 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006905 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006906 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006907 }
6908
Liu Bobee6ec82018-08-17 05:05:28 +08006909 /* Chances are we'll be called again, so go ahead and do readahead */
6910 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006911
6912 /*
6913 * The same explanation in load_free_space_cache applies here as well,
6914 * we only read when we're loading the free space cache, and at that
6915 * point the commit_root has everything we need.
6916 */
6917 if (btrfs_is_free_space_inode(inode)) {
6918 path->search_commit_root = 1;
6919 path->skip_locking = 1;
6920 }
Josef Bacik51899412020-08-20 11:46:01 -04006921
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006922 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006923 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006924 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006925 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006926 if (path->slots[0] == 0)
6927 goto not_found;
6928 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006929 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 }
6931
Chris Mason5f39d392007-10-15 16:14:19 -04006932 leaf = path->nodes[0];
6933 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006934 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006935 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006936 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006937 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006938 /*
6939 * If we backup past the first extent we want to move forward
6940 * and see if there is an extent in front of us, otherwise we'll
6941 * say there is a hole for our whole search range which can
6942 * cause problems.
6943 */
6944 extent_end = start;
6945 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006946 }
6947
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006948 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006949 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006950 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006951 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6952 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006953 /* Only regular file could have regular/prealloc extent */
6954 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006955 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006956 btrfs_crit(fs_info,
6957 "regular/prealloc extent found for non-regular inode %llu",
6958 btrfs_ino(inode));
6959 goto out;
6960 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006961 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6962 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006963 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006964 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6965 path->slots[0],
6966 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006967 }
Josef Bacik25a50342013-10-14 12:08:38 -04006968next:
Yan Zheng9036c102008-10-30 14:19:41 -04006969 if (start >= extent_end) {
6970 path->slots[0]++;
6971 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6972 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006973 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006974 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006975 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006976 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006977
Yan Zheng9036c102008-10-30 14:19:41 -04006978 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006979 }
Yan Zheng9036c102008-10-30 14:19:41 -04006980 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6981 if (found_key.objectid != objectid ||
6982 found_key.type != BTRFS_EXTENT_DATA_KEY)
6983 goto not_found;
6984 if (start + len <= found_key.offset)
6985 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006986 if (start > found_key.offset)
6987 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006988
6989 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006990 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006991 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006992 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006993 em->block_start = EXTENT_MAP_HOLE;
6994 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006995 }
6996
Omar Sandoval39b07b52019-12-02 17:34:23 -08006997 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006998
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006999 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7000 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007001 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007002 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007003 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007004 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007005 size_t size;
7006 size_t extent_offset;
7007 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007008
Omar Sandoval39b07b52019-12-02 17:34:23 -08007009 if (!page)
Yan689f9342007-10-29 11:41:07 -04007010 goto out;
Yan689f9342007-10-29 11:41:07 -04007011
Qu Wenruoe41ca582018-06-06 15:41:49 +08007012 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007013 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007014 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7015 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007016 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007017 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007018 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007019 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007020 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007021
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007022 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007023 if (btrfs_file_extent_compression(leaf, item) !=
7024 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007025 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007026 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007027 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007028 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007029 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007030 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007031 read_extent_buffer(leaf, map + pg_offset, ptr,
7032 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007033 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007034 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007035 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007036 copy_size);
7037 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007038 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007039 }
Chris Mason179e29e2007-11-01 11:28:41 -04007040 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007041 }
Chris Masond1310b22008-01-24 16:13:08 -05007042 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007043 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007044 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007045 }
7046not_found:
7047 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007048 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007049 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007050 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007051insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007052 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007053 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007054 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007055 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007056 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7057 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007058 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007059 goto out;
7060 }
Chris Masond1310b22008-01-24 16:13:08 -05007061
Chris Mason890871b2009-09-02 16:24:52 -04007062 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007063 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007064 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007065out:
Liu Boc6414282018-08-23 07:36:17 +08007066 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007067
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007068 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007069
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007070 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007071 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007072 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007073 }
7074 return em;
7075}
7076
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007077struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007078 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007079{
7080 struct extent_map *em;
7081 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007082 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007083 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007084 u64 delalloc_len;
7085 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007086 int err = 0;
7087
Omar Sandoval39b07b52019-12-02 17:34:23 -08007088 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007089 if (IS_ERR(em))
7090 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007091 /*
7092 * If our em maps to:
7093 * - a hole or
7094 * - a pre-alloc extent,
7095 * there might actually be delalloc bytes behind it.
7096 */
7097 if (em->block_start != EXTENT_MAP_HOLE &&
7098 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7099 return em;
7100 else
7101 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007102
7103 /* check to see if we've wrapped (len == -1 or similar) */
7104 end = start + len;
7105 if (end < start)
7106 end = (u64)-1;
7107 else
7108 end -= 1;
7109
7110 em = NULL;
7111
7112 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007113 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007114 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007115 delalloc_end = delalloc_start + delalloc_len;
7116 if (delalloc_end < delalloc_start)
7117 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007118
7119 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007120 * We didn't find anything useful, return the original results from
7121 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007122 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007123 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007124 em = hole_em;
7125 hole_em = NULL;
7126 goto out;
7127 }
7128
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007129 /*
7130 * Adjust the delalloc_start to make sure it doesn't go backwards from
7131 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007132 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007133 delalloc_start = max(start, delalloc_start);
7134 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007135
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007136 if (delalloc_len > 0) {
7137 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007138 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007139 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007140
David Sterba172ddd62011-04-21 00:48:27 +02007141 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007142 if (!em) {
7143 err = -ENOMEM;
7144 goto out;
7145 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007146
7147 ASSERT(hole_em);
7148 /*
7149 * When btrfs_get_extent can't find anything it returns one
7150 * huge hole
7151 *
7152 * Make sure what it found really fits our range, and adjust to
7153 * make sure it is based on the start from the caller
7154 */
7155 if (hole_end <= start || hole_em->start > end) {
7156 free_extent_map(hole_em);
7157 hole_em = NULL;
7158 } else {
7159 hole_start = max(hole_em->start, start);
7160 hole_len = hole_end - hole_start;
7161 }
7162
7163 if (hole_em && delalloc_start > hole_start) {
7164 /*
7165 * Our hole starts before our delalloc, so we have to
7166 * return just the parts of the hole that go until the
7167 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007168 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007169 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007170 em->start = hole_start;
7171 em->orig_start = hole_start;
7172 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007173 * Don't adjust block start at all, it is fixed at
7174 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007175 */
7176 em->block_start = hole_em->block_start;
7177 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007178 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7179 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007180 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007181 /*
7182 * Hole is out of passed range or it starts after
7183 * delalloc range
7184 */
7185 em->start = delalloc_start;
7186 em->len = delalloc_len;
7187 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007188 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007189 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007190 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007191 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007192 return hole_em;
7193 }
7194out:
7195
7196 free_extent_map(hole_em);
7197 if (err) {
7198 free_extent_map(em);
7199 return ERR_PTR(err);
7200 }
7201 return em;
7202}
7203
Nikolay Borisov64f54182020-06-03 08:55:31 +03007204static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007205 const u64 start,
7206 const u64 len,
7207 const u64 orig_start,
7208 const u64 block_start,
7209 const u64 block_len,
7210 const u64 orig_block_len,
7211 const u64 ram_bytes,
7212 const int type)
7213{
7214 struct extent_map *em = NULL;
7215 int ret;
7216
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007217 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007218 em = create_io_em(inode, start, len, orig_start, block_start,
7219 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007220 BTRFS_COMPRESS_NONE, /* compress_type */
7221 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007222 if (IS_ERR(em))
7223 goto out;
7224 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007225 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7226 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007227 if (ret) {
7228 if (em) {
7229 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007230 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007231 }
7232 em = ERR_PTR(ret);
7233 }
7234 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007235
7236 return em;
7237}
7238
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007239static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240 u64 start, u64 len)
7241{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007242 struct btrfs_root *root = inode->root;
7243 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007244 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245 struct btrfs_key ins;
7246 u64 alloc_hint;
7247 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007249 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007250 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007251 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007252 if (ret)
7253 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007255 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007256 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007257 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007258 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007259 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007260 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7261 1);
Josef Bacik00361582013-08-14 14:02:47 -04007262
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 return em;
7264}
7265
Anand Jainf4639632021-02-10 21:25:16 -08007266static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007267{
7268 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007269 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007270
7271 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7272 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007273 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007274 if (block_group)
7275 btrfs_put_block_group(block_group);
7276 return readonly;
7277}
7278
Chris Mason46bfbb52010-05-26 11:04:10 -04007279/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007280 * Check if we can do nocow write into the range [@offset, @offset + @len)
7281 *
7282 * @offset: File offset
7283 * @len: The length to write, will be updated to the nocow writeable
7284 * range
7285 * @orig_start: (optional) Return the original file offset of the file extent
7286 * @orig_len: (optional) Return the original on-disk length of the file extent
7287 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007288 * @strict: if true, omit optimizations that might force us into unnecessary
7289 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007290 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007291 * Return:
7292 * >0 and update @len if we can do nocow write
7293 * 0 if we can't do nocow write
7294 * <0 if error happened
7295 *
7296 * NOTE: This only checks the file extents, caller is responsible to wait for
7297 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007298 */
Josef Bacik00361582013-08-14 14:02:47 -04007299noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007300 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007301 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007302{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007304 struct btrfs_path *path;
7305 int ret;
7306 struct extent_buffer *leaf;
7307 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007308 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007309 struct btrfs_file_extent_item *fi;
7310 struct btrfs_key key;
7311 u64 disk_bytenr;
7312 u64 backref_offset;
7313 u64 extent_end;
7314 u64 num_bytes;
7315 int slot;
7316 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007317 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007318
Chris Mason46bfbb52010-05-26 11:04:10 -04007319 path = btrfs_alloc_path();
7320 if (!path)
7321 return -ENOMEM;
7322
David Sterbaf85b7372017-01-20 14:54:07 +01007323 ret = btrfs_lookup_file_extent(NULL, root, path,
7324 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007325 if (ret < 0)
7326 goto out;
7327
7328 slot = path->slots[0];
7329 if (ret == 1) {
7330 if (slot == 0) {
7331 /* can't find the item, must cow */
7332 ret = 0;
7333 goto out;
7334 }
7335 slot--;
7336 }
7337 ret = 0;
7338 leaf = path->nodes[0];
7339 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007340 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 key.type != BTRFS_EXTENT_DATA_KEY) {
7342 /* not our file or wrong item type, must cow */
7343 goto out;
7344 }
7345
7346 if (key.offset > offset) {
7347 /* Wrong offset, must cow */
7348 goto out;
7349 }
7350
7351 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7352 found_type = btrfs_file_extent_type(leaf, fi);
7353 if (found_type != BTRFS_FILE_EXTENT_REG &&
7354 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7355 /* not a regular extent, must cow */
7356 goto out;
7357 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007358
7359 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7360 goto out;
7361
Miao Xiee77751a2013-12-27 21:11:50 +08007362 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7363 if (extent_end <= offset)
7364 goto out;
7365
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367 if (disk_bytenr == 0)
7368 goto out;
7369
7370 if (btrfs_file_extent_compression(leaf, fi) ||
7371 btrfs_file_extent_encryption(leaf, fi) ||
7372 btrfs_file_extent_other_encoding(leaf, fi))
7373 goto out;
7374
Ethan Lien78d42952018-05-17 14:58:29 +08007375 /*
7376 * Do the same check as in btrfs_cross_ref_exist but without the
7377 * unnecessary search.
7378 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007379 if (!strict &&
7380 (btrfs_file_extent_generation(leaf, fi) <=
7381 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007382 goto out;
7383
Chris Mason46bfbb52010-05-26 11:04:10 -04007384 backref_offset = btrfs_file_extent_offset(leaf, fi);
7385
Josef Bacik7ee9e442013-06-21 16:37:03 -04007386 if (orig_start) {
7387 *orig_start = key.offset - backref_offset;
7388 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7389 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7390 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007391
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007392 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007393 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007394
7395 num_bytes = min(offset + *len, extent_end) - offset;
7396 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7397 u64 range_end;
7398
Jeff Mahoneyda170662016-06-15 09:22:56 -04007399 range_end = round_up(offset + num_bytes,
7400 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007401 ret = test_range_bit(io_tree, offset, range_end,
7402 EXTENT_DELALLOC, 0, NULL);
7403 if (ret) {
7404 ret = -EAGAIN;
7405 goto out;
7406 }
7407 }
7408
Josef Bacik1bda19e2013-10-18 12:10:36 -04007409 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007410
7411 /*
7412 * look for other files referencing this extent, if we
7413 * find any we must cow
7414 */
Josef Bacik00361582013-08-14 14:02:47 -04007415
Liu Boe4c3b2d2017-01-30 12:25:28 -08007416 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007417 key.offset - backref_offset, disk_bytenr,
7418 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007419 if (ret) {
7420 ret = 0;
7421 goto out;
7422 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007423
7424 /*
7425 * adjust disk_bytenr and num_bytes to cover just the bytes
7426 * in this extent we are about to write. If there
7427 * are any csums in that range we have to cow in order
7428 * to keep the csums correct
7429 */
7430 disk_bytenr += backref_offset;
7431 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007432 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7433 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007434 /*
7435 * all of the above have passed, it is safe to overwrite this extent
7436 * without cow
7437 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007438 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007439 ret = 1;
7440out:
7441 btrfs_free_path(path);
7442 return ret;
7443}
7444
Josef Bacikeb838e72012-07-31 16:28:48 -04007445static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007446 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007447{
7448 struct btrfs_ordered_extent *ordered;
7449 int ret = 0;
7450
7451 while (1) {
7452 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007453 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007454 /*
7455 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007456 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007457 * extents in this range.
7458 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007459 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007460 lockend - lockstart + 1);
7461
7462 /*
7463 * We need to make sure there are no buffered pages in this
7464 * range either, we could have raced between the invalidate in
7465 * generic_file_direct_write and locking the extent. The
7466 * invalidate needs to happen so that reads after a write do not
7467 * get stale data.
7468 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007469 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007470 (!writing || !filemap_range_has_page(inode->i_mapping,
7471 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007472 break;
7473
7474 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007475 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007476
7477 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007478 /*
7479 * If we are doing a DIO read and the ordered extent we
7480 * found is for a buffered write, we can not wait for it
7481 * to complete and retry, because if we do so we can
7482 * deadlock with concurrent buffered writes on page
7483 * locks. This happens only if our DIO read covers more
7484 * than one extent map, if at this point has already
7485 * created an ordered extent for a previous extent map
7486 * and locked its range in the inode's io tree, and a
7487 * concurrent write against that previous extent map's
7488 * range and this range started (we unlock the ranges
7489 * in the io tree only when the bios complete and
7490 * buffered writes always lock pages before attempting
7491 * to lock range in the io tree).
7492 */
7493 if (writing ||
7494 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007495 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007496 else
7497 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007498 btrfs_put_ordered_extent(ordered);
7499 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007500 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007501 * We could trigger writeback for this range (and wait
7502 * for it to complete) and then invalidate the pages for
7503 * this range (through invalidate_inode_pages2_range()),
7504 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007505 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007506 * triggered a readahead) on a page lock due to an
7507 * ordered dio extent we created before but did not have
7508 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007509 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007510 * ordered extent to complete while holding a lock on
7511 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007512 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007513 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007514 }
7515
Filipe Mananaade77022016-02-18 14:28:55 +00007516 if (ret)
7517 break;
7518
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 cond_resched();
7520 }
7521
7522 return ret;
7523}
7524
Liu Bo6f9994d2017-01-31 07:50:22 -08007525/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007526static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7527 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007528 u64 block_len, u64 orig_block_len,
7529 u64 ram_bytes, int compress_type,
7530 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007531{
7532 struct extent_map_tree *em_tree;
7533 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007534 int ret;
7535
Liu Bo6f9994d2017-01-31 07:50:22 -08007536 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7537 type == BTRFS_ORDERED_COMPRESSED ||
7538 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007539 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007540
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007541 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007542 em = alloc_extent_map();
7543 if (!em)
7544 return ERR_PTR(-ENOMEM);
7545
7546 em->start = start;
7547 em->orig_start = orig_start;
7548 em->len = len;
7549 em->block_len = block_len;
7550 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007551 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007552 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007553 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007554 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007555 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007556 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007557 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007558 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7559 em->compress_type = compress_type;
7560 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007561
7562 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007563 btrfs_drop_extent_cache(inode, em->start,
7564 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007565 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007566 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007567 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007568 /*
7569 * The caller has taken lock_extent(), who could race with us
7570 * to add em?
7571 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007572 } while (ret == -EEXIST);
7573
7574 if (ret) {
7575 free_extent_map(em);
7576 return ERR_PTR(ret);
7577 }
7578
Liu Bo6f9994d2017-01-31 07:50:22 -08007579 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007580 return em;
7581}
7582
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007583
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007584static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007585 struct inode *inode,
7586 struct btrfs_dio_data *dio_data,
7587 u64 start, u64 len)
7588{
7589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7590 struct extent_map *em = *map;
7591 int ret = 0;
7592
7593 /*
7594 * We don't allocate a new extent in the following cases
7595 *
7596 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7597 * existing extent.
7598 * 2) The extent is marked as PREALLOC. We're good to go here and can
7599 * just use the extent.
7600 *
7601 */
7602 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7603 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7604 em->block_start != EXTENT_MAP_HOLE)) {
7605 int type;
7606 u64 block_start, orig_start, orig_block_len, ram_bytes;
7607
7608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7609 type = BTRFS_ORDERED_PREALLOC;
7610 else
7611 type = BTRFS_ORDERED_NOCOW;
7612 len = min(len, em->len - (start - em->start));
7613 block_start = em->block_start + (start - em->start);
7614
7615 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007616 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007617 btrfs_inc_nocow_writers(fs_info, block_start)) {
7618 struct extent_map *em2;
7619
Nikolay Borisov64f54182020-06-03 08:55:31 +03007620 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007621 orig_start, block_start,
7622 len, orig_block_len,
7623 ram_bytes, type);
7624 btrfs_dec_nocow_writers(fs_info, block_start);
7625 if (type == BTRFS_ORDERED_PREALLOC) {
7626 free_extent_map(em);
7627 *map = em = em2;
7628 }
7629
7630 if (em2 && IS_ERR(em2)) {
7631 ret = PTR_ERR(em2);
7632 goto out;
7633 }
7634 /*
7635 * For inode marked NODATACOW or extent marked PREALLOC,
7636 * use the existing or preallocated extent, so does not
7637 * need to adjust btrfs_space_info's bytes_may_use.
7638 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007639 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007640 goto skip_cow;
7641 }
7642 }
7643
7644 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007645 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007646 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007647 if (IS_ERR(em)) {
7648 ret = PTR_ERR(em);
7649 goto out;
7650 }
7651
7652 len = min(len, em->len - (start - em->start));
7653
7654skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007655 /*
7656 * Need to update the i_size under the extent lock so buffered
7657 * readers will get the updated i_size when we unlock.
7658 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007659 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007660 i_size_write(inode, start + len);
7661
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007662 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007663out:
7664 return ret;
7665}
7666
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007667static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7668 loff_t length, unsigned int flags, struct iomap *iomap,
7669 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007670{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007671 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007673 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307674 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007676 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007677 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007678 u64 len = length;
7679 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007680
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007681 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007682 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007683
Josef Bacikc3298612012-08-03 16:49:19 -04007684 lockstart = start;
7685 lockend = start + len - 1;
7686
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007687 /*
7688 * The generic stuff only does filemap_write_and_wait_range, which
7689 * isn't enough if we've written compressed pages to this area, so we
7690 * need to flush the dirty pages again to make absolutely sure that any
7691 * outstanding dirty pages are on disk.
7692 */
7693 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7694 &BTRFS_I(inode)->runtime_flags)) {
7695 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7696 start + length - 1);
7697 if (ret)
7698 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007699 }
7700
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007701 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7702 if (!dio_data)
7703 return -ENOMEM;
7704
7705 dio_data->length = length;
7706 if (write) {
7707 dio_data->reserve = round_up(length, fs_info->sectorsize);
7708 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7709 &dio_data->data_reserved,
7710 start, dio_data->reserve);
7711 if (ret) {
7712 extent_changeset_free(dio_data->data_reserved);
7713 kfree(dio_data);
7714 return ret;
7715 }
7716 }
7717 iomap->private = dio_data;
7718
7719
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 /*
7721 * If this errors out it's because we couldn't invalidate pagecache for
7722 * this range and we need to fallback to buffered.
7723 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007724 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007725 ret = -ENOTBLK;
7726 goto err;
7727 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007728
Omar Sandoval39b07b52019-12-02 17:34:23 -08007729 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007730 if (IS_ERR(em)) {
7731 ret = PTR_ERR(em);
7732 goto unlock_err;
7733 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007734
7735 /*
7736 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7737 * io. INLINE is special, and we could probably kludge it in here, but
7738 * it's still buffered so for safety lets just fall back to the generic
7739 * buffered path.
7740 *
7741 * For COMPRESSED we _have_ to read the entire extent in so we can
7742 * decompress it, so there will be buffering required no matter what we
7743 * do, so go ahead and fallback to buffered.
7744 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007745 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746 * to buffered IO. Don't blame me, this is the price we pay for using
7747 * the generic code.
7748 */
7749 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7750 em->block_start == EXTENT_MAP_INLINE) {
7751 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007752 ret = -ENOTBLK;
7753 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754 }
7755
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007756 len = min(len, em->len - (start - em->start));
7757 if (write) {
7758 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7759 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007760 if (ret < 0)
7761 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007762 unlock_extents = true;
7763 /* Recalc len in case the new em is smaller than requested */
7764 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007765 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007766 /*
7767 * We need to unlock only the end area that we aren't using.
7768 * The rest is going to be unlocked by the endio routine.
7769 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007770 lockstart = start + len;
7771 if (lockstart < lockend)
7772 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 }
7774
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007775 if (unlock_extents)
7776 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7777 lockstart, lockend, &cached_state);
7778 else
7779 free_extent_state(cached_state);
7780
7781 /*
7782 * Translate extent map information to iomap.
7783 * We trim the extents (and move the addr) even though iomap code does
7784 * that, since we have locked only the parts we are performing I/O in.
7785 */
7786 if ((em->block_start == EXTENT_MAP_HOLE) ||
7787 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7788 iomap->addr = IOMAP_NULL_ADDR;
7789 iomap->type = IOMAP_HOLE;
7790 } else {
7791 iomap->addr = em->block_start + (start - em->start);
7792 iomap->type = IOMAP_MAPPED;
7793 }
7794 iomap->offset = start;
7795 iomap->bdev = fs_info->fs_devices->latest_bdev;
7796 iomap->length = len;
7797
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007798 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007799 iomap->flags |= IOMAP_F_ZONE_APPEND;
7800
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801 free_extent_map(em);
7802
7803 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007804
7805unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007806 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7807 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007808err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007809 if (dio_data) {
7810 btrfs_delalloc_release_space(BTRFS_I(inode),
7811 dio_data->data_reserved, start,
7812 dio_data->reserve, true);
7813 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7814 extent_changeset_free(dio_data->data_reserved);
7815 kfree(dio_data);
7816 }
7817 return ret;
7818}
7819
7820static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7821 ssize_t written, unsigned int flags, struct iomap *iomap)
7822{
7823 int ret = 0;
7824 struct btrfs_dio_data *dio_data = iomap->private;
7825 size_t submitted = dio_data->submitted;
7826 const bool write = !!(flags & IOMAP_WRITE);
7827
7828 if (!write && (iomap->type == IOMAP_HOLE)) {
7829 /* If reading from a hole, unlock and return */
7830 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7831 goto out;
7832 }
7833
7834 if (submitted < length) {
7835 pos += submitted;
7836 length -= submitted;
7837 if (write)
7838 __endio_write_update_ordered(BTRFS_I(inode), pos,
7839 length, false);
7840 else
7841 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7842 pos + length - 1);
7843 ret = -ENOTBLK;
7844 }
7845
7846 if (write) {
7847 if (dio_data->reserve)
7848 btrfs_delalloc_release_space(BTRFS_I(inode),
7849 dio_data->data_reserved, pos,
7850 dio_data->reserve, true);
7851 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7852 extent_changeset_free(dio_data->data_reserved);
7853 }
7854out:
7855 kfree(dio_data);
7856 iomap->private = NULL;
7857
Josef Bacikeb838e72012-07-31 16:28:48 -04007858 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007859}
7860
Omar Sandoval769b4f22020-04-16 14:46:22 -07007861static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7862{
7863 /*
7864 * This implies a barrier so that stores to dio_bio->bi_status before
7865 * this and loads of dio_bio->bi_status after this are fully ordered.
7866 */
7867 if (!refcount_dec_and_test(&dip->refs))
7868 return;
7869
Naohiro Aotacfe94442021-02-04 19:21:59 +09007870 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007871 __endio_write_update_ordered(BTRFS_I(dip->inode),
7872 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007873 dip->bytes,
7874 !dip->dio_bio->bi_status);
7875 } else {
7876 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7877 dip->logical_offset,
7878 dip->logical_offset + dip->bytes - 1);
7879 }
7880
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007881 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007882 kfree(dip);
7883}
7884
Omar Sandoval77d5d682020-04-16 14:46:25 -07007885static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7886 int mirror_num,
7887 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007888{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007889 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007891 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007892
Mike Christie37226b22016-06-05 14:31:52 -05007893 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007894
Omar Sandoval5c047a62020-04-16 14:46:24 -07007895 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007896 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007897 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007898
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007899 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007900 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007901 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007902 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007903 return ret;
7904}
7905
Omar Sandoval769b4f22020-04-16 14:46:22 -07007906static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7907 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007908 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007909{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007910 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7911 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007912 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007913 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7914 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007915 struct bio_vec bvec;
7916 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007917 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007918 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007919 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007920
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007921 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7922 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923
Liu Bo17347ce2017-05-15 15:33:27 -07007924 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7925 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007926 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007927 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007928 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007929 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007930 bio_offset, bvec.bv_page,
7931 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007932 clean_io_failure(fs_info, failure_tree, io_tree,
7933 start, bvec.bv_page,
7934 btrfs_ino(BTRFS_I(inode)),
7935 pgoff);
7936 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08007937 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007938
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007939 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08007940 ret = btrfs_repair_one_sector(inode,
7941 &io_bio->bio,
7942 start - io_bio->logical,
7943 bvec.bv_page, pgoff,
7944 start, io_bio->mirror_num,
7945 submit_dio_repair_bio);
7946 if (ret)
7947 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007948 }
7949 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007950 ASSERT(bio_offset + sectorsize > bio_offset);
7951 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307953 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007954 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007955 return err;
7956}
7957
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007958static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007959 const u64 offset, const u64 bytes,
7960 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007961{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007962 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007963 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007964 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007965 u64 ordered_offset = offset;
7966 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007967 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007968
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007969 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007970 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007971 else
Qu Wenruo524272602017-03-08 10:25:52 +08007972 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007973
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007974 while (ordered_offset < offset + bytes) {
7975 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007976 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007977 &ordered_offset,
7978 ordered_bytes,
7979 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007980 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7981 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007982 btrfs_queue_work(wq, &ordered->work);
7983 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08007984
7985 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007986 if (ordered_offset == last_offset)
7987 return;
7988 /*
7989 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007990 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007991 */
7992 if (ordered_offset < offset + bytes) {
7993 ordered_bytes = offset + bytes - ordered_offset;
7994 ordered = NULL;
7995 }
Chris Mason163cf092010-11-28 19:56:33 -05007996 }
Filipe Manana14543772015-11-24 16:23:54 +00007997}
7998
Qu Wenruo8896a082020-10-21 14:24:53 +08007999static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008000 struct bio *bio,
8001 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008002{
Qu Wenruo1941b642020-12-02 14:47:57 +08008003 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008004}
8005
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008006static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008007{
8008 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008009 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008010
Miao Xie8b110e32014-09-12 18:44:03 +08008011 if (err)
8012 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008013 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008014 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008015 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008016 bio->bi_iter.bi_size, err);
8017
Omar Sandoval769b4f22020-04-16 14:46:22 -07008018 if (bio_op(bio) == REQ_OP_READ) {
8019 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008020 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008021 }
8022
Omar Sandoval769b4f22020-04-16 14:46:22 -07008023 if (err)
8024 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008025
Naohiro Aota544d24f2021-02-04 19:22:06 +09008026 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8027
Miao Xiee65e1532010-11-22 03:04:43 +00008028 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008029 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008030}
8031
David Sterbad0ee3932018-03-08 14:35:48 +01008032static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8033 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008034{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008035 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008036 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008037 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008038 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008039
Liu Bo4c274bc2017-11-01 17:19:27 -06008040 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008041 if (async_submit)
8042 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8043
Josef Bacik5fd02042012-05-02 14:00:54 -04008044 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008045 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008046 if (ret)
8047 goto err;
8048 }
Miao Xiee65e1532010-11-22 03:04:43 +00008049
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008050 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008051 goto map;
8052
8053 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008054 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008055 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008056 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008057 } else if (write) {
8058 /*
8059 * If we aren't doing async submit, calculate the csum of the
8060 * bio now.
8061 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008062 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008063 if (ret)
8064 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008065 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008066 u64 csum_offset;
8067
8068 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008069 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008070 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008071 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008072 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008073map:
Chris Mason08635ba2019-07-10 12:28:14 -07008074 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008075err:
Miao Xiee65e1532010-11-22 03:04:43 +00008076 return ret;
8077}
8078
Omar Sandovalc36cac22020-04-16 14:46:13 -07008079/*
8080 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8081 * or ordered extents whether or not we submit any bios.
8082 */
8083static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8084 struct inode *inode,
8085 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008086{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008087 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008088 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8089 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008090 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008091
Omar Sandoval85879572020-04-16 14:46:21 -07008092 dip_size = sizeof(*dip);
8093 if (!write && csum) {
8094 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008095 size_t nblocks;
8096
David Sterba265fdfa2020-07-01 21:19:09 +02008097 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008098 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008099 }
8100
8101 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008102 if (!dip)
8103 return NULL;
8104
Omar Sandovalc36cac22020-04-16 14:46:13 -07008105 dip->inode = inode;
8106 dip->logical_offset = file_offset;
8107 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008108 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008109 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008110 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008111 return dip;
8112}
8113
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008114static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8115 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008116{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008117 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008119 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8120 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008121 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008122 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008123 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008124 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008125 u64 submit_len;
8126 int clone_offset = 0;
8127 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008128 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308129 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008130 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008131 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008132 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008133 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008134
Omar Sandovalc36cac22020-04-16 14:46:13 -07008135 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8136 if (!dip) {
8137 if (!write) {
8138 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8139 file_offset + dio_bio->bi_iter.bi_size - 1);
8140 }
8141 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008142 bio_endio(dio_bio);
8143 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008144 }
8145
Josef Bacik334c16d2020-10-16 11:29:14 -04008146 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008147 /*
8148 * Load the csums up front to reduce csum tree searches and
8149 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008150 *
8151 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008152 */
Qu Wenruo62751932020-12-02 14:48:06 +08008153 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008154 if (status != BLK_STS_OK)
8155 goto out_err;
8156 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008157
Omar Sandoval769b4f22020-04-16 14:46:22 -07008158 start_sector = dio_bio->bi_iter.bi_sector;
8159 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008160
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008161 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008162 logical = start_sector << 9;
8163 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8164 if (IS_ERR(em)) {
8165 status = errno_to_blk_status(PTR_ERR(em));
8166 em = NULL;
8167 goto out_err_em;
8168 }
8169 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008170 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008171 if (ret) {
8172 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008173 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008174 }
8175 ASSERT(geom.len <= INT_MAX);
8176
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008177 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008178
Liu Bo725130b2017-05-16 09:51:39 -07008179 /*
8180 * This will never fail as it's passing GPF_NOFS and
8181 * the allocation is backed by btrfs_bioset.
8182 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008183 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008184 bio->bi_private = dip;
8185 bio->bi_end_io = btrfs_end_dio_bio;
8186 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008187
Naohiro Aota544d24f2021-02-04 19:22:06 +09008188 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8189 status = extract_ordered_extent(BTRFS_I(inode), bio,
8190 file_offset);
8191 if (status) {
8192 bio_put(bio);
8193 goto out_err;
8194 }
8195 }
8196
Liu Bo725130b2017-05-16 09:51:39 -07008197 ASSERT(submit_len >= clone_len);
8198 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008199
Liu Bo725130b2017-05-16 09:51:39 -07008200 /*
8201 * Increase the count before we submit the bio so we know
8202 * the end IO handler won't happen before we increase the
8203 * count. Otherwise, the dip might get freed before we're
8204 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008205 *
8206 * We transfer the initial reference to the last bio, so we
8207 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008208 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008209 if (submit_len > 0) {
8210 refcount_inc(&dip->refs);
8211 /*
8212 * If we are submitting more than one bio, submit them
8213 * all asynchronously. The exception is RAID 5 or 6, as
8214 * asynchronous checksums make it difficult to collect
8215 * full stripe writes.
8216 */
8217 if (!raid56)
8218 async_submit = 1;
8219 }
Miao Xiee65e1532010-11-22 03:04:43 +00008220
David Sterbad0ee3932018-03-08 14:35:48 +01008221 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008222 async_submit);
8223 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008224 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008225 if (submit_len > 0)
8226 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008227 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008228 }
Liu Bo725130b2017-05-16 09:51:39 -07008229
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008230 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008231 clone_offset += clone_len;
8232 start_sector += clone_len >> 9;
8233 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008234
8235 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008236 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008237 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008238
Michal Rostecki42034312021-01-27 14:57:27 +01008239out_err_em:
8240 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008241out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008242 dip->dio_bio->bi_status = status;
8243 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008244
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008245 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008246}
8247
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008248const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008249 .iomap_begin = btrfs_dio_iomap_begin,
8250 .iomap_end = btrfs_dio_iomap_end,
8251};
8252
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008253const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008254 .submit_io = btrfs_submit_direct,
8255};
8256
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008257static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008258 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008259{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008260 int ret;
8261
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008262 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008263 if (ret)
8264 return ret;
8265
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008266 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008267}
8268
Chris Mason9ebefb182007-06-15 13:50:00 -04008269int btrfs_readpage(struct file *file, struct page *page)
8270{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008271 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8272 u64 start = page_offset(page);
8273 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008274 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008275 int ret;
8276
Nikolay Borisov0f208812020-09-14 14:39:16 +03008277 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8278
Qu Wenruo390ed292021-04-14 16:42:15 +08008279 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8280 if (bio_ctrl.bio)
8281 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008282 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008283}
Chris Mason1832a6d2007-12-21 16:27:21 -05008284
Chris Mason39279cc2007-06-12 06:35:45 -04008285static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8286{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008287 struct inode *inode = page->mapping->host;
8288 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008289
8290 if (current->flags & PF_MEMALLOC) {
8291 redirty_page_for_writepage(wbc, page);
8292 unlock_page(page);
8293 return 0;
8294 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008295
8296 /*
8297 * If we are under memory pressure we will call this directly from the
8298 * VM, we need to make sure we have the inode referenced for the ordered
8299 * extent. If not just return like we didn't do anything.
8300 */
8301 if (!igrab(inode)) {
8302 redirty_page_for_writepage(wbc, page);
8303 return AOP_WRITEPAGE_ACTIVATE;
8304 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008305 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008306 btrfs_add_delayed_iput(inode);
8307 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008308}
Chris Mason39279cc2007-06-12 06:35:45 -04008309
Eric Sandeen48a3b632013-04-25 20:41:01 +00008310static int btrfs_writepages(struct address_space *mapping,
8311 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008312{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008313 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008314}
8315
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008316static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008317{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008318 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008319}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008320
Chris Masone6dcd2d2008-07-17 12:53:50 -04008321static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008322{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008323 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008324 if (ret == 1)
Qu Wenruo32443de2021-01-26 16:34:00 +08008325 clear_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008326 return ret;
8327}
Chris Mason39279cc2007-06-12 06:35:45 -04008328
Chris Masone6dcd2d2008-07-17 12:53:50 -04008329static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8330{
Chris Mason98509cf2008-09-11 15:51:43 -04008331 if (PageWriteback(page) || PageDirty(page))
8332 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008333 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008334}
8335
Roman Gushchinf8e66082020-03-04 16:57:35 -08008336#ifdef CONFIG_MIGRATION
8337static int btrfs_migratepage(struct address_space *mapping,
8338 struct page *newpage, struct page *page,
8339 enum migrate_mode mode)
8340{
8341 int ret;
8342
8343 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8344 if (ret != MIGRATEPAGE_SUCCESS)
8345 return ret;
8346
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008347 if (page_has_private(page))
8348 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008349
8350 if (PagePrivate2(page)) {
8351 ClearPagePrivate2(page);
8352 SetPagePrivate2(newpage);
8353 }
8354
8355 if (mode != MIGRATE_SYNC_NO_COPY)
8356 migrate_page_copy(newpage, page);
8357 else
8358 migrate_page_states(newpage, page);
8359 return MIGRATEPAGE_SUCCESS;
8360}
8361#endif
8362
Lukas Czernerd47992f2013-05-21 23:17:23 -04008363static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8364 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008365{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008366 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8367 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008368 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008369 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008370 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008371 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308372 u64 start;
8373 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008374 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008375 bool found_ordered = false;
8376 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008377
Chris Mason8b62b722009-09-02 16:53:46 -04008378 /*
8379 * we have the page locked, so new writeback can't start,
8380 * and the dirty bit won't be cleared while we are here.
8381 *
8382 * Wait for IO on this page so that we can safely clear
8383 * the PagePrivate2 bit and do ordered accounting
8384 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008385 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008386
Chris Masone6dcd2d2008-07-17 12:53:50 -04008387 if (offset) {
8388 btrfs_releasepage(page, GFP_NOFS);
8389 return;
8390 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008391
8392 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008393 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008394
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308395 start = page_start;
Qu Wenruo951c80f2021-01-27 14:38:48 +08008396again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008397 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008398 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008399 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008400 end = min(page_end,
8401 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008402 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008403 * IO on this page will never be started, so we need to account
8404 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8405 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008406 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008407 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308408 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008409 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008410 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008411 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008412 /*
8413 * whoever cleared the private bit is responsible
8414 * for the finish_ordered_io
8415 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008416 if (TestClearPagePrivate2(page)) {
Qu Wenruod239bcb2021-03-25 15:14:35 +08008417 spin_lock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008418 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Qu Wenruoac5804e2021-03-25 15:14:34 +08008419 ordered->truncated_len = min(ordered->truncated_len,
8420 start - ordered->file_offset);
Qu Wenruod239bcb2021-03-25 15:14:35 +08008421 spin_unlock_irq(&inode->ordered_tree.lock);
Josef Bacik77cef2e2013-08-29 13:57:21 -04008422
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008423 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8424 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008425 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008426 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008427 completed_ordered = true;
8428 }
Chris Mason8b62b722009-09-02 16:53:46 -04008429 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008430 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008431 if (!inode_evicting) {
8432 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308433 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008434 &cached_state);
8435 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308436
8437 start = end + 1;
8438 if (start < page_end)
8439 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008440 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008441
Qu Wenruob9d0b382015-09-29 10:35:16 +08008442 /*
8443 * Qgroup reserved space handler
8444 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008445 * 1) Already written to disk or ordered extent already submitted
8446 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8447 * Qgroup will be handled by its qgroup_record then.
8448 * btrfs_qgroup_free_data() call will do nothing here.
8449 *
8450 * 2) Not written to disk yet
8451 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8452 * bit of its io_tree, and free the qgroup reserved data space.
8453 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008454 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008455 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008456 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008457 bool delete = true;
8458
8459 /*
8460 * If there's an ordered extent for this range and we have not
8461 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8462 * in the range for the ordered extent completion. We must also
8463 * not delete the range, otherwise we would lose that bit (and
8464 * any other bits set in the range). Make sure EXTENT_UPTODATE
8465 * is cleared if we don't delete, otherwise it can lead to
8466 * corruptions if the i_size is extented later.
8467 */
8468 if (found_ordered && !completed_ordered)
8469 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008470 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008471 EXTENT_DELALLOC | EXTENT_UPTODATE |
8472 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8473 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008474
8475 __btrfs_releasepage(page, GFP_NOFS);
8476 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008477
Chris Mason4a096752008-07-21 10:29:44 -04008478 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008479 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008480}
8481
Chris Mason9ebefb182007-06-15 13:50:00 -04008482/*
8483 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8484 * called from a page fault handler when a page is first dirtied. Hence we must
8485 * be careful to check for EOF conditions here. We set the page up correctly
8486 * for a written page which means we get ENOSPC checking when writing into
8487 * holes and correct delalloc and unwritten extent mapping on filesystems that
8488 * support these features.
8489 *
8490 * We are not allowed to take the i_mutex here so we have to play games to
8491 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008492 * truncate_setsize() writes the inode size before removing pages, once we have
8493 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008494 * beyond EOF, then the page is guaranteed safe against truncation until we
8495 * unlock the page.
8496 */
Souptick Joardera528a242018-06-06 19:54:44 +05308497vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008498{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008499 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008500 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008501 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008502 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8503 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008504 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008505 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008506 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008507 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308508 vm_fault_t ret;
8509 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008510 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308511 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008512 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008513 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308514 u64 end;
8515
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008516 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008517
Jan Karab2b5ef52012-06-12 16:20:45 +02008518 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008519 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008520 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308521 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008522
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308523 /*
8524 * Reserving delalloc space after obtaining the page lock can lead to
8525 * deadlock. For example, if a dirty page is locked by this function
8526 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8527 * dirty page write out, then the btrfs_writepage() function could
8528 * end up waiting indefinitely to get a lock on the page currently
8529 * being processed by btrfs_page_mkwrite() function.
8530 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008531 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8532 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308533 if (!ret2) {
8534 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008535 reserved = 1;
8536 }
Souptick Joardera528a242018-06-06 19:54:44 +05308537 if (ret2) {
8538 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008539 if (reserved)
8540 goto out;
8541 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008542 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008543
Nick Piggin56a76f82009-03-31 15:23:23 -07008544 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008545again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008546 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008547 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008548 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008549
Chris Mason9ebefb182007-06-15 13:50:00 -04008550 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008551 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008552 /* page got truncated out from underneath us */
8553 goto out_unlock;
8554 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008555 wait_on_page_writeback(page);
8556
David Sterbaff13db42015-12-03 14:30:40 +01008557 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008558 ret2 = set_page_extent_mapped(page);
8559 if (ret2 < 0) {
8560 ret = vmf_error(ret2);
8561 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8562 goto out_unlock;
8563 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008564
Chris Masoneb84ae02008-07-17 13:53:27 -04008565 /*
8566 * we can't set the delalloc bits if there are pending ordered
8567 * extents. Drop our locks and wait for them to finish
8568 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008569 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8570 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008571 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008572 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008573 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008574 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008575 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008576 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008577 btrfs_put_ordered_extent(ordered);
8578 goto again;
8579 }
8580
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008581 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008582 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008583 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008584 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308585 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008586 btrfs_delalloc_release_space(BTRFS_I(inode),
8587 data_reserved, page_start,
8588 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308589 }
8590 }
8591
Josef Bacikfbf19082009-10-01 17:10:23 -04008592 /*
Liu Bo54160342016-12-13 12:15:19 -08008593 * page_mkwrite gets called when the page is firstly dirtied after it's
8594 * faulted in, but write(2) could also dirty a page and set delalloc
8595 * bits, thus in this case for space account reason, we still need to
8596 * clear any delalloc bits within this page range since we have to
8597 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008598 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308599 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008600 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8601 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008602
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008603 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008604 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308605 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008606 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008607 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008608 ret = VM_FAULT_SIGBUS;
8609 goto out_unlock;
8610 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008611
8612 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008613 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008614 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008615 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008616 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008617
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008618 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008619 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008620 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008621 }
Chris Mason247e7432008-07-17 12:53:51 -04008622 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008623 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008624 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008625
Filipe Mananabc0939f2021-02-23 12:08:48 +00008626 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008627
David Sterbae43bbe52017-12-12 21:43:52 +01008628 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008629 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008630
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008631 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8632 sb_end_pagefault(inode->i_sb);
8633 extent_changeset_free(data_reserved);
8634 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008635
8636out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008637 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008638 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008639out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008640 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008641 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008642 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008643out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008644 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008645 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008646 return ret;
8647}
8648
Filipe Manana213e8c52018-02-06 20:40:31 +00008649static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008652 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008653 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008654 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008655 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008656 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008657 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008658 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008659
Filipe Manana213e8c52018-02-06 20:40:31 +00008660 if (!skip_writeback) {
8661 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8662 (u64)-1);
8663 if (ret)
8664 return ret;
8665 }
Chris Mason39279cc2007-06-12 06:35:45 -04008666
Josef Bacikfcb80c22011-05-03 10:40:22 -04008667 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008668 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8669 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008670 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008671 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008672 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008673 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008674 * be free'd up by the truncate operation, but also have some slack
8675 * space reserved in case it uses space during the truncate (thank you
8676 * very much snapshotting).
8677 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008678 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008679 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008680 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008681 * doesn't end up using space reserved for updating the inode. We also
8682 * need to be able to stop the transaction and start a new one, which
8683 * means we need to be able to update the inode several times, and we
8684 * have no idea of knowing how many times that will be, so we can't just
8685 * reserve 1 item for the entirety of the operation, so that has to be
8686 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008687 *
8688 * So that leaves us with
8689 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008690 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008691 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008692 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008693 * updating the inode.
8694 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008695 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008696 if (!rsv)
8697 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008698 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008699 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008700
Josef Bacik907cbce2011-08-08 13:46:15 -04008701 /*
Josef Bacik07127182011-08-19 10:29:59 -04008702 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008703 * 1 for updating the inode.
8704 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008705 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008706 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008707 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008708 goto out;
8709 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008710
Josef Bacik907cbce2011-08-08 13:46:15 -04008711 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008712 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008713 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008714 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008715
Josef Bacikca7e70f2012-08-27 17:48:15 -04008716 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008717
Yan, Zheng80825102009-11-12 09:35:36 +00008718 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008719 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008720 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008721 BTRFS_EXTENT_DATA_KEY,
8722 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008723 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008724 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008725 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008726
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008727 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008728 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008729 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008730
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008731 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008732 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008733
8734 trans = btrfs_start_transaction(root, 2);
8735 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008736 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008737 trans = NULL;
8738 break;
8739 }
8740
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008741 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008742 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008743 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008744 BUG_ON(ret); /* shouldn't happen */
8745 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008746 }
8747
Josef Bacikddfae632017-10-19 14:16:02 -04008748 /*
8749 * We can't call btrfs_truncate_block inside a trans handle as we could
8750 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8751 * we've truncated everything except the last little bit, and can do
8752 * btrfs_truncate_block and then update the disk_i_size.
8753 */
8754 if (ret == NEED_TRUNCATE_BLOCK) {
8755 btrfs_end_transaction(trans);
8756 btrfs_btree_balance_dirty(fs_info);
8757
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008758 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008759 if (ret)
8760 goto out;
8761 trans = btrfs_start_transaction(root, 1);
8762 if (IS_ERR(trans)) {
8763 ret = PTR_ERR(trans);
8764 goto out;
8765 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008766 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008767 }
8768
Chris Mason917c16b2011-11-08 14:49:59 -05008769 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008770 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008771
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008772 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008773 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008774 if (ret2 && !ret)
8775 ret = ret2;
8776
8777 ret2 = btrfs_end_transaction(trans);
8778 if (ret2 && !ret)
8779 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008780 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008781 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008782out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008783 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008784 /*
8785 * So if we truncate and then write and fsync we normally would just
8786 * write the extents that changed, which is a problem if we need to
8787 * first truncate that entire inode. So set this flag so we write out
8788 * all of the extents in the inode to the sync log so we're completely
8789 * safe.
8790 *
8791 * If no extents were dropped or trimmed we don't need to force the next
8792 * fsync to truncate all the inode's items from the log and re-log them
8793 * all. This means the truncate operation did not change the file size,
8794 * or changed it to a smaller size but there was only an implicit hole
8795 * between the old i_size and the new i_size, and there were no prealloc
8796 * extents beyond i_size to drop.
8797 */
8798 if (extents_found > 0)
8799 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008800
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008801 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008802}
8803
Sven Wegener3b963622008-06-09 21:57:42 -04008804/*
Chris Masond352ac62008-09-29 15:18:18 -04008805 * create a new subvolume directory/inode (helper for the ioctl).
8806 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008807int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008808 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008809 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008810{
Chris Mason39279cc2007-06-12 06:35:45 -04008811 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008812 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008813 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008814 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008815
Nikolay Borisov23125102020-12-07 17:32:36 +02008816 err = btrfs_get_free_objectid(new_root, &ino);
8817 if (err < 0)
8818 return err;
8819
8820 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008821 S_IFDIR | (~current_umask() & S_IRWXUGO),
8822 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008823 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008824 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008825 inode->i_op = &btrfs_dir_inode_operations;
8826 inode->i_fop = &btrfs_dir_file_operations;
8827
Miklos Szeredibfe86842011-10-28 14:13:29 +02008828 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008829 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008830 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008831
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008832 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8833 if (err)
8834 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008835 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008836 new_root->root_key.objectid, err);
8837
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008838 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008839
Yan, Zheng76dda932009-09-21 16:00:26 -04008840 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008841 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008842}
8843
Chris Mason39279cc2007-06-12 06:35:45 -04008844struct inode *btrfs_alloc_inode(struct super_block *sb)
8845{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008846 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008847 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008848 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008849
David Sterba712e36c2017-10-31 17:08:27 +01008850 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008851 if (!ei)
8852 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008853
8854 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008855 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008856 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008857 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008858 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008859 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008860 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008861 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008862 ei->disk_i_size = 0;
8863 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008864 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008865 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008866 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008867 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008868 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008869 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008870
Josef Bacik9e0baf62011-07-15 15:16:44 +00008871 spin_lock_init(&ei->lock);
8872 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008873 if (sb->s_magic != BTRFS_TEST_MAGIC)
8874 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8875 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008876 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008877 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008878 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008879
Miao Xie16cdcec2011-04-22 18:12:22 +08008880 ei->delayed_node = NULL;
8881
chandan r9cc97d62012-07-04 12:48:07 +05308882 ei->i_otime.tv_sec = 0;
8883 ei->i_otime.tv_nsec = 0;
8884
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008885 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008886 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008887 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8888 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8889 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008890 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8891 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008892 ei->io_tree.track_uptodate = true;
8893 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008894 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008895 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008896 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008897 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008898 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008899 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05008900 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008901
8902 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008903}
8904
Josef Bacikaaedb552013-10-11 14:44:09 -04008905#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8906void btrfs_test_destroy_inode(struct inode *inode)
8907{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008908 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008909 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8910}
8911#endif
8912
Al Viro26602ca2019-04-10 15:14:41 -04008913void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008914{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008915 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8916}
8917
Nikolay Borisov633cc812020-09-18 12:15:49 +03008918void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008919{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008920 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008921 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8922 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008923
Nikolay Borisov633cc812020-09-18 12:15:49 +03008924 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8925 WARN_ON(vfs_inode->i_data.nrpages);
8926 WARN_ON(inode->block_rsv.reserved);
8927 WARN_ON(inode->block_rsv.size);
8928 WARN_ON(inode->outstanding_extents);
8929 WARN_ON(inode->delalloc_bytes);
8930 WARN_ON(inode->new_delalloc_bytes);
8931 WARN_ON(inode->csum_bytes);
8932 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008933
Chris Mason5a3f23d2009-03-31 13:27:11 -04008934 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008935 * This can happen where we create an inode, but somebody else also
8936 * created the same inode and we need to destroy the one we already
8937 * created.
8938 */
8939 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008940 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008941
Chris Masond3977122009-01-05 21:25:51 -05008942 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008943 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944 if (!ordered)
8945 break;
8946 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008947 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008948 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008949 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008950 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 btrfs_put_ordered_extent(ordered);
8952 btrfs_put_ordered_extent(ordered);
8953 }
8954 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008955 btrfs_qgroup_check_reserved_leak(inode);
8956 inode_tree_del(inode);
8957 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8958 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8959 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008960}
8961
Al Viro45321ac2010-06-07 13:43:19 -04008962int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008963{
8964 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008965
Naohiro Aota6379ef92013-06-06 09:56:34 +00008966 if (root == NULL)
8967 return 1;
8968
Liu Bofa6ac872013-02-20 14:10:23 +00008969 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008970 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008971 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008972 else
Al Viro45321ac2010-06-07 13:43:19 -04008973 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008974}
8975
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008976static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008977{
8978 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8979
8980 inode_init_once(&ei->vfs_inode);
8981}
8982
David Sterbae67c7182018-02-19 17:24:18 +01008983void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008984{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008985 /*
8986 * Make sure all delayed rcu free inodes are flushed before we
8987 * destroy cache.
8988 */
8989 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008990 kmem_cache_destroy(btrfs_inode_cachep);
8991 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008992 kmem_cache_destroy(btrfs_path_cachep);
8993 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008994 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008995}
8996
Liu Bof5c29bd2017-11-02 17:21:50 -06008997int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008998{
David Sterba837e1972012-09-07 03:00:48 -06008999 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009000 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009001 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9002 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009003 if (!btrfs_inode_cachep)
9004 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009005
David Sterba837e1972012-09-07 03:00:48 -06009006 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009007 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009008 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009009 if (!btrfs_trans_handle_cachep)
9010 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009011
David Sterba837e1972012-09-07 03:00:48 -06009012 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009013 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009014 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009015 if (!btrfs_path_cachep)
9016 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009017
David Sterba837e1972012-09-07 03:00:48 -06009018 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009019 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009020 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009021 if (!btrfs_free_space_cachep)
9022 goto fail;
9023
Christophe Leroy3acd4852019-08-21 15:05:55 +00009024 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9025 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009026 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009027 if (!btrfs_free_space_bitmap_cachep)
9028 goto fail;
9029
Chris Mason39279cc2007-06-12 06:35:45 -04009030 return 0;
9031fail:
9032 btrfs_destroy_cachep();
9033 return -ENOMEM;
9034}
9035
Christian Brauner549c7292021-01-21 14:19:43 +01009036static int btrfs_getattr(struct user_namespace *mnt_userns,
9037 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009038 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009039{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009040 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009041 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009042 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009043 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009044 u32 bi_flags = BTRFS_I(inode)->flags;
9045
9046 stat->result_mask |= STATX_BTIME;
9047 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9048 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9049 if (bi_flags & BTRFS_INODE_APPEND)
9050 stat->attributes |= STATX_ATTR_APPEND;
9051 if (bi_flags & BTRFS_INODE_COMPRESS)
9052 stat->attributes |= STATX_ATTR_COMPRESSED;
9053 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9054 stat->attributes |= STATX_ATTR_IMMUTABLE;
9055 if (bi_flags & BTRFS_INODE_NODUMP)
9056 stat->attributes |= STATX_ATTR_NODUMP;
9057
9058 stat->attributes_mask |= (STATX_ATTR_APPEND |
9059 STATX_ATTR_COMPRESSED |
9060 STATX_ATTR_IMMUTABLE |
9061 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009062
Christian Brauner0d56a452021-01-21 14:19:30 +01009063 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009064 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009065
9066 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009067 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009068 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009069 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009070 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009071 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009072 return 0;
9073}
9074
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075static int btrfs_rename_exchange(struct inode *old_dir,
9076 struct dentry *old_dentry,
9077 struct inode *new_dir,
9078 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009079{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009080 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009081 struct btrfs_trans_handle *trans;
9082 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009083 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009084 struct inode *new_inode = new_dentry->d_inode;
9085 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009086 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009087 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9088 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009089 u64 old_idx = 0;
9090 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009091 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009092 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009093 bool root_log_pinned = false;
9094 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009095 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009096
9097 /* we only allow rename subvolume link between subvolumes */
9098 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9099 return -EXDEV;
9100
9101 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009102 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9103 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009104 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009105
9106 /*
9107 * We want to reserve the absolute worst case amount of items. So if
9108 * both inodes are subvols and we need to unlink them then that would
9109 * require 4 item modifications, but if they are both normal inodes it
9110 * would require 5 item modifications, so we'll assume their normal
9111 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9112 * should cover the worst case number of items we'll modify.
9113 */
9114 trans = btrfs_start_transaction(root, 12);
9115 if (IS_ERR(trans)) {
9116 ret = PTR_ERR(trans);
9117 goto out_notrans;
9118 }
9119
Josef Bacik00aa8e82021-03-12 15:25:02 -05009120 if (dest != root) {
9121 ret = btrfs_record_root_in_trans(trans, dest);
9122 if (ret)
9123 goto out_fail;
9124 }
Josef Bacik3e174092019-11-15 15:43:06 -05009125
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009126 /*
9127 * We need to find a free sequence number both in the source and
9128 * in the destination directory for the exchange.
9129 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009130 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009131 if (ret)
9132 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009133 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009134 if (ret)
9135 goto out_fail;
9136
9137 BTRFS_I(old_inode)->dir_index = 0ULL;
9138 BTRFS_I(new_inode)->dir_index = 0ULL;
9139
9140 /* Reference for the source. */
9141 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9142 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009143 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009144 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009145 btrfs_pin_log_trans(root);
9146 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009147 ret = btrfs_insert_inode_ref(trans, dest,
9148 new_dentry->d_name.name,
9149 new_dentry->d_name.len,
9150 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009151 btrfs_ino(BTRFS_I(new_dir)),
9152 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009153 if (ret)
9154 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009155 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009156 }
9157
9158 /* And now for the dest. */
9159 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9160 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009161 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009162 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009163 btrfs_pin_log_trans(dest);
9164 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009165 ret = btrfs_insert_inode_ref(trans, root,
9166 old_dentry->d_name.name,
9167 old_dentry->d_name.len,
9168 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009169 btrfs_ino(BTRFS_I(old_dir)),
9170 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009171 if (ret) {
9172 if (need_abort)
9173 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009174 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009175 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009176 }
9177
9178 /* Update inode version and ctime/mtime. */
9179 inode_inc_iversion(old_dir);
9180 inode_inc_iversion(new_dir);
9181 inode_inc_iversion(old_inode);
9182 inode_inc_iversion(new_inode);
9183 old_dir->i_ctime = old_dir->i_mtime = ctime;
9184 new_dir->i_ctime = new_dir->i_mtime = ctime;
9185 old_inode->i_ctime = ctime;
9186 new_inode->i_ctime = ctime;
9187
9188 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009189 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9190 BTRFS_I(old_inode), 1);
9191 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9192 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009193 }
9194
9195 /* src is a subvolume */
9196 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009197 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009198 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009199 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9200 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009201 old_dentry->d_name.name,
9202 old_dentry->d_name.len);
9203 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009204 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009205 }
9206 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009207 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009208 goto out_fail;
9209 }
9210
9211 /* dest is a subvolume */
9212 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009213 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009214 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009215 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9216 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009217 new_dentry->d_name.name,
9218 new_dentry->d_name.len);
9219 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009220 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009221 }
9222 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009223 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009224 goto out_fail;
9225 }
9226
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009227 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009228 new_dentry->d_name.name,
9229 new_dentry->d_name.len, 0, old_idx);
9230 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009231 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009232 goto out_fail;
9233 }
9234
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009235 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009236 old_dentry->d_name.name,
9237 old_dentry->d_name.len, 0, new_idx);
9238 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009239 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009240 goto out_fail;
9241 }
9242
9243 if (old_inode->i_nlink == 1)
9244 BTRFS_I(old_inode)->dir_index = old_idx;
9245 if (new_inode->i_nlink == 1)
9246 BTRFS_I(new_inode)->dir_index = new_idx;
9247
Filipe Manana86e8aa02016-05-05 02:02:27 +01009248 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009249 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9250 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009251 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009252 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009253 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009254 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009255 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9256 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009257 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009258 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009259 }
9260out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009261 /*
9262 * If we have pinned a log and an error happened, we unpin tasks
9263 * trying to sync the log and force them to fallback to a transaction
9264 * commit if the log currently contains any of the inodes involved in
9265 * this rename operation (to ensure we do not persist a log with an
9266 * inconsistent state for any of these inodes or leading to any
9267 * inconsistencies when replayed). If the transaction was aborted, the
9268 * abortion reason is propagated to userspace when attempting to commit
9269 * the transaction. If the log does not contain any of these inodes, we
9270 * allow the tasks to sync it.
9271 */
9272 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009273 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9274 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9275 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009276 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009277 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009278 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009279
9280 if (root_log_pinned) {
9281 btrfs_end_log_trans(root);
9282 root_log_pinned = false;
9283 }
9284 if (dest_log_pinned) {
9285 btrfs_end_log_trans(dest);
9286 dest_log_pinned = false;
9287 }
9288 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009289 ret2 = btrfs_end_transaction(trans);
9290 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009291out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009292 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9293 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009294 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009295
9296 return ret;
9297}
9298
9299static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9300 struct btrfs_root *root,
9301 struct inode *dir,
9302 struct dentry *dentry)
9303{
9304 int ret;
9305 struct inode *inode;
9306 u64 objectid;
9307 u64 index;
9308
Nikolay Borisov543068a2020-12-07 17:32:33 +02009309 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009310 if (ret)
9311 return ret;
9312
9313 inode = btrfs_new_inode(trans, root, dir,
9314 dentry->d_name.name,
9315 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009316 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009317 objectid,
9318 S_IFCHR | WHITEOUT_MODE,
9319 &index);
9320
9321 if (IS_ERR(inode)) {
9322 ret = PTR_ERR(inode);
9323 return ret;
9324 }
9325
9326 inode->i_op = &btrfs_special_inode_operations;
9327 init_special_inode(inode, inode->i_mode,
9328 WHITEOUT_DEV);
9329
9330 ret = btrfs_init_inode_security(trans, inode, dir,
9331 &dentry->d_name);
9332 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009333 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009334
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009335 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9336 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009337 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009338 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009339
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009340 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009341out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009342 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009343 if (ret)
9344 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009345 iput(inode);
9346
Filipe Mananac9901612016-05-05 01:41:57 +01009347 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009348}
9349
Chris Mason39279cc2007-06-12 06:35:45 -04009350static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009351 struct inode *new_dir, struct dentry *new_dentry,
9352 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009354 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009355 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009356 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009357 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9358 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009359 struct inode *new_inode = d_inode(new_dentry);
9360 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009361 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009362 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009363 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009364 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009365 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009366
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009367 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009368 return -EPERM;
9369
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009370 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009371 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009372 return -EXDEV;
9373
Li Zefan33345d012011-04-20 10:31:50 +08009374 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009375 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009376 return -ENOTEMPTY;
9377
Chris Mason39279cc2007-06-12 06:35:45 -04009378 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009379 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009380 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009381
9382
9383 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009384 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009385 new_dentry->d_name.name,
9386 new_dentry->d_name.len);
9387
9388 if (ret) {
9389 if (ret == -EEXIST) {
9390 /* we shouldn't get
9391 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309392 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009393 return ret;
9394 }
9395 } else {
9396 /* maybe -EOVERFLOW */
9397 return ret;
9398 }
9399 }
9400 ret = 0;
9401
Chris Mason5a3f23d2009-03-31 13:27:11 -04009402 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009403 * we're using rename to replace one file with another. Start IO on it
9404 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009405 */
Chris Mason8d875f92014-08-12 10:47:42 -07009406 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009407 filemap_flush(old_inode->i_mapping);
9408
Yan, Zheng76dda932009-09-21 16:00:26 -04009409 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009410 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009411 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009412 /*
9413 * We want to reserve the absolute worst case amount of items. So if
9414 * both inodes are subvols and we need to unlink them then that would
9415 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009416 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009417 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9418 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009419 * If our rename has the whiteout flag, we need more 5 units for the
9420 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9421 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009422 */
Filipe Manana5062af32016-05-05 10:26:26 +01009423 trans_num_items = 11;
9424 if (flags & RENAME_WHITEOUT)
9425 trans_num_items += 5;
9426 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009427 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009428 ret = PTR_ERR(trans);
9429 goto out_notrans;
9430 }
Chris Mason5f39d392007-10-15 16:14:19 -04009431
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009432 if (dest != root) {
9433 ret = btrfs_record_root_in_trans(trans, dest);
9434 if (ret)
9435 goto out_fail;
9436 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009437
Nikolay Borisov877574e2017-02-20 13:50:33 +02009438 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009439 if (ret)
9440 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009441
Miao Xie67de1172013-12-26 13:07:06 +08009442 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009443 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009444 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009445 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009446 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009447 btrfs_pin_log_trans(root);
9448 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009449 ret = btrfs_insert_inode_ref(trans, dest,
9450 new_dentry->d_name.name,
9451 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009452 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009453 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009454 if (ret)
9455 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009456 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009457
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009458 inode_inc_iversion(old_dir);
9459 inode_inc_iversion(new_dir);
9460 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009461 old_dir->i_ctime = old_dir->i_mtime =
9462 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009463 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009464
Chris Mason12fcfd22009-03-24 10:24:20 -04009465 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009466 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9467 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009468
Li Zefan33345d012011-04-20 10:31:50 +08009469 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009470 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009471 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009472 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9473 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009474 old_dentry->d_name.name,
9475 old_dentry->d_name.len);
9476 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009477 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009478 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009479 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009480 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009481 goto out_fail;
9482 }
Chris Mason39279cc2007-06-12 06:35:45 -04009483
9484 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009485 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009486 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009487 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009488 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009489 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009490 BUG_ON(new_inode->i_nlink == 0);
9491 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009492 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9493 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009494 new_dentry->d_name.name,
9495 new_dentry->d_name.len);
9496 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009497 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009498 ret = btrfs_orphan_add(trans,
9499 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009500 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009501 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009502 goto out_fail;
9503 }
Chris Mason39279cc2007-06-12 06:35:45 -04009504 }
Josef Bacikaec74772008-07-24 12:12:38 -04009505
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009506 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009507 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009508 new_dentry->d_name.len, 0, index);
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
Miao Xie67de1172013-12-26 13:07:06 +08009514 if (old_inode->i_nlink == 1)
9515 BTRFS_I(old_inode)->dir_index = index;
9516
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009517 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009518 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9519 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009520 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009521 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009522 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009523
9524 if (flags & RENAME_WHITEOUT) {
9525 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9526 old_dentry);
9527
9528 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009529 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530 goto out_fail;
9531 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009532 }
Chris Mason39279cc2007-06-12 06:35:45 -04009533out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009534 /*
9535 * If we have pinned the log and an error happened, we unpin tasks
9536 * trying to sync the log and force them to fallback to a transaction
9537 * commit if the log currently contains any of the inodes involved in
9538 * this rename operation (to ensure we do not persist a log with an
9539 * inconsistent state for any of these inodes or leading to any
9540 * inconsistencies when replayed). If the transaction was aborted, the
9541 * abortion reason is propagated to userspace when attempting to commit
9542 * the transaction. If the log does not contain any of these inodes, we
9543 * allow the tasks to sync it.
9544 */
9545 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009546 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9547 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9548 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009549 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009550 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009551 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009552
9553 btrfs_end_log_trans(root);
9554 log_pinned = false;
9555 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009556 ret2 = btrfs_end_transaction(trans);
9557 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009558out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009559 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009560 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009561
Chris Mason39279cc2007-06-12 06:35:45 -04009562 return ret;
9563}
9564
Christian Brauner549c7292021-01-21 14:19:43 +01009565static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9566 struct dentry *old_dentry, struct inode *new_dir,
9567 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009568{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009570 return -EINVAL;
9571
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 if (flags & RENAME_EXCHANGE)
9573 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9574 new_dentry);
9575
9576 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009577}
9578
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009579struct btrfs_delalloc_work {
9580 struct inode *inode;
9581 struct completion completion;
9582 struct list_head list;
9583 struct btrfs_work work;
9584};
9585
Miao Xie8ccf6f192012-10-25 09:28:04 +00009586static void btrfs_run_delalloc_work(struct btrfs_work *work)
9587{
9588 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009589 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009590
9591 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9592 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009593 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009594 filemap_flush(inode->i_mapping);
9595 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9596 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009597 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009598
Nikolay Borisov076da912018-04-23 10:54:16 +03009599 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009600 complete(&delalloc_work->completion);
9601}
9602
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009603static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009604{
9605 struct btrfs_delalloc_work *work;
9606
David Sterba100d5702015-12-08 14:39:32 +01009607 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009608 if (!work)
9609 return NULL;
9610
9611 init_completion(&work->completion);
9612 INIT_LIST_HEAD(&work->list);
9613 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009614 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009615
9616 return work;
9617}
9618
Chris Masond352ac62008-09-29 15:18:18 -04009619/*
9620 * some fairly slow code that needs optimization. This walks the list
9621 * of all the inodes with pending delalloc and forces them to disk.
9622 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009623static int start_delalloc_inodes(struct btrfs_root *root,
9624 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009625 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009626{
Chris Masonea8c2812008-08-04 23:17:27 -04009627 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009628 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009629 struct btrfs_delalloc_work *work, *next;
9630 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009631 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009632 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009633 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009634
Miao Xie8ccf6f192012-10-25 09:28:04 +00009635 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009636 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009637
Miao Xie573bfb72014-03-06 13:55:03 +08009638 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009639 spin_lock(&root->delalloc_lock);
9640 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009641 while (!list_empty(&splice)) {
9642 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009643 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009644
Miao Xieeb73c1b2013-05-15 07:48:22 +00009645 list_move_tail(&binode->delalloc_inodes,
9646 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009647
9648 if (in_reclaim_context &&
9649 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9650 continue;
9651
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009652 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009653 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009654 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009655 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009656 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009657 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009658
Ethan Lien3cd24c62018-11-01 14:49:03 +08009659 if (snapshot)
9660 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9661 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009662 if (full_flush) {
9663 work = btrfs_alloc_delalloc_work(inode);
9664 if (!work) {
9665 iput(inode);
9666 ret = -ENOMEM;
9667 goto out;
9668 }
9669 list_add_tail(&work->list, &works);
9670 btrfs_queue_work(root->fs_info->flush_workers,
9671 &work->work);
9672 } else {
9673 ret = sync_inode(inode, wbc);
9674 if (!ret &&
9675 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9676 &BTRFS_I(inode)->runtime_flags))
9677 ret = sync_inode(inode, wbc);
9678 btrfs_add_delayed_iput(inode);
9679 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009680 goto out;
9681 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009682 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009683 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009684 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009685 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009686
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009687out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009688 list_for_each_entry_safe(work, next, &works, list) {
9689 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009690 wait_for_completion(&work->completion);
9691 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009692 }
9693
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009694 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009695 spin_lock(&root->delalloc_lock);
9696 list_splice_tail(&splice, &root->delalloc_inodes);
9697 spin_unlock(&root->delalloc_lock);
9698 }
Miao Xie573bfb72014-03-06 13:55:03 +08009699 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009700 return ret;
9701}
9702
Filipe Mananaf9baa502021-04-22 12:08:05 +01009703int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009704{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009705 struct writeback_control wbc = {
9706 .nr_to_write = LONG_MAX,
9707 .sync_mode = WB_SYNC_NONE,
9708 .range_start = 0,
9709 .range_end = LLONG_MAX,
9710 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009712
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009714 return -EROFS;
9715
Filipe Mananaf9baa502021-04-22 12:08:05 +01009716 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009717}
9718
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009719int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009720 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009721{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009722 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009723 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009724 .sync_mode = WB_SYNC_NONE,
9725 .range_start = 0,
9726 .range_end = LLONG_MAX,
9727 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009728 struct btrfs_root *root;
9729 struct list_head splice;
9730 int ret;
9731
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009732 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009733 return -EROFS;
9734
9735 INIT_LIST_HEAD(&splice);
9736
Miao Xie573bfb72014-03-06 13:55:03 +08009737 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009738 spin_lock(&fs_info->delalloc_root_lock);
9739 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009740 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009741 /*
9742 * Reset nr_to_write here so we know that we're doing a full
9743 * flush.
9744 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009745 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009746 wbc.nr_to_write = LONG_MAX;
9747
Miao Xieeb73c1b2013-05-15 07:48:22 +00009748 root = list_first_entry(&splice, struct btrfs_root,
9749 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009750 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009751 BUG_ON(!root);
9752 list_move_tail(&root->delalloc_root,
9753 &fs_info->delalloc_roots);
9754 spin_unlock(&fs_info->delalloc_root_lock);
9755
Josef Bacike076ab2a2021-01-07 17:08:30 -05009756 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009757 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009758 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009759 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009760 spin_lock(&fs_info->delalloc_root_lock);
9761 }
9762 spin_unlock(&fs_info->delalloc_root_lock);
9763
Miao Xie6c255e62014-03-06 13:55:01 +08009764 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009765out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009766 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009767 spin_lock(&fs_info->delalloc_root_lock);
9768 list_splice_tail(&splice, &fs_info->delalloc_roots);
9769 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009770 }
Miao Xie573bfb72014-03-06 13:55:03 +08009771 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009772 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009773}
9774
Christian Brauner549c7292021-01-21 14:19:43 +01009775static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9776 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009777{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009778 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009779 struct btrfs_trans_handle *trans;
9780 struct btrfs_root *root = BTRFS_I(dir)->root;
9781 struct btrfs_path *path;
9782 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009783 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009784 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009785 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309786 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009787 int name_len;
9788 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009789 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009790 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009791 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009792
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009793 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009794 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009795 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009796
Josef Bacik9ed74f22009-09-11 16:12:44 -04009797 /*
9798 * 2 items for inode item and ref
9799 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009800 * 1 item for updating parent inode item
9801 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009802 * 1 item for xattr if selinux is on
9803 */
Filipe Manana9269d122015-12-31 18:16:29 +00009804 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009805 if (IS_ERR(trans))
9806 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009807
Nikolay Borisov543068a2020-12-07 17:32:33 +02009808 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009809 if (err)
9810 goto out_unlock;
9811
Josef Bacikaec74772008-07-24 12:12:38 -04009812 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009813 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9814 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009815 if (IS_ERR(inode)) {
9816 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009817 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009818 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009819 }
Chris Mason39279cc2007-06-12 06:35:45 -04009820
Casey Schauflerad19db72011-12-15 10:09:07 -05009821 /*
9822 * If the active LSM wants to access the inode during
9823 * d_instantiate it needs these. Smack checks to see
9824 * if the filesystem supports xattrs by looking at the
9825 * ops vector.
9826 */
9827 inode->i_fop = &btrfs_file_operations;
9828 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009829 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009830
9831 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9832 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009833 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009834
Chris Mason39279cc2007-06-12 06:35:45 -04009835 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009836 if (!path) {
9837 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009838 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009839 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009840 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009841 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009842 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009843 datasize = btrfs_file_extent_calc_inline_size(name_len);
9844 err = btrfs_insert_empty_item(trans, root, path, &key,
9845 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009846 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009847 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009848 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009849 }
Chris Mason5f39d392007-10-15 16:14:19 -04009850 leaf = path->nodes[0];
9851 ei = btrfs_item_ptr(leaf, path->slots[0],
9852 struct btrfs_file_extent_item);
9853 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9854 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009855 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009856 btrfs_set_file_extent_encryption(leaf, ei, 0);
9857 btrfs_set_file_extent_compression(leaf, ei, 0);
9858 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9859 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9860
Chris Mason39279cc2007-06-12 06:35:45 -04009861 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009862 write_extent_buffer(leaf, symname, ptr, name_len);
9863 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009864 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009865
Chris Mason39279cc2007-06-12 06:35:45 -04009866 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009867 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009868 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009869 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009870 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009871 /*
9872 * Last step, add directory indexes for our symlink inode. This is the
9873 * last step to avoid extra cleanup of these indexes if an error happens
9874 * elsewhere above.
9875 */
9876 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009877 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9878 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009879 if (err)
9880 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009881
Al Viro1e2e5472018-05-04 08:23:01 -04009882 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009883
9884out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009885 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009886 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009887 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009888 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009889 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009890 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009891 return err;
9892}
Chris Mason16432982008-04-10 10:23:21 -04009893
Filipe Manana8fccebf2020-09-08 11:27:20 +01009894static struct btrfs_trans_handle *insert_prealloc_file_extent(
9895 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009896 struct btrfs_inode *inode,
9897 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009898 u64 file_offset)
9899{
9900 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009901 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009902 struct btrfs_trans_handle *trans = trans_in;
9903 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009904 u64 start = ins->objectid;
9905 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009906 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009907 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009908
9909 memset(&stack_fi, 0, sizeof(stack_fi));
9910
9911 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9912 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9913 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9914 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9915 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9916 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9917 /* Encryption and other encoding is reserved and all 0 */
9918
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009919 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9920 if (qgroup_released < 0)
9921 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009922
9923 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009924 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009925 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009926 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009927 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009928 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009929 return trans;
9930 }
9931
9932 extent_info.disk_offset = start;
9933 extent_info.disk_len = len;
9934 extent_info.data_offset = 0;
9935 extent_info.data_len = len;
9936 extent_info.file_offset = file_offset;
9937 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009938 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009939 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009940 extent_info.insertions = 0;
9941
9942 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009943 if (!path) {
9944 ret = -ENOMEM;
9945 goto free_qgroup;
9946 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009947
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009948 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009949 file_offset + len - 1, &extent_info,
9950 &trans);
9951 btrfs_free_path(path);
9952 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009953 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009954 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009955
9956free_qgroup:
9957 /*
9958 * We have released qgroup data range at the beginning of the function,
9959 * and normally qgroup_released bytes will be freed when committing
9960 * transaction.
9961 * But if we error out early, we have to free what we have released
9962 * or we leak qgroup data reservation.
9963 */
9964 btrfs_qgroup_free_refroot(inode->root->fs_info,
9965 inode->root->root_key.objectid, qgroup_released,
9966 BTRFS_QGROUP_RSV_DATA);
9967 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009968}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009969
Josef Bacik0af3d002010-06-21 14:48:16 -04009970static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9971 u64 start, u64 num_bytes, u64 min_size,
9972 loff_t actual_len, u64 *alloc_hint,
9973 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009974{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009976 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9977 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009978 struct btrfs_root *root = BTRFS_I(inode)->root;
9979 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009980 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009981 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009982 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009983 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009984 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009985 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009986 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009987 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009988
Josef Bacik0af3d002010-06-21 14:48:16 -04009989 if (trans)
9990 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009991 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009992 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009993 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009994 /*
9995 * If we are severely fragmented we could end up with really
9996 * small allocations, so if the allocator is returning small
9997 * chunks lets make its job easier by only searching for those
9998 * sized chunks.
9999 */
10000 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010001 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10002 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010003 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010004 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010005
10006 /*
10007 * We've reserved this space, and thus converted it from
10008 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10009 * from here on out we will only need to clear our reservation
10010 * for the remaining unreserved area, so advance our
10011 * clear_offset by our extent size.
10012 */
10013 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010014
Josef Bacik0b670dc2015-09-23 17:11:16 -040010015 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010016 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10017 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010018 /*
10019 * Now that we inserted the prealloc extent we can finally
10020 * decrement the number of reservations in the block group.
10021 * If we did it before, we could race with relocation and have
10022 * relocation miss the reserved extent, making it fail later.
10023 */
10024 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010025 if (IS_ERR(trans)) {
10026 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010027 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010028 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010029 break;
10030 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010031
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010032 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010033 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010034
Josef Bacik5dc562c2012-08-17 13:14:17 -040010035 em = alloc_extent_map();
10036 if (!em) {
10037 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10038 &BTRFS_I(inode)->runtime_flags);
10039 goto next;
10040 }
10041
10042 em->start = cur_offset;
10043 em->orig_start = cur_offset;
10044 em->len = ins.offset;
10045 em->block_start = ins.objectid;
10046 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010047 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010048 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010049 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10050 em->generation = trans->transid;
10051
10052 while (1) {
10053 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010054 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010055 write_unlock(&em_tree->lock);
10056 if (ret != -EEXIST)
10057 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010058 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010059 cur_offset + ins.offset - 1,
10060 0);
10061 }
10062 free_extent_map(em);
10063next:
Yan Zhengd899e052008-10-30 14:25:28 -040010064 num_bytes -= ins.offset;
10065 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010066 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010067
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010068 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010069 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010070 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010071 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010072 (actual_len > inode->i_size) &&
10073 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010074 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010075 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010076 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010077 i_size = cur_offset;
10078 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010079 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010080 }
10081
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010082 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010083
10084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010085 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010086 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010087 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010088 break;
10089 }
Yan Zhengd899e052008-10-30 14:25:28 -040010090
Filipe Manana8fccebf2020-09-08 11:27:20 +010010091 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010092 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010093 trans = NULL;
10094 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010095 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010096 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010097 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010098 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010099 return ret;
10100}
10101
Josef Bacik0af3d002010-06-21 14:48:16 -040010102int btrfs_prealloc_file_range(struct inode *inode, int mode,
10103 u64 start, u64 num_bytes, u64 min_size,
10104 loff_t actual_len, u64 *alloc_hint)
10105{
10106 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10107 min_size, actual_len, alloc_hint,
10108 NULL);
10109}
10110
10111int btrfs_prealloc_file_range_trans(struct inode *inode,
10112 struct btrfs_trans_handle *trans, int mode,
10113 u64 start, u64 num_bytes, u64 min_size,
10114 loff_t actual_len, u64 *alloc_hint)
10115{
10116 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10117 min_size, actual_len, alloc_hint, trans);
10118}
10119
Chris Masone6dcd2d2008-07-17 12:53:50 -040010120static int btrfs_set_page_dirty(struct page *page)
10121{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010122 return __set_page_dirty_nobuffers(page);
10123}
10124
Christian Brauner549c7292021-01-21 14:19:43 +010010125static int btrfs_permission(struct user_namespace *mnt_userns,
10126 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010127{
Li Zefanb83cc962010-12-20 16:04:08 +080010128 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010129 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010130
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010131 if (mask & MAY_WRITE &&
10132 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10133 if (btrfs_root_readonly(root))
10134 return -EROFS;
10135 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10136 return -EACCES;
10137 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010138 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010139}
Chris Mason39279cc2007-06-12 06:35:45 -040010140
Christian Brauner549c7292021-01-21 14:19:43 +010010141static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10142 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010143{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010144 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010145 struct btrfs_trans_handle *trans;
10146 struct btrfs_root *root = BTRFS_I(dir)->root;
10147 struct inode *inode = NULL;
10148 u64 objectid;
10149 u64 index;
10150 int ret = 0;
10151
10152 /*
10153 * 5 units required for adding orphan entry
10154 */
10155 trans = btrfs_start_transaction(root, 5);
10156 if (IS_ERR(trans))
10157 return PTR_ERR(trans);
10158
Nikolay Borisov543068a2020-12-07 17:32:33 +020010159 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010160 if (ret)
10161 goto out;
10162
10163 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010164 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010165 if (IS_ERR(inode)) {
10166 ret = PTR_ERR(inode);
10167 inode = NULL;
10168 goto out;
10169 }
10170
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010171 inode->i_fop = &btrfs_file_operations;
10172 inode->i_op = &btrfs_file_inode_operations;
10173
10174 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010175
Chris Masonb0d5d102014-09-08 13:08:51 -070010176 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10177 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010178 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010179
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010180 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010181 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010182 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010183 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010184 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010185 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010186
Filipe Manana5762b5c2014-08-01 00:10:32 +010010187 /*
10188 * We set number of links to 0 in btrfs_new_inode(), and here we set
10189 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10190 * through:
10191 *
10192 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10193 */
10194 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010195 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010196 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010197 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010198out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010199 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010200 if (ret && inode)
10201 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010202 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010203 return ret;
10204}
10205
David Sterba5cdc84b2018-07-18 20:32:52 +020010206void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010207{
David Sterba5cdc84b2018-07-18 20:32:52 +020010208 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010209 unsigned long index = start >> PAGE_SHIFT;
10210 unsigned long end_index = end >> PAGE_SHIFT;
10211 struct page *page;
10212
10213 while (index <= end_index) {
10214 page = find_get_page(inode->i_mapping, index);
10215 ASSERT(page); /* Pages should be in the extent_io_tree */
10216 set_page_writeback(page);
10217 put_page(page);
10218 index++;
10219 }
10220}
10221
Omar Sandovaled46ff32016-11-03 10:28:14 -070010222#ifdef CONFIG_SWAP
10223/*
10224 * Add an entry indicating a block group or device which is pinned by a
10225 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10226 * negative errno on failure.
10227 */
10228static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10229 bool is_block_group)
10230{
10231 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10232 struct btrfs_swapfile_pin *sp, *entry;
10233 struct rb_node **p;
10234 struct rb_node *parent = NULL;
10235
10236 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10237 if (!sp)
10238 return -ENOMEM;
10239 sp->ptr = ptr;
10240 sp->inode = inode;
10241 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010242 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010243
10244 spin_lock(&fs_info->swapfile_pins_lock);
10245 p = &fs_info->swapfile_pins.rb_node;
10246 while (*p) {
10247 parent = *p;
10248 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10249 if (sp->ptr < entry->ptr ||
10250 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10251 p = &(*p)->rb_left;
10252 } else if (sp->ptr > entry->ptr ||
10253 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10254 p = &(*p)->rb_right;
10255 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010256 if (is_block_group)
10257 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010258 spin_unlock(&fs_info->swapfile_pins_lock);
10259 kfree(sp);
10260 return 1;
10261 }
10262 }
10263 rb_link_node(&sp->node, parent, p);
10264 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10265 spin_unlock(&fs_info->swapfile_pins_lock);
10266 return 0;
10267}
10268
10269/* Free all of the entries pinned by this swapfile. */
10270static void btrfs_free_swapfile_pins(struct inode *inode)
10271{
10272 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10273 struct btrfs_swapfile_pin *sp;
10274 struct rb_node *node, *next;
10275
10276 spin_lock(&fs_info->swapfile_pins_lock);
10277 node = rb_first(&fs_info->swapfile_pins);
10278 while (node) {
10279 next = rb_next(node);
10280 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10281 if (sp->inode == inode) {
10282 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010283 if (sp->is_block_group) {
10284 btrfs_dec_block_group_swap_extents(sp->ptr,
10285 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010286 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010287 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010288 kfree(sp);
10289 }
10290 node = next;
10291 }
10292 spin_unlock(&fs_info->swapfile_pins_lock);
10293}
10294
10295struct btrfs_swap_info {
10296 u64 start;
10297 u64 block_start;
10298 u64 block_len;
10299 u64 lowest_ppage;
10300 u64 highest_ppage;
10301 unsigned long nr_pages;
10302 int nr_extents;
10303};
10304
10305static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10306 struct btrfs_swap_info *bsi)
10307{
10308 unsigned long nr_pages;
10309 u64 first_ppage, first_ppage_reported, next_ppage;
10310 int ret;
10311
10312 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10313 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10314 PAGE_SIZE) >> PAGE_SHIFT;
10315
10316 if (first_ppage >= next_ppage)
10317 return 0;
10318 nr_pages = next_ppage - first_ppage;
10319
10320 first_ppage_reported = first_ppage;
10321 if (bsi->start == 0)
10322 first_ppage_reported++;
10323 if (bsi->lowest_ppage > first_ppage_reported)
10324 bsi->lowest_ppage = first_ppage_reported;
10325 if (bsi->highest_ppage < (next_ppage - 1))
10326 bsi->highest_ppage = next_ppage - 1;
10327
10328 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10329 if (ret < 0)
10330 return ret;
10331 bsi->nr_extents += ret;
10332 bsi->nr_pages += nr_pages;
10333 return 0;
10334}
10335
10336static void btrfs_swap_deactivate(struct file *file)
10337{
10338 struct inode *inode = file_inode(file);
10339
10340 btrfs_free_swapfile_pins(inode);
10341 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10342}
10343
10344static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10345 sector_t *span)
10346{
10347 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010348 struct btrfs_root *root = BTRFS_I(inode)->root;
10349 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010350 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10351 struct extent_state *cached_state = NULL;
10352 struct extent_map *em = NULL;
10353 struct btrfs_device *device = NULL;
10354 struct btrfs_swap_info bsi = {
10355 .lowest_ppage = (sector_t)-1ULL,
10356 };
10357 int ret = 0;
10358 u64 isize;
10359 u64 start;
10360
10361 /*
10362 * If the swap file was just created, make sure delalloc is done. If the
10363 * file changes again after this, the user is doing something stupid and
10364 * we don't really care.
10365 */
10366 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10367 if (ret)
10368 return ret;
10369
10370 /*
10371 * The inode is locked, so these flags won't change after we check them.
10372 */
10373 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10374 btrfs_warn(fs_info, "swapfile must not be compressed");
10375 return -EINVAL;
10376 }
10377 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10378 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10379 return -EINVAL;
10380 }
10381 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10382 btrfs_warn(fs_info, "swapfile must not be checksummed");
10383 return -EINVAL;
10384 }
10385
10386 /*
10387 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010388 * under us. The exclop protection makes sure they aren't running/won't
10389 * run concurrently while we are mapping the swap extents, and
10390 * fs_info->swapfile_pins prevents them from running while the swap
10391 * file is active and moving the extents. Note that this also prevents
10392 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010393 * really worth the trouble to allow it.
10394 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010395 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010396 btrfs_warn(fs_info,
10397 "cannot activate swapfile while exclusive operation is running");
10398 return -EBUSY;
10399 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010400
10401 /*
10402 * Prevent snapshot creation while we are activating the swap file.
10403 * We do not want to race with snapshot creation. If snapshot creation
10404 * already started before we bumped nr_swapfiles from 0 to 1 and
10405 * completes before the first write into the swap file after it is
10406 * activated, than that write would fallback to COW.
10407 */
10408 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10409 btrfs_exclop_finish(fs_info);
10410 btrfs_warn(fs_info,
10411 "cannot activate swapfile because snapshot creation is in progress");
10412 return -EINVAL;
10413 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010414 /*
10415 * Snapshots can create extents which require COW even if NODATACOW is
10416 * set. We use this counter to prevent snapshots. We must increment it
10417 * before walking the extents because we don't want a concurrent
10418 * snapshot to run after we've already checked the extents.
10419 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010420 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010421
10422 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10423
10424 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10425 start = 0;
10426 while (start < isize) {
10427 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010428 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010429 u64 len = isize - start;
10430
Omar Sandoval39b07b52019-12-02 17:34:23 -080010431 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010432 if (IS_ERR(em)) {
10433 ret = PTR_ERR(em);
10434 goto out;
10435 }
10436
10437 if (em->block_start == EXTENT_MAP_HOLE) {
10438 btrfs_warn(fs_info, "swapfile must not have holes");
10439 ret = -EINVAL;
10440 goto out;
10441 }
10442 if (em->block_start == EXTENT_MAP_INLINE) {
10443 /*
10444 * It's unlikely we'll ever actually find ourselves
10445 * here, as a file small enough to fit inline won't be
10446 * big enough to store more than the swap header, but in
10447 * case something changes in the future, let's catch it
10448 * here rather than later.
10449 */
10450 btrfs_warn(fs_info, "swapfile must not be inline");
10451 ret = -EINVAL;
10452 goto out;
10453 }
10454 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10455 btrfs_warn(fs_info, "swapfile must not be compressed");
10456 ret = -EINVAL;
10457 goto out;
10458 }
10459
10460 logical_block_start = em->block_start + (start - em->start);
10461 len = min(len, em->len - (start - em->start));
10462 free_extent_map(em);
10463 em = NULL;
10464
Boris Burkova84d5d42020-08-18 11:00:05 -070010465 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010466 if (ret < 0) {
10467 goto out;
10468 } else if (ret) {
10469 ret = 0;
10470 } else {
10471 btrfs_warn(fs_info,
10472 "swapfile must not be copy-on-write");
10473 ret = -EINVAL;
10474 goto out;
10475 }
10476
10477 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10478 if (IS_ERR(em)) {
10479 ret = PTR_ERR(em);
10480 goto out;
10481 }
10482
10483 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10484 btrfs_warn(fs_info,
10485 "swapfile must have single data profile");
10486 ret = -EINVAL;
10487 goto out;
10488 }
10489
10490 if (device == NULL) {
10491 device = em->map_lookup->stripes[0].dev;
10492 ret = btrfs_add_swapfile_pin(inode, device, false);
10493 if (ret == 1)
10494 ret = 0;
10495 else if (ret)
10496 goto out;
10497 } else if (device != em->map_lookup->stripes[0].dev) {
10498 btrfs_warn(fs_info, "swapfile must be on one device");
10499 ret = -EINVAL;
10500 goto out;
10501 }
10502
10503 physical_block_start = (em->map_lookup->stripes[0].physical +
10504 (logical_block_start - em->start));
10505 len = min(len, em->len - (logical_block_start - em->start));
10506 free_extent_map(em);
10507 em = NULL;
10508
10509 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10510 if (!bg) {
10511 btrfs_warn(fs_info,
10512 "could not find block group containing swapfile");
10513 ret = -EINVAL;
10514 goto out;
10515 }
10516
Filipe Manana195a49e2021-02-05 12:55:37 +000010517 if (!btrfs_inc_block_group_swap_extents(bg)) {
10518 btrfs_warn(fs_info,
10519 "block group for swapfile at %llu is read-only%s",
10520 bg->start,
10521 atomic_read(&fs_info->scrubs_running) ?
10522 " (scrub running)" : "");
10523 btrfs_put_block_group(bg);
10524 ret = -EINVAL;
10525 goto out;
10526 }
10527
Omar Sandovaled46ff32016-11-03 10:28:14 -070010528 ret = btrfs_add_swapfile_pin(inode, bg, true);
10529 if (ret) {
10530 btrfs_put_block_group(bg);
10531 if (ret == 1)
10532 ret = 0;
10533 else
10534 goto out;
10535 }
10536
10537 if (bsi.block_len &&
10538 bsi.block_start + bsi.block_len == physical_block_start) {
10539 bsi.block_len += len;
10540 } else {
10541 if (bsi.block_len) {
10542 ret = btrfs_add_swap_extent(sis, &bsi);
10543 if (ret)
10544 goto out;
10545 }
10546 bsi.start = start;
10547 bsi.block_start = physical_block_start;
10548 bsi.block_len = len;
10549 }
10550
10551 start += len;
10552 }
10553
10554 if (bsi.block_len)
10555 ret = btrfs_add_swap_extent(sis, &bsi);
10556
10557out:
10558 if (!IS_ERR_OR_NULL(em))
10559 free_extent_map(em);
10560
10561 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10562
10563 if (ret)
10564 btrfs_swap_deactivate(file);
10565
Filipe Mananadd0734f2021-02-05 12:55:38 +000010566 btrfs_drew_write_unlock(&root->snapshot_lock);
10567
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010568 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010569
10570 if (ret)
10571 return ret;
10572
10573 if (device)
10574 sis->bdev = device->bdev;
10575 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10576 sis->max = bsi.nr_pages;
10577 sis->pages = bsi.nr_pages - 1;
10578 sis->highest_bit = bsi.nr_pages - 1;
10579 return bsi.nr_extents;
10580}
10581#else
10582static void btrfs_swap_deactivate(struct file *file)
10583{
10584}
10585
10586static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10587 sector_t *span)
10588{
10589 return -EOPNOTSUPP;
10590}
10591#endif
10592
Filipe Manana2766ff62020-11-04 11:07:34 +000010593/*
10594 * Update the number of bytes used in the VFS' inode. When we replace extents in
10595 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10596 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10597 * always get a correct value.
10598 */
10599void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10600 const u64 add_bytes,
10601 const u64 del_bytes)
10602{
10603 if (add_bytes == del_bytes)
10604 return;
10605
10606 spin_lock(&inode->lock);
10607 if (del_bytes > 0)
10608 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10609 if (add_bytes > 0)
10610 inode_add_bytes(&inode->vfs_inode, add_bytes);
10611 spin_unlock(&inode->lock);
10612}
10613
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010614static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010615 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010616 .lookup = btrfs_lookup,
10617 .create = btrfs_create,
10618 .unlink = btrfs_unlink,
10619 .link = btrfs_link,
10620 .mkdir = btrfs_mkdir,
10621 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010622 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010623 .symlink = btrfs_symlink,
10624 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010625 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010626 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010627 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010628 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010629 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010630 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010631 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010632 .fileattr_get = btrfs_fileattr_get,
10633 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010634};
Yan, Zheng76dda932009-09-21 16:00:26 -040010635
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010636static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010637 .llseek = generic_file_llseek,
10638 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010639 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010640 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010641 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010642#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010643 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010644#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010645 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010646 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010647};
10648
Chris Mason35054392009-01-21 13:11:13 -050010649/*
10650 * btrfs doesn't support the bmap operation because swapfiles
10651 * use bmap to make a mapping of extents in the file. They assume
10652 * these extents won't change over the life of the file and they
10653 * use the bmap result to do IO directly to the drive.
10654 *
10655 * the btrfs bmap call would return logical addresses that aren't
10656 * suitable for IO and they also will change frequently as COW
10657 * operations happen. So, swapfile + btrfs == corruption.
10658 *
10659 * For now we're avoiding this by dropping bmap.
10660 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010661static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010662 .readpage = btrfs_readpage,
10663 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010664 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010665 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010666 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010667 .invalidatepage = btrfs_invalidatepage,
10668 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010669#ifdef CONFIG_MIGRATION
10670 .migratepage = btrfs_migratepage,
10671#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010672 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010673 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010674 .swap_activate = btrfs_swap_activate,
10675 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010676};
10677
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010678static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010679 .getattr = btrfs_getattr,
10680 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010681 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010682 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010683 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010684 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010685 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010686 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010687 .fileattr_get = btrfs_fileattr_get,
10688 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010689};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010690static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010691 .getattr = btrfs_getattr,
10692 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010693 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010694 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010695 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010696 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010697 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010698};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010699static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010700 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010701 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010702 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010703 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010704 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010705 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010706};
Yan, Zheng76dda932009-09-21 16:00:26 -040010707
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010708const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010709 .d_delete = btrfs_dentry_delete,
10710};