blob: b6caa35ae03a6dacb9e86694a9543637fef9850c [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
105 */
106int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
107{
108 if (ilock_flags & BTRFS_ILOCK_SHARED) {
109 if (ilock_flags & BTRFS_ILOCK_TRY) {
110 if (!inode_trylock_shared(inode))
111 return -EAGAIN;
112 else
113 return 0;
114 }
115 inode_lock_shared(inode);
116 } else {
117 if (ilock_flags & BTRFS_ILOCK_TRY) {
118 if (!inode_trylock(inode))
119 return -EAGAIN;
120 else
121 return 0;
122 }
123 inode_lock(inode);
124 }
125 return 0;
126}
127
128/*
129 * btrfs_inode_unlock - unock inode i_rwsem
130 *
131 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
132 * to decide whether the lock acquired is shared or exclusive.
133 */
134void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
135{
136 if (ilock_flags & BTRFS_ILOCK_SHARED)
137 inode_unlock_shared(inode);
138 else
139 inode_unlock(inode);
140}
141
142/*
Qu Wenruo524272602017-03-08 10:25:52 +0800143 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100144 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800145 *
146 * NOTE: caller must ensure that when an error happens, it can not call
147 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
148 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
149 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200150 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800151 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300152static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200153 struct page *locked_page,
154 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800155{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900156 unsigned long index = offset >> PAGE_SHIFT;
157 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200158 u64 page_start = page_offset(locked_page);
159 u64 page_end = page_start + PAGE_SIZE - 1;
160
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900161 struct page *page;
162
163 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300164 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900165 index++;
166 if (!page)
167 continue;
168 ClearPagePrivate2(page);
169 put_page(page);
170 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200171
172 /*
173 * In case this page belongs to the delalloc range being instantiated
174 * then skip it, since the first page of a range is going to be
175 * properly cleaned up by the caller of run_delalloc_range
176 */
177 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
178 offset += PAGE_SIZE;
179 bytes -= PAGE_SIZE;
180 }
181
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300182 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800183}
184
Eric Sandeen48a3b632013-04-25 20:41:01 +0000185static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400186
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000187static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500188 struct inode *inode, struct inode *dir,
189 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500190{
191 int err;
192
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000193 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500194 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500195 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500196 return err;
197}
198
Chris Masond352ac62008-09-29 15:18:18 -0400199/*
Chris Masonc8b97812008-10-29 14:49:59 -0400200 * this does all the hard work for inserting an inline extent into
201 * the btree. The caller should have done a btrfs_drop_extents so that
202 * no overlapping inline items exist in the btree
203 */
Chris Mason40f76582014-05-21 13:35:51 -0700204static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000205 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root, struct inode *inode,
207 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000208 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400209 struct page **compressed_pages)
210{
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct extent_buffer *leaf;
212 struct page *page = NULL;
213 char *kaddr;
214 unsigned long ptr;
215 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 int ret;
217 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400218 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400219
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800220 ASSERT((compressed_size > 0 && compressed_pages) ||
221 (compressed_size == 0 && !compressed_pages));
222
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400224 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400225
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000226 if (!extent_inserted) {
227 struct btrfs_key key;
228 size_t datasize;
229
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200230 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000231 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200232 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000233
234 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000235 ret = btrfs_insert_empty_item(trans, root, path, &key,
236 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200237 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000238 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400239 }
240 leaf = path->nodes[0];
241 ei = btrfs_item_ptr(leaf, path->slots[0],
242 struct btrfs_file_extent_item);
243 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
244 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
245 btrfs_set_file_extent_encryption(leaf, ei, 0);
246 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
247 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
248 ptr = btrfs_file_extent_inline_start(ei);
249
Li Zefan261507a02010-12-17 14:21:50 +0800250 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400251 struct page *cpage;
252 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500253 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400254 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500255 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300256 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Cong Wang7ac687d2011-11-25 23:14:28 +0800258 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800260 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400261
262 i++;
263 ptr += cur_size;
264 compressed_size -= cur_size;
265 }
266 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800267 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400268 } else {
269 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300270 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400271 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800272 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100273 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400274 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800275 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300276 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400277 }
278 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000279 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400280
Yan, Zhengc2167752009-11-12 09:34:21 +0000281 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500282 * We align size to sectorsize for inline extents just for simplicity
283 * sake.
284 */
285 size = ALIGN(size, root->fs_info->sectorsize);
286 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
287 if (ret)
288 goto fail;
289
290 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000291 * we're an inline extent, so nobody can
292 * extend the file past i_size without locking
293 * a page we already have locked.
294 *
295 * We must do any isize and inode updates
296 * before we unlock the pages. Otherwise we
297 * could end up racing with unlink.
298 */
Chris Masonc8b97812008-10-29 14:49:59 -0400299 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400300fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200301 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400302}
303
304
305/*
306 * conditionally insert an inline extent into the file. This
307 * does the checks required to make sure the data is small enough
308 * to fit as an inline extent.
309 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300310static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400311 u64 end, size_t compressed_size,
312 int compress_type,
313 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400314{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000315 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300316 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400317 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400318 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300319 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400320 u64 actual_end = min(end + 1, isize);
321 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400322 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400323 u64 data_len = inline_len;
324 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000325 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400326
327 if (compressed_size)
328 data_len = compressed_size;
329
330 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400331 actual_end > fs_info->sectorsize ||
332 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400333 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400334 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400335 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400336 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400337 return 1;
338 }
339
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000340 path = btrfs_alloc_path();
341 if (!path)
342 return -ENOMEM;
343
Josef Bacik00361582013-08-14 14:02:47 -0400344 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000345 if (IS_ERR(trans)) {
346 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400347 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000348 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300349 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400350
Filipe Manana5893dfb2020-11-04 11:07:32 +0000351 drop_args.path = path;
352 drop_args.start = start;
353 drop_args.end = aligned_end;
354 drop_args.drop_cache = true;
355 drop_args.replace_extent = true;
356
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000357 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000358 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000359 compressed_size);
360 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000361 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000362 inline_len);
363
Filipe Manana5893dfb2020-11-04 11:07:32 +0000364 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400365 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400366 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400367 goto out;
368 }
Chris Masonc8b97812008-10-29 14:49:59 -0400369
370 if (isize > actual_end)
371 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000372 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300373 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400374 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000375 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400376 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400377 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400378 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400379 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400380 ret = 1;
381 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100382 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400383
Filipe Manana2766ff62020-11-04 11:07:34 +0000384 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200385 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000386 if (ret && ret != -ENOSPC) {
387 btrfs_abort_transaction(trans, ret);
388 goto out;
389 } else if (ret == -ENOSPC) {
390 ret = 1;
391 goto out;
392 }
393
Nikolay Borisova0349402020-06-03 08:55:12 +0300394 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400395out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800396 /*
397 * Don't forget to free the reserved space, as for inlined extent
398 * it won't count as data extent, free them directly here.
399 * And at reserve time, it's always aligned to page size, so
400 * just free one page here.
401 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300402 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000403 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400404 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400405 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400406}
407
Chris Mason771ed682008-11-06 22:02:51 -0500408struct async_extent {
409 u64 start;
410 u64 ram_size;
411 u64 compressed_size;
412 struct page **pages;
413 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800414 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500415 struct list_head list;
416};
417
Nikolay Borisov97db1202019-03-12 17:20:24 +0200418struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500420 struct page *locked_page;
421 u64 start;
422 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600423 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500424 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700425 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500426 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200427 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500428};
429
Nikolay Borisov97db1202019-03-12 17:20:24 +0200430struct async_cow {
431 /* Number of chunks in flight; must be first in the structure */
432 atomic_t num_chunks;
433 struct async_chunk chunks[];
434};
435
436static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500437 u64 start, u64 ram_size,
438 u64 compressed_size,
439 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800440 unsigned long nr_pages,
441 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500442{
443 struct async_extent *async_extent;
444
445 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100446 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500447 async_extent->start = start;
448 async_extent->ram_size = ram_size;
449 async_extent->compressed_size = compressed_size;
450 async_extent->pages = pages;
451 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800452 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500453 list_add_tail(&async_extent->list, &cow->extents);
454 return 0;
455}
456
Qu Wenruo42c16da2019-07-01 05:12:46 +0000457/*
458 * Check if the inode has flags compatible with compression
459 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300460static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000461{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300462 if (inode->flags & BTRFS_INODE_NODATACOW ||
463 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000464 return false;
465 return true;
466}
467
468/*
469 * Check if the inode needs to be submitted to compression, based on mount
470 * options, defragmentation, properties or heuristics.
471 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300472static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
473 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800474{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300475 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800476
Nikolay Borisov808a1292020-06-03 08:55:27 +0300477 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000478 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
479 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300480 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000481 return 0;
482 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800483 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800485 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200486 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300487 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200488 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800489 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300490 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800491 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300493 inode->flags & BTRFS_INODE_COMPRESS ||
494 inode->prop_compress)
495 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800496 return 0;
497}
498
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200499static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800500 u64 start, u64 end, u64 num_bytes, u64 small_write)
501{
502 /* If this is a small write inside eof, kick off a defrag */
503 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200504 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800505 btrfs_add_inode_defrag(NULL, inode);
506}
507
Chris Masonc8b97812008-10-29 14:49:59 -0400508/*
Chris Mason771ed682008-11-06 22:02:51 -0500509 * we create compressed extents in two phases. The first
510 * phase compresses a range of pages that have already been
511 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400512 *
Chris Mason771ed682008-11-06 22:02:51 -0500513 * This is done inside an ordered work queue, and the compression
514 * is spread across many cpus. The actual IO submission is step
515 * two, and the ordered work queue takes care of making sure that
516 * happens in the same order things were put onto the queue by
517 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 *
Chris Mason771ed682008-11-06 22:02:51 -0500519 * If this code finds it can't get good compression, it puts an
520 * entry onto the work queue to write the uncompressed bytes. This
521 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300522 * are written in the same order that the flusher thread sent them
523 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400524 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300525static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400526{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200527 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400528 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400529 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200530 u64 start = async_chunk->start;
531 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400532 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400533 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400534 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400535 struct page **pages = NULL;
536 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400537 unsigned long total_compressed = 0;
538 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400539 int i;
540 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400541 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300542 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400543 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400544
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200545 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
546 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400547
Josef Bacikd98da492019-10-11 09:03:54 -0400548 /*
549 * We need to save i_size before now because it could change in between
550 * us evaluating the size and assigning it. This is because we lock and
551 * unlock the page in truncate and fallocate, and then modify the i_size
552 * later on.
553 *
554 * The barriers are to emulate READ_ONCE, remove that once i_size_read
555 * does that for us.
556 */
557 barrier();
558 i_size = i_size_read(inode);
559 barrier();
560 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400561again:
562 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300563 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100564 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
565 nr_pages = min_t(unsigned long, nr_pages,
566 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400567
Chris Masonf03d9301f2009-02-04 09:31:06 -0500568 /*
569 * we don't want to send crud past the end of i_size through
570 * compression, that's just a waste of CPU time. So, if the
571 * end of the file is before the start of our current
572 * requested range of bytes, we bail out to the uncompressed
573 * cleanup code that can deal with all of this.
574 *
575 * It isn't really the fastest way to fix things, but this is a
576 * very uncommon corner.
577 */
578 if (actual_end <= start)
579 goto cleanup_and_bail_uncompressed;
580
Chris Masonc8b97812008-10-29 14:49:59 -0400581 total_compressed = actual_end - start;
582
Shilong Wang4bcbb332014-10-07 18:44:35 -0400583 /*
584 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400585 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400586 */
587 if (total_compressed <= blocksize &&
588 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
589 goto cleanup_and_bail_uncompressed;
590
David Sterba069eac72017-02-14 19:36:54 +0100591 total_compressed = min_t(unsigned long, total_compressed,
592 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400593 total_in = 0;
594 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400595
Chris Mason771ed682008-11-06 22:02:51 -0500596 /*
597 * we do compression for mount -o compress and when the
598 * inode has not been flagged as nocompress. This flag can
599 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400600 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300601 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400602 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100603 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800604 if (!pages) {
605 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100606 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800607 goto cont;
608 }
Chris Mason179e29e2007-11-01 11:28:41 -0400609
David Sterbaeec63c62017-07-17 19:41:31 +0200610 if (BTRFS_I(inode)->defrag_compress)
611 compress_type = BTRFS_I(inode)->defrag_compress;
612 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200613 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800614
Chris Mason4adaa612013-03-26 13:07:00 -0400615 /*
616 * we need to call clear_page_dirty_for_io on each
617 * page in the range. Otherwise applications with the file
618 * mmap'd can wander in and change the page contents while
619 * we are compressing them.
620 *
621 * If the compression fails for any reason, we set the pages
622 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300623 *
624 * Note that the remaining part is redirtied, the start pointer
625 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400626 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300627 if (!redirty) {
628 extent_range_clear_dirty_for_io(inode, start, end);
629 redirty = 1;
630 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200631
632 /* Compression level is applied here and only here */
633 ret = btrfs_compress_pages(
634 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800635 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100636 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800638 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100639 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400640
641 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100642 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100643 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400644 char *kaddr;
645
646 /* zero the tail end of the last page, we might be
647 * sending it down to disk
648 */
649 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800650 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400651 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300652 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800653 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400654 }
655 will_compress = 1;
656 }
657 }
Li Zefan560f7d72011-09-08 10:22:01 +0800658cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400659 if (start == 0) {
660 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300661 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400662 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500663 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400664 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300665 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
666 0, BTRFS_COMPRESS_NONE,
667 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400668 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500669 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300670 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000671 total_compressed,
672 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100674 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400675 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400676 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
677 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100678 unsigned long page_error_op;
679
Filipe Mananae6eb4312014-10-10 10:45:12 +0100680 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400681
Chris Mason771ed682008-11-06 22:02:51 -0500682 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100683 * inline extent creation worked or returned error,
684 * we don't need to create any more async work items.
685 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400686 *
687 * We use DO_ACCOUNTING here because we need the
688 * delalloc_release_metadata to be done _after_ we drop
689 * our outstanding extent for clearing delalloc for this
690 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500691 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300692 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
693 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300694 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800695 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800696 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100697 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400698 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300699
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800700 /*
701 * Ensure we only free the compressed pages if we have
702 * them allocated, as we can still reach here with
703 * inode_need_compress() == false.
704 */
705 if (pages) {
706 for (i = 0; i < nr_pages; i++) {
707 WARN_ON(pages[i]->mapping);
708 put_page(pages[i]);
709 }
710 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300711 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300712 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400713 }
714 }
715
716 if (will_compress) {
717 /*
718 * we aren't doing an inline extent round the compressed size
719 * up to a block size boundary so the allocator does sane
720 * things
721 */
Qu Wenruofda28322013-02-26 08:10:22 +0000722 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400723
724 /*
725 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300726 * win, compare the page count read with the blocks on disk,
727 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400728 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300729 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300730 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300731 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732
733 /*
734 * The async work queues will take care of doing actual
735 * allocation on disk for these compressed pages, and
736 * will submit them to the elevator.
737 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100739 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 compress_type);
741
Timofey Titovets11708622017-10-03 18:06:01 +0300742 if (start + total_in < end) {
743 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700744 pages = NULL;
745 cond_resched();
746 goto again;
747 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300748 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400749 }
750 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700751 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400752 /*
753 * the compression code ran but failed to make things smaller,
754 * free any pages it allocated and our page pointer array
755 */
David Sterba4d3a8002017-02-14 19:04:07 +0100756 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400757 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300758 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400759 }
760 kfree(pages);
761 pages = NULL;
762 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100763 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400764
765 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200767 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500768 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500769 }
Chris Masonc8b97812008-10-29 14:49:59 -0400770 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500771cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700772 /*
773 * No compression, but we still need to write the pages in the file
774 * we've been given so far. redirty the locked page if it corresponds
775 * to our extent and set things up for the async work queue to run
776 * cow_file_range to do the normal delalloc dance.
777 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700778 if (async_chunk->locked_page &&
779 (page_offset(async_chunk->locked_page) >= start &&
780 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200781 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700782 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700783 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784
785 if (redirty)
786 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200787 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700788 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300789 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500790
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300791 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500792}
Chris Mason771ed682008-11-06 22:02:51 -0500793
Filipe Manana40ae8372014-10-06 22:14:24 +0100794static void free_async_extent_pages(struct async_extent *async_extent)
795{
796 int i;
797
798 if (!async_extent->pages)
799 return;
800
801 for (i = 0; i < async_extent->nr_pages; i++) {
802 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300803 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100804 }
805 kfree(async_extent->pages);
806 async_extent->nr_pages = 0;
807 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500808}
809
810/*
811 * phase two of compressed writeback. This is the ordered portion
812 * of the code, which only gets called in the order the work was
813 * queued. We walk all the async extents created by compress_file_range
814 * and send them down to the disk.
815 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200816static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500817{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300818 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
819 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct async_extent *async_extent;
821 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500822 struct btrfs_key ins;
823 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300824 struct btrfs_root *root = inode->root;
825 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500826 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500827
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200829 while (!list_empty(&async_chunk->extents)) {
830 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500831 struct async_extent, list);
832 list_del(&async_extent->list);
833
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500834retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200835 lock_extent(io_tree, async_extent->start,
836 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500837 /* did the compression code fall back to uncompressed IO? */
838 if (!async_extent->pages) {
839 int page_started = 0;
840 unsigned long nr_written = 0;
841
Chris Mason771ed682008-11-06 22:02:51 -0500842 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300843 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500844 async_extent->start,
845 async_extent->start +
846 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300847 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100849 /* JDM XXX */
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 /*
852 * if page_started, cow_file_range inserted an
853 * inline extent and took care of all the unlocking
854 * and IO for us. Otherwise, we need to submit
855 * all those pages down to the drive.
856 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500857 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300858 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200859 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500860 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500861 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500862 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700863 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200864 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500865 kfree(async_extent);
866 cond_resched();
867 continue;
868 }
869
Wang Xiaoguang18513092016-07-25 15:51:40 +0800870 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500871 async_extent->compressed_size,
872 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800873 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500874 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400877 if (ret == -ENOSPC) {
878 unlock_extent(io_tree, async_extent->start,
879 async_extent->start +
880 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800881
882 /*
883 * we need to redirty the pages if we decide to
884 * fallback to uncompressed IO, otherwise we
885 * will not submit these pages down to lower
886 * layers.
887 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300888 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800889 async_extent->start,
890 async_extent->start +
891 async_extent->ram_size - 1);
892
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400894 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500895 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500896 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000897 /*
898 * here we're doing allocation and writeback of the
899 * compressed pages
900 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300901 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800902 async_extent->ram_size, /* len */
903 async_extent->start, /* orig_start */
904 ins.objectid, /* block_start */
905 ins.offset, /* block_len */
906 ins.offset, /* orig_block_len */
907 async_extent->ram_size, /* ram_bytes */
908 async_extent->compress_type,
909 BTRFS_ORDERED_COMPRESSED);
910 if (IS_ERR(em))
911 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500912 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800913 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300915 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800916 async_extent->start,
917 ins.objectid,
918 async_extent->ram_size,
919 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800920 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100921 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300922 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100923 async_extent->start +
924 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500925 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100926 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400927 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500928
Chris Mason771ed682008-11-06 22:02:51 -0500929 /*
930 * clear dirty, set writeback and unlock the pages.
931 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300932 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400933 async_extent->start +
934 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400935 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800936 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300937 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500938 async_extent->ram_size,
939 ins.objectid,
940 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600941 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700942 async_chunk->write_flags,
943 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100944 struct page *p = async_extent->pages[0];
945 const u64 start = async_extent->start;
946 const u64 end = start + async_extent->ram_size - 1;
947
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300948 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200949 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200950
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100951 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300952 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100953 PAGE_END_WRITEBACK |
954 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100955 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100956 }
Chris Mason771ed682008-11-06 22:02:51 -0500957 alloc_hint = ins.objectid + ins.offset;
958 kfree(async_extent);
959 cond_resched();
960 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100961 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500962out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400963 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400964 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300966 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500967 async_extent->start +
968 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400969 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100970 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400971 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800972 PAGE_UNLOCK | PAGE_START_WRITEBACK |
973 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100974 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100975 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500976 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500977}
978
Nikolay Borisov43c69842020-06-03 08:55:02 +0300979static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400980 u64 num_bytes)
981{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300982 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400983 struct extent_map *em;
984 u64 alloc_hint = 0;
985
986 read_lock(&em_tree->lock);
987 em = search_extent_mapping(em_tree, start, num_bytes);
988 if (em) {
989 /*
990 * if block start isn't an actual block number then find the
991 * first block in this inode and use that as a hint. If that
992 * block is also bogus then just don't worry about it.
993 */
994 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
995 free_extent_map(em);
996 em = search_extent_mapping(em_tree, 0, 0);
997 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
998 alloc_hint = em->block_start;
999 if (em)
1000 free_extent_map(em);
1001 } else {
1002 alloc_hint = em->block_start;
1003 free_extent_map(em);
1004 }
1005 }
1006 read_unlock(&em_tree->lock);
1007
1008 return alloc_hint;
1009}
1010
Chris Mason771ed682008-11-06 22:02:51 -05001011/*
1012 * when extent_io.c finds a delayed allocation range in the file,
1013 * the call backs end up in this code. The basic idea is to
1014 * allocate extents on disk for the range, and create ordered data structs
1015 * in ram to track those extents.
1016 *
1017 * locked_page is the page that writepage had locked already. We use
1018 * it to make sure we don't do extra locks or unlocks.
1019 *
1020 * *page_started is set to one if we unlock locked_page and do everything
1021 * required to start IO on it. It may be clean and already done with
1022 * IO when we return.
1023 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001024static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001025 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001026 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001027 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001028{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001029 struct btrfs_root *root = inode->root;
1030 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001031 u64 alloc_hint = 0;
1032 u64 num_bytes;
1033 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001034 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001035 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001036 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001037 struct btrfs_key ins;
1038 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001039 unsigned clear_bits;
1040 unsigned long page_ops;
1041 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001042 int ret = 0;
1043
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001044 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001045 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001046 ret = -EINVAL;
1047 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001048 }
Chris Mason771ed682008-11-06 22:02:51 -05001049
Qu Wenruofda28322013-02-26 08:10:22 +00001050 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001051 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001052 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001053
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001054 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001055
Chris Mason771ed682008-11-06 22:02:51 -05001056 if (start == 0) {
1057 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001058 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001059 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001060 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001061 /*
1062 * We use DO_ACCOUNTING here because we need the
1063 * delalloc_release_metadata to be run _after_ we drop
1064 * our outstanding extent for clearing delalloc for this
1065 * range.
1066 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001067 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001068 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001069 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1070 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001071 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001072 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001074 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001075 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001078 }
1079 }
Chris Masonc8b97812008-10-29 14:49:59 -04001080
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001081 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1082 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001083
Filipe Manana432cd2a2020-06-08 13:32:55 +01001084 /*
1085 * Relocation relies on the relocated extents to have exactly the same
1086 * size as the original extents. Normally writeback for relocation data
1087 * extents follows a NOCOW path because relocation preallocates the
1088 * extents. However, due to an operation such as scrub turning a block
1089 * group to RO mode, it may fallback to COW mode, so we must make sure
1090 * an extent allocated during COW has exactly the requested size and can
1091 * not be split into smaller extents, otherwise relocation breaks and
1092 * fails during the stage where it updates the bytenr of file extent
1093 * items.
1094 */
1095 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1096 min_alloc_size = num_bytes;
1097 else
1098 min_alloc_size = fs_info->sectorsize;
1099
Anand Jain3752d222018-02-15 12:29:38 +08001100 while (num_bytes > 0) {
1101 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001102 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001103 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001104 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001105 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001106 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001107 cur_alloc_size = ins.offset;
1108 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001109
Chris Mason771ed682008-11-06 22:02:51 -05001110 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001111 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001112 start, /* orig_start */
1113 ins.objectid, /* block_start */
1114 ins.offset, /* block_len */
1115 ins.offset, /* orig_block_len */
1116 ram_size, /* ram_bytes */
1117 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001118 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001119 if (IS_ERR(em)) {
1120 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001121 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001122 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001123 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001124
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001125 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001126 ram_size, cur_alloc_size,
1127 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001128 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001129 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001130
Yan Zheng17d217f2008-12-12 10:03:38 -05001131 if (root->root_key.objectid ==
1132 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001133 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001134 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001135 /*
1136 * Only drop cache here, and process as normal.
1137 *
1138 * We must not allow extent_clear_unlock_delalloc()
1139 * at out_unlock label to free meta of this ordered
1140 * extent, as its meta should be freed by
1141 * btrfs_finish_ordered_io().
1142 *
1143 * So we must continue until @start is increased to
1144 * skip current ordered extent.
1145 */
Josef Bacik00361582013-08-14 14:02:47 -04001146 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001147 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001148 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001149 }
1150
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001151 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001152
Chris Masonc8b97812008-10-29 14:49:59 -04001153 /* we're not doing compressed IO, don't unlock the first
1154 * page (which the caller expects to stay locked), don't
1155 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001156 *
1157 * Do set the Private2 bit so we know this page was properly
1158 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001159 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001160 page_ops = unlock ? PAGE_UNLOCK : 0;
1161 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001162
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001163 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001164 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001165 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001166 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001167 if (num_bytes < cur_alloc_size)
1168 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001169 else
Anand Jain3752d222018-02-15 12:29:38 +08001170 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001171 alloc_hint = ins.objectid + ins.offset;
1172 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001173 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001174
1175 /*
1176 * btrfs_reloc_clone_csums() error, since start is increased
1177 * extent_clear_unlock_delalloc() at out_unlock label won't
1178 * free metadata of current ordered extent, we're OK to exit.
1179 */
1180 if (ret)
1181 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001182 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001185
Filipe Mananad9f85962014-08-25 10:43:00 +01001186out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001187 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001188out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001189 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001190 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001192 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1193 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001194 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001195 /*
1196 * If we reserved an extent for our delalloc range (or a subrange) and
1197 * failed to create the respective ordered extent, then it means that
1198 * when we reserved the extent we decremented the extent's size from
1199 * the data space_info's bytes_may_use counter and incremented the
1200 * space_info's bytes_reserved counter by the same amount. We must make
1201 * sure extent_clear_unlock_delalloc() does not try to decrement again
1202 * the data space_info's bytes_may_use counter, therefore we do not pass
1203 * it the flag EXTENT_CLEAR_DATA_RESV.
1204 */
1205 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001206 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001207 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001208 locked_page,
1209 clear_bits,
1210 page_ops);
1211 start += cur_alloc_size;
1212 if (start >= end)
1213 goto out;
1214 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001215 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001216 clear_bits | EXTENT_CLEAR_DATA_RESV,
1217 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001219}
Chris Masonc8b97812008-10-29 14:49:59 -04001220
Chris Mason771ed682008-11-06 22:02:51 -05001221/*
1222 * work queue call back to started compression on a file and pages
1223 */
1224static noinline void async_cow_start(struct btrfs_work *work)
1225{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001226 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001227 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001228
Nikolay Borisovb5326272019-03-12 17:20:25 +02001229 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001231 compressed_extents = compress_file_range(async_chunk);
1232 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 btrfs_add_delayed_iput(async_chunk->inode);
1234 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001235 }
Chris Mason771ed682008-11-06 22:02:51 -05001236}
1237
1238/*
1239 * work queue call back to submit previously compressed pages
1240 */
1241static noinline void async_cow_submit(struct btrfs_work *work)
1242{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001243 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1244 work);
1245 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247
Nikolay Borisovb5326272019-03-12 17:20:25 +02001248 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001249 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001250
David Sterba093258e2018-02-26 16:15:17 +01001251 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001252 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001253 5 * SZ_1M)
1254 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001255
Nikolay Borisov4546d172019-01-03 10:50:03 +02001256 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001257 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001258 * in which case we don't have anything to submit, yet we need to
1259 * always adjust ->async_delalloc_pages as its paired with the init
1260 * happening in cow_file_range_async
1261 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001262 if (async_chunk->inode)
1263 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001264}
Chris Masonc8b97812008-10-29 14:49:59 -04001265
Chris Mason771ed682008-11-06 22:02:51 -05001266static noinline void async_cow_free(struct btrfs_work *work)
1267{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001268 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001269
Nikolay Borisovb5326272019-03-12 17:20:25 +02001270 async_chunk = container_of(work, struct async_chunk, work);
1271 if (async_chunk->inode)
1272 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001273 if (async_chunk->blkcg_css)
1274 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001275 /*
1276 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001277 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001279 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001280 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001281}
1282
Nikolay Borisov751b6432020-06-03 08:55:22 +03001283static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001284 struct writeback_control *wbc,
1285 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001286 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001287 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001288{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001289 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001290 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001291 struct async_cow *ctx;
1292 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001293 unsigned long nr_pages;
1294 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1296 int i;
1297 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001298 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001299 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001300
Nikolay Borisov751b6432020-06-03 08:55:22 +03001301 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001302
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1305 num_chunks = 1;
1306 should_compress = false;
1307 } else {
1308 should_compress = true;
1309 }
1310
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001311 nofs_flag = memalloc_nofs_save();
1312 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1313 memalloc_nofs_restore(nofs_flag);
1314
Nikolay Borisov97db1202019-03-12 17:20:24 +02001315 if (!ctx) {
1316 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1317 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1318 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001319 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1320 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001321
Nikolay Borisov751b6432020-06-03 08:55:22 +03001322 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1323 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001324 return -ENOMEM;
1325 }
1326
1327 async_chunk = ctx->chunks;
1328 atomic_set(&ctx->num_chunks, num_chunks);
1329
1330 for (i = 0; i < num_chunks; i++) {
1331 if (should_compress)
1332 cur_end = min(end, start + SZ_512K - 1);
1333 else
1334 cur_end = end;
1335
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001336 /*
1337 * igrab is called higher up in the call chain, take only the
1338 * lightweight reference for the callback lifetime
1339 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001340 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001341 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001342 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001343 async_chunk[i].start = start;
1344 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001345 async_chunk[i].write_flags = write_flags;
1346 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001347
Chris Mason1d53c9e2019-07-10 12:28:16 -07001348 /*
1349 * The locked_page comes all the way from writepage and its
1350 * the original page we were actually given. As we spread
1351 * this large delalloc region across multiple async_chunk
1352 * structs, only the first struct needs a pointer to locked_page
1353 *
1354 * This way we don't need racey decisions about who is supposed
1355 * to unlock it.
1356 */
1357 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001358 /*
1359 * Depending on the compressibility, the pages might or
1360 * might not go through async. We want all of them to
1361 * be accounted against wbc once. Let's do it here
1362 * before the paths diverge. wbc accounting is used
1363 * only for foreign writeback detection and doesn't
1364 * need full accuracy. Just account the whole thing
1365 * against the first page.
1366 */
1367 wbc_account_cgroup_owner(wbc, locked_page,
1368 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001369 async_chunk[i].locked_page = locked_page;
1370 locked_page = NULL;
1371 } else {
1372 async_chunk[i].locked_page = NULL;
1373 }
1374
Chris Masonec39f762019-07-10 12:28:17 -07001375 if (blkcg_css != blkcg_root_css) {
1376 css_get(blkcg_css);
1377 async_chunk[i].blkcg_css = blkcg_css;
1378 } else {
1379 async_chunk[i].blkcg_css = NULL;
1380 }
1381
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001382 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1383 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001384
Nikolay Borisov97db1202019-03-12 17:20:24 +02001385 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001386 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001387
Nikolay Borisov97db1202019-03-12 17:20:24 +02001388 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001389
Chris Mason771ed682008-11-06 22:02:51 -05001390 *nr_written += nr_pages;
1391 start = cur_end + 1;
1392 }
1393 *page_started = 1;
1394 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001395}
1396
Naohiro Aota42c01102021-02-04 19:22:07 +09001397static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1398 struct page *locked_page, u64 start,
1399 u64 end, int *page_started,
1400 unsigned long *nr_written)
1401{
1402 int ret;
1403
1404 ret = cow_file_range(inode, locked_page, start, end, page_started,
1405 nr_written, 0);
1406 if (ret)
1407 return ret;
1408
1409 if (*page_started)
1410 return 0;
1411
1412 __set_page_dirty_nobuffers(locked_page);
1413 account_page_redirty(locked_page);
1414 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1415 *page_started = 1;
1416
1417 return 0;
1418}
1419
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001420static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001421 u64 bytenr, u64 num_bytes)
1422{
1423 int ret;
1424 struct btrfs_ordered_sum *sums;
1425 LIST_HEAD(list);
1426
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001428 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001429 if (ret == 0 && list_empty(&list))
1430 return 0;
1431
1432 while (!list_empty(&list)) {
1433 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1434 list_del(&sums->list);
1435 kfree(sums);
1436 }
Liu Bo58113752018-01-31 17:09:13 -07001437 if (ret < 0)
1438 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001439 return 1;
1440}
1441
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001442static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001443 const u64 start, const u64 end,
1444 int *page_started, unsigned long *nr_written)
1445{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001446 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1447 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001448 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001449 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001450 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001451 u64 range_start = start;
1452 u64 count;
1453
1454 /*
1455 * If EXTENT_NORESERVE is set it means that when the buffered write was
1456 * made we had not enough available data space and therefore we did not
1457 * reserve data space for it, since we though we could do NOCOW for the
1458 * respective file range (either there is prealloc extent or the inode
1459 * has the NOCOW bit set).
1460 *
1461 * However when we need to fallback to COW mode (because for example the
1462 * block group for the corresponding extent was turned to RO mode by a
1463 * scrub or relocation) we need to do the following:
1464 *
1465 * 1) We increment the bytes_may_use counter of the data space info.
1466 * If COW succeeds, it allocates a new data extent and after doing
1467 * that it decrements the space info's bytes_may_use counter and
1468 * increments its bytes_reserved counter by the same amount (we do
1469 * this at btrfs_add_reserved_bytes()). So we need to increment the
1470 * bytes_may_use counter to compensate (when space is reserved at
1471 * buffered write time, the bytes_may_use counter is incremented);
1472 *
1473 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1474 * that if the COW path fails for any reason, it decrements (through
1475 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1476 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001477 *
1478 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001479 * space cache inode or an inode of the data relocation tree, we must
1480 * also increment bytes_may_use of the data space_info for the same
1481 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001482 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001483 * group that contains that extent to RO mode and therefore force COW
1484 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001485 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001486 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001487 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001488 if (count > 0 || is_space_ino || is_reloc_ino) {
1489 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001490 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001491 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1492
Filipe Manana6bd335b2020-06-08 13:33:05 +01001493 if (is_space_ino || is_reloc_ino)
1494 bytes = range_bytes;
1495
Filipe Manana467dc472020-05-27 11:16:07 +01001496 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001497 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001498 spin_unlock(&sinfo->lock);
1499
Filipe Manana2166e5e2020-05-27 11:16:19 +01001500 if (count > 0)
1501 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1502 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001503 }
1504
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001505 return cow_file_range(inode, locked_page, start, end, page_started,
1506 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * when nowcow writeback call back. This checks for snapshots or COW copies
1511 * of the extents that exist in the file, and COWs the file as required.
1512 *
1513 * If no cow copies or snapshots exist, we write directly to the existing
1514 * blocks on disk
1515 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001516static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001517 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001518 const u64 start, const u64 end,
1519 int *page_started, int force,
1520 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001521{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001522 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1523 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001524 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001525 u64 cow_start = (u64)-1;
1526 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001527 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001528 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001529 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1530 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001531 bool nocow = false;
1532 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001533
1534 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001536 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001538 EXTENT_DO_ACCOUNTING |
1539 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001540 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001542 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001543 }
Li Zefan82d59022011-04-20 10:33:24 +08001544
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001546 struct btrfs_key found_key;
1547 struct btrfs_file_extent_item *fi;
1548 struct extent_buffer *leaf;
1549 u64 extent_end;
1550 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001551 u64 num_bytes = 0;
1552 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001553 u64 ram_bytes;
1554 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001555
1556 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001557
Liu Boe4c3b2d2017-01-30 12:25:28 -08001558 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001559 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001560 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001561 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001562
1563 /*
1564 * If there is no extent for our range when doing the initial
1565 * search, then go back to the previous slot as it will be the
1566 * one containing the search offset
1567 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1569 leaf = path->nodes[0];
1570 btrfs_item_key_to_cpu(leaf, &found_key,
1571 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001572 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001573 found_key.type == BTRFS_EXTENT_DATA_KEY)
1574 path->slots[0]--;
1575 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001576 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001577next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001578 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001579 leaf = path->nodes[0];
1580 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1581 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001582 if (ret < 0) {
1583 if (cow_start != (u64)-1)
1584 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001585 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001586 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001587 if (ret > 0)
1588 break;
1589 leaf = path->nodes[0];
1590 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001591
Yan Zheng80ff3852008-10-30 14:20:02 -04001592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001593
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001594 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001595 if (found_key.objectid > ino)
1596 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001597 /*
1598 * Keep searching until we find an EXTENT_ITEM or there are no
1599 * more extents for this inode
1600 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001601 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1602 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1603 path->slots[0]++;
1604 goto next_slot;
1605 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001606
1607 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001608 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001609 found_key.offset > end)
1610 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001611
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001612 /*
1613 * If the found extent starts after requested offset, then
1614 * adjust extent_end to be right before this extent begins
1615 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001616 if (found_key.offset > cur_offset) {
1617 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001618 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001619 goto out_check;
1620 }
Chris Masonc31f8832008-01-08 15:46:31 -05001621
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001622 /*
1623 * Found extent which begins before our range and potentially
1624 * intersect it
1625 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001626 fi = btrfs_item_ptr(leaf, path->slots[0],
1627 struct btrfs_file_extent_item);
1628 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001629
Josef Bacikcc95bef2013-04-04 14:31:27 -04001630 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001631 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1632 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001633 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001635 extent_end = found_key.offset +
1636 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001637 disk_num_bytes =
1638 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001639 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001640 * If the extent we got ends before our current offset,
1641 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001642 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001643 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001644 path->slots[0]++;
1645 goto next_slot;
1646 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001647 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001648 if (disk_bytenr == 0)
1649 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001650 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001651 if (btrfs_file_extent_compression(leaf, fi) ||
1652 btrfs_file_extent_encryption(leaf, fi) ||
1653 btrfs_file_extent_other_encoding(leaf, fi))
1654 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001655 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001656 * If extent is created before the last volume's snapshot
1657 * this implies the extent is shared, hence we can't do
1658 * nocow. This is the same check as in
1659 * btrfs_cross_ref_exist but without calling
1660 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001661 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001662 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001663 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001664 btrfs_root_last_snapshot(&root->root_item))
1665 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001666 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1667 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001668
1669 /*
1670 * The following checks can be expensive, as they need to
1671 * take other locks and do btree or rbtree searches, so
1672 * release the path to avoid blocking other tasks for too
1673 * long.
1674 */
1675 btrfs_release_path(path);
1676
Liu Bo58113752018-01-31 17:09:13 -07001677 ret = btrfs_cross_ref_exist(root, ino,
1678 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001679 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001680 if (ret) {
1681 /*
1682 * ret could be -EIO if the above fails to read
1683 * metadata.
1684 */
1685 if (ret < 0) {
1686 if (cow_start != (u64)-1)
1687 cur_offset = cow_start;
1688 goto error;
1689 }
1690
Nikolay Borisov3e024842019-08-21 10:42:03 +03001691 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001692 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001693 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001695 disk_bytenr += cur_offset - found_key.offset;
1696 num_bytes = min(end + 1, extent_end) - cur_offset;
1697 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001698 * If there are pending snapshots for this root, we
1699 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001700 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001701 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001702 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001703 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001704 * force cow if csum exists in the range.
1705 * this ensure that csum for a given extent are
1706 * either valid or do not exist.
1707 */
Liu Bo58113752018-01-31 17:09:13 -07001708 ret = csum_exist_in_range(fs_info, disk_bytenr,
1709 num_bytes);
1710 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001711 /*
1712 * ret could be -EIO if the above fails to read
1713 * metadata.
1714 */
1715 if (ret < 0) {
1716 if (cow_start != (u64)-1)
1717 cur_offset = cow_start;
1718 goto error;
1719 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001720 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001721 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001722 }
Filipe Manana20903032021-02-05 12:55:36 +00001723 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001724 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001725 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001726 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001727 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001728 extent_end = found_key.offset + ram_bytes;
1729 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001730 /* Skip extents outside of our requested range */
1731 if (extent_end <= start) {
1732 path->slots[0]++;
1733 goto next_slot;
1734 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001735 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001736 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001737 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001738 }
1739out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001740 /*
1741 * If nocow is false then record the beginning of the range
1742 * that needs to be COWed
1743 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001744 if (!nocow) {
1745 if (cow_start == (u64)-1)
1746 cow_start = cur_offset;
1747 cur_offset = extent_end;
1748 if (cur_offset > end)
1749 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001750 if (!path->nodes[0])
1751 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001752 path->slots[0]++;
1753 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001754 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001755
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001756 /*
1757 * COW range from cow_start to found_key.offset - 1. As the key
1758 * will contain the beginning of the first extent that can be
1759 * NOCOW, following one which needs to be COW'ed
1760 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001761 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001762 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001763 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001764 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001765 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001767 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001768 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001769
Yan Zhengd899e052008-10-30 14:25:28 -04001770 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001771 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001772 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001773
Nikolay Borisov968322c2020-06-03 08:55:21 +03001774 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001775 orig_start,
1776 disk_bytenr, /* block_start */
1777 num_bytes, /* block_len */
1778 disk_num_bytes, /* orig_block_len */
1779 ram_bytes, BTRFS_COMPRESS_NONE,
1780 BTRFS_ORDERED_PREALLOC);
1781 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001782 ret = PTR_ERR(em);
1783 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001784 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001785 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001786 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001787 disk_bytenr, num_bytes,
1788 num_bytes,
1789 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001790 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001791 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001792 cur_offset + num_bytes - 1,
1793 0);
1794 goto error;
1795 }
Yan Zhengd899e052008-10-30 14:25:28 -04001796 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001797 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001798 disk_bytenr, num_bytes,
1799 num_bytes,
1800 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001801 if (ret)
1802 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001803 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001804
Filipe Mananaf78c4362016-05-09 13:15:41 +01001805 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001806 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001807 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001808
Yan, Zhengefa56462010-05-16 10:49:59 -04001809 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001810 BTRFS_DATA_RELOC_TREE_OBJECTID)
1811 /*
1812 * Error handled later, as we must prevent
1813 * extent_clear_unlock_delalloc() in error handler
1814 * from freeing metadata of created ordered extent.
1815 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001816 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001817 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001818
Nikolay Borisov968322c2020-06-03 08:55:21 +03001819 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001820 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001821 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001822 EXTENT_DELALLOC |
1823 EXTENT_CLEAR_DATA_RESV,
1824 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1825
Yan Zheng80ff3852008-10-30 14:20:02 -04001826 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001827
1828 /*
1829 * btrfs_reloc_clone_csums() error, now we're OK to call error
1830 * handler, as metadata for created ordered extent will only
1831 * be freed by btrfs_finish_ordered_io().
1832 */
1833 if (ret)
1834 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001835 if (cur_offset > end)
1836 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001837 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001838 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001839
Robbie Ko506481b2018-10-30 18:04:04 +08001840 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001841 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001842
Yan Zheng80ff3852008-10-30 14:20:02 -04001843 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001844 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001845 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1846 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001847 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001848 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001849 }
1850
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001851error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001852 if (nocow)
1853 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1854
Josef Bacik17ca04a2012-05-31 15:58:55 -04001855 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001856 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001857 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001858 EXTENT_DELALLOC | EXTENT_DEFRAG |
1859 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001860 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001861 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001862 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001864}
1865
Nikolay Borisov0c494222020-06-03 08:55:28 +03001866static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001867{
1868
Nikolay Borisov0c494222020-06-03 08:55:28 +03001869 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1870 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001871 return 0;
1872
1873 /*
1874 * @defrag_bytes is a hint value, no spinlock held here,
1875 * if is not zero, it means the file is defragging.
1876 * Force cow if given extent needs to be defragged.
1877 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001878 if (inode->defrag_bytes &&
1879 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001880 return 1;
1881
1882 return 0;
1883}
1884
Chris Masond352ac62008-09-29 15:18:18 -04001885/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001886 * Function to process delayed allocation (create CoW) for ranges which are
1887 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001888 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001889int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001890 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1891 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001892{
Chris Masonbe20aa92007-12-17 20:14:01 -05001893 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001894 int force_cow = need_force_cow(inode, start, end);
Naohiro Aota42c01102021-02-04 19:22:07 +09001895 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001896
Nikolay Borisov98456b92020-06-03 08:55:29 +03001897 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001898 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001899 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001900 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001901 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001902 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001903 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001904 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001905 } else if (!inode_can_compress(inode) ||
1906 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001907 if (zoned)
1908 ret = run_delalloc_zoned(inode, locked_page, start, end,
1909 page_started, nr_written);
1910 else
1911 ret = cow_file_range(inode, locked_page, start, end,
1912 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001913 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001914 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1915 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001916 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001917 }
Qu Wenruo524272602017-03-08 10:25:52 +08001918 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001919 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001920 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001921 return ret;
1922}
1923
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001924void btrfs_split_delalloc_extent(struct inode *inode,
1925 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001926{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001927 u64 size;
1928
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001929 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001930 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001931 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932
Josef Bacikdcab6a32015-02-11 15:08:59 -05001933 size = orig->end - orig->start + 1;
1934 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001935 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001936 u64 new_size;
1937
1938 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001939 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001940 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001941 */
1942 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001943 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001944 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001945 num_extents += count_max_extents(new_size);
1946 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001947 return;
1948 }
1949
Josef Bacik9e0baf62011-07-15 15:16:44 +00001950 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001951 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001952 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001953}
1954
1955/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001956 * Handle merged delayed allocation extents so we can keep track of new extents
1957 * that are just merged onto old extents, such as when we are doing sequential
1958 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001959 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001960void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1961 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001962{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001963 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001964 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001965
Josef Bacik9ed74f22009-09-11 16:12:44 -04001966 /* not delalloc, ignore it */
1967 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001968 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001969
Josef Bacik8461a3d2015-03-13 15:12:08 -04001970 if (new->start > other->start)
1971 new_size = new->end - other->start + 1;
1972 else
1973 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001974
1975 /* we're not bigger than the max, unreserve the space and go */
1976 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1977 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001978 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001979 spin_unlock(&BTRFS_I(inode)->lock);
1980 return;
1981 }
1982
1983 /*
Josef Bacikba117212015-03-13 15:01:24 -04001984 * We have to add up either side to figure out how many extents were
1985 * accounted for before we merged into one big extent. If the number of
1986 * extents we accounted for is <= the amount we need for the new range
1987 * then we can return, otherwise drop. Think of it like this
1988 *
1989 * [ 4k][MAX_SIZE]
1990 *
1991 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1992 * need 2 outstanding extents, on one side we have 1 and the other side
1993 * we have 1 so they are == and we can return. But in this case
1994 *
1995 * [MAX_SIZE+4k][MAX_SIZE+4k]
1996 *
1997 * Each range on their own accounts for 2 extents, but merged together
1998 * they are only 3 extents worth of accounting, so we need to drop in
1999 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002000 */
Josef Bacikba117212015-03-13 15:01:24 -04002001 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002002 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002003 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002004 num_extents += count_max_extents(old_size);
2005 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002006 return;
2007
Josef Bacik9e0baf62011-07-15 15:16:44 +00002008 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002009 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002010 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002011}
2012
Miao Xieeb73c1b2013-05-15 07:48:22 +00002013static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2014 struct inode *inode)
2015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2017
Miao Xieeb73c1b2013-05-15 07:48:22 +00002018 spin_lock(&root->delalloc_lock);
2019 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2020 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2021 &root->delalloc_inodes);
2022 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2023 &BTRFS_I(inode)->runtime_flags);
2024 root->nr_delalloc_inodes++;
2025 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002026 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002027 BUG_ON(!list_empty(&root->delalloc_root));
2028 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002029 &fs_info->delalloc_roots);
2030 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002031 }
2032 }
2033 spin_unlock(&root->delalloc_lock);
2034}
2035
Nikolay Borisov2b877332018-04-27 12:21:51 +03002036
2037void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2038 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002039{
David Sterba3ffbd682018-06-29 10:56:42 +02002040 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002041
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002042 if (!list_empty(&inode->delalloc_inodes)) {
2043 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002044 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002045 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002046 root->nr_delalloc_inodes--;
2047 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002048 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002049 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002050 BUG_ON(list_empty(&root->delalloc_root));
2051 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002052 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002053 }
2054 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002055}
2056
2057static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2058 struct btrfs_inode *inode)
2059{
2060 spin_lock(&root->delalloc_lock);
2061 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002062 spin_unlock(&root->delalloc_lock);
2063}
2064
Chris Masond352ac62008-09-29 15:18:18 -04002065/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002066 * Properly track delayed allocation bytes in the inode and to maintain the
2067 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002068 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002069void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2070 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002071{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2073
Wang Shilong47059d92014-07-03 18:22:07 +08002074 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2075 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002076 /*
2077 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002078 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002079 * bit, which is only set or cleared with irqs on
2080 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002081 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002082 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002083 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002084 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002085 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002086
Josef Bacik8b62f872017-10-19 14:15:55 -04002087 spin_lock(&BTRFS_I(inode)->lock);
2088 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2089 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002090
Josef Bacik6a3891c2015-03-16 17:38:52 -04002091 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002092 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002093 return;
2094
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002095 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2096 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002097 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002098 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002099 if (*bits & EXTENT_DEFRAG)
2100 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002101 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002102 &BTRFS_I(inode)->runtime_flags))
2103 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002104 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002105 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002106
2107 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2108 (*bits & EXTENT_DELALLOC_NEW)) {
2109 spin_lock(&BTRFS_I(inode)->lock);
2110 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2111 state->start;
2112 spin_unlock(&BTRFS_I(inode)->lock);
2113 }
Chris Mason291d6732008-01-29 15:55:23 -05002114}
2115
Chris Masond352ac62008-09-29 15:18:18 -04002116/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002117 * Once a range is no longer delalloc this function ensures that proper
2118 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002119 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002120void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2121 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002122{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002123 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2124 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002125 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002126 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002127
Filipe Manana4a4b9642017-07-27 19:52:55 +01002128 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2129 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002130 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002131 spin_unlock(&inode->lock);
2132 }
Wang Shilong47059d92014-07-03 18:22:07 +08002133
Chris Mason75eff682008-12-15 15:54:40 -05002134 /*
2135 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002136 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002137 * bit, which is only set or cleared with irqs on
2138 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002139 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002140 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002141 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002142
Josef Bacik8b62f872017-10-19 14:15:55 -04002143 spin_lock(&inode->lock);
2144 btrfs_mod_outstanding_extents(inode, -num_extents);
2145 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002146
Josef Bacikb6d08f02013-09-27 14:57:43 -04002147 /*
2148 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002149 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002150 * error.
2151 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002152 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002153 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002154 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002155
Josef Bacik6a3891c2015-03-16 17:38:52 -04002156 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002157 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002158 return;
2159
Filipe Mananaa315e682017-03-06 23:04:20 +00002160 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2161 do_list && !(state->state & EXTENT_NORESERVE) &&
2162 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002163 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002164
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002165 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2166 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002167 spin_lock(&inode->lock);
2168 inode->delalloc_bytes -= len;
2169 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002170 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002171 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002172 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002173 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002174 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002175
2176 if ((state->state & EXTENT_DELALLOC_NEW) &&
2177 (*bits & EXTENT_DELALLOC_NEW)) {
2178 spin_lock(&inode->lock);
2179 ASSERT(inode->new_delalloc_bytes >= len);
2180 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002181 if (*bits & EXTENT_ADD_INODE_BYTES)
2182 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002183 spin_unlock(&inode->lock);
2184 }
Chris Mason291d6732008-01-29 15:55:23 -05002185}
2186
Chris Masond352ac62008-09-29 15:18:18 -04002187/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002188 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2189 * in a chunk's stripe. This function ensures that bios do not span a
2190 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002191 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002192 * @page - The page we are about to add to the bio
2193 * @size - size we want to add to the bio
2194 * @bio - bio we want to ensure is smaller than a stripe
2195 * @bio_flags - flags of the bio
2196 *
2197 * return 1 if page cannot be added to the bio
2198 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002199 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002200 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002201int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2202 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002204 struct inode *inode = page->mapping->host;
2205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002206 u64 logical = bio->bi_iter.bi_sector << 9;
Michal Rostecki42034312021-01-27 14:57:27 +01002207 struct extent_map *em;
Chris Mason239b14b2008-03-24 15:02:07 -04002208 u64 length = 0;
2209 u64 map_length;
Michal Rostecki42034312021-01-27 14:57:27 +01002210 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002211 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002212
Chris Mason771ed682008-11-06 22:02:51 -05002213 if (bio_flags & EXTENT_BIO_COMPRESSED)
2214 return 0;
2215
Kent Overstreet4f024f32013-10-11 15:44:27 -07002216 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002217 map_length = length;
Michal Rostecki42034312021-01-27 14:57:27 +01002218 em = btrfs_get_chunk_map(fs_info, logical, map_length);
2219 if (IS_ERR(em))
2220 return PTR_ERR(em);
2221 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical,
2222 map_length, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002223 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002224 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002225
2226 if (geom.len < length + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002227 ret = 1;
2228out:
2229 free_extent_map(em);
2230 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002231}
2232
Chris Masond352ac62008-09-29 15:18:18 -04002233/*
2234 * in order to insert checksums into the metadata in large chunks,
2235 * we wait until bio submission time. All the pages in the bio are
2236 * checksummed and sums are attached onto the ordered extent record.
2237 *
2238 * At IO completion time the cums attached on the ordered extent record
2239 * are inserted into the btree
2240 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002241static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002242 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002243{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002244 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002245}
Chris Masone0156402008-04-16 11:15:20 -04002246
Johannes Thumshirncacb2ce2021-02-04 19:22:01 +09002247bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio,
2248 unsigned int size)
2249{
2250 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2251 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2252 struct btrfs_ordered_extent *ordered;
2253 u64 len = bio->bi_iter.bi_size + size;
2254 bool ret = true;
2255
2256 ASSERT(btrfs_is_zoned(fs_info));
2257 ASSERT(fs_info->max_zone_append_size > 0);
2258 ASSERT(bio_op(bio) == REQ_OP_ZONE_APPEND);
2259
2260 /* Ordered extent not yet created, so we're good */
2261 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
2262 if (!ordered)
2263 return ret;
2264
2265 if ((bio->bi_iter.bi_sector << SECTOR_SHIFT) + len >
2266 ordered->disk_bytenr + ordered->disk_num_bytes)
2267 ret = false;
2268
2269 btrfs_put_ordered_extent(ordered);
2270
2271 return ret;
2272}
2273
Naohiro Aotad22002f2021-02-04 19:22:00 +09002274static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2275 struct bio *bio, loff_t file_offset)
2276{
2277 struct btrfs_ordered_extent *ordered;
2278 struct extent_map *em = NULL, *em_new = NULL;
2279 struct extent_map_tree *em_tree = &inode->extent_tree;
2280 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2281 u64 len = bio->bi_iter.bi_size;
2282 u64 end = start + len;
2283 u64 ordered_end;
2284 u64 pre, post;
2285 int ret = 0;
2286
2287 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2288 if (WARN_ON_ONCE(!ordered))
2289 return BLK_STS_IOERR;
2290
2291 /* No need to split */
2292 if (ordered->disk_num_bytes == len)
2293 goto out;
2294
2295 /* We cannot split once end_bio'd ordered extent */
2296 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2297 ret = -EINVAL;
2298 goto out;
2299 }
2300
2301 /* We cannot split a compressed ordered extent */
2302 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2303 ret = -EINVAL;
2304 goto out;
2305 }
2306
2307 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2308 /* bio must be in one ordered extent */
2309 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2310 ret = -EINVAL;
2311 goto out;
2312 }
2313
2314 /* Checksum list should be empty */
2315 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2316 ret = -EINVAL;
2317 goto out;
2318 }
2319
2320 pre = start - ordered->disk_bytenr;
2321 post = ordered_end - end;
2322
2323 ret = btrfs_split_ordered_extent(ordered, pre, post);
2324 if (ret)
2325 goto out;
2326
2327 read_lock(&em_tree->lock);
2328 em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
2329 if (!em) {
2330 read_unlock(&em_tree->lock);
2331 ret = -EIO;
2332 goto out;
2333 }
2334 read_unlock(&em_tree->lock);
2335
2336 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2337 /*
2338 * We cannot reuse em_new here but have to create a new one, as
2339 * unpin_extent_cache() expects the start of the extent map to be the
2340 * logical offset of the file, which does not hold true anymore after
2341 * splitting.
2342 */
2343 em_new = create_io_em(inode, em->start + pre, len,
2344 em->start + pre, em->block_start + pre, len,
2345 len, len, BTRFS_COMPRESS_NONE,
2346 BTRFS_ORDERED_REGULAR);
2347 if (IS_ERR(em_new)) {
2348 ret = PTR_ERR(em_new);
2349 goto out;
2350 }
2351 free_extent_map(em_new);
2352
2353out:
2354 free_extent_map(em);
2355 btrfs_put_ordered_extent(ordered);
2356
2357 return errno_to_blk_status(ret);
2358}
2359
Chris Mason4a69a412008-11-06 22:03:00 -05002360/*
Chris Masoncad321a2008-12-17 14:51:42 -05002361 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002362 * on write, or reading the csums from the tree before a read.
2363 *
2364 * Rules about async/sync submit,
2365 * a) read: sync submit
2366 *
2367 * b) write without checksum: sync submit
2368 *
2369 * c) write with checksum:
2370 * c-1) if bio is issued by fsync: sync submit
2371 * (sync_writers != 0)
2372 *
2373 * c-2) if root is reloc root: sync submit
2374 * (only in case of buffered IO)
2375 *
2376 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002377 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002378blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2379 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002380
Chris Mason44b8bd72008-04-16 11:14:51 -04002381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002383 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302384 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002385 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002386 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002387 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002388
Josef Bacik42437a62020-10-16 11:29:18 -04002389 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2390 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002391
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002392 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302393 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002394
Naohiro Aotad22002f2021-02-04 19:22:00 +09002395 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2396 struct page *page = bio_first_bvec_all(bio)->bv_page;
2397 loff_t file_offset = page_offset(page);
2398
2399 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2400 if (ret)
2401 goto out;
2402 }
2403
Naohiro Aotacfe94442021-02-04 19:21:59 +09002404 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002405 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002406 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002407 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002408
Chris Masond20f7042008-12-08 16:58:54 -05002409 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002410 ret = btrfs_submit_compressed_read(inode, bio,
2411 mirror_num,
2412 bio_flags);
2413 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002414 } else {
2415 /*
2416 * Lookup bio sums does extra checks around whether we
2417 * need to csum or not, which is why we ignore skip_sum
2418 * here.
2419 */
Qu Wenruo62751932020-12-02 14:48:06 +08002420 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002421 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002422 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002423 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002424 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002425 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002426 /* csum items have already been cloned */
2427 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2428 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002429 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002430 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2431 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002432 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002433 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002434 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002435 if (ret)
2436 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002437 }
2438
Chris Mason0b86a832008-03-24 15:01:56 -04002439mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002440 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002441
2442out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002443 if (ret) {
2444 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002445 bio_endio(bio);
2446 }
Stefan Behrens61891922012-11-05 18:51:52 +01002447 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002448}
Chris Mason6885f302008-02-20 16:11:05 -05002449
Chris Masond352ac62008-09-29 15:18:18 -04002450/*
2451 * given a list of ordered sums record them in the inode. This happens
2452 * at IO completion time based on sums calculated at bio submission time.
2453 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002454static int add_pending_csums(struct btrfs_trans_handle *trans,
2455 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002456{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002457 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002458 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002459
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002460 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002461 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002462 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002463 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002464 if (ret)
2465 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002466 }
2467 return 0;
2468}
2469
Filipe Mananac3347302020-11-04 11:07:31 +00002470static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2471 const u64 start,
2472 const u64 len,
2473 struct extent_state **cached_state)
2474{
2475 u64 search_start = start;
2476 const u64 end = start + len - 1;
2477
2478 while (search_start < end) {
2479 const u64 search_len = end - search_start + 1;
2480 struct extent_map *em;
2481 u64 em_len;
2482 int ret = 0;
2483
2484 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2485 if (IS_ERR(em))
2486 return PTR_ERR(em);
2487
2488 if (em->block_start != EXTENT_MAP_HOLE)
2489 goto next;
2490
2491 em_len = em->len;
2492 if (em->start < search_start)
2493 em_len -= search_start - em->start;
2494 if (em_len > search_len)
2495 em_len = search_len;
2496
2497 ret = set_extent_bit(&inode->io_tree, search_start,
2498 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002499 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2500 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002501next:
2502 search_start = extent_map_end(em);
2503 free_extent_map(em);
2504 if (ret)
2505 return ret;
2506 }
2507 return 0;
2508}
2509
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002510int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002511 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002512 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002513{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002514 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002515
2516 if (start >= i_size_read(&inode->vfs_inode) &&
2517 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2518 /*
2519 * There can't be any extents following eof in this case so just
2520 * set the delalloc new bit for the range directly.
2521 */
2522 extra_bits |= EXTENT_DELALLOC_NEW;
2523 } else {
2524 int ret;
2525
2526 ret = btrfs_find_new_delalloc_bytes(inode, start,
2527 end + 1 - start,
2528 cached_state);
2529 if (ret)
2530 return ret;
2531 }
2532
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002533 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2534 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002535}
2536
Chris Masond352ac62008-09-29 15:18:18 -04002537/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002538struct btrfs_writepage_fixup {
2539 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002540 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002541 struct btrfs_work work;
2542};
2543
Christoph Hellwigb2950862008-12-02 09:54:17 -05002544static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002545{
2546 struct btrfs_writepage_fixup *fixup;
2547 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002548 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002549 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002550 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002551 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002552 u64 page_start;
2553 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002554 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002555 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002556
2557 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2558 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002559 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002560 page_start = page_offset(page);
2561 page_end = page_offset(page) + PAGE_SIZE - 1;
2562
2563 /*
2564 * This is similar to page_mkwrite, we need to reserve the space before
2565 * we take the page lock.
2566 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002567 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2568 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002569again:
Chris Mason247e7432008-07-17 12:53:51 -04002570 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002571
Chris Mason25f3c502020-01-21 11:51:42 -05002572 /*
2573 * Before we queued this fixup, we took a reference on the page.
2574 * page->mapping may go NULL, but it shouldn't be moved to a different
2575 * address space.
2576 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002577 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2578 /*
2579 * Unfortunately this is a little tricky, either
2580 *
2581 * 1) We got here and our page had already been dealt with and
2582 * we reserved our space, thus ret == 0, so we need to just
2583 * drop our space reservation and bail. This can happen the
2584 * first time we come into the fixup worker, or could happen
2585 * while waiting for the ordered extent.
2586 * 2) Our page was already dealt with, but we happened to get an
2587 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2588 * this case we obviously don't have anything to release, but
2589 * because the page was already dealt with we don't want to
2590 * mark the page with an error, so make sure we're resetting
2591 * ret to 0. This is why we have this check _before_ the ret
2592 * check, because we do not want to have a surprise ENOSPC
2593 * when the page was already properly dealt with.
2594 */
2595 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002596 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2597 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002598 page_start, PAGE_SIZE,
2599 true);
2600 }
2601 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002602 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002603 }
Chris Mason25f3c502020-01-21 11:51:42 -05002604
2605 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002606 * We can't mess with the page state unless it is locked, so now that
2607 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002608 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002609 if (ret)
2610 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002611
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002612 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002613
2614 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002615 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002616 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002617
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002618 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002619 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002620 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2621 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002622 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002623 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002624 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002625 goto again;
2626 }
Chris Mason247e7432008-07-17 12:53:51 -04002627
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002628 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002629 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002630 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002631 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002632
Chris Mason25f3c502020-01-21 11:51:42 -05002633 /*
2634 * Everything went as planned, we're now the owner of a dirty page with
2635 * delayed allocation bits set and space reserved for our COW
2636 * destination.
2637 *
2638 * The page was dirty when we started, nothing should have cleaned it.
2639 */
2640 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002641 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002642out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002643 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002644 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002645 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2646 PAGE_SIZE, true);
2647 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002648 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002649out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002650 if (ret) {
2651 /*
2652 * We hit ENOSPC or other errors. Update the mapping and page
2653 * to reflect the errors and clean the page.
2654 */
2655 mapping_set_error(page->mapping, ret);
2656 end_extent_writepage(page, ret, page_start, page_end);
2657 clear_page_dirty_for_io(page);
2658 SetPageError(page);
2659 }
2660 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002661 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002662 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002663 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002664 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002665 /*
2666 * As a precaution, do a delayed iput in case it would be the last iput
2667 * that could need flushing space. Recursing back to fixup worker would
2668 * deadlock.
2669 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002670 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002671}
2672
2673/*
2674 * There are a few paths in the higher layers of the kernel that directly
2675 * set the page dirty bit without asking the filesystem if it is a
2676 * good idea. This causes problems because we want to make sure COW
2677 * properly happens and the data=ordered rules are followed.
2678 *
Chris Masonc8b97812008-10-29 14:49:59 -04002679 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002680 * hasn't been properly setup for IO. We kick off an async process
2681 * to fix it up. The async helper will wait for ordered extents, set
2682 * the delalloc bit and make it safe to write the page.
2683 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002684int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002685{
2686 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002688 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002689
Chris Mason8b62b722009-09-02 16:53:46 -04002690 /* this page is properly in the ordered list */
2691 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002692 return 0;
2693
Chris Mason25f3c502020-01-21 11:51:42 -05002694 /*
2695 * PageChecked is set below when we create a fixup worker for this page,
2696 * don't try to create another one if we're already PageChecked()
2697 *
2698 * The extent_io writepage code will redirty the page if we send back
2699 * EAGAIN.
2700 */
Chris Mason247e7432008-07-17 12:53:51 -04002701 if (PageChecked(page))
2702 return -EAGAIN;
2703
2704 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2705 if (!fixup)
2706 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002707
Josef Bacikf4b13632020-01-21 14:34:52 -05002708 /*
2709 * We are already holding a reference to this inode from
2710 * write_cache_pages. We need to hold it because the space reservation
2711 * takes place outside of the page lock, and we can't trust
2712 * page->mapping outside of the page lock.
2713 */
2714 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002715 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002716 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002717 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002718 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002719 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002721
2722 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002723}
2724
Yan Zhengd899e052008-10-30 14:25:28 -04002725static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002726 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002727 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002728 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002729 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002730{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002731 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002732 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002733 struct btrfs_path *path;
2734 struct extent_buffer *leaf;
2735 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002736 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2737 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2738 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2739 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002740 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002741 int ret;
2742
2743 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002744 if (!path)
2745 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002746
Chris Masona1ed8352009-09-11 12:27:37 -04002747 /*
2748 * we may be replacing one extent in the tree with another.
2749 * The new extent is pinned in the extent map, and we don't want
2750 * to drop it from the cache until it is completely in the btree.
2751 *
2752 * So, tell btrfs_drop_extents to leave this extent in the cache.
2753 * the caller is expected to unpin it and allow it to be merged
2754 * with the others.
2755 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002756 drop_args.path = path;
2757 drop_args.start = file_pos;
2758 drop_args.end = file_pos + num_bytes;
2759 drop_args.replace_extent = true;
2760 drop_args.extent_item_size = sizeof(*stack_fi);
2761 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002762 if (ret)
2763 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002764
Filipe Manana5893dfb2020-11-04 11:07:32 +00002765 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002766 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002767 ins.offset = file_pos;
2768 ins.type = BTRFS_EXTENT_DATA_KEY;
2769
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002770 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002771 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002772 if (ret)
2773 goto out;
2774 }
Yan Zhengd899e052008-10-30 14:25:28 -04002775 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002776 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2777 write_extent_buffer(leaf, stack_fi,
2778 btrfs_item_ptr_offset(leaf, path->slots[0]),
2779 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002780
Yan Zhengd899e052008-10-30 14:25:28 -04002781 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002782 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002783
Filipe Manana2766ff62020-11-04 11:07:34 +00002784 /*
2785 * If we dropped an inline extent here, we know the range where it is
2786 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2787 * number of bytes only for that range contaning the inline extent.
2788 * The remaining of the range will be processed when clearning the
2789 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2790 */
2791 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2792 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2793
2794 inline_size = drop_args.bytes_found - inline_size;
2795 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2796 drop_args.bytes_found -= inline_size;
2797 num_bytes -= sectorsize;
2798 }
2799
2800 if (update_inode_bytes)
2801 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002802
2803 ins.objectid = disk_bytenr;
2804 ins.offset = disk_num_bytes;
2805 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002806
Nikolay Borisovc553f942020-06-03 08:55:19 +03002807 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002808 if (ret)
2809 goto out;
2810
Nikolay Borisovc553f942020-06-03 08:55:19 +03002811 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002812 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002813out:
Yan Zhengd899e052008-10-30 14:25:28 -04002814 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002815
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002817}
2818
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002819static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002820 u64 start, u64 len)
2821{
David Sterba32da53862019-10-29 19:20:18 +01002822 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002823
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002825 ASSERT(cache);
2826
2827 spin_lock(&cache->lock);
2828 cache->delalloc_bytes -= len;
2829 spin_unlock(&cache->lock);
2830
2831 btrfs_put_block_group(cache);
2832}
2833
Qu Wenruo203f44c52020-06-10 09:04:40 +08002834static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002835 struct btrfs_ordered_extent *oe)
2836{
2837 struct btrfs_file_extent_item stack_fi;
2838 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002839 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002840
2841 memset(&stack_fi, 0, sizeof(stack_fi));
2842 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2843 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2844 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2845 oe->disk_num_bytes);
2846 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2847 logical_len = oe->truncated_len;
2848 else
2849 logical_len = oe->num_bytes;
2850 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2851 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2852 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2853 /* Encryption and other encoding is reserved and all 0 */
2854
Filipe Manana2766ff62020-11-04 11:07:34 +00002855 /*
2856 * For delalloc, when completing an ordered extent we update the inode's
2857 * bytes when clearing the range in the inode's io tree, so pass false
2858 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2859 * except if the ordered extent was truncated.
2860 */
2861 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2862 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2863
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002864 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2865 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002866 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002867}
2868
2869/*
2870 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002871 * an ordered extent if the range of bytes in the file it covers are
2872 * fully written.
2873 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002874static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002875{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002876 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2877 struct btrfs_root *root = inode->root;
2878 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002879 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002880 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002881 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002882 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002883 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002884 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002885 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002886 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002887 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002888 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002889 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002890
Omar Sandovalbffe6332019-12-02 17:34:19 -08002891 start = ordered_extent->file_offset;
2892 end = start + ordered_extent->num_bytes - 1;
2893
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002894 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2895 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2896 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002897 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002899 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002900
Josef Bacik5fd02042012-05-02 14:00:54 -04002901 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2902 ret = -EIO;
2903 goto out;
2904 }
2905
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002906 if (ordered_extent->disk)
2907 btrfs_rewrite_logical_zoned(ordered_extent);
2908
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002909 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002910
Josef Bacik77cef2e2013-08-29 13:57:21 -04002911 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2912 truncated = true;
2913 logical_len = ordered_extent->truncated_len;
2914 /* Truncated the entire extent, don't bother adding */
2915 if (!logical_len)
2916 goto out;
2917 }
2918
Yan, Zhengc2167752009-11-12 09:34:21 +00002919 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002920 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002921
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002922 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002923 if (freespace_inode)
2924 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002925 else
2926 trans = btrfs_join_transaction(root);
2927 if (IS_ERR(trans)) {
2928 ret = PTR_ERR(trans);
2929 trans = NULL;
2930 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002931 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002932 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002933 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002934 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002935 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002936 goto out;
2937 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002938
Filipe Manana2766ff62020-11-04 11:07:34 +00002939 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002940 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941
Nikolay Borisov8d510122019-10-08 20:43:06 +03002942 if (freespace_inode)
2943 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002945 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002946 if (IS_ERR(trans)) {
2947 ret = PTR_ERR(trans);
2948 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002949 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002950 }
Chris Masona79b7d42014-05-22 16:18:52 -07002951
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002952 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002953
Chris Masonc8b97812008-10-29 14:49:59 -04002954 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002955 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002956 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002957 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002958 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002962 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002963 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002964 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002965 if (!ret) {
2966 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002967 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002968 ordered_extent->disk_bytenr,
2969 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002970 }
Yan Zhengd899e052008-10-30 14:25:28 -04002971 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002972 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002973 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002975 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002976 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002978
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002979 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002980 if (ret) {
2981 btrfs_abort_transaction(trans, ret);
2982 goto out;
2983 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984
Filipe Manana2766ff62020-11-04 11:07:34 +00002985 /*
2986 * If this is a new delalloc range, clear its new delalloc flag to
2987 * update the inode's number of bytes. This needs to be done first
2988 * before updating the inode item.
2989 */
2990 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2991 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002992 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002993 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2994 0, 0, &cached_state);
2995
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002996 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002997 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002998 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002999 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003000 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003001 }
3002 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003003out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003004 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003005 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003006 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003007
Miao Xiea698d0752012-09-20 01:51:59 -06003008 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003009 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003010
Josef Bacik77cef2e2013-08-29 13:57:21 -04003011 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003012 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003013
3014 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003015 unwritten_start += logical_len;
3016 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003017
3018 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003019 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003020
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003021 /*
3022 * If the ordered extent had an IOERR or something else went
3023 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003024 * back to the allocator. We only free the extent in the
3025 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003026 *
3027 * If we made it past insert_reserved_file_extent before we
3028 * errored out then we don't need to do this as the accounting
3029 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003030 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003031 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003032 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003033 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003034 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3035 /*
3036 * Discard the range before returning it back to the
3037 * free space pool
3038 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003039 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003040 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003041 ordered_extent->disk_bytenr,
3042 ordered_extent->disk_num_bytes,
3043 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003044 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003045 ordered_extent->disk_bytenr,
3046 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003047 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003048 }
3049
Josef Bacik5fd02042012-05-02 14:00:54 -04003050 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003051 * This needs to be done to make sure anybody waiting knows we are done
3052 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003053 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003054 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055
Chris Masone6dcd2d2008-07-17 12:53:50 -04003056 /* once for us */
3057 btrfs_put_ordered_extent(ordered_extent);
3058 /* once for the tree */
3059 btrfs_put_ordered_extent(ordered_extent);
3060
Josef Bacik5fd02042012-05-02 14:00:54 -04003061 return ret;
3062}
3063
3064static void finish_ordered_fn(struct btrfs_work *work)
3065{
3066 struct btrfs_ordered_extent *ordered_extent;
3067 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3068 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069}
3070
Nikolay Borisovc6297322018-11-08 10:18:08 +02003071void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3072 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003073{
Nikolay Borisov3347c482020-08-31 14:42:44 +03003074 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
3075 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04003076 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003077 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04003078
liubo1abe9b82011-03-24 11:18:59 +00003079 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3080
Chris Mason8b62b722009-09-02 16:53:46 -04003081 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03003082 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3083 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003084 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003085
Nikolay Borisov3347c482020-08-31 14:42:44 +03003086 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003087 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003088 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003089 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04003090
Omar Sandovala0cac0e2019-09-16 11:30:57 -07003091 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08003092 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003093}
3094
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003095/*
3096 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003097 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003098 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003099 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003100 * @page: page where is the data to be verified
3101 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003102 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003103 *
3104 * The length of such check is always one sector size.
3105 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003106static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003107 u32 bio_offset, struct page *page, u32 pgoff,
3108 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003109{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3111 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003112 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003113 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003114 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003115 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003116 u8 *csum_expected;
3117 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003118
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003119 ASSERT(pgoff + len <= PAGE_SIZE);
3120
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003121 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3122 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003123
3124 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003125 shash->tfm = fs_info->csum_shash;
3126
Eric Biggersfd080012020-04-30 23:51:59 -07003127 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003128
3129 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003130 goto zeroit;
3131
3132 kunmap_atomic(kaddr);
3133 return 0;
3134zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003135 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3136 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003137 if (io_bio->device)
3138 btrfs_dev_stat_inc_and_print(io_bio->device,
3139 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003140 memset(kaddr + pgoff, 1, len);
3141 flush_dcache_page(page);
3142 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003143 return -EIO;
3144}
3145
Chris Masond352ac62008-09-29 15:18:18 -04003146/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003147 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003148 * if there's a match, we allow the bio to finish. If not, the code in
3149 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003150 *
3151 * @bio_offset: offset to the beginning of the bio (in bytes)
3152 * @start: file offset of the range start
3153 * @end: file offset of the range end (inclusive)
3154 * @mirror: mirror number
Chris Masond352ac62008-09-29 15:18:18 -04003155 */
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003156int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
Nikolay Borisov9a446d62020-09-18 16:34:33 +03003157 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003158{
Chris Mason07157aa2007-08-30 08:50:51 -04003159 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003160 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003161 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003162 const u32 sectorsize = root->fs_info->sectorsize;
3163 u32 pg_off;
Chris Masond1310b22008-01-24 16:13:08 -05003164
Chris Masond20f7042008-12-08 16:58:54 -05003165 if (PageChecked(page)) {
3166 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003167 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003168 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003169
3170 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003171 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003172
Josef Bacik42437a62020-10-16 11:29:18 -04003173 if (!root->fs_info->csum_root)
3174 return 0;
3175
Yan Zheng17d217f2008-12-12 10:03:38 -05003176 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003177 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003178 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003179 return 0;
3180 }
3181
Qu Wenruof44cf412020-12-02 14:47:59 +08003182 ASSERT(page_offset(page) <= start &&
3183 end <= page_offset(page) + PAGE_SIZE - 1);
3184 for (pg_off = offset_in_page(start);
3185 pg_off < offset_in_page(end);
3186 pg_off += sectorsize, bio_offset += sectorsize) {
3187 int ret;
3188
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003189 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3190 page_offset(page) + pg_off);
Qu Wenruof44cf412020-12-02 14:47:59 +08003191 if (ret < 0)
3192 return -EIO;
3193 }
3194 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -04003195}
Chris Masonb888db22007-08-27 16:49:44 -04003196
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003197/*
3198 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3199 *
3200 * @inode: The inode we want to perform iput on
3201 *
3202 * This function uses the generic vfs_inode::i_count to track whether we should
3203 * just decrement it (in case it's > 1) or if this is the last iput then link
3204 * the inode to the delayed iput machinery. Delayed iputs are processed at
3205 * transaction commit time/superblock commit/cleaner kthread.
3206 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003207void btrfs_add_delayed_iput(struct inode *inode)
3208{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003209 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003210 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003211
3212 if (atomic_add_unless(&inode->i_count, -1, 1))
3213 return;
3214
Josef Bacik034f7842018-12-03 11:06:52 -05003215 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003217 ASSERT(list_empty(&binode->delayed_iput));
3218 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003220 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3221 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003222}
3223
Josef Bacik63611e72019-06-18 10:59:18 -04003224static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3225 struct btrfs_inode *inode)
3226{
3227 list_del_init(&inode->delayed_iput);
3228 spin_unlock(&fs_info->delayed_iput_lock);
3229 iput(&inode->vfs_inode);
3230 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3231 wake_up(&fs_info->delayed_iputs_wait);
3232 spin_lock(&fs_info->delayed_iput_lock);
3233}
3234
3235static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3236 struct btrfs_inode *inode)
3237{
3238 if (!list_empty(&inode->delayed_iput)) {
3239 spin_lock(&fs_info->delayed_iput_lock);
3240 if (!list_empty(&inode->delayed_iput))
3241 run_delayed_iput_locked(fs_info, inode);
3242 spin_unlock(&fs_info->delayed_iput_lock);
3243 }
3244}
3245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003246void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003248
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003250 while (!list_empty(&fs_info->delayed_iputs)) {
3251 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003252
David Sterba8089fe62015-11-19 14:15:51 +01003253 inode = list_first_entry(&fs_info->delayed_iputs,
3254 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003255 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003256 }
David Sterba8089fe62015-11-19 14:15:51 +01003257 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003258}
3259
Josef Bacik034f7842018-12-03 11:06:52 -05003260/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003261 * Wait for flushing all delayed iputs
3262 *
3263 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003264 *
3265 * This will wait on any delayed iputs that are currently running with KILLABLE
3266 * set. Once they are all done running we will return, unless we are killed in
3267 * which case we return EINTR. This helps in user operations like fallocate etc
3268 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003269 *
3270 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003271 */
3272int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3273{
3274 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3275 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3276 if (ret)
3277 return -EINTR;
3278 return 0;
3279}
3280
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003282 * This creates an orphan entry for the given inode in case something goes wrong
3283 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003284 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003285int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003286 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003287{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003289
Omar Sandoval27919062018-05-11 13:13:37 -07003290 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3291 if (ret && ret != -EEXIST) {
3292 btrfs_abort_transaction(trans, ret);
3293 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003294 }
3295
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003297}
3298
3299/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003300 * We have done the delete so we can go ahead and remove the orphan item for
3301 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003302 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003303static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003304 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003305{
Omar Sandoval27919062018-05-11 13:13:37 -07003306 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003307}
3308
3309/*
3310 * this cleans up any orphans that may be left on the list from the last use
3311 * of this root.
3312 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003313int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003315 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003316 struct btrfs_path *path;
3317 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003318 struct btrfs_key key, found_key;
3319 struct btrfs_trans_handle *trans;
3320 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003321 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003322 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003323
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003325 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003326
3327 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328 if (!path) {
3329 ret = -ENOMEM;
3330 goto out;
3331 }
David Sterbae4058b52015-11-27 16:31:35 +01003332 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
3334 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003335 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003336 key.offset = (u64)-1;
3337
Josef Bacik7b128762008-07-24 12:17:14 -04003338 while (1) {
3339 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 if (ret < 0)
3341 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003342
3343 /*
3344 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003345 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003346 * find the key and see if we have stuff that matches
3347 */
3348 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003349 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003350 if (path->slots[0] == 0)
3351 break;
3352 path->slots[0]--;
3353 }
3354
3355 /* pull out the item */
3356 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003357 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3358
3359 /* make sure the item matches what we want */
3360 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3361 break;
David Sterba962a2982014-06-04 18:41:45 +02003362 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003363 break;
3364
3365 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003366 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003367
3368 /*
3369 * this is where we are basically btrfs_lookup, without the
3370 * crossing root thing. we store the inode number in the
3371 * offset of the orphan item.
3372 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003373
3374 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 btrfs_err(fs_info,
3376 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003377 ret = -EINVAL;
3378 goto out;
3379 }
3380
3381 last_objectid = found_key.offset;
3382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003383 found_key.objectid = found_key.offset;
3384 found_key.type = BTRFS_INODE_ITEM_KEY;
3385 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003386 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303387 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003388 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003389 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003391 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003392 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003393 int is_dead_root = 0;
3394
3395 /*
3396 * this is an orphan in the tree root. Currently these
3397 * could come from 2 sources:
3398 * a) a snapshot deletion in progress
3399 * b) a free space cache inode
3400 * We need to distinguish those two, as the snapshot
3401 * orphan must not get deleted.
3402 * find_dead_roots already ran before us, so if this
3403 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003404 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003405 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003406
3407 spin_lock(&fs_info->fs_roots_radix_lock);
3408 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3409 (unsigned long)found_key.objectid);
3410 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3411 is_dead_root = 1;
3412 spin_unlock(&fs_info->fs_roots_radix_lock);
3413
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003414 if (is_dead_root) {
3415 /* prevent this orphan from being found again */
3416 key.offset = found_key.objectid - 1;
3417 continue;
3418 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003419
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003420 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003421
Josef Bacika8c9e572011-09-21 16:55:59 -04003422 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003423 * If we have an inode with links, there are a couple of
3424 * possibilities. Old kernels (before v3.12) used to create an
3425 * orphan item for truncate indicating that there were possibly
3426 * extent items past i_size that needed to be deleted. In v3.12,
3427 * truncate was changed to update i_size in sync with the extent
3428 * items, but the (useless) orphan item was still created. Since
3429 * v4.18, we don't create the orphan item for truncate at all.
3430 *
3431 * So, this item could mean that we need to do a truncate, but
3432 * only if this filesystem was last used on a pre-v3.12 kernel
3433 * and was not cleanly unmounted. The odds of that are quite
3434 * slim, and it's a pain to do the truncate now, so just delete
3435 * the orphan item.
3436 *
3437 * It's also possible that this orphan item was supposed to be
3438 * deleted but wasn't. The inode number may have been reused,
3439 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003440 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003441 if (ret == -ENOENT || inode->i_nlink) {
3442 if (!ret)
3443 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003444 trans = btrfs_start_transaction(root, 1);
3445 if (IS_ERR(trans)) {
3446 ret = PTR_ERR(trans);
3447 goto out;
3448 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003449 btrfs_debug(fs_info, "auto deleting %Lu",
3450 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003451 ret = btrfs_del_orphan_item(trans, root,
3452 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003453 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003454 if (ret)
3455 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003456 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 }
Josef Bacik7b128762008-07-24 12:17:14 -04003458
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003459 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003460
3461 /* this will do delete_inode and everything for us */
3462 iput(inode);
3463 }
Miao Xie3254c872011-11-10 20:45:05 -05003464 /* release the path since we're done with it */
3465 btrfs_release_path(path);
3466
Yan, Zhengd68fc572010-05-16 10:49:58 -04003467 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3468
Omar Sandovala575cee2018-05-11 13:13:38 -07003469 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003470 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003471 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003472 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003473 }
Josef Bacik7b128762008-07-24 12:17:14 -04003474
3475 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003477
3478out:
3479 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003480 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481 btrfs_free_path(path);
3482 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003483}
3484
Chris Masond352ac62008-09-29 15:18:18 -04003485/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003486 * very simple check to peek ahead in the leaf looking for xattrs. If we
3487 * don't find any xattrs, we know there can't be any acls.
3488 *
3489 * slot is the slot the inode is in, objectid is the objectid of the inode
3490 */
3491static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003492 int slot, u64 objectid,
3493 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003494{
3495 u32 nritems = btrfs_header_nritems(leaf);
3496 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003497 static u64 xattr_access = 0;
3498 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003499 int scanned = 0;
3500
Josef Bacikf23b5a52013-06-19 10:16:26 -04003501 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003502 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3503 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3504 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3505 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003506 }
3507
Chris Mason46a53cc2009-04-27 11:47:50 -04003508 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003509 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003510 while (slot < nritems) {
3511 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3512
3513 /* we found a different objectid, there must not be acls */
3514 if (found_key.objectid != objectid)
3515 return 0;
3516
3517 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003518 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003519 if (*first_xattr_slot == -1)
3520 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 if (found_key.offset == xattr_access ||
3522 found_key.offset == xattr_default)
3523 return 1;
3524 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003525
3526 /*
3527 * we found a key greater than an xattr key, there can't
3528 * be any acls later on
3529 */
3530 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3531 return 0;
3532
3533 slot++;
3534 scanned++;
3535
3536 /*
3537 * it goes inode, inode backrefs, xattrs, extents,
3538 * so if there are a ton of hard links to an inode there can
3539 * be a lot of backrefs. Don't waste time searching too hard,
3540 * this is just an optimization
3541 */
3542 if (scanned >= 8)
3543 break;
3544 }
3545 /* we hit the end of the leaf before we found an xattr or
3546 * something larger than an xattr. We have to assume the inode
3547 * has acls
3548 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003549 if (*first_xattr_slot == -1)
3550 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003551 return 1;
3552}
3553
3554/*
Chris Masond352ac62008-09-29 15:18:18 -04003555 * read an inode from the btree into the in-memory inode
3556 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003557static int btrfs_read_locked_inode(struct inode *inode,
3558 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003561 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003562 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003563 struct btrfs_inode_item *inode_item;
3564 struct btrfs_root *root = BTRFS_I(inode)->root;
3565 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003566 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003567 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003568 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003570 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003571 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003572
3573 ret = btrfs_fill_inode(inode, &rdev);
3574 if (!ret)
3575 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003576
Filipe Manana4222ea72018-10-24 10:13:03 +01003577 if (!path) {
3578 path = btrfs_alloc_path();
3579 if (!path)
3580 return -ENOMEM;
3581 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003582
Chris Mason39279cc2007-06-12 06:35:45 -04003583 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003584
Chris Mason39279cc2007-06-12 06:35:45 -04003585 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003586 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003587 if (path != in_path)
3588 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003589 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003590 }
Chris Mason39279cc2007-06-12 06:35:45 -04003591
Chris Mason5f39d392007-10-15 16:14:19 -04003592 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003593
3594 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003595 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003596
Chris Mason5f39d392007-10-15 16:14:19 -04003597 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3598 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003599 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003600 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003601 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3602 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003603 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003604 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3605 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003606
David Sterbaa937b972014-12-12 17:39:12 +01003607 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3608 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003609
David Sterbaa937b972014-12-12 17:39:12 +01003610 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3611 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003612
David Sterbaa937b972014-12-12 17:39:12 +01003613 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3614 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003615
chandan r9cc97d62012-07-04 12:48:07 +05303616 BTRFS_I(inode)->i_otime.tv_sec =
3617 btrfs_timespec_sec(leaf, &inode_item->otime);
3618 BTRFS_I(inode)->i_otime.tv_nsec =
3619 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003620
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003621 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003622 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003623 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3624
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003625 inode_set_iversion_queried(inode,
3626 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003627 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003628 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003629 rdev = btrfs_inode_rdev(leaf, inode_item);
3630
Josef Bacikaec74772008-07-24 12:12:38 -04003631 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003632 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003633
3634cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003635 /*
3636 * If we were modified in the current generation and evicted from memory
3637 * and then re-read we need to do a full sync since we don't have any
3638 * idea about which extents were modified before we were evicted from
3639 * cache.
3640 *
3641 * This is required for both inode re-read from disk and delayed inode
3642 * in delayed_nodes_tree.
3643 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003644 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003645 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3646 &BTRFS_I(inode)->runtime_flags);
3647
Filipe Mananabde6c242015-07-24 00:00:19 +01003648 /*
3649 * We don't persist the id of the transaction where an unlink operation
3650 * against the inode was last made. So here we assume the inode might
3651 * have been evicted, and therefore the exact value of last_unlink_trans
3652 * lost, and set it to last_trans to avoid metadata inconsistencies
3653 * between the inode and its parent if the inode is fsync'ed and the log
3654 * replayed. For example, in the scenario:
3655 *
3656 * touch mydir/foo
3657 * ln mydir/foo mydir/bar
3658 * sync
3659 * unlink mydir/bar
3660 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3661 * xfs_io -c fsync mydir/foo
3662 * <power failure>
3663 * mount fs, triggers fsync log replay
3664 *
3665 * We must make sure that when we fsync our inode foo we also log its
3666 * parent inode, otherwise after log replay the parent still has the
3667 * dentry with the "bar" name but our inode foo has a link count of 1
3668 * and doesn't have an inode ref with the name "bar" anymore.
3669 *
3670 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003671 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003672 * transaction commits on fsync if our inode is a directory, or if our
3673 * inode is not a directory, logging its parent unnecessarily.
3674 */
3675 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3676
Filipe Manana3ebac172020-07-15 12:30:43 +01003677 /*
3678 * Same logic as for last_unlink_trans. We don't persist the generation
3679 * of the last transaction where this inode was used for a reflink
3680 * operation, so after eviction and reloading the inode we must be
3681 * pessimistic and assume the last transaction that modified the inode.
3682 */
3683 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3684
Miao Xie67de1172013-12-26 13:07:06 +08003685 path->slots[0]++;
3686 if (inode->i_nlink != 1 ||
3687 path->slots[0] >= btrfs_header_nritems(leaf))
3688 goto cache_acl;
3689
3690 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003691 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003692 goto cache_acl;
3693
3694 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3695 if (location.type == BTRFS_INODE_REF_KEY) {
3696 struct btrfs_inode_ref *ref;
3697
3698 ref = (struct btrfs_inode_ref *)ptr;
3699 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3700 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3701 struct btrfs_inode_extref *extref;
3702
3703 extref = (struct btrfs_inode_extref *)ptr;
3704 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3705 extref);
3706 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003707cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003708 /*
3709 * try to precache a NULL acl entry for files that don't have
3710 * any xattrs or acls
3711 */
Li Zefan33345d012011-04-20 10:31:50 +08003712 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003713 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003714 if (first_xattr_slot != -1) {
3715 path->slots[0] = first_xattr_slot;
3716 ret = btrfs_load_inode_props(inode, path);
3717 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003718 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003719 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003720 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003721 root->root_key.objectid, ret);
3722 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003723 if (path != in_path)
3724 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725
Al Viro72c04902009-06-24 16:58:48 -04003726 if (!maybe_acls)
3727 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003728
Chris Mason39279cc2007-06-12 06:35:45 -04003729 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003730 case S_IFREG:
3731 inode->i_mapping->a_ops = &btrfs_aops;
3732 inode->i_fop = &btrfs_file_operations;
3733 inode->i_op = &btrfs_file_inode_operations;
3734 break;
3735 case S_IFDIR:
3736 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003737 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 break;
3739 case S_IFLNK:
3740 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003741 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003742 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003744 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003745 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003746 init_special_inode(inode, inode->i_mode, rdev);
3747 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003749
David Sterba7b6a2212018-03-26 18:40:21 +02003750 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003751 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003752}
3753
Chris Masond352ac62008-09-29 15:18:18 -04003754/*
3755 * given a leaf and an inode, copy the inode fields into the leaf
3756 */
Chris Masone02119d2008-09-05 16:13:11 -04003757static void fill_inode_item(struct btrfs_trans_handle *trans,
3758 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct inode *inode)
3761{
Liu Bo51fab692012-12-27 09:01:21 +00003762 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003763
David Sterbac82f8232019-08-09 17:48:21 +02003764 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003765
David Sterbacc4c13d2020-04-29 02:15:56 +02003766 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3767 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3768 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3769 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3770 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003771
David Sterbacc4c13d2020-04-29 02:15:56 +02003772 btrfs_set_token_timespec_sec(&token, &item->atime,
3773 inode->i_atime.tv_sec);
3774 btrfs_set_token_timespec_nsec(&token, &item->atime,
3775 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
David Sterbacc4c13d2020-04-29 02:15:56 +02003777 btrfs_set_token_timespec_sec(&token, &item->mtime,
3778 inode->i_mtime.tv_sec);
3779 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3780 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003781
David Sterbacc4c13d2020-04-29 02:15:56 +02003782 btrfs_set_token_timespec_sec(&token, &item->ctime,
3783 inode->i_ctime.tv_sec);
3784 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3785 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003786
David Sterbacc4c13d2020-04-29 02:15:56 +02003787 btrfs_set_token_timespec_sec(&token, &item->otime,
3788 BTRFS_I(inode)->i_otime.tv_sec);
3789 btrfs_set_token_timespec_nsec(&token, &item->otime,
3790 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303791
David Sterbacc4c13d2020-04-29 02:15:56 +02003792 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3793 btrfs_set_token_inode_generation(&token, item,
3794 BTRFS_I(inode)->generation);
3795 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3796 btrfs_set_token_inode_transid(&token, item, trans->transid);
3797 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3798 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3799 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003800}
3801
Chris Masond352ac62008-09-29 15:18:18 -04003802/*
3803 * copy everything in the in-memory inode into the btree.
3804 */
Chris Mason21151332011-11-10 20:39:08 -05003805static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003806 struct btrfs_root *root,
3807 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003808{
3809 struct btrfs_inode_item *inode_item;
3810 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003811 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 int ret;
3813
3814 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003815 if (!path)
3816 return -ENOMEM;
3817
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003818 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003819 if (ret) {
3820 if (ret > 0)
3821 ret = -ENOENT;
3822 goto failed;
3823 }
3824
Chris Mason5f39d392007-10-15 16:14:19 -04003825 leaf = path->nodes[0];
3826 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003828
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003829 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003830 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003831 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 ret = 0;
3833failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003834 btrfs_free_path(path);
3835 return ret;
3836}
3837
Chris Masond352ac62008-09-29 15:18:18 -04003838/*
Chris Mason21151332011-11-10 20:39:08 -05003839 * copy everything in the in-memory inode into the btree.
3840 */
3841noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003842 struct btrfs_root *root,
3843 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003844{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003845 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003846 int ret;
3847
3848 /*
3849 * If the inode is a free space inode, we can deadlock during commit
3850 * if we put it into the delayed code.
3851 *
3852 * The data relocation inode should also be directly updated
3853 * without delay
3854 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003855 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003856 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003857 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003858 btrfs_update_root_times(trans, root);
3859
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003860 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003861 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003862 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003863 return ret;
3864 }
3865
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003866 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003867}
3868
Nikolay Borisov729f7962020-11-02 16:49:06 +02003869int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3870 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003871{
3872 int ret;
3873
Nikolay Borisov729f7962020-11-02 16:49:06 +02003874 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003875 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003876 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003877 return ret;
3878}
3879
3880/*
Chris Masond352ac62008-09-29 15:18:18 -04003881 * unlink helper that gets used here in inode.c and in the tree logging
3882 * recovery code. It remove a link in a directory with a given name, and
3883 * also drops the back refs in the inode to the directory
3884 */
Al Viro92986792011-03-04 17:14:37 +00003885static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3886 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003887 struct btrfs_inode *dir,
3888 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003889 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003891 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003893 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003894 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003895 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003896 u64 ino = btrfs_ino(inode);
3897 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003898
3899 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003900 if (!path) {
3901 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003902 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003903 }
3904
Li Zefan33345d012011-04-20 10:31:50 +08003905 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003906 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003907 if (IS_ERR_OR_NULL(di)) {
3908 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 goto err;
3910 }
Chris Mason39279cc2007-06-12 06:35:45 -04003911 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003912 if (ret)
3913 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003914 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003915
Miao Xie67de1172013-12-26 13:07:06 +08003916 /*
3917 * If we don't have dir index, we have to get it by looking up
3918 * the inode ref, since we get the inode ref, remove it directly,
3919 * it is unnecessary to do delayed deletion.
3920 *
3921 * But if we have dir index, needn't search inode ref to get it.
3922 * Since the inode ref is close to the inode item, it is better
3923 * that we delay to delete it, and just do this deletion when
3924 * we update the inode item.
3925 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003926 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003927 ret = btrfs_delayed_delete_inode_ref(inode);
3928 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003929 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003930 goto skip_backref;
3931 }
3932 }
3933
Li Zefan33345d012011-04-20 10:31:50 +08003934 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3935 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003936 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003937 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003938 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003939 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003940 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003941 goto err;
3942 }
Miao Xie67de1172013-12-26 13:07:06 +08003943skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003944 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003945 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003946 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003947 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003948 }
Chris Mason39279cc2007-06-12 06:35:45 -04003949
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003950 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3951 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003952 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003953 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003954 goto err;
3955 }
Chris Masone02119d2008-09-05 16:13:11 -04003956
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003957 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3958 index);
Chris Mason6418c962010-10-30 07:34:24 -04003959 if (ret == -ENOENT)
3960 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003961 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003962 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003963
3964 /*
3965 * If we have a pending delayed iput we could end up with the final iput
3966 * being run in btrfs-cleaner context. If we have enough of these built
3967 * up we can end up burning a lot of time in btrfs-cleaner without any
3968 * way to throttle the unlinks. Since we're currently holding a ref on
3969 * the inode we can run the delayed iput here without any issues as the
3970 * final iput won't be done until after we drop the ref we're currently
3971 * holding.
3972 */
3973 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003974err:
3975 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003976 if (ret)
3977 goto out;
3978
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003979 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003980 inode_inc_iversion(&inode->vfs_inode);
3981 inode_inc_iversion(&dir->vfs_inode);
3982 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3983 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003984 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003985out:
Chris Mason39279cc2007-06-12 06:35:45 -04003986 return ret;
3987}
3988
Al Viro92986792011-03-04 17:14:37 +00003989int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3990 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003991 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003992 const char *name, int name_len)
3993{
3994 int ret;
3995 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3996 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003997 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003998 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00003999 }
4000 return ret;
4001}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004002
4003/*
4004 * helper to start transaction for unlink and rmdir.
4005 *
Josef Bacikd52be812013-05-29 14:54:47 -04004006 * unlink and rmdir are special in btrfs, they do not always free space, so
4007 * if we cannot make our reservations the normal way try and see if there is
4008 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4009 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010 */
Josef Bacikd52be812013-05-29 14:54:47 -04004011static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004013 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014
Josef Bacike70bea52011-10-11 14:18:24 -04004015 /*
4016 * 1 for the possible orphan item
4017 * 1 for the dir item
4018 * 1 for the dir index
4019 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004020 * 1 for the inode
4021 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004022 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023}
4024
Chris Mason39279cc2007-06-12 06:35:45 -04004025static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4026{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004027 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004028 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004029 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 int ret;
4031
Josef Bacikd52be812013-05-29 14:54:47 -04004032 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004033 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004034 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004035
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004036 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4037 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004038
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004039 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4040 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4041 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004042 if (ret)
4043 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004044
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004046 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004047 if (ret)
4048 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004049 }
Josef Bacik7b128762008-07-24 12:17:14 -04004050
Tsutomu Itohb5324022011-07-19 07:27:20 +00004051out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004052 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004053 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 return ret;
4055}
4056
Misono Tomohirof60a2362018-04-18 11:34:52 +09004057static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004058 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004059{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004060 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004061 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062 struct btrfs_path *path;
4063 struct extent_buffer *leaf;
4064 struct btrfs_dir_item *di;
4065 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004066 const char *name = dentry->d_name.name;
4067 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068 u64 index;
4069 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004070 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004071 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072
Josef Bacik045d3962019-12-18 17:20:27 -05004073 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4074 objectid = inode->root->root_key.objectid;
4075 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4076 objectid = inode->location.objectid;
4077 } else {
4078 WARN_ON(1);
4079 return -EINVAL;
4080 }
4081
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082 path = btrfs_alloc_path();
4083 if (!path)
4084 return -ENOMEM;
4085
Li Zefan33345d012011-04-20 10:31:50 +08004086 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004089 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 goto out;
4091 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092
4093 leaf = path->nodes[0];
4094 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4095 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4096 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004098 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 goto out;
4100 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004101 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
Josef Bacikd49d3282019-12-18 17:20:28 -05004103 /*
4104 * This is a placeholder inode for a subvolume we didn't have a
4105 * reference to at the time of the snapshot creation. In the meantime
4106 * we could have renamed the real subvol link into our snapshot, so
4107 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4108 * Instead simply lookup the dir_index_item for this entry so we can
4109 * remove it. Otherwise we know we have a ref to the root and we can
4110 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4111 */
4112 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004113 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 if (IS_ERR_OR_NULL(di)) {
4116 if (!di)
4117 ret = -ENOENT;
4118 else
4119 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004120 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004121 goto out;
4122 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004123
4124 leaf = path->nodes[0];
4125 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004126 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004127 btrfs_release_path(path);
4128 } else {
4129 ret = btrfs_del_root_ref(trans, objectid,
4130 root->root_key.objectid, dir_ino,
4131 &index, name, name_len);
4132 if (ret) {
4133 btrfs_abort_transaction(trans, ret);
4134 goto out;
4135 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004137
Lu Fengqi9add2942018-08-01 11:32:26 +08004138 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 goto out;
4142 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004143
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004144 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004145 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004146 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004147 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004149 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004151 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153}
4154
Misono Tomohiroec42f162018-04-18 11:34:13 +09004155/*
4156 * Helper to check if the subvolume references other subvolumes or if it's
4157 * default.
4158 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004159static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004160{
4161 struct btrfs_fs_info *fs_info = root->fs_info;
4162 struct btrfs_path *path;
4163 struct btrfs_dir_item *di;
4164 struct btrfs_key key;
4165 u64 dir_id;
4166 int ret;
4167
4168 path = btrfs_alloc_path();
4169 if (!path)
4170 return -ENOMEM;
4171
4172 /* Make sure this root isn't set as the default subvol */
4173 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4174 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4175 dir_id, "default", 7, 0);
4176 if (di && !IS_ERR(di)) {
4177 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4178 if (key.objectid == root->root_key.objectid) {
4179 ret = -EPERM;
4180 btrfs_err(fs_info,
4181 "deleting default subvolume %llu is not allowed",
4182 key.objectid);
4183 goto out;
4184 }
4185 btrfs_release_path(path);
4186 }
4187
4188 key.objectid = root->root_key.objectid;
4189 key.type = BTRFS_ROOT_REF_KEY;
4190 key.offset = (u64)-1;
4191
4192 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4193 if (ret < 0)
4194 goto out;
4195 BUG_ON(ret == 0);
4196
4197 ret = 0;
4198 if (path->slots[0] > 0) {
4199 path->slots[0]--;
4200 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4201 if (key.objectid == root->root_key.objectid &&
4202 key.type == BTRFS_ROOT_REF_KEY)
4203 ret = -ENOTEMPTY;
4204 }
4205out:
4206 btrfs_free_path(path);
4207 return ret;
4208}
4209
Nikolay Borisov20a68002018-04-27 14:36:24 +03004210/* Delete all dentries for inodes belonging to the root */
4211static void btrfs_prune_dentries(struct btrfs_root *root)
4212{
4213 struct btrfs_fs_info *fs_info = root->fs_info;
4214 struct rb_node *node;
4215 struct rb_node *prev;
4216 struct btrfs_inode *entry;
4217 struct inode *inode;
4218 u64 objectid = 0;
4219
4220 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4221 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4222
4223 spin_lock(&root->inode_lock);
4224again:
4225 node = root->inode_tree.rb_node;
4226 prev = NULL;
4227 while (node) {
4228 prev = node;
4229 entry = rb_entry(node, struct btrfs_inode, rb_node);
4230
David Sterba37508512018-06-29 10:56:40 +02004231 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004232 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004233 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004234 node = node->rb_right;
4235 else
4236 break;
4237 }
4238 if (!node) {
4239 while (prev) {
4240 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004241 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004242 node = prev;
4243 break;
4244 }
4245 prev = rb_next(prev);
4246 }
4247 }
4248 while (node) {
4249 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004250 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004251 inode = igrab(&entry->vfs_inode);
4252 if (inode) {
4253 spin_unlock(&root->inode_lock);
4254 if (atomic_read(&inode->i_count) > 1)
4255 d_prune_aliases(inode);
4256 /*
4257 * btrfs_drop_inode will have it removed from the inode
4258 * cache when its usage count hits zero.
4259 */
4260 iput(inode);
4261 cond_resched();
4262 spin_lock(&root->inode_lock);
4263 goto again;
4264 }
4265
4266 if (cond_resched_lock(&root->inode_lock))
4267 goto again;
4268
4269 node = rb_next(node);
4270 }
4271 spin_unlock(&root->inode_lock);
4272}
4273
Misono Tomohirof60a2362018-04-18 11:34:52 +09004274int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4275{
4276 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4277 struct btrfs_root *root = BTRFS_I(dir)->root;
4278 struct inode *inode = d_inode(dentry);
4279 struct btrfs_root *dest = BTRFS_I(inode)->root;
4280 struct btrfs_trans_handle *trans;
4281 struct btrfs_block_rsv block_rsv;
4282 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004283 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004284
4285 /*
4286 * Don't allow to delete a subvolume with send in progress. This is
4287 * inside the inode lock so the error handling that has to drop the bit
4288 * again is not run concurrently.
4289 */
4290 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004291 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004292 spin_unlock(&dest->root_item_lock);
4293 btrfs_warn(fs_info,
4294 "attempt to delete subvolume %llu during send",
4295 dest->root_key.objectid);
4296 return -EPERM;
4297 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004298 root_flags = btrfs_root_flags(&dest->root_item);
4299 btrfs_set_root_flags(&dest->root_item,
4300 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4301 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004302
4303 down_write(&fs_info->subvol_sem);
4304
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004305 ret = may_destroy_subvol(dest);
4306 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004307 goto out_up_write;
4308
4309 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4310 /*
4311 * One for dir inode,
4312 * two for dir entries,
4313 * two for root ref/backref.
4314 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004315 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4316 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004317 goto out_up_write;
4318
4319 trans = btrfs_start_transaction(root, 0);
4320 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004321 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004322 goto out_release;
4323 }
4324 trans->block_rsv = &block_rsv;
4325 trans->bytes_reserved = block_rsv.size;
4326
4327 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4328
Josef Bacik045d3962019-12-18 17:20:27 -05004329 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004330 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004331 btrfs_abort_transaction(trans, ret);
4332 goto out_end_trans;
4333 }
4334
4335 btrfs_record_root_in_trans(trans, dest);
4336
4337 memset(&dest->root_item.drop_progress, 0,
4338 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004339 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004340 btrfs_set_root_refs(&dest->root_item, 0);
4341
4342 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4343 ret = btrfs_insert_orphan_item(trans,
4344 fs_info->tree_root,
4345 dest->root_key.objectid);
4346 if (ret) {
4347 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004348 goto out_end_trans;
4349 }
4350 }
4351
Lu Fengqid1957792018-05-29 15:01:54 +08004352 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004353 BTRFS_UUID_KEY_SUBVOL,
4354 dest->root_key.objectid);
4355 if (ret && ret != -ENOENT) {
4356 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004357 goto out_end_trans;
4358 }
4359 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004360 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004361 dest->root_item.received_uuid,
4362 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4363 dest->root_key.objectid);
4364 if (ret && ret != -ENOENT) {
4365 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004366 goto out_end_trans;
4367 }
4368 }
4369
Qu Wenruo082b6c92020-06-16 10:17:37 +08004370 free_anon_bdev(dest->anon_dev);
4371 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004372out_end_trans:
4373 trans->block_rsv = NULL;
4374 trans->bytes_reserved = 0;
4375 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004376 inode->i_flags |= S_DEAD;
4377out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004378 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004379out_up_write:
4380 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004381 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004382 spin_lock(&dest->root_item_lock);
4383 root_flags = btrfs_root_flags(&dest->root_item);
4384 btrfs_set_root_flags(&dest->root_item,
4385 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4386 spin_unlock(&dest->root_item_lock);
4387 } else {
4388 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004389 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004390 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004391 }
4392
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004393 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004394}
4395
Chris Mason39279cc2007-06-12 06:35:45 -04004396static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4397{
David Howells2b0143b2015-03-17 22:25:59 +00004398 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004399 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004400 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004401 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004402 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004403
David Sterbab3ae2442012-09-13 16:04:34 -06004404 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004405 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004406 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004407 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004408
Josef Bacikd52be812013-05-29 14:54:47 -04004409 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004410 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004411 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004412
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004413 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004414 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004415 goto out;
4416 }
4417
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004418 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004419 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004420 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004421
Filipe Manana44f714d2016-06-06 16:11:13 +01004422 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4423
Chris Mason39279cc2007-06-12 06:35:45 -04004424 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004425 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4426 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4427 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004428 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004429 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004430 /*
4431 * Propagate the last_unlink_trans value of the deleted dir to
4432 * its parent directory. This is to prevent an unrecoverable
4433 * log tree in the case we do something like this:
4434 * 1) create dir foo
4435 * 2) create snapshot under dir foo
4436 * 3) delete the snapshot
4437 * 4) rmdir foo
4438 * 5) mkdir foo
4439 * 6) fsync foo or some file inside foo
4440 */
4441 if (last_unlink_trans >= trans->transid)
4442 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4443 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004444out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004445 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004446 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004447
Chris Mason39279cc2007-06-12 06:35:45 -04004448 return err;
4449}
4450
Josef Bacikddfae632017-10-19 14:16:02 -04004451/*
4452 * Return this if we need to call truncate_block for the last bit of the
4453 * truncate.
4454 */
4455#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004456
Chris Mason323ac952008-10-01 19:05:46 -04004457/*
Chris Mason39279cc2007-06-12 06:35:45 -04004458 * this can truncate away extent items, csum items and directory items.
4459 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004460 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004461 *
4462 * csum items that cross the new i_size are truncated to the new size
4463 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004464 *
4465 * min_type is the minimum key type to truncate down to. If set to 0, this
4466 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004467 */
Yan, Zheng80825102009-11-12 09:35:36 +00004468int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4469 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004470 struct btrfs_inode *inode,
Yan, Zheng80825102009-11-12 09:35:36 +00004471 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004472{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004473 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004475 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004477 struct btrfs_key key;
4478 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004480 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004481 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004483 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004484 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004485 int found_extent;
4486 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004487 int pending_del_nr = 0;
4488 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004489 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004490 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004491 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004492 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004493 bool be_nice = false;
4494 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004495 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4496 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004497
4498 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004499
Chris Mason28ed1342014-12-17 09:41:04 -08004500 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004501 * For non-free space inodes and non-shareable roots, we want to back
4502 * off from time to time. This means all inodes in subvolume roots,
4503 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004504 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004505 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004506 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004507 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004508
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004509 path = btrfs_alloc_path();
4510 if (!path)
4511 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004512 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004513
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004514 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004515 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004516 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004517
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004518 /*
4519 * We want to drop from the next block forward in case this
4520 * new size is not block aligned since we will be keeping the
4521 * last block of the extent just the way it is.
4522 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004523 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004524 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004525 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004526 }
Yan, Zheng80825102009-11-12 09:35:36 +00004527
Miao Xie16cdcec2011-04-22 18:12:22 +08004528 /*
4529 * This function is also used to drop the items in the log tree before
4530 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004531 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004532 * items.
4533 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004534 if (min_type == 0 && root == inode->root)
4535 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004536
Li Zefan33345d012011-04-20 10:31:50 +08004537 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004538 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004539 key.type = (u8)-1;
4540
Chris Mason85e21ba2008-01-29 15:11:36 -05004541search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004542 /*
4543 * with a 16K leaf size and 128MB extents, you can actually queue
4544 * up a huge file in a single leaf. Most of the time that
4545 * bytes_deleted is > 0, it will be huge by the time we get here
4546 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004547 if (be_nice && bytes_deleted > SZ_32M &&
4548 btrfs_should_end_transaction(trans)) {
4549 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004550 goto out;
4551 }
Chris Masond3977122009-01-05 21:25:51 -05004552
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004553 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4554 if (ret < 0)
4555 goto out;
4556
Chris Mason85e21ba2008-01-29 15:11:36 -05004557 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004558 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004559 /* there are no items in the tree for us to truncate, we're
4560 * done
4561 */
Yan, Zheng80825102009-11-12 09:35:36 +00004562 if (path->slots[0] == 0)
4563 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004564 path->slots[0]--;
4565 }
4566
Chris Masond3977122009-01-05 21:25:51 -05004567 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004568 u64 clear_start = 0, clear_len = 0;
4569
Chris Mason39279cc2007-06-12 06:35:45 -04004570 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004571 leaf = path->nodes[0];
4572 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004573 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004574
Li Zefan33345d012011-04-20 10:31:50 +08004575 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004576 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004577
Chris Mason85e21ba2008-01-29 15:11:36 -05004578 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004579 break;
4580
Chris Mason5f39d392007-10-15 16:14:19 -04004581 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004582 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004583 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004584 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004585 extent_type = btrfs_file_extent_type(leaf, fi);
4586 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004587 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004588 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004589
4590 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004591 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004592 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004593 item_end += btrfs_file_extent_ram_bytes(leaf,
4594 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004595
4596 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004597 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004598 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004599 }
Yan008630c2007-11-07 13:31:09 -05004600 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
Yan, Zheng80825102009-11-12 09:35:36 +00004602 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004603 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004604 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004605 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004606 break;
4607 if (found_key.offset >= new_size)
4608 del_item = 1;
4609 else
4610 del_item = 0;
4611 }
Chris Mason39279cc2007-06-12 06:35:45 -04004612 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004613 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004614 if (found_type != BTRFS_EXTENT_DATA_KEY)
4615 goto delete;
4616
4617 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004618 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004619
4620 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004621 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004622 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004623 u64 orig_num_bytes =
4624 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004625 extent_num_bytes = ALIGN(new_size -
4626 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004627 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004628 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004629 btrfs_set_file_extent_num_bytes(leaf, fi,
4630 extent_num_bytes);
4631 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004632 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004633 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004634 &root->state) &&
4635 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004636 inode_sub_bytes(&inode->vfs_inode,
4637 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004638 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004639 } else {
Chris Masondb945352007-10-15 16:15:53 -04004640 extent_num_bytes =
4641 btrfs_file_extent_disk_num_bytes(leaf,
4642 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004643 extent_offset = found_key.offset -
4644 btrfs_file_extent_offset(leaf, fi);
4645
Chris Mason39279cc2007-06-12 06:35:45 -04004646 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004647 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004648 if (extent_start != 0) {
4649 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004650 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004651 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004652 inode_sub_bytes(&inode->vfs_inode,
4653 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004654 }
4655 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004656 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004657 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004658 /*
4659 * we can't truncate inline items that have had
4660 * special encodings
4661 */
4662 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004663 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004664 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4665 btrfs_file_extent_compression(leaf, fi) == 0) {
4666 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004667
Josef Bacikddfae632017-10-19 14:16:02 -04004668 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4669 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004670 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004671 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004672 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004673 * We have to bail so the last_size is set to
4674 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004675 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004676 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004677 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004678 } else {
4679 /*
4680 * Inline extents are special, we just treat
4681 * them as a full sector worth in the file
4682 * extent tree just for simplicity sake.
4683 */
4684 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004685 }
Josef Bacikddfae632017-10-19 14:16:02 -04004686
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004687 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004688 inode_sub_bytes(&inode->vfs_inode,
4689 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004690 }
Chris Mason179e29e2007-11-01 11:28:41 -04004691delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004692 /*
4693 * We use btrfs_truncate_inode_items() to clean up log trees for
4694 * multiple fsyncs, and in this case we don't want to clear the
4695 * file extent range because it's just the log.
4696 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004697 if (root == inode->root) {
4698 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004699 clear_start, clear_len);
4700 if (ret) {
4701 btrfs_abort_transaction(trans, ret);
4702 break;
4703 }
4704 }
4705
Josef Bacikddfae632017-10-19 14:16:02 -04004706 if (del_item)
4707 last_size = found_key.offset;
4708 else
4709 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004710 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004711 if (!pending_del_nr) {
4712 /* no pending yet, add ourselves */
4713 pending_del_slot = path->slots[0];
4714 pending_del_nr = 1;
4715 } else if (pending_del_nr &&
4716 path->slots[0] + 1 == pending_del_slot) {
4717 /* hop on the pending chunk */
4718 pending_del_nr++;
4719 pending_del_slot = path->slots[0];
4720 } else {
Chris Masond3977122009-01-05 21:25:51 -05004721 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004722 }
Chris Mason39279cc2007-06-12 06:35:45 -04004723 } else {
4724 break;
4725 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004726 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004727
Miao Xie27cdeb72014-04-02 19:51:05 +08004728 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004729 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004730 struct btrfs_ref ref = { 0 };
4731
Chris Mason28ed1342014-12-17 09:41:04 -08004732 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004733
4734 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4735 extent_start, extent_num_bytes, 0);
4736 ref.real_root = root->root_key.objectid;
4737 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4738 ino, extent_offset);
4739 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004740 if (ret) {
4741 btrfs_abort_transaction(trans, ret);
4742 break;
4743 }
Chris Mason28f75a02015-02-04 06:59:29 -08004744 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004745 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004746 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004747 }
Chris Mason39279cc2007-06-12 06:35:45 -04004748 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004749
Yan, Zheng80825102009-11-12 09:35:36 +00004750 if (found_type == BTRFS_INODE_ITEM_KEY)
4751 break;
4752
4753 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004754 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004755 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004756 if (pending_del_nr) {
4757 ret = btrfs_del_items(trans, root, path,
4758 pending_del_slot,
4759 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004760 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004761 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004762 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004763 }
Yan, Zheng80825102009-11-12 09:35:36 +00004764 pending_del_nr = 0;
4765 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004766 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004767
Chris Mason28f75a02015-02-04 06:59:29 -08004768 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004769 * We can generate a lot of delayed refs, so we need to
4770 * throttle every once and a while and make sure we're
4771 * adding enough space to keep up with the work we are
4772 * generating. Since we hold a transaction here we
4773 * can't flush, and we don't want to FLUSH_LIMIT because
4774 * we could have generated too many delayed refs to
4775 * actually allocate, so just bail if we're short and
4776 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004777 */
Josef Bacik28bad212018-12-03 10:20:38 -05004778 if (should_throttle) {
4779 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4780 BTRFS_RESERVE_NO_FLUSH);
4781 if (ret) {
4782 ret = -EAGAIN;
4783 break;
4784 }
Chris Mason28f75a02015-02-04 06:59:29 -08004785 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004786 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004787 } else {
4788 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004789 }
Chris Mason39279cc2007-06-12 06:35:45 -04004790 }
Yan, Zheng80825102009-11-12 09:35:36 +00004791out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004792 if (ret >= 0 && pending_del_nr) {
4793 int err;
4794
4795 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004796 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004797 if (err) {
4798 btrfs_abort_transaction(trans, err);
4799 ret = err;
4800 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004801 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004802 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4803 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004804 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004805 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004806 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4807 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4808 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004809 }
Chris Mason28ed1342014-12-17 09:41:04 -08004810
Chris Mason39279cc2007-06-12 06:35:45 -04004811 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004812 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004813}
4814
Chris Masona52d9a82007-08-27 16:49:44 -04004815/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004817 * @inode - inode that we're zeroing
4818 * @from - the offset to start zeroing
4819 * @len - the length to zero, 0 to zero the entire range respective to the
4820 * offset
4821 * @front - zero up to the offset instead of from the offset on
4822 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004824 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004825 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004826int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4827 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004828{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004829 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4830 struct address_space *mapping = inode->vfs_inode.i_mapping;
4831 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004833 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004834 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004836 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004837 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004838 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304839 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004840 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004841 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004842 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004843 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 u64 block_start;
4845 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004846
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004847 if (IS_ALIGNED(offset, blocksize) &&
4848 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004849 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304850
Josef Bacik8b62f872017-10-19 14:15:55 -04004851 block_start = round_down(from, blocksize);
4852 block_end = block_start + blocksize - 1;
4853
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004854 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4855 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004856 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004857 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004858 /* For nocow case, no need to reserve data space */
4859 only_release_metadata = true;
4860 } else {
4861 goto out;
4862 }
4863 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004864 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004865 if (ret < 0) {
4866 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004867 btrfs_free_reserved_data_space(inode, data_reserved,
4868 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004869 goto out;
4870 }
Chris Mason211c17f2008-05-15 09:13:45 -04004871again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004872 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004873 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004874 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4875 blocksize, true);
4876 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004877 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004878 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004879 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004880 ret = set_page_extent_mapped(page);
4881 if (ret < 0)
4882 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004883
Chris Masona52d9a82007-08-27 16:49:44 -04004884 if (!PageUptodate(page)) {
4885 ret = btrfs_readpage(NULL, page);
4886 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004887 if (page->mapping != mapping) {
4888 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004889 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004890 goto again;
4891 }
Chris Masona52d9a82007-08-27 16:49:44 -04004892 if (!PageUptodate(page)) {
4893 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004894 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004895 }
4896 }
Chris Mason211c17f2008-05-15 09:13:45 -04004897 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004898
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304899 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004900
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004901 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004902 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304903 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004904 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004905 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004906 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004907 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 btrfs_put_ordered_extent(ordered);
4909 goto again;
4910 }
4911
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004912 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004913 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4914 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004915
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004916 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004917 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004918 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304919 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004920 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004921 goto out_unlock;
4922 }
4923
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304924 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004925 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304926 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004927 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004928 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304929 memset(kaddr + (block_start - page_offset(page)),
4930 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004931 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304932 memset(kaddr + (block_start - page_offset(page)) + offset,
4933 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004934 flush_dcache_page(page);
4935 kunmap(page);
4936 }
Chris Mason247e7432008-07-17 12:53:51 -04004937 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004938 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004939 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004940
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004941 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004942 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004943 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004944
Chris Mason89642222008-07-24 09:41:53 -04004945out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004946 if (ret) {
4947 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004948 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004949 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004950 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004951 block_start, blocksize, true);
4952 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004953 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004954 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004955 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004956out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004957 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004958 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004959 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004960 return ret;
4961}
4962
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004963static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004964 u64 offset, u64 len)
4965{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004966 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004967 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004968 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004969 int ret;
4970
4971 /*
4972 * Still need to make sure the inode looks like it's been updated so
4973 * that any holes get logged if we fsync.
4974 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004975 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004976 inode->last_trans = fs_info->generation;
4977 inode->last_sub_trans = root->log_transid;
4978 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004979 return 0;
4980 }
4981
4982 /*
4983 * 1 - for the one we're dropping
4984 * 1 - for the one we're adding
4985 * 1 - for updating the inode.
4986 */
4987 trans = btrfs_start_transaction(root, 3);
4988 if (IS_ERR(trans))
4989 return PTR_ERR(trans);
4990
Filipe Manana5893dfb2020-11-04 11:07:32 +00004991 drop_args.start = offset;
4992 drop_args.end = offset + len;
4993 drop_args.drop_cache = true;
4994
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004995 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004996 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004997 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004998 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004999 return ret;
5000 }
5001
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005002 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005003 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005005 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005006 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005007 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5008 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005009 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005010 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005011 return ret;
5012}
5013
Josef Bacik695a0d02011-03-04 15:46:53 -05005014/*
5015 * This function puts in dummy file extents for the area we're creating a hole
5016 * for. So if we are truncating this file to a larger size we need to insert
5017 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5018 * the range between oldsize and size
5019 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005020int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005021{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005022 struct btrfs_root *root = inode->root;
5023 struct btrfs_fs_info *fs_info = root->fs_info;
5024 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005025 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005026 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005027 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5029 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005030 u64 last_byte;
5031 u64 cur_offset;
5032 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005033 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005034
Josef Bacika71754f2013-06-17 17:14:39 -04005035 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305036 * If our size started in the middle of a block we need to zero out the
5037 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005038 * expose stale data.
5039 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005040 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005041 if (err)
5042 return err;
5043
Yan Zheng9036c102008-10-30 14:19:41 -04005044 if (size <= hole_start)
5045 return 0;
5046
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005047 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5048 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005049 cur_offset = hole_start;
5050 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005051 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005052 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005053 if (IS_ERR(em)) {
5054 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005055 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005056 break;
5057 }
Yan Zheng9036c102008-10-30 14:19:41 -04005058 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005059 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005060 hole_size = last_byte - cur_offset;
5061
Yan, Zheng80825102009-11-12 09:35:36 +00005062 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005063 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005064
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005065 err = maybe_insert_hole(root, inode, cur_offset,
5066 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005067 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005068 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005069
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005070 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005071 cur_offset, hole_size);
5072 if (err)
5073 break;
5074
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005075 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005076 cur_offset + hole_size - 1, 0);
5077 hole_em = alloc_extent_map();
5078 if (!hole_em) {
5079 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005080 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005081 goto next;
5082 }
5083 hole_em->start = cur_offset;
5084 hole_em->len = hole_size;
5085 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005086
Josef Bacik5dc562c2012-08-17 13:14:17 -04005087 hole_em->block_start = EXTENT_MAP_HOLE;
5088 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005089 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005090 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005091 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005092 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005093
5094 while (1) {
5095 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005096 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005097 write_unlock(&em_tree->lock);
5098 if (err != -EEXIST)
5099 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005100 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005101 cur_offset +
5102 hole_size - 1, 0);
5103 }
5104 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005105 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005106 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005107 cur_offset, hole_size);
5108 if (err)
5109 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005110 }
Josef Bacik16e75492013-10-22 12:18:51 -04005111next:
Yan Zheng9036c102008-10-30 14:19:41 -04005112 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005113 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005114 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005115 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005116 break;
5117 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005118 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005119 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005120 return err;
5121}
5122
Eric Sandeen3972f262013-01-12 02:57:22 +00005123static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005124{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005125 struct btrfs_root *root = BTRFS_I(inode)->root;
5126 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005127 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005128 loff_t newsize = attr->ia_size;
5129 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005130 int ret;
5131
Eric Sandeen3972f262013-01-12 02:57:22 +00005132 /*
5133 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5134 * special case where we need to update the times despite not having
5135 * these flags set. For all other operations the VFS set these flags
5136 * explicitly if it wants a timestamp update.
5137 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005138 if (newsize != oldsize) {
5139 inode_inc_iversion(inode);
5140 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5141 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005142 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005143 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005144
Josef Bacika41ad392011-01-31 15:30:16 -05005145 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005146 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005147 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005148 * This is to ensure the snapshot captures a fully consistent
5149 * state of this file - if the snapshot captures this expanding
5150 * truncation, it must capture all writes that happened before
5151 * this truncation.
5152 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005153 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005154 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005155 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005156 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005157 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005158 }
Yan, Zheng80825102009-11-12 09:35:36 +00005159
Miao Xief4a2f4c2011-12-14 20:12:01 -05005160 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005161 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005162 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005163 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005164 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005165
5166 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005167 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305168 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005169 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005170 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005171 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005172 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5174
5175 if (btrfs_is_zoned(fs_info)) {
5176 ret = btrfs_wait_ordered_range(inode,
5177 ALIGN(newsize, fs_info->sectorsize),
5178 (u64)-1);
5179 if (ret)
5180 return ret;
5181 }
Yan, Zheng80825102009-11-12 09:35:36 +00005182
Josef Bacika41ad392011-01-31 15:30:16 -05005183 /*
5184 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005185 * zero. Make sure any new writes to the file get on disk
5186 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005187 */
5188 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005189 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005190 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005191
Josef Bacika41ad392011-01-31 15:30:16 -05005192 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005193
Miao Xie2e60a512013-02-08 07:01:08 +00005194 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005195
Filipe Manana213e8c52018-02-06 20:40:31 +00005196 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005197 if (ret && inode->i_nlink) {
5198 int err;
5199
5200 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005201 * Truncate failed, so fix up the in-memory size. We
5202 * adjusted disk_i_size down as we removed extents, so
5203 * wait for disk_i_size to be stable and then update the
5204 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005205 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005206 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005207 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005208 return err;
5209 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005210 }
Yan, Zheng80825102009-11-12 09:35:36 +00005211 }
5212
Josef Bacika41ad392011-01-31 15:30:16 -05005213 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005214}
5215
Christian Brauner549c7292021-01-21 14:19:43 +01005216static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5217 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005218{
David Howells2b0143b2015-03-17 22:25:59 +00005219 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005220 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005221 int err;
5222
Li Zefanb83cc962010-12-20 16:04:08 +08005223 if (btrfs_root_readonly(root))
5224 return -EROFS;
5225
Christian Brauner2f221d62021-01-21 14:19:26 +01005226 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005227 if (err)
5228 return err;
5229
Chris Mason5a3f23d2009-03-31 13:27:11 -04005230 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005231 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005232 if (err)
5233 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005234 }
Yan Zheng9036c102008-10-30 14:19:41 -04005235
Christoph Hellwig10257742010-06-04 11:30:02 +02005236 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005237 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005238 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005239 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005240
Josef Bacik22c44fe2011-11-30 10:45:38 -05005241 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005242 err = posix_acl_chmod(&init_user_ns, inode,
5243 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005244 }
5245
Chris Mason39279cc2007-06-12 06:35:45 -04005246 return err;
5247}
Chris Mason61295eb2008-01-14 16:24:38 -05005248
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005249/*
5250 * While truncating the inode pages during eviction, we get the VFS calling
5251 * btrfs_invalidatepage() against each page of the inode. This is slow because
5252 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5253 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5254 * extent_state structures over and over, wasting lots of time.
5255 *
5256 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5257 * those expensive operations on a per page basis and do only the ordered io
5258 * finishing, while we release here the extent_map and extent_state structures,
5259 * without the excessive merging and splitting.
5260 */
5261static void evict_inode_truncate_pages(struct inode *inode)
5262{
5263 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5264 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5265 struct rb_node *node;
5266
5267 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005268 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269
5270 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005271 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005272 struct extent_map *em;
5273
Liu Bo07e1ce02018-08-23 03:51:52 +08005274 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005276 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5277 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278 remove_extent_mapping(map_tree, em);
5279 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005280 if (need_resched()) {
5281 write_unlock(&map_tree->lock);
5282 cond_resched();
5283 write_lock(&map_tree->lock);
5284 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005285 }
5286 write_unlock(&map_tree->lock);
5287
Filipe Manana6ca07092015-05-26 00:55:42 +01005288 /*
5289 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005290 * We can have ongoing bios started by readahead that have
5291 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005292 * still in progress (unlocked the pages in the bio but did not yet
5293 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005294 * ranges can still be locked and eviction started because before
5295 * submitting those bios, which are executed by a separate task (work
5296 * queue kthread), inode references (inode->i_count) were not taken
5297 * (which would be dropped in the end io callback of each bio).
5298 * Therefore here we effectively end up waiting for those bios and
5299 * anyone else holding locked ranges without having bumped the inode's
5300 * reference count - if we don't do it, when they access the inode's
5301 * io_tree to unlock a range it may be too late, leading to an
5302 * use-after-free issue.
5303 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005304 spin_lock(&io_tree->lock);
5305 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5306 struct extent_state *state;
5307 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005308 u64 start;
5309 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005310 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005311
5312 node = rb_first(&io_tree->state);
5313 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005314 start = state->start;
5315 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005316 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005317 spin_unlock(&io_tree->lock);
5318
David Sterbaff13db42015-12-03 14:30:40 +01005319 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005320
5321 /*
5322 * If still has DELALLOC flag, the extent didn't reach disk,
5323 * and its reserved space won't be freed by delayed_ref.
5324 * So we need to free its reserved space here.
5325 * (Refer to comment in btrfs_invalidatepage, case 2)
5326 *
5327 * Note, end is the bytenr of last byte, so we need + 1 here.
5328 */
Filipe Manana421f0922018-10-12 13:02:48 +01005329 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005330 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5331 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005332
Filipe Manana6ca07092015-05-26 00:55:42 +01005333 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005334 EXTENT_LOCKED | EXTENT_DELALLOC |
5335 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5336 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005337
Filipe Manana7064dd52014-08-08 02:47:05 +01005338 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005339 spin_lock(&io_tree->lock);
5340 }
5341 spin_unlock(&io_tree->lock);
5342}
5343
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005344static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005345 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005346{
5347 struct btrfs_fs_info *fs_info = root->fs_info;
5348 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005349 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005350 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005351 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005352
Josef Bacikd3984c92019-08-01 18:19:37 -04005353 /*
5354 * Eviction should be taking place at some place safe because of our
5355 * delayed iputs. However the normal flushing code will run delayed
5356 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5357 *
5358 * We reserve the delayed_refs_extra here again because we can't use
5359 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5360 * above. We reserve our extra bit here because we generate a ton of
5361 * delayed refs activity by truncating.
5362 *
5363 * If we cannot make our reservation we'll attempt to steal from the
5364 * global reserve, because we really want to be able to free up space.
5365 */
5366 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5367 BTRFS_RESERVE_FLUSH_EVICT);
5368 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005369 /*
5370 * Try to steal from the global reserve if there is space for
5371 * it.
5372 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005373 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5374 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5375 btrfs_warn(fs_info,
5376 "could not allocate space for delete; will truncate on mount");
5377 return ERR_PTR(-ENOSPC);
5378 }
5379 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005380 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005381
5382 trans = btrfs_join_transaction(root);
5383 if (IS_ERR(trans))
5384 return trans;
5385
5386 if (delayed_refs_extra) {
5387 trans->block_rsv = &fs_info->trans_block_rsv;
5388 trans->bytes_reserved = delayed_refs_extra;
5389 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5390 delayed_refs_extra, 1);
5391 }
5392 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005393}
5394
Al Virobd555972010-06-07 11:35:40 -04005395void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005398 struct btrfs_trans_handle *trans;
5399 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005400 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005401 int ret;
5402
liubo1abe9b82011-03-24 11:18:59 +00005403 trace_btrfs_inode_evict(inode);
5404
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005405 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005406 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005407 return;
5408 }
5409
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005410 evict_inode_truncate_pages(inode);
5411
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005412 if (inode->i_nlink &&
5413 ((btrfs_root_refs(&root->root_item) != 0 &&
5414 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005415 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005416 goto no_delete;
5417
Omar Sandoval27919062018-05-11 13:13:37 -07005418 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005419 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005420
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005421 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005422
Omar Sandoval7b40b692018-05-11 13:13:33 -07005423 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005424 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005425
Yan, Zheng76dda932009-09-21 16:00:26 -04005426 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005427 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5428 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005429 goto no_delete;
5430 }
5431
Nikolay Borisovaa790212017-01-10 20:35:40 +02005432 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005433 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005434 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005435
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005437 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005438 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005439 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005440 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005441
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005442 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005443
Yan, Zheng80825102009-11-12 09:35:36 +00005444 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005445 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005446 if (IS_ERR(trans))
5447 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005448
5449 trans->block_rsv = rsv;
5450
Nikolay Borisov50743392020-11-02 16:48:55 +02005451 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5452 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005453 trans->block_rsv = &fs_info->trans_block_rsv;
5454 btrfs_end_transaction(trans);
5455 btrfs_btree_balance_dirty(fs_info);
5456 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5457 goto free_rsv;
5458 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005459 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005460 }
5461
Josef Bacik4ef31a42013-08-13 14:10:08 -04005462 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005463 * Errors here aren't a big deal, it just means we leave orphan items in
5464 * the tree. They will be cleaned up on the next mount. If the inode
5465 * number gets reused, cleanup deletes the orphan item without doing
5466 * anything, and unlink reuses the existing orphan item.
5467 *
5468 * If it turns out that we are dropping too many of these, we might want
5469 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005470 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005471 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005472 if (!IS_ERR(trans)) {
5473 trans->block_rsv = rsv;
5474 btrfs_orphan_del(trans, BTRFS_I(inode));
5475 trans->block_rsv = &fs_info->trans_block_rsv;
5476 btrfs_end_transaction(trans);
5477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005478
Omar Sandoval27919062018-05-11 13:13:37 -07005479free_rsv:
5480 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005481no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005482 /*
5483 * If we didn't successfully delete, the orphan item will still be in
5484 * the tree and we'll retry on the next mount. Again, we might also want
5485 * to retry these periodically in the future.
5486 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005487 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005488 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005489}
5490
5491/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005492 * Return the key found in the dir entry in the location pointer, fill @type
5493 * with BTRFS_FT_*, and return 0.
5494 *
Su Yue005d6712018-03-05 17:13:37 +08005495 * If no dir entries were found, returns -ENOENT.
5496 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005497 */
5498static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005499 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005500{
5501 const char *name = dentry->d_name.name;
5502 int namelen = dentry->d_name.len;
5503 struct btrfs_dir_item *di;
5504 struct btrfs_path *path;
5505 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005506 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005507
5508 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005509 if (!path)
5510 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005511
David Sterbaf85b7372017-01-20 14:54:07 +01005512 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5513 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005514 if (IS_ERR_OR_NULL(di)) {
5515 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005516 goto out;
5517 }
Chris Masond3977122009-01-05 21:25:51 -05005518
Chris Mason5f39d392007-10-15 16:14:19 -04005519 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005520 if (location->type != BTRFS_INODE_ITEM_KEY &&
5521 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005522 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005523 btrfs_warn(root->fs_info,
5524"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5525 __func__, name, btrfs_ino(BTRFS_I(dir)),
5526 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005527 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005528 if (!ret)
5529 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005530out:
Chris Mason39279cc2007-06-12 06:35:45 -04005531 btrfs_free_path(path);
5532 return ret;
5533}
5534
5535/*
5536 * when we hit a tree root in a directory, the btrfs part of the inode
5537 * needs to be changed to reflect the root directory of the tree root. This
5538 * is kind of like crossing a mount point.
5539 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005540static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 struct inode *dir,
5542 struct dentry *dentry,
5543 struct btrfs_key *location,
5544 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005545{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 struct btrfs_path *path;
5547 struct btrfs_root *new_root;
5548 struct btrfs_root_ref *ref;
5549 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005550 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 int ret;
5552 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005553
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005554 path = btrfs_alloc_path();
5555 if (!path) {
5556 err = -ENOMEM;
5557 goto out;
5558 }
Chris Mason39279cc2007-06-12 06:35:45 -04005559
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005560 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005561 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5562 key.type = BTRFS_ROOT_REF_KEY;
5563 key.offset = location->objectid;
5564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005566 if (ret) {
5567 if (ret < 0)
5568 err = ret;
5569 goto out;
5570 }
Chris Mason39279cc2007-06-12 06:35:45 -04005571
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005572 leaf = path->nodes[0];
5573 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005574 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005575 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5576 goto out;
5577
5578 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5579 (unsigned long)(ref + 1),
5580 dentry->d_name.len);
5581 if (ret)
5582 goto out;
5583
David Sterbab3b4aa72011-04-21 01:20:15 +02005584 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585
David Sterba56e93572020-05-15 19:35:55 +02005586 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005587 if (IS_ERR(new_root)) {
5588 err = PTR_ERR(new_root);
5589 goto out;
5590 }
5591
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005592 *sub_root = new_root;
5593 location->objectid = btrfs_root_dirid(&new_root->root_item);
5594 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005595 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005596 err = 0;
5597out:
5598 btrfs_free_path(path);
5599 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005600}
5601
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602static void inode_tree_add(struct inode *inode)
5603{
5604 struct btrfs_root *root = BTRFS_I(inode)->root;
5605 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005606 struct rb_node **p;
5607 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005608 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005609 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610
Al Viro1d3382cb2010-10-23 15:19:20 -04005611 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005613 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005615 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616 while (*p) {
5617 parent = *p;
5618 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5619
David Sterba37508512018-06-29 10:56:40 +02005620 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005621 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005622 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005623 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624 else {
5625 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005626 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005627 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005628 RB_CLEAR_NODE(parent);
5629 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005630 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005631 }
5632 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005633 rb_link_node(new, parent, p);
5634 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635 spin_unlock(&root->inode_lock);
5636}
5637
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005638static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005640 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005643 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005644 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5645 rb_erase(&inode->rb_node, &root->inode_tree);
5646 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005648 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005649 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005650
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005651 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005652 spin_lock(&root->inode_lock);
5653 empty = RB_EMPTY_ROOT(&root->inode_tree);
5654 spin_unlock(&root->inode_lock);
5655 if (empty)
5656 btrfs_add_dead_root(root);
5657 }
5658}
5659
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005660
Chris Masone02119d2008-09-05 16:13:11 -04005661static int btrfs_init_locked_inode(struct inode *inode, void *p)
5662{
5663 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005664
5665 inode->i_ino = args->ino;
5666 BTRFS_I(inode)->location.objectid = args->ino;
5667 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5668 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005669 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5670 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005671 return 0;
5672}
5673
5674static int btrfs_find_actor(struct inode *inode, void *opaque)
5675{
5676 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005677
5678 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005679 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005680}
5681
David Sterba0202e832020-05-15 19:35:59 +02005682static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005683 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005684{
5685 struct inode *inode;
5686 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005687 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005688
David Sterba0202e832020-05-15 19:35:59 +02005689 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005690 args.root = root;
5691
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005692 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005693 btrfs_init_locked_inode,
5694 (void *)&args);
5695 return inode;
5696}
5697
David Sterba4c66e0d2019-10-03 19:09:35 +02005698/*
David Sterba0202e832020-05-15 19:35:59 +02005699 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005700 * Path can be preallocated to prevent recursing back to iget through
5701 * allocator. NULL is also valid but may require an additional allocation
5702 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305703 */
David Sterba0202e832020-05-15 19:35:59 +02005704struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005705 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305706{
5707 struct inode *inode;
5708
David Sterba0202e832020-05-15 19:35:59 +02005709 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305710 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005711 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305712
5713 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005714 int ret;
5715
Filipe Manana4222ea72018-10-24 10:13:03 +01005716 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005717 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005718 inode_tree_add(inode);
5719 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005720 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005721 iget_failed(inode);
5722 /*
5723 * ret > 0 can come from btrfs_search_slot called by
5724 * btrfs_read_locked_inode, this means the inode item
5725 * was not found.
5726 */
5727 if (ret > 0)
5728 ret = -ENOENT;
5729 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005730 }
5731 }
5732
Balaji Rao1a54ef82008-07-21 02:01:04 +05305733 return inode;
5734}
5735
David Sterba0202e832020-05-15 19:35:59 +02005736struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005737{
David Sterba0202e832020-05-15 19:35:59 +02005738 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005739}
5740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005741static struct inode *new_simple_dir(struct super_block *s,
5742 struct btrfs_key *key,
5743 struct btrfs_root *root)
5744{
5745 struct inode *inode = new_inode(s);
5746
5747 if (!inode)
5748 return ERR_PTR(-ENOMEM);
5749
Josef Bacik5c8fd992020-02-14 16:11:43 -05005750 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005752 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753
5754 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005755 /*
5756 * We only need lookup, the rest is read-only and there's no inode
5757 * associated with the dentry
5758 */
5759 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005760 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761 inode->i_fop = &simple_dir_operations;
5762 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005763 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305764 inode->i_atime = inode->i_mtime;
5765 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005766 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767
5768 return inode;
5769}
5770
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005771static inline u8 btrfs_inode_type(struct inode *inode)
5772{
5773 /*
5774 * Compile-time asserts that generic FT_* types still match
5775 * BTRFS_FT_* types
5776 */
5777 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5778 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5779 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5780 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5781 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5782 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5783 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5784 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5785
5786 return fs_umode_to_ftype(inode->i_mode);
5787}
5788
Chris Mason3de45862008-11-17 21:02:50 -05005789struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005791 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005792 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005793 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005794 struct btrfs_root *sub_root = root;
5795 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005796 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005797 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005798
5799 if (dentry->d_name.len > BTRFS_NAME_LEN)
5800 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005801
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005802 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 if (ret < 0)
5804 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005806 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005807 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005808 if (IS_ERR(inode))
5809 return inode;
5810
5811 /* Do extra check against inode mode with di_type */
5812 if (btrfs_inode_type(inode) != di_type) {
5813 btrfs_crit(fs_info,
5814"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5815 inode->i_mode, btrfs_inode_type(inode),
5816 di_type);
5817 iput(inode);
5818 return ERR_PTR(-EUCLEAN);
5819 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005820 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005821 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005822
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005823 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005824 &location, &sub_root);
5825 if (ret < 0) {
5826 if (ret != -ENOENT)
5827 inode = ERR_PTR(ret);
5828 else
5829 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5830 } else {
David Sterba0202e832020-05-15 19:35:59 +02005831 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005832 }
Josef Bacik87270022020-01-24 09:32:31 -05005833 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005834 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005835
Julia Lawall34d19ba2011-01-24 19:55:19 +00005836 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005837 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005838 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005839 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005840 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005841 if (ret) {
5842 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005843 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005844 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005845 }
5846
Chris Mason3de45862008-11-17 21:02:50 -05005847 return inode;
5848}
5849
Nick Pigginfe15ce42011-01-07 17:49:23 +11005850static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005851{
5852 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005853 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005854
Li Zefan848cce02012-02-21 17:04:28 +08005855 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005856 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005857
Li Zefan848cce02012-02-21 17:04:28 +08005858 if (inode) {
5859 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005860 if (btrfs_root_refs(&root->root_item) == 0)
5861 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005862
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005863 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005864 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005865 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005866 return 0;
5867}
5868
Chris Mason3de45862008-11-17 21:02:50 -05005869static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005870 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005871{
Al Viro3837d202018-10-10 16:38:27 -04005872 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005873
Al Viro3837d202018-10-10 16:38:27 -04005874 if (inode == ERR_PTR(-ENOENT))
5875 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005876 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005877}
5878
Josef Bacik23b5ec72017-07-24 15:14:25 -04005879/*
5880 * All this infrastructure exists because dir_emit can fault, and we are holding
5881 * the tree lock when doing readdir. For now just allocate a buffer and copy
5882 * our information into that, and then dir_emit from the buffer. This is
5883 * similar to what NFS does, only we don't keep the buffer around in pagecache
5884 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5885 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5886 * tree lock.
5887 */
5888static int btrfs_opendir(struct inode *inode, struct file *file)
5889{
5890 struct btrfs_file_private *private;
5891
5892 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5893 if (!private)
5894 return -ENOMEM;
5895 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5896 if (!private->filldir_buf) {
5897 kfree(private);
5898 return -ENOMEM;
5899 }
5900 file->private_data = private;
5901 return 0;
5902}
5903
5904struct dir_entry {
5905 u64 ino;
5906 u64 offset;
5907 unsigned type;
5908 int name_len;
5909};
5910
5911static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5912{
5913 while (entries--) {
5914 struct dir_entry *entry = addr;
5915 char *name = (char *)(entry + 1);
5916
David Sterba92d32172018-04-16 21:10:14 +02005917 ctx->pos = get_unaligned(&entry->offset);
5918 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5919 get_unaligned(&entry->ino),
5920 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005921 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005922 addr += sizeof(struct dir_entry) +
5923 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005924 ctx->pos++;
5925 }
5926 return 0;
5927}
5928
Al Viro9cdda8d2013-05-22 16:48:09 -04005929static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005930{
Al Viro9cdda8d2013-05-22 16:48:09 -04005931 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005933 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005934 struct btrfs_dir_item *di;
5935 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005936 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005938 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005939 struct list_head ins_list;
5940 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005941 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005942 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005943 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005944 char *name_ptr;
5945 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005946 int entries = 0;
5947 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005948 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005950
Al Viro9cdda8d2013-05-22 16:48:09 -04005951 if (!dir_emit_dots(file, ctx))
5952 return 0;
5953
David Woodhouse49593bf2008-08-17 17:08:36 +01005954 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005955 if (!path)
5956 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005957
Josef Bacik23b5ec72017-07-24 15:14:25 -04005958 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005959 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005960
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 INIT_LIST_HEAD(&ins_list);
5962 INIT_LIST_HEAD(&del_list);
5963 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005964
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005967 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005968 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005969
Chris Mason39279cc2007-06-12 06:35:45 -04005970 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5971 if (ret < 0)
5972 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005973
5974 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005975 struct dir_entry *entry;
5976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005978 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005979 if (slot >= btrfs_header_nritems(leaf)) {
5980 ret = btrfs_next_leaf(root, path);
5981 if (ret < 0)
5982 goto err;
5983 else if (ret > 0)
5984 break;
5985 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005986 }
Chris Mason3de45862008-11-17 21:02:50 -05005987
Chris Mason5f39d392007-10-15 16:14:19 -04005988 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5989
5990 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005991 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005992 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005993 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005994 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005995 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005997 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005998 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005999 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006000 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6001 PAGE_SIZE) {
6002 btrfs_release_path(path);
6003 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6004 if (ret)
6005 goto nopos;
6006 addr = private->filldir_buf;
6007 entries = 0;
6008 total_len = 0;
6009 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006010 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006011
6012 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006013 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006015 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6016 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006017 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006018 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006019 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006020 put_unaligned(location.objectid, &entry->ino);
6021 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006022 entries++;
6023 addr += sizeof(struct dir_entry) + name_len;
6024 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006025next:
6026 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006027 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006028 btrfs_release_path(path);
6029
6030 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6031 if (ret)
6032 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006033
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006034 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006035 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006036 goto nopos;
6037
Zach Browndb62efb2013-07-11 16:19:42 -07006038 /*
6039 * Stop new entries from being returned after we return the last
6040 * entry.
6041 *
6042 * New directory entries are assigned a strictly increasing
6043 * offset. This means that new entries created during readdir
6044 * are *guaranteed* to be seen in the future by that readdir.
6045 * This has broken buggy programs which operate on names as
6046 * they're returned by readdir. Until we re-use freed offsets
6047 * we have this hack to stop new entries from being returned
6048 * under the assumption that they'll never reach this huge
6049 * offset.
6050 *
6051 * This is being careful not to overflow 32bit loff_t unless the
6052 * last entry requires it because doing so has broken 32bit apps
6053 * in the past.
6054 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006055 if (ctx->pos >= INT_MAX)
6056 ctx->pos = LLONG_MAX;
6057 else
6058 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006059nopos:
6060 ret = 0;
6061err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006062 if (put)
6063 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006064 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006065 return ret;
6066}
6067
Chris Mason39279cc2007-06-12 06:35:45 -04006068/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006069 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006070 * inode changes. But, it is most likely to find the inode in cache.
6071 * FIXME, needs more benchmarking...there are no reasons other than performance
6072 * to keep or drop this code.
6073 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006074static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006075{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006077 struct btrfs_root *root = BTRFS_I(inode)->root;
6078 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006079 int ret;
6080
Josef Bacik72ac3c02012-05-23 14:13:11 -04006081 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006082 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006083
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006084 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006085 if (IS_ERR(trans))
6086 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006087
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006088 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006089 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006090 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006091 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006092 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006093 if (IS_ERR(trans))
6094 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006095
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006096 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006097 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006098 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006099 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006100 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006101
6102 return ret;
6103}
6104
6105/*
6106 * This is a copy of file_update_time. We need this so we can return error on
6107 * ENOSPC for updating the inode in the case of file write and mmap writes.
6108 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006109static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006110 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006111{
Alexander Block2bc5565282012-06-15 09:49:33 +02006112 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006113 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006114
6115 if (btrfs_root_readonly(root))
6116 return -EROFS;
6117
Josef Bacike41f9412012-03-26 09:46:47 -04006118 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006119 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006120 if (flags & S_CTIME)
6121 inode->i_ctime = *now;
6122 if (flags & S_MTIME)
6123 inode->i_mtime = *now;
6124 if (flags & S_ATIME)
6125 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006126 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006127}
6128
Chris Masond352ac62008-09-29 15:18:18 -04006129/*
6130 * find the highest existing sequence number in a directory
6131 * and then set the in-memory index_cnt variable to reflect
6132 * free sequence numbers
6133 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006134static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006135{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006136 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 struct btrfs_key key, found_key;
6138 struct btrfs_path *path;
6139 struct extent_buffer *leaf;
6140 int ret;
6141
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006142 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006143 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006144 key.offset = (u64)-1;
6145
6146 path = btrfs_alloc_path();
6147 if (!path)
6148 return -ENOMEM;
6149
6150 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6151 if (ret < 0)
6152 goto out;
6153 /* FIXME: we should be able to handle this */
6154 if (ret == 0)
6155 goto out;
6156 ret = 0;
6157
6158 /*
6159 * MAGIC NUMBER EXPLANATION:
6160 * since we search a directory based on f_pos we have to start at 2
6161 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6162 * else has to start at 2
6163 */
6164 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006165 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006166 goto out;
6167 }
6168
6169 path->slots[0]--;
6170
6171 leaf = path->nodes[0];
6172 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6173
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006174 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006175 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006176 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006177 goto out;
6178 }
6179
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006180 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006181out:
6182 btrfs_free_path(path);
6183 return ret;
6184}
6185
Chris Masond352ac62008-09-29 15:18:18 -04006186/*
6187 * helper to find a free sequence number in a given directory. This current
6188 * code is very simple, later versions will do smarter things in the btree
6189 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006190int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006191{
6192 int ret = 0;
6193
Nikolay Borisov877574e2017-02-20 13:50:33 +02006194 if (dir->index_cnt == (u64)-1) {
6195 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006196 if (ret) {
6197 ret = btrfs_set_inode_index_count(dir);
6198 if (ret)
6199 return ret;
6200 }
Josef Bacikaec74772008-07-24 12:12:38 -04006201 }
6202
Nikolay Borisov877574e2017-02-20 13:50:33 +02006203 *index = dir->index_cnt;
6204 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006205
6206 return ret;
6207}
6208
Chris Masonb0d5d102014-09-08 13:08:51 -07006209static int btrfs_insert_inode_locked(struct inode *inode)
6210{
6211 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006212
6213 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006214 args.root = BTRFS_I(inode)->root;
6215
6216 return insert_inode_locked4(inode,
6217 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6218 btrfs_find_actor, &args);
6219}
6220
Anand Jain19aee8d2017-07-18 17:37:05 +08006221/*
6222 * Inherit flags from the parent inode.
6223 *
6224 * Currently only the compression flags and the cow flags are inherited.
6225 */
6226static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6227{
6228 unsigned int flags;
6229
6230 if (!dir)
6231 return;
6232
6233 flags = BTRFS_I(dir)->flags;
6234
6235 if (flags & BTRFS_INODE_NOCOMPRESS) {
6236 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6237 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6238 } else if (flags & BTRFS_INODE_COMPRESS) {
6239 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6240 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6241 }
6242
6243 if (flags & BTRFS_INODE_NODATACOW) {
6244 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6245 if (S_ISREG(inode->i_mode))
6246 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6247 }
6248
David Sterba7b6a2212018-03-26 18:40:21 +02006249 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006250}
6251
Chris Mason39279cc2007-06-12 06:35:45 -04006252static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6253 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006254 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006255 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006256 u64 ref_objectid, u64 objectid,
6257 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006259 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006260 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006261 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006262 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006263 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006264 struct btrfs_inode_ref *ref;
6265 struct btrfs_key key[2];
6266 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006267 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006268 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006269 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006270 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006271
Chris Mason5f39d392007-10-15 16:14:19 -04006272 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006273 if (!path)
6274 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006275
Josef Bacik11a19a92019-09-09 10:12:04 -04006276 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006278 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006279 if (!inode) {
6280 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006281 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006282 }
Chris Mason39279cc2007-06-12 06:35:45 -04006283
Li Zefan581bb052011-04-20 10:06:11 +08006284 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006285 * O_TMPFILE, set link count to 0, so that after this point,
6286 * we fill in an inode item with the correct link count.
6287 */
6288 if (!name)
6289 set_nlink(inode, 0);
6290
6291 /*
Li Zefan581bb052011-04-20 10:06:11 +08006292 * we have to initialize this early, so we can reclaim the inode
6293 * number if we fail afterwards in this function.
6294 */
6295 inode->i_ino = objectid;
6296
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006297 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006298 trace_btrfs_inode_request(dir);
6299
Nikolay Borisov877574e2017-02-20 13:50:33 +02006300 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006301 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006302 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006303 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006304 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006305 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006306 } else if (dir) {
6307 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006308 }
6309 /*
6310 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006311 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006312 * number
6313 */
6314 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006315 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006316 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006317 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006318 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006319
Josef Bacik5dc562c2012-08-17 13:14:17 -04006320 /*
6321 * We could have gotten an inode number from somebody who was fsynced
6322 * and then removed in this same transaction, so let's just set full
6323 * sync since it will be a full sync anyway and this will blow away the
6324 * old info in the log.
6325 */
6326 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6327
Chris Mason9c583092008-01-29 15:15:18 -05006328 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006329 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006330 key[0].offset = 0;
6331
Chris Mason9c583092008-01-29 15:15:18 -05006332 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006333
6334 if (name) {
6335 /*
6336 * Start new inodes with an inode_ref. This is slightly more
6337 * efficient for small numbers of hard links since they will
6338 * be packed into one item. Extended refs will kick in if we
6339 * add more hard links than can fit in the ref item.
6340 */
6341 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006342 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006343 key[1].offset = ref_objectid;
6344
6345 sizes[1] = name_len + sizeof(*ref);
6346 }
Chris Mason9c583092008-01-29 15:15:18 -05006347
Chris Masonb0d5d102014-09-08 13:08:51 -07006348 location = &BTRFS_I(inode)->location;
6349 location->objectid = objectid;
6350 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006351 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006352
6353 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006354 if (ret < 0) {
6355 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006356 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006357 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006358
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006359 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006360 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006361 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006362
Christian Brauner21cb47b2021-01-21 14:19:25 +01006363 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006364 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306365
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006366 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306367 inode->i_atime = inode->i_mtime;
6368 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006369 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306370
Chris Mason5f39d392007-10-15 16:14:19 -04006371 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6372 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006373 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006374 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006375 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006376
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006377 if (name) {
6378 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6379 struct btrfs_inode_ref);
6380 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6381 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6382 ptr = (unsigned long)(ref + 1);
6383 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6384 }
Chris Mason9c583092008-01-29 15:15:18 -05006385
Chris Mason5f39d392007-10-15 16:14:19 -04006386 btrfs_mark_buffer_dirty(path->nodes[0]);
6387 btrfs_free_path(path);
6388
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006389 btrfs_inherit_iflags(inode, dir);
6390
Al Viro569254b2011-07-24 17:08:40 -04006391 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006392 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006393 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006394 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006395 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6396 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006397 }
6398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006399 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006400
6401 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006402 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006403
Alexander Block8ea05e32012-07-25 17:35:53 +02006404 btrfs_update_root_times(trans, root);
6405
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006406 ret = btrfs_inode_inherit_props(trans, inode, dir);
6407 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006408 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006409 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006410 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006411
Chris Mason39279cc2007-06-12 06:35:45 -04006412 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006413
6414fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006415 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006416fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006417 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006418 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006419 btrfs_free_path(path);
6420 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006421}
6422
Chris Masond352ac62008-09-29 15:18:18 -04006423/*
6424 * utility function to add 'inode' into 'parent_inode' with
6425 * a give name and a given sequence number.
6426 * if 'add_backref' is true, also insert a backref from the
6427 * inode to the parent directory.
6428 */
Chris Masone02119d2008-09-05 16:13:11 -04006429int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006430 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006431 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006432{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006433 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 struct btrfs_root *root = parent_inode->root;
6436 u64 ino = btrfs_ino(inode);
6437 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006438
Li Zefan33345d012011-04-20 10:31:50 +08006439 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006440 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006441 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006442 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006443 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006444 key.offset = 0;
6445 }
Chris Mason39279cc2007-06-12 06:35:45 -04006446
Li Zefan33345d012011-04-20 10:31:50 +08006447 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006448 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006449 root->root_key.objectid, parent_ino,
6450 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006451 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006452 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6453 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454 }
6455
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006456 /* Nothing to clean up yet */
6457 if (ret)
6458 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006459
Lu Fengqi684572d2018-08-04 21:10:57 +08006460 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006461 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006462 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006463 goto fail_dir_item;
6464 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006465 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006466 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006467 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006468
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006469 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006470 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006471 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006472 /*
6473 * If we are replaying a log tree, we do not want to update the mtime
6474 * and ctime of the parent directory with the current time, since the
6475 * log replay procedure is responsible for setting them to their correct
6476 * values (the ones it had when the fsync was done).
6477 */
6478 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6479 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6480
6481 parent_inode->vfs_inode.i_mtime = now;
6482 parent_inode->vfs_inode.i_ctime = now;
6483 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006484 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006486 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006487 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006488
6489fail_dir_item:
6490 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6491 u64 local_index;
6492 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006493 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006494 root->root_key.objectid, parent_ino,
6495 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006496 if (err)
6497 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006498 } else if (add_backref) {
6499 u64 local_index;
6500 int err;
6501
6502 err = btrfs_del_inode_ref(trans, root, name, name_len,
6503 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006504 if (err)
6505 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006506 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006507
6508 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006509 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006510}
6511
6512static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006513 struct btrfs_inode *dir, struct dentry *dentry,
6514 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006515{
Josef Bacika1b075d2010-11-19 20:36:11 +00006516 int err = btrfs_add_link(trans, dir, inode,
6517 dentry->d_name.name, dentry->d_name.len,
6518 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006519 if (err > 0)
6520 err = -EEXIST;
6521 return err;
6522}
6523
Christian Brauner549c7292021-01-21 14:19:43 +01006524static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6525 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006526{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006527 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006528 struct btrfs_trans_handle *trans;
6529 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006530 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006531 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006532 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006533 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006534
Josef Bacik9ed74f22009-09-11 16:12:44 -04006535 /*
6536 * 2 for inode item and ref
6537 * 2 for dir items
6538 * 1 for xattr if selinux is on
6539 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006540 trans = btrfs_start_transaction(root, 5);
6541 if (IS_ERR(trans))
6542 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006543
Nikolay Borisov543068a2020-12-07 17:32:33 +02006544 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006545 if (err)
6546 goto out_unlock;
6547
Josef Bacikaec74772008-07-24 12:12:38 -04006548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006549 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6550 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006551 if (IS_ERR(inode)) {
6552 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006553 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006554 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006555 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006556
Casey Schauflerad19db72011-12-15 10:09:07 -05006557 /*
6558 * If the active LSM wants to access the inode during
6559 * d_instantiate it needs these. Smack checks to see
6560 * if the filesystem supports xattrs by looking at the
6561 * ops vector.
6562 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006563 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006564 init_special_inode(inode, inode->i_mode, rdev);
6565
6566 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006567 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006568 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006569
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006570 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6571 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006572 if (err)
6573 goto out_unlock;
6574
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006575 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006576 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006577
Josef Bacik618e21d2007-07-11 10:18:17 -04006578out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006579 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006581 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006582 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006583 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006585 return err;
6586}
6587
Christian Brauner549c7292021-01-21 14:19:43 +01006588static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6589 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006590{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006591 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 struct btrfs_trans_handle *trans;
6593 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006594 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006595 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006596 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006597 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006598
Josef Bacik9ed74f22009-09-11 16:12:44 -04006599 /*
6600 * 2 for inode item and ref
6601 * 2 for dir items
6602 * 1 for xattr if selinux is on
6603 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006604 trans = btrfs_start_transaction(root, 5);
6605 if (IS_ERR(trans))
6606 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006607
Nikolay Borisov543068a2020-12-07 17:32:33 +02006608 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006609 if (err)
6610 goto out_unlock;
6611
Josef Bacikaec74772008-07-24 12:12:38 -04006612 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006613 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6614 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006615 if (IS_ERR(inode)) {
6616 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006617 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006618 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006619 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006620 /*
6621 * If the active LSM wants to access the inode during
6622 * d_instantiate it needs these. Smack checks to see
6623 * if the filesystem supports xattrs by looking at the
6624 * ops vector.
6625 */
6626 inode->i_fop = &btrfs_file_operations;
6627 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006628 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006629
6630 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6631 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006632 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006633
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006634 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006635 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006636 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006637
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006638 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6639 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006640 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006641 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006642
Al Viro1e2e5472018-05-04 08:23:01 -04006643 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006644
Chris Mason39279cc2007-06-12 06:35:45 -04006645out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006646 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006647 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006648 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006649 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006651 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006652 return err;
6653}
6654
6655static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6656 struct dentry *dentry)
6657{
Filipe Manana271dba452016-01-05 16:24:05 +00006658 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006659 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006660 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006662 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006663 int err;
6664 int drop_inode = 0;
6665
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006666 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006667 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006668 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006669
Mark Fashehf1863732012-08-08 11:32:27 -07006670 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006671 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006672
Nikolay Borisov877574e2017-02-20 13:50:33 +02006673 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006674 if (err)
6675 goto fail;
6676
Yan, Zhenga22285a2010-05-16 10:48:46 -04006677 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006678 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006679 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006680 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006681 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006683 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006684 if (IS_ERR(trans)) {
6685 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006686 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006687 goto fail;
6688 }
Chris Mason5f39d392007-10-15 16:14:19 -04006689
Miao Xie67de1172013-12-26 13:07:06 +08006690 /* There are several dir indexes for this inode, clear the cache. */
6691 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006692 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006693 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006694 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006695 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006696 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006697
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006698 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6699 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006700
Yan, Zhenga5719522009-09-24 09:17:31 -04006701 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006702 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006703 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006704 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006705
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006706 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006707 if (err)
6708 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006709 if (inode->i_nlink == 1) {
6710 /*
6711 * If new hard link count is 1, it's a file created
6712 * with open(2) O_TMPFILE flag.
6713 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006714 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006715 if (err)
6716 goto fail;
6717 }
Al Viro08c422c2011-12-23 07:58:13 -05006718 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006719 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006720 }
Chris Mason39279cc2007-06-12 06:35:45 -04006721
Chris Mason1832a6d2007-12-21 16:27:21 -05006722fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006723 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006724 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006725 if (drop_inode) {
6726 inode_dec_link_count(inode);
6727 iput(inode);
6728 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006729 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006730 return err;
6731}
6732
Christian Brauner549c7292021-01-21 14:19:43 +01006733static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6734 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006735{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006736 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006737 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006738 struct btrfs_trans_handle *trans;
6739 struct btrfs_root *root = BTRFS_I(dir)->root;
6740 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006741 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006742 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006743
Josef Bacik9ed74f22009-09-11 16:12:44 -04006744 /*
6745 * 2 items for inode and ref
6746 * 2 items for dir items
6747 * 1 for xattr if selinux is on
6748 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006749 trans = btrfs_start_transaction(root, 5);
6750 if (IS_ERR(trans))
6751 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006752
Nikolay Borisov543068a2020-12-07 17:32:33 +02006753 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006754 if (err)
6755 goto out_fail;
6756
Josef Bacikaec74772008-07-24 12:12:38 -04006757 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006758 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6759 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006760 if (IS_ERR(inode)) {
6761 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006762 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006763 goto out_fail;
6764 }
Chris Mason5f39d392007-10-15 16:14:19 -04006765
Chris Masonb0d5d102014-09-08 13:08:51 -07006766 /* these must be set before we unlock the inode */
6767 inode->i_op = &btrfs_dir_inode_operations;
6768 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006769
Eric Paris2a7dba32011-02-01 11:05:39 -05006770 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006771 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006772 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006773
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006774 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006775 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006776 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006777 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006778
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006779 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6780 dentry->d_name.name,
6781 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006782 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006783 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006784
Al Viro1e2e5472018-05-04 08:23:01 -04006785 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006786
6787out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006788 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006789 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006790 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006791 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006792 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006793 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006794 return err;
6795}
6796
Chris Masonc8b97812008-10-29 14:49:59 -04006797static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006798 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006799 size_t pg_offset, u64 extent_offset,
6800 struct btrfs_file_extent_item *item)
6801{
6802 int ret;
6803 struct extent_buffer *leaf = path->nodes[0];
6804 char *tmp;
6805 size_t max_size;
6806 unsigned long inline_size;
6807 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006808 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006809
6810 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006811 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006812 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6813 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006814 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006815 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006816 if (!tmp)
6817 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006818 ptr = btrfs_file_extent_inline_start(item);
6819
6820 read_extent_buffer(leaf, tmp, ptr, inline_size);
6821
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006822 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006823 ret = btrfs_decompress(compress_type, tmp, page,
6824 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006825
6826 /*
6827 * decompression code contains a memset to fill in any space between the end
6828 * of the uncompressed data and the end of max_size in case the decompressed
6829 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6830 * the end of an inline extent and the beginning of the next block, so we
6831 * cover that region here.
6832 */
6833
6834 if (max_size + pg_offset < PAGE_SIZE) {
6835 char *map = kmap(page);
6836 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6837 kunmap(page);
6838 }
Chris Masonc8b97812008-10-29 14:49:59 -04006839 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006840 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006841}
6842
Omar Sandoval39b07b52019-12-02 17:34:23 -08006843/**
6844 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6845 * @inode: file to search in
6846 * @page: page to read extent data into if the extent is inline
6847 * @pg_offset: offset into @page to copy to
6848 * @start: file offset
6849 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006850 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006851 * This returns the first &struct extent_map which overlaps with the given
6852 * range, reading it from the B-tree and caching it if necessary. Note that
6853 * there may be more extents which overlap the given range after the returned
6854 * extent_map.
6855 *
6856 * If @page is not NULL and the extent is inline, this also reads the extent
6857 * data directly into the page and marks the extent up to date in the io_tree.
6858 *
6859 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006860 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006861struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006862 struct page *page, size_t pg_offset,
6863 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006864{
David Sterba3ffbd682018-06-29 10:56:42 +02006865 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006866 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006867 u64 extent_start = 0;
6868 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006869 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006870 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006871 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006872 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006873 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006874 struct extent_buffer *leaf;
6875 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006876 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006877 struct extent_map_tree *em_tree = &inode->extent_tree;
6878 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006879
Chris Mason890871b2009-09-02 16:24:52 -04006880 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006881 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006882 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006883
Chris Masona52d9a82007-08-27 16:49:44 -04006884 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006885 if (em->start > start || em->start + em->len <= start)
6886 free_extent_map(em);
6887 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006888 free_extent_map(em);
6889 else
6890 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006891 }
David Sterba172ddd62011-04-21 00:48:27 +02006892 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006893 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006894 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006895 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006896 }
Chris Masond1310b22008-01-24 16:13:08 -05006897 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006898 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006899 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006900 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006901
Liu Bobee6ec82018-08-17 05:05:28 +08006902 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006903 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006904 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006905 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006906 }
6907
Liu Bobee6ec82018-08-17 05:05:28 +08006908 /* Chances are we'll be called again, so go ahead and do readahead */
6909 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006910
6911 /*
6912 * The same explanation in load_free_space_cache applies here as well,
6913 * we only read when we're loading the free space cache, and at that
6914 * point the commit_root has everything we need.
6915 */
6916 if (btrfs_is_free_space_inode(inode)) {
6917 path->search_commit_root = 1;
6918 path->skip_locking = 1;
6919 }
Josef Bacik51899412020-08-20 11:46:01 -04006920
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006921 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006923 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006924 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006925 if (path->slots[0] == 0)
6926 goto not_found;
6927 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006928 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006929 }
6930
Chris Mason5f39d392007-10-15 16:14:19 -04006931 leaf = path->nodes[0];
6932 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006933 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006934 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006935 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006936 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006937 /*
6938 * If we backup past the first extent we want to move forward
6939 * and see if there is an extent in front of us, otherwise we'll
6940 * say there is a hole for our whole search range which can
6941 * cause problems.
6942 */
6943 extent_end = start;
6944 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006945 }
6946
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006947 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006948 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006949 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006950 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6951 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006952 /* Only regular file could have regular/prealloc extent */
6953 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006954 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006955 btrfs_crit(fs_info,
6956 "regular/prealloc extent found for non-regular inode %llu",
6957 btrfs_ino(inode));
6958 goto out;
6959 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006960 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6961 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006962 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006963 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6964 path->slots[0],
6965 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006966 }
Josef Bacik25a50342013-10-14 12:08:38 -04006967next:
Yan Zheng9036c102008-10-30 14:19:41 -04006968 if (start >= extent_end) {
6969 path->slots[0]++;
6970 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6971 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006972 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006973 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006974 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006975 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006976
Yan Zheng9036c102008-10-30 14:19:41 -04006977 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006978 }
Yan Zheng9036c102008-10-30 14:19:41 -04006979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6980 if (found_key.objectid != objectid ||
6981 found_key.type != BTRFS_EXTENT_DATA_KEY)
6982 goto not_found;
6983 if (start + len <= found_key.offset)
6984 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006985 if (start > found_key.offset)
6986 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006987
6988 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006989 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006990 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006991 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006992 em->block_start = EXTENT_MAP_HOLE;
6993 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006994 }
6995
Omar Sandoval39b07b52019-12-02 17:34:23 -08006996 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006997
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006998 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6999 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007000 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007001 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007002 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007003 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007004 size_t size;
7005 size_t extent_offset;
7006 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007007
Omar Sandoval39b07b52019-12-02 17:34:23 -08007008 if (!page)
Yan689f9342007-10-29 11:41:07 -04007009 goto out;
Yan689f9342007-10-29 11:41:07 -04007010
Qu Wenruoe41ca582018-06-06 15:41:49 +08007011 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007012 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007013 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7014 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007015 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007016 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007017 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007018 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007019 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007020
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007021 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007022 if (btrfs_file_extent_compression(leaf, item) !=
7023 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007024 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007025 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007026 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007027 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007028 } else {
7029 map = kmap(page);
7030 read_extent_buffer(leaf, map + pg_offset, ptr,
7031 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007032 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007033 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007034 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007035 copy_size);
7036 }
Chris Masonc8b97812008-10-29 14:49:59 -04007037 kunmap(page);
7038 }
Chris Mason179e29e2007-11-01 11:28:41 -04007039 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007040 }
Chris Masond1310b22008-01-24 16:13:08 -05007041 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007042 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007043 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007044 }
7045not_found:
7046 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007047 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007048 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007049 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007050insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007051 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007052 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007053 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007054 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007055 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7056 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007057 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007058 goto out;
7059 }
Chris Masond1310b22008-01-24 16:13:08 -05007060
Chris Mason890871b2009-09-02 16:24:52 -04007061 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007062 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007063 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007064out:
Liu Boc6414282018-08-23 07:36:17 +08007065 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007066
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007067 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007068
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007069 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007070 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007071 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007072 }
7073 return em;
7074}
7075
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007076struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007077 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007078{
7079 struct extent_map *em;
7080 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007081 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007082 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007083 u64 delalloc_len;
7084 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007085 int err = 0;
7086
Omar Sandoval39b07b52019-12-02 17:34:23 -08007087 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007088 if (IS_ERR(em))
7089 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007090 /*
7091 * If our em maps to:
7092 * - a hole or
7093 * - a pre-alloc extent,
7094 * there might actually be delalloc bytes behind it.
7095 */
7096 if (em->block_start != EXTENT_MAP_HOLE &&
7097 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7098 return em;
7099 else
7100 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007101
7102 /* check to see if we've wrapped (len == -1 or similar) */
7103 end = start + len;
7104 if (end < start)
7105 end = (u64)-1;
7106 else
7107 end -= 1;
7108
7109 em = NULL;
7110
7111 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007112 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007113 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007114 delalloc_end = delalloc_start + delalloc_len;
7115 if (delalloc_end < delalloc_start)
7116 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007117
7118 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007119 * We didn't find anything useful, return the original results from
7120 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007121 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007122 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007123 em = hole_em;
7124 hole_em = NULL;
7125 goto out;
7126 }
7127
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007128 /*
7129 * Adjust the delalloc_start to make sure it doesn't go backwards from
7130 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007131 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007132 delalloc_start = max(start, delalloc_start);
7133 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007134
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007135 if (delalloc_len > 0) {
7136 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007137 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007138 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007139
David Sterba172ddd62011-04-21 00:48:27 +02007140 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007141 if (!em) {
7142 err = -ENOMEM;
7143 goto out;
7144 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007145
7146 ASSERT(hole_em);
7147 /*
7148 * When btrfs_get_extent can't find anything it returns one
7149 * huge hole
7150 *
7151 * Make sure what it found really fits our range, and adjust to
7152 * make sure it is based on the start from the caller
7153 */
7154 if (hole_end <= start || hole_em->start > end) {
7155 free_extent_map(hole_em);
7156 hole_em = NULL;
7157 } else {
7158 hole_start = max(hole_em->start, start);
7159 hole_len = hole_end - hole_start;
7160 }
7161
7162 if (hole_em && delalloc_start > hole_start) {
7163 /*
7164 * Our hole starts before our delalloc, so we have to
7165 * return just the parts of the hole that go until the
7166 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007167 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007168 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007169 em->start = hole_start;
7170 em->orig_start = hole_start;
7171 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007172 * Don't adjust block start at all, it is fixed at
7173 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007174 */
7175 em->block_start = hole_em->block_start;
7176 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007177 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7178 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007179 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007180 /*
7181 * Hole is out of passed range or it starts after
7182 * delalloc range
7183 */
7184 em->start = delalloc_start;
7185 em->len = delalloc_len;
7186 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007187 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007188 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007189 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007190 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007191 return hole_em;
7192 }
7193out:
7194
7195 free_extent_map(hole_em);
7196 if (err) {
7197 free_extent_map(em);
7198 return ERR_PTR(err);
7199 }
7200 return em;
7201}
7202
Nikolay Borisov64f54182020-06-03 08:55:31 +03007203static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007204 const u64 start,
7205 const u64 len,
7206 const u64 orig_start,
7207 const u64 block_start,
7208 const u64 block_len,
7209 const u64 orig_block_len,
7210 const u64 ram_bytes,
7211 const int type)
7212{
7213 struct extent_map *em = NULL;
7214 int ret;
7215
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007216 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007217 em = create_io_em(inode, start, len, orig_start, block_start,
7218 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007219 BTRFS_COMPRESS_NONE, /* compress_type */
7220 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007221 if (IS_ERR(em))
7222 goto out;
7223 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007224 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7225 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007226 if (ret) {
7227 if (em) {
7228 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007229 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007230 }
7231 em = ERR_PTR(ret);
7232 }
7233 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007234
7235 return em;
7236}
7237
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007238static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007239 u64 start, u64 len)
7240{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007241 struct btrfs_root *root = inode->root;
7242 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007243 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244 struct btrfs_key ins;
7245 u64 alloc_hint;
7246 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007248 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007249 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007250 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007251 if (ret)
7252 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007254 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007255 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007256 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007257 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007258 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007259 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7260 1);
Josef Bacik00361582013-08-14 14:02:47 -04007261
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262 return em;
7263}
7264
Anand Jainf4639632021-02-10 21:25:16 -08007265static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007266{
7267 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007268 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007269
7270 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7271 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007272 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007273 if (block_group)
7274 btrfs_put_block_group(block_group);
7275 return readonly;
7276}
7277
Chris Mason46bfbb52010-05-26 11:04:10 -04007278/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007279 * Check if we can do nocow write into the range [@offset, @offset + @len)
7280 *
7281 * @offset: File offset
7282 * @len: The length to write, will be updated to the nocow writeable
7283 * range
7284 * @orig_start: (optional) Return the original file offset of the file extent
7285 * @orig_len: (optional) Return the original on-disk length of the file extent
7286 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007287 * @strict: if true, omit optimizations that might force us into unnecessary
7288 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007289 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007290 * Return:
7291 * >0 and update @len if we can do nocow write
7292 * 0 if we can't do nocow write
7293 * <0 if error happened
7294 *
7295 * NOTE: This only checks the file extents, caller is responsible to wait for
7296 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007297 */
Josef Bacik00361582013-08-14 14:02:47 -04007298noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007300 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007301{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007302 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007303 struct btrfs_path *path;
7304 int ret;
7305 struct extent_buffer *leaf;
7306 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007307 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007308 struct btrfs_file_extent_item *fi;
7309 struct btrfs_key key;
7310 u64 disk_bytenr;
7311 u64 backref_offset;
7312 u64 extent_end;
7313 u64 num_bytes;
7314 int slot;
7315 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007316 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007317
Chris Mason46bfbb52010-05-26 11:04:10 -04007318 path = btrfs_alloc_path();
7319 if (!path)
7320 return -ENOMEM;
7321
David Sterbaf85b7372017-01-20 14:54:07 +01007322 ret = btrfs_lookup_file_extent(NULL, root, path,
7323 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 if (ret < 0)
7325 goto out;
7326
7327 slot = path->slots[0];
7328 if (ret == 1) {
7329 if (slot == 0) {
7330 /* can't find the item, must cow */
7331 ret = 0;
7332 goto out;
7333 }
7334 slot--;
7335 }
7336 ret = 0;
7337 leaf = path->nodes[0];
7338 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007339 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007340 key.type != BTRFS_EXTENT_DATA_KEY) {
7341 /* not our file or wrong item type, must cow */
7342 goto out;
7343 }
7344
7345 if (key.offset > offset) {
7346 /* Wrong offset, must cow */
7347 goto out;
7348 }
7349
7350 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7351 found_type = btrfs_file_extent_type(leaf, fi);
7352 if (found_type != BTRFS_FILE_EXTENT_REG &&
7353 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7354 /* not a regular extent, must cow */
7355 goto out;
7356 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007357
7358 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7359 goto out;
7360
Miao Xiee77751a2013-12-27 21:11:50 +08007361 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7362 if (extent_end <= offset)
7363 goto out;
7364
Chris Mason46bfbb52010-05-26 11:04:10 -04007365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007366 if (disk_bytenr == 0)
7367 goto out;
7368
7369 if (btrfs_file_extent_compression(leaf, fi) ||
7370 btrfs_file_extent_encryption(leaf, fi) ||
7371 btrfs_file_extent_other_encoding(leaf, fi))
7372 goto out;
7373
Ethan Lien78d42952018-05-17 14:58:29 +08007374 /*
7375 * Do the same check as in btrfs_cross_ref_exist but without the
7376 * unnecessary search.
7377 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007378 if (!strict &&
7379 (btrfs_file_extent_generation(leaf, fi) <=
7380 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007381 goto out;
7382
Chris Mason46bfbb52010-05-26 11:04:10 -04007383 backref_offset = btrfs_file_extent_offset(leaf, fi);
7384
Josef Bacik7ee9e442013-06-21 16:37:03 -04007385 if (orig_start) {
7386 *orig_start = key.offset - backref_offset;
7387 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7388 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7389 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007390
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007392 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007393
7394 num_bytes = min(offset + *len, extent_end) - offset;
7395 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7396 u64 range_end;
7397
Jeff Mahoneyda170662016-06-15 09:22:56 -04007398 range_end = round_up(offset + num_bytes,
7399 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007400 ret = test_range_bit(io_tree, offset, range_end,
7401 EXTENT_DELALLOC, 0, NULL);
7402 if (ret) {
7403 ret = -EAGAIN;
7404 goto out;
7405 }
7406 }
7407
Josef Bacik1bda19e2013-10-18 12:10:36 -04007408 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007409
7410 /*
7411 * look for other files referencing this extent, if we
7412 * find any we must cow
7413 */
Josef Bacik00361582013-08-14 14:02:47 -04007414
Liu Boe4c3b2d2017-01-30 12:25:28 -08007415 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007416 key.offset - backref_offset, disk_bytenr,
7417 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007418 if (ret) {
7419 ret = 0;
7420 goto out;
7421 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007422
7423 /*
7424 * adjust disk_bytenr and num_bytes to cover just the bytes
7425 * in this extent we are about to write. If there
7426 * are any csums in that range we have to cow in order
7427 * to keep the csums correct
7428 */
7429 disk_bytenr += backref_offset;
7430 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007431 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7432 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007433 /*
7434 * all of the above have passed, it is safe to overwrite this extent
7435 * without cow
7436 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007437 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007438 ret = 1;
7439out:
7440 btrfs_free_path(path);
7441 return ret;
7442}
7443
Josef Bacikeb838e72012-07-31 16:28:48 -04007444static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007445 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007446{
7447 struct btrfs_ordered_extent *ordered;
7448 int ret = 0;
7449
7450 while (1) {
7451 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007452 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007453 /*
7454 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007455 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007456 * extents in this range.
7457 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007458 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007459 lockend - lockstart + 1);
7460
7461 /*
7462 * We need to make sure there are no buffered pages in this
7463 * range either, we could have raced between the invalidate in
7464 * generic_file_direct_write and locking the extent. The
7465 * invalidate needs to happen so that reads after a write do not
7466 * get stale data.
7467 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007468 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007469 (!writing || !filemap_range_has_page(inode->i_mapping,
7470 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007471 break;
7472
7473 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007474 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007475
7476 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007477 /*
7478 * If we are doing a DIO read and the ordered extent we
7479 * found is for a buffered write, we can not wait for it
7480 * to complete and retry, because if we do so we can
7481 * deadlock with concurrent buffered writes on page
7482 * locks. This happens only if our DIO read covers more
7483 * than one extent map, if at this point has already
7484 * created an ordered extent for a previous extent map
7485 * and locked its range in the inode's io tree, and a
7486 * concurrent write against that previous extent map's
7487 * range and this range started (we unlock the ranges
7488 * in the io tree only when the bios complete and
7489 * buffered writes always lock pages before attempting
7490 * to lock range in the io tree).
7491 */
7492 if (writing ||
7493 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007494 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007495 else
7496 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007497 btrfs_put_ordered_extent(ordered);
7498 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007499 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007500 * We could trigger writeback for this range (and wait
7501 * for it to complete) and then invalidate the pages for
7502 * this range (through invalidate_inode_pages2_range()),
7503 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007504 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007505 * triggered a readahead) on a page lock due to an
7506 * ordered dio extent we created before but did not have
7507 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007508 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007509 * ordered extent to complete while holding a lock on
7510 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007512 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007513 }
7514
Filipe Mananaade77022016-02-18 14:28:55 +00007515 if (ret)
7516 break;
7517
Josef Bacikeb838e72012-07-31 16:28:48 -04007518 cond_resched();
7519 }
7520
7521 return ret;
7522}
7523
Liu Bo6f9994d2017-01-31 07:50:22 -08007524/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007525static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7526 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007527 u64 block_len, u64 orig_block_len,
7528 u64 ram_bytes, int compress_type,
7529 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007530{
7531 struct extent_map_tree *em_tree;
7532 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007533 int ret;
7534
Liu Bo6f9994d2017-01-31 07:50:22 -08007535 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7536 type == BTRFS_ORDERED_COMPRESSED ||
7537 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007538 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007539
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007540 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007541 em = alloc_extent_map();
7542 if (!em)
7543 return ERR_PTR(-ENOMEM);
7544
7545 em->start = start;
7546 em->orig_start = orig_start;
7547 em->len = len;
7548 em->block_len = block_len;
7549 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007550 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007551 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007552 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007553 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007554 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007555 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007556 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007557 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7558 em->compress_type = compress_type;
7559 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007560
7561 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007562 btrfs_drop_extent_cache(inode, em->start,
7563 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007564 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007565 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007566 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007567 /*
7568 * The caller has taken lock_extent(), who could race with us
7569 * to add em?
7570 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007571 } while (ret == -EEXIST);
7572
7573 if (ret) {
7574 free_extent_map(em);
7575 return ERR_PTR(ret);
7576 }
7577
Liu Bo6f9994d2017-01-31 07:50:22 -08007578 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007579 return em;
7580}
7581
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007582
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007583static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007584 struct inode *inode,
7585 struct btrfs_dio_data *dio_data,
7586 u64 start, u64 len)
7587{
7588 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7589 struct extent_map *em = *map;
7590 int ret = 0;
7591
7592 /*
7593 * We don't allocate a new extent in the following cases
7594 *
7595 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7596 * existing extent.
7597 * 2) The extent is marked as PREALLOC. We're good to go here and can
7598 * just use the extent.
7599 *
7600 */
7601 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7602 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7603 em->block_start != EXTENT_MAP_HOLE)) {
7604 int type;
7605 u64 block_start, orig_start, orig_block_len, ram_bytes;
7606
7607 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7608 type = BTRFS_ORDERED_PREALLOC;
7609 else
7610 type = BTRFS_ORDERED_NOCOW;
7611 len = min(len, em->len - (start - em->start));
7612 block_start = em->block_start + (start - em->start);
7613
7614 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007615 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007616 btrfs_inc_nocow_writers(fs_info, block_start)) {
7617 struct extent_map *em2;
7618
Nikolay Borisov64f54182020-06-03 08:55:31 +03007619 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007620 orig_start, block_start,
7621 len, orig_block_len,
7622 ram_bytes, type);
7623 btrfs_dec_nocow_writers(fs_info, block_start);
7624 if (type == BTRFS_ORDERED_PREALLOC) {
7625 free_extent_map(em);
7626 *map = em = em2;
7627 }
7628
7629 if (em2 && IS_ERR(em2)) {
7630 ret = PTR_ERR(em2);
7631 goto out;
7632 }
7633 /*
7634 * For inode marked NODATACOW or extent marked PREALLOC,
7635 * use the existing or preallocated extent, so does not
7636 * need to adjust btrfs_space_info's bytes_may_use.
7637 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007638 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007639 goto skip_cow;
7640 }
7641 }
7642
7643 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007644 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007645 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007646 if (IS_ERR(em)) {
7647 ret = PTR_ERR(em);
7648 goto out;
7649 }
7650
7651 len = min(len, em->len - (start - em->start));
7652
7653skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007654 /*
7655 * Need to update the i_size under the extent lock so buffered
7656 * readers will get the updated i_size when we unlock.
7657 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007658 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007659 i_size_write(inode, start + len);
7660
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007661 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007662out:
7663 return ret;
7664}
7665
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007666static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7667 loff_t length, unsigned int flags, struct iomap *iomap,
7668 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007670 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307673 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007674 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007675 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007676 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007677 u64 len = length;
7678 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007679
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007680 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007681 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007682
Josef Bacikc3298612012-08-03 16:49:19 -04007683 lockstart = start;
7684 lockend = start + len - 1;
7685
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007686 /*
7687 * The generic stuff only does filemap_write_and_wait_range, which
7688 * isn't enough if we've written compressed pages to this area, so we
7689 * need to flush the dirty pages again to make absolutely sure that any
7690 * outstanding dirty pages are on disk.
7691 */
7692 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7693 &BTRFS_I(inode)->runtime_flags)) {
7694 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7695 start + length - 1);
7696 if (ret)
7697 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007698 }
7699
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007700 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7701 if (!dio_data)
7702 return -ENOMEM;
7703
7704 dio_data->length = length;
7705 if (write) {
7706 dio_data->reserve = round_up(length, fs_info->sectorsize);
7707 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7708 &dio_data->data_reserved,
7709 start, dio_data->reserve);
7710 if (ret) {
7711 extent_changeset_free(dio_data->data_reserved);
7712 kfree(dio_data);
7713 return ret;
7714 }
7715 }
7716 iomap->private = dio_data;
7717
7718
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 /*
7720 * If this errors out it's because we couldn't invalidate pagecache for
7721 * this range and we need to fallback to buffered.
7722 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007723 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007724 ret = -ENOTBLK;
7725 goto err;
7726 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007727
Omar Sandoval39b07b52019-12-02 17:34:23 -08007728 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007729 if (IS_ERR(em)) {
7730 ret = PTR_ERR(em);
7731 goto unlock_err;
7732 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007733
7734 /*
7735 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7736 * io. INLINE is special, and we could probably kludge it in here, but
7737 * it's still buffered so for safety lets just fall back to the generic
7738 * buffered path.
7739 *
7740 * For COMPRESSED we _have_ to read the entire extent in so we can
7741 * decompress it, so there will be buffering required no matter what we
7742 * do, so go ahead and fallback to buffered.
7743 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007744 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 * to buffered IO. Don't blame me, this is the price we pay for using
7746 * the generic code.
7747 */
7748 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7749 em->block_start == EXTENT_MAP_INLINE) {
7750 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007751 ret = -ENOTBLK;
7752 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 }
7754
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007755 len = min(len, em->len - (start - em->start));
7756 if (write) {
7757 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7758 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007759 if (ret < 0)
7760 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007761 unlock_extents = true;
7762 /* Recalc len in case the new em is smaller than requested */
7763 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007764 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007765 /*
7766 * We need to unlock only the end area that we aren't using.
7767 * The rest is going to be unlocked by the endio routine.
7768 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007769 lockstart = start + len;
7770 if (lockstart < lockend)
7771 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772 }
7773
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007774 if (unlock_extents)
7775 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7776 lockstart, lockend, &cached_state);
7777 else
7778 free_extent_state(cached_state);
7779
7780 /*
7781 * Translate extent map information to iomap.
7782 * We trim the extents (and move the addr) even though iomap code does
7783 * that, since we have locked only the parts we are performing I/O in.
7784 */
7785 if ((em->block_start == EXTENT_MAP_HOLE) ||
7786 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7787 iomap->addr = IOMAP_NULL_ADDR;
7788 iomap->type = IOMAP_HOLE;
7789 } else {
7790 iomap->addr = em->block_start + (start - em->start);
7791 iomap->type = IOMAP_MAPPED;
7792 }
7793 iomap->offset = start;
7794 iomap->bdev = fs_info->fs_devices->latest_bdev;
7795 iomap->length = len;
7796
Naohiro Aota544d24f2021-02-04 19:22:06 +09007797 if (write && btrfs_use_zone_append(BTRFS_I(inode), em))
7798 iomap->flags |= IOMAP_F_ZONE_APPEND;
7799
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 free_extent_map(em);
7801
7802 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007803
7804unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007805 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7806 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007807err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007808 if (dio_data) {
7809 btrfs_delalloc_release_space(BTRFS_I(inode),
7810 dio_data->data_reserved, start,
7811 dio_data->reserve, true);
7812 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7813 extent_changeset_free(dio_data->data_reserved);
7814 kfree(dio_data);
7815 }
7816 return ret;
7817}
7818
7819static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7820 ssize_t written, unsigned int flags, struct iomap *iomap)
7821{
7822 int ret = 0;
7823 struct btrfs_dio_data *dio_data = iomap->private;
7824 size_t submitted = dio_data->submitted;
7825 const bool write = !!(flags & IOMAP_WRITE);
7826
7827 if (!write && (iomap->type == IOMAP_HOLE)) {
7828 /* If reading from a hole, unlock and return */
7829 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7830 goto out;
7831 }
7832
7833 if (submitted < length) {
7834 pos += submitted;
7835 length -= submitted;
7836 if (write)
7837 __endio_write_update_ordered(BTRFS_I(inode), pos,
7838 length, false);
7839 else
7840 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7841 pos + length - 1);
7842 ret = -ENOTBLK;
7843 }
7844
7845 if (write) {
7846 if (dio_data->reserve)
7847 btrfs_delalloc_release_space(BTRFS_I(inode),
7848 dio_data->data_reserved, pos,
7849 dio_data->reserve, true);
7850 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7851 extent_changeset_free(dio_data->data_reserved);
7852 }
7853out:
7854 kfree(dio_data);
7855 iomap->private = NULL;
7856
Josef Bacikeb838e72012-07-31 16:28:48 -04007857 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858}
7859
Omar Sandoval769b4f22020-04-16 14:46:22 -07007860static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7861{
7862 /*
7863 * This implies a barrier so that stores to dio_bio->bi_status before
7864 * this and loads of dio_bio->bi_status after this are fully ordered.
7865 */
7866 if (!refcount_dec_and_test(&dip->refs))
7867 return;
7868
Naohiro Aotacfe94442021-02-04 19:21:59 +09007869 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007870 __endio_write_update_ordered(BTRFS_I(dip->inode),
7871 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007872 dip->bytes,
7873 !dip->dio_bio->bi_status);
7874 } else {
7875 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7876 dip->logical_offset,
7877 dip->logical_offset + dip->bytes - 1);
7878 }
7879
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007880 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007881 kfree(dip);
7882}
7883
Omar Sandoval77d5d682020-04-16 14:46:25 -07007884static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7885 int mirror_num,
7886 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007887{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007888 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007890 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007891
Mike Christie37226b22016-06-05 14:31:52 -05007892 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007893
Omar Sandoval5c047a62020-04-16 14:46:24 -07007894 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007895 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007896 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007897
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007898 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007899 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007900 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007901 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007902 return ret;
7903}
7904
Omar Sandoval769b4f22020-04-16 14:46:22 -07007905static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7906 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007907 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007908{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007909 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7910 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007911 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7913 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007914 struct bio_vec bvec;
7915 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007916 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007917 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007919
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007920 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7921 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922
Liu Bo17347ce2017-05-15 15:33:27 -07007923 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7924 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007925 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007926 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007927 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007928 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007929 bio_offset, bvec.bv_page,
7930 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007931 clean_io_failure(fs_info, failure_tree, io_tree,
7932 start, bvec.bv_page,
7933 btrfs_ino(BTRFS_I(inode)),
7934 pgoff);
7935 } else {
7936 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007937
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007938 ASSERT((start - io_bio->logical) < UINT_MAX);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007939 status = btrfs_submit_read_repair(inode,
7940 &io_bio->bio,
7941 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007942 bvec.bv_page, pgoff,
7943 start,
7944 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007945 io_bio->mirror_num,
7946 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007947 if (status)
7948 err = status;
7949 }
7950 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007951 ASSERT(bio_offset + sectorsize > bio_offset);
7952 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307953 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307954 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007955 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007956 return err;
7957}
7958
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007959static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007960 const u64 offset, const u64 bytes,
7961 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007962{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007963 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007964 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007965 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007966 u64 ordered_offset = offset;
7967 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007968 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007969
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007970 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007971 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007972 else
Qu Wenruo524272602017-03-08 10:25:52 +08007973 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007974
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007975 while (ordered_offset < offset + bytes) {
7976 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007977 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007978 &ordered_offset,
7979 ordered_bytes,
7980 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007981 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7982 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007983 btrfs_queue_work(wq, &ordered->work);
7984 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08007985
7986 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007987 if (ordered_offset == last_offset)
7988 return;
7989 /*
7990 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007991 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007992 */
7993 if (ordered_offset < offset + bytes) {
7994 ordered_bytes = offset + bytes - ordered_offset;
7995 ordered = NULL;
7996 }
Chris Mason163cf092010-11-28 19:56:33 -05007997 }
Filipe Manana14543772015-11-24 16:23:54 +00007998}
7999
Qu Wenruo8896a082020-10-21 14:24:53 +08008000static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008001 struct bio *bio,
8002 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008003{
Qu Wenruo1941b642020-12-02 14:47:57 +08008004 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008005}
8006
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008007static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008008{
8009 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008010 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008011
Miao Xie8b110e32014-09-12 18:44:03 +08008012 if (err)
8013 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008014 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008015 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008016 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008017 bio->bi_iter.bi_size, err);
8018
Omar Sandoval769b4f22020-04-16 14:46:22 -07008019 if (bio_op(bio) == REQ_OP_READ) {
8020 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008021 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008022 }
8023
Omar Sandoval769b4f22020-04-16 14:46:22 -07008024 if (err)
8025 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008026
Naohiro Aota544d24f2021-02-04 19:22:06 +09008027 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8028
Miao Xiee65e1532010-11-22 03:04:43 +00008029 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008030 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008031}
8032
David Sterbad0ee3932018-03-08 14:35:48 +01008033static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8034 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008035{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008036 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008037 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008038 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008039 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008040
Liu Bo4c274bc2017-11-01 17:19:27 -06008041 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008042 if (async_submit)
8043 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8044
Josef Bacik5fd02042012-05-02 14:00:54 -04008045 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008046 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008047 if (ret)
8048 goto err;
8049 }
Miao Xiee65e1532010-11-22 03:04:43 +00008050
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008051 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008052 goto map;
8053
8054 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008055 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008056 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008057 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008058 } else if (write) {
8059 /*
8060 * If we aren't doing async submit, calculate the csum of the
8061 * bio now.
8062 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008063 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008064 if (ret)
8065 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008066 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008067 u64 csum_offset;
8068
8069 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008070 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008071 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008072 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008073 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008074map:
Chris Mason08635ba2019-07-10 12:28:14 -07008075 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008076err:
Miao Xiee65e1532010-11-22 03:04:43 +00008077 return ret;
8078}
8079
Omar Sandovalc36cac22020-04-16 14:46:13 -07008080/*
8081 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8082 * or ordered extents whether or not we submit any bios.
8083 */
8084static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8085 struct inode *inode,
8086 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008087{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008088 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008089 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8090 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008091 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008092
Omar Sandoval85879572020-04-16 14:46:21 -07008093 dip_size = sizeof(*dip);
8094 if (!write && csum) {
8095 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008096 size_t nblocks;
8097
David Sterba265fdfa2020-07-01 21:19:09 +02008098 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008099 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008100 }
8101
8102 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008103 if (!dip)
8104 return NULL;
8105
Omar Sandovalc36cac22020-04-16 14:46:13 -07008106 dip->inode = inode;
8107 dip->logical_offset = file_offset;
8108 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008109 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008110 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008111 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008112 return dip;
8113}
8114
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008115static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8116 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008117{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008118 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008120 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8121 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008122 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008123 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008124 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008125 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008126 u64 submit_len;
8127 int clone_offset = 0;
8128 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008129 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308130 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008131 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008132 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008133 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008134 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008135
Omar Sandovalc36cac22020-04-16 14:46:13 -07008136 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8137 if (!dip) {
8138 if (!write) {
8139 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8140 file_offset + dio_bio->bi_iter.bi_size - 1);
8141 }
8142 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008143 bio_endio(dio_bio);
8144 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008145 }
8146
Josef Bacik334c16d2020-10-16 11:29:14 -04008147 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008148 /*
8149 * Load the csums up front to reduce csum tree searches and
8150 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008151 *
8152 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008153 */
Qu Wenruo62751932020-12-02 14:48:06 +08008154 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008155 if (status != BLK_STS_OK)
8156 goto out_err;
8157 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008158
Omar Sandoval769b4f22020-04-16 14:46:22 -07008159 start_sector = dio_bio->bi_iter.bi_sector;
8160 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008161
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008162 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008163 logical = start_sector << 9;
8164 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8165 if (IS_ERR(em)) {
8166 status = errno_to_blk_status(PTR_ERR(em));
8167 em = NULL;
8168 goto out_err_em;
8169 }
8170 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
8171 logical, submit_len, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008172 if (ret) {
8173 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008174 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008175 }
8176 ASSERT(geom.len <= INT_MAX);
8177
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008178 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008179
Liu Bo725130b2017-05-16 09:51:39 -07008180 /*
8181 * This will never fail as it's passing GPF_NOFS and
8182 * the allocation is backed by btrfs_bioset.
8183 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008184 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008185 bio->bi_private = dip;
8186 bio->bi_end_io = btrfs_end_dio_bio;
8187 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008188
Naohiro Aota544d24f2021-02-04 19:22:06 +09008189 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8190 status = extract_ordered_extent(BTRFS_I(inode), bio,
8191 file_offset);
8192 if (status) {
8193 bio_put(bio);
8194 goto out_err;
8195 }
8196 }
8197
Liu Bo725130b2017-05-16 09:51:39 -07008198 ASSERT(submit_len >= clone_len);
8199 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008200
Liu Bo725130b2017-05-16 09:51:39 -07008201 /*
8202 * Increase the count before we submit the bio so we know
8203 * the end IO handler won't happen before we increase the
8204 * count. Otherwise, the dip might get freed before we're
8205 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008206 *
8207 * We transfer the initial reference to the last bio, so we
8208 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008209 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008210 if (submit_len > 0) {
8211 refcount_inc(&dip->refs);
8212 /*
8213 * If we are submitting more than one bio, submit them
8214 * all asynchronously. The exception is RAID 5 or 6, as
8215 * asynchronous checksums make it difficult to collect
8216 * full stripe writes.
8217 */
8218 if (!raid56)
8219 async_submit = 1;
8220 }
Miao Xiee65e1532010-11-22 03:04:43 +00008221
David Sterbad0ee3932018-03-08 14:35:48 +01008222 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008223 async_submit);
8224 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008225 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008226 if (submit_len > 0)
8227 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008228 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008229 }
Liu Bo725130b2017-05-16 09:51:39 -07008230
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008231 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008232 clone_offset += clone_len;
8233 start_sector += clone_len >> 9;
8234 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008235
8236 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008237 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008238 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008239
Michal Rostecki42034312021-01-27 14:57:27 +01008240out_err_em:
8241 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008242out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008243 dip->dio_bio->bi_status = status;
8244 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008245
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008246 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008247}
8248
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008249const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008250 .iomap_begin = btrfs_dio_iomap_begin,
8251 .iomap_end = btrfs_dio_iomap_end,
8252};
8253
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008254const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008255 .submit_io = btrfs_submit_direct,
8256};
8257
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008258static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008259 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008260{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008261 int ret;
8262
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008263 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008264 if (ret)
8265 return ret;
8266
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008267 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008268}
8269
Chris Mason9ebefb182007-06-15 13:50:00 -04008270int btrfs_readpage(struct file *file, struct page *page)
8271{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008272 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8273 u64 start = page_offset(page);
8274 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008275 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008276 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008277 int ret;
8278
Nikolay Borisov0f208812020-09-14 14:39:16 +03008279 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8280
8281 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008282 if (bio)
8283 ret = submit_one_bio(bio, 0, bio_flags);
8284 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008285}
Chris Mason1832a6d2007-12-21 16:27:21 -05008286
Chris Mason39279cc2007-06-12 06:35:45 -04008287static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8288{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008289 struct inode *inode = page->mapping->host;
8290 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008291
8292 if (current->flags & PF_MEMALLOC) {
8293 redirty_page_for_writepage(wbc, page);
8294 unlock_page(page);
8295 return 0;
8296 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008297
8298 /*
8299 * If we are under memory pressure we will call this directly from the
8300 * VM, we need to make sure we have the inode referenced for the ordered
8301 * extent. If not just return like we didn't do anything.
8302 */
8303 if (!igrab(inode)) {
8304 redirty_page_for_writepage(wbc, page);
8305 return AOP_WRITEPAGE_ACTIVATE;
8306 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008307 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008308 btrfs_add_delayed_iput(inode);
8309 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008310}
Chris Mason39279cc2007-06-12 06:35:45 -04008311
Eric Sandeen48a3b632013-04-25 20:41:01 +00008312static int btrfs_writepages(struct address_space *mapping,
8313 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008314{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008315 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008316}
8317
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008318static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008319{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008320 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008321}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008322
Chris Masone6dcd2d2008-07-17 12:53:50 -04008323static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008324{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008325 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008326 if (ret == 1)
Qu Wenruo32443de2021-01-26 16:34:00 +08008327 clear_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008328 return ret;
8329}
Chris Mason39279cc2007-06-12 06:35:45 -04008330
Chris Masone6dcd2d2008-07-17 12:53:50 -04008331static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8332{
Chris Mason98509cf2008-09-11 15:51:43 -04008333 if (PageWriteback(page) || PageDirty(page))
8334 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008335 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008336}
8337
Roman Gushchinf8e66082020-03-04 16:57:35 -08008338#ifdef CONFIG_MIGRATION
8339static int btrfs_migratepage(struct address_space *mapping,
8340 struct page *newpage, struct page *page,
8341 enum migrate_mode mode)
8342{
8343 int ret;
8344
8345 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8346 if (ret != MIGRATEPAGE_SUCCESS)
8347 return ret;
8348
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008349 if (page_has_private(page))
8350 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008351
8352 if (PagePrivate2(page)) {
8353 ClearPagePrivate2(page);
8354 SetPagePrivate2(newpage);
8355 }
8356
8357 if (mode != MIGRATE_SYNC_NO_COPY)
8358 migrate_page_copy(newpage, page);
8359 else
8360 migrate_page_states(newpage, page);
8361 return MIGRATEPAGE_SUCCESS;
8362}
8363#endif
8364
Lukas Czernerd47992f2013-05-21 23:17:23 -04008365static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8366 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008367{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008368 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8369 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008370 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008371 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008372 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008373 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308374 u64 start;
8375 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008376 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008377 bool found_ordered = false;
8378 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008379
Chris Mason8b62b722009-09-02 16:53:46 -04008380 /*
8381 * we have the page locked, so new writeback can't start,
8382 * and the dirty bit won't be cleared while we are here.
8383 *
8384 * Wait for IO on this page so that we can safely clear
8385 * the PagePrivate2 bit and do ordered accounting
8386 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008387 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008388
Chris Masone6dcd2d2008-07-17 12:53:50 -04008389 if (offset) {
8390 btrfs_releasepage(page, GFP_NOFS);
8391 return;
8392 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008393
8394 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008395 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008396
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308397 start = page_start;
Qu Wenruo951c80f2021-01-27 14:38:48 +08008398again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008399 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008400 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008401 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008402 end = min(page_end,
8403 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008404 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008405 * IO on this page will never be started, so we need to account
8406 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8407 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008408 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008409 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308410 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008411 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008412 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008413 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008414 /*
8415 * whoever cleared the private bit is responsible
8416 * for the finish_ordered_io
8417 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008418 if (TestClearPagePrivate2(page)) {
8419 struct btrfs_ordered_inode_tree *tree;
8420 u64 new_len;
8421
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008422 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008423
8424 spin_lock_irq(&tree->lock);
8425 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308426 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008427 if (new_len < ordered->truncated_len)
8428 ordered->truncated_len = new_len;
8429 spin_unlock_irq(&tree->lock);
8430
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008431 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8432 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008433 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008434 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008435 completed_ordered = true;
8436 }
Chris Mason8b62b722009-09-02 16:53:46 -04008437 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008438 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008439 if (!inode_evicting) {
8440 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308441 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008442 &cached_state);
8443 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308444
8445 start = end + 1;
8446 if (start < page_end)
8447 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008448 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008449
Qu Wenruob9d0b382015-09-29 10:35:16 +08008450 /*
8451 * Qgroup reserved space handler
8452 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008453 * 1) Already written to disk or ordered extent already submitted
8454 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8455 * Qgroup will be handled by its qgroup_record then.
8456 * btrfs_qgroup_free_data() call will do nothing here.
8457 *
8458 * 2) Not written to disk yet
8459 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8460 * bit of its io_tree, and free the qgroup reserved data space.
8461 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008462 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008463 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008464 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008465 bool delete = true;
8466
8467 /*
8468 * If there's an ordered extent for this range and we have not
8469 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8470 * in the range for the ordered extent completion. We must also
8471 * not delete the range, otherwise we would lose that bit (and
8472 * any other bits set in the range). Make sure EXTENT_UPTODATE
8473 * is cleared if we don't delete, otherwise it can lead to
8474 * corruptions if the i_size is extented later.
8475 */
8476 if (found_ordered && !completed_ordered)
8477 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008478 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008479 EXTENT_DELALLOC | EXTENT_UPTODATE |
8480 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8481 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008482
8483 __btrfs_releasepage(page, GFP_NOFS);
8484 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008485
Chris Mason4a096752008-07-21 10:29:44 -04008486 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008487 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008488}
8489
Chris Mason9ebefb182007-06-15 13:50:00 -04008490/*
8491 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8492 * called from a page fault handler when a page is first dirtied. Hence we must
8493 * be careful to check for EOF conditions here. We set the page up correctly
8494 * for a written page which means we get ENOSPC checking when writing into
8495 * holes and correct delalloc and unwritten extent mapping on filesystems that
8496 * support these features.
8497 *
8498 * We are not allowed to take the i_mutex here so we have to play games to
8499 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008500 * truncate_setsize() writes the inode size before removing pages, once we have
8501 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008502 * beyond EOF, then the page is guaranteed safe against truncation until we
8503 * unlock the page.
8504 */
Souptick Joardera528a242018-06-06 19:54:44 +05308505vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008506{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008507 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008508 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008509 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008510 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8511 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008512 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008513 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008514 char *kaddr;
8515 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008516 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308517 vm_fault_t ret;
8518 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008519 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308520 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008521 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008522 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308523 u64 end;
8524
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008525 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008526
Jan Karab2b5ef52012-06-12 16:20:45 +02008527 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008528 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008529 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308530 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008531
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308532 /*
8533 * Reserving delalloc space after obtaining the page lock can lead to
8534 * deadlock. For example, if a dirty page is locked by this function
8535 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8536 * dirty page write out, then the btrfs_writepage() function could
8537 * end up waiting indefinitely to get a lock on the page currently
8538 * being processed by btrfs_page_mkwrite() function.
8539 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008540 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8541 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308542 if (!ret2) {
8543 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008544 reserved = 1;
8545 }
Souptick Joardera528a242018-06-06 19:54:44 +05308546 if (ret2) {
8547 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008548 if (reserved)
8549 goto out;
8550 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008551 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008552
Nick Piggin56a76f82009-03-31 15:23:23 -07008553 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008554again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008555 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008556 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008557
Chris Mason9ebefb182007-06-15 13:50:00 -04008558 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008559 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008560 /* page got truncated out from underneath us */
8561 goto out_unlock;
8562 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008563 wait_on_page_writeback(page);
8564
David Sterbaff13db42015-12-03 14:30:40 +01008565 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008566 ret2 = set_page_extent_mapped(page);
8567 if (ret2 < 0) {
8568 ret = vmf_error(ret2);
8569 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8570 goto out_unlock;
8571 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008572
Chris Masoneb84ae02008-07-17 13:53:27 -04008573 /*
8574 * we can't set the delalloc bits if there are pending ordered
8575 * extents. Drop our locks and wait for them to finish
8576 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008577 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8578 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008579 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008580 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008581 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008582 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008583 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008584 btrfs_put_ordered_extent(ordered);
8585 goto again;
8586 }
8587
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008588 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008589 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008590 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008591 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308592 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008593 btrfs_delalloc_release_space(BTRFS_I(inode),
8594 data_reserved, page_start,
8595 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308596 }
8597 }
8598
Josef Bacikfbf19082009-10-01 17:10:23 -04008599 /*
Liu Bo54160342016-12-13 12:15:19 -08008600 * page_mkwrite gets called when the page is firstly dirtied after it's
8601 * faulted in, but write(2) could also dirty a page and set delalloc
8602 * bits, thus in this case for space account reason, we still need to
8603 * clear any delalloc bits within this page range since we have to
8604 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008605 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308606 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008607 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8608 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008609
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008610 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008611 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308612 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008613 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008614 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008615 ret = VM_FAULT_SIGBUS;
8616 goto out_unlock;
8617 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008618
8619 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008620 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008621 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008622 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008623 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008624
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008625 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008627 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008628 flush_dcache_page(page);
8629 kunmap(page);
8630 }
Chris Mason247e7432008-07-17 12:53:51 -04008631 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008633 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008634
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008635 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008636 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008637 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008638
David Sterbae43bbe52017-12-12 21:43:52 +01008639 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008640
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008641 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8642 sb_end_pagefault(inode->i_sb);
8643 extent_changeset_free(data_reserved);
8644 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008645
8646out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008647 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008648out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008649 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008650 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008651 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008652out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008653 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008654 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008655 return ret;
8656}
8657
Filipe Manana213e8c52018-02-06 20:40:31 +00008658static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008659{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008660 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008661 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008662 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008663 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008664 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008665 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008666 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008667
Filipe Manana213e8c52018-02-06 20:40:31 +00008668 if (!skip_writeback) {
8669 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8670 (u64)-1);
8671 if (ret)
8672 return ret;
8673 }
Chris Mason39279cc2007-06-12 06:35:45 -04008674
Josef Bacikfcb80c22011-05-03 10:40:22 -04008675 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008676 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8677 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008678 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008679 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008680 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008681 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008682 * be free'd up by the truncate operation, but also have some slack
8683 * space reserved in case it uses space during the truncate (thank you
8684 * very much snapshotting).
8685 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008686 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008687 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008688 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008689 * doesn't end up using space reserved for updating the inode. We also
8690 * need to be able to stop the transaction and start a new one, which
8691 * means we need to be able to update the inode several times, and we
8692 * have no idea of knowing how many times that will be, so we can't just
8693 * reserve 1 item for the entirety of the operation, so that has to be
8694 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008695 *
8696 * So that leaves us with
8697 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008698 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008699 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008700 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008701 * updating the inode.
8702 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008703 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008704 if (!rsv)
8705 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008706 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008707 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008708
Josef Bacik907cbce2011-08-08 13:46:15 -04008709 /*
Josef Bacik07127182011-08-19 10:29:59 -04008710 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008711 * 1 for updating the inode.
8712 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008713 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008714 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008715 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008716 goto out;
8717 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008718
Josef Bacik907cbce2011-08-08 13:46:15 -04008719 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008720 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008721 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008722 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008723
Chris Mason5a3f23d2009-03-31 13:27:11 -04008724 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008725 * So if we truncate and then write and fsync we normally would just
8726 * write the extents that changed, which is a problem if we need to
8727 * first truncate that entire inode. So set this flag so we write out
8728 * all of the extents in the inode to the sync log so we're completely
8729 * safe.
8730 */
8731 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008732 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008733
Yan, Zheng80825102009-11-12 09:35:36 +00008734 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008735 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008736 inode->i_size,
8737 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008738 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008739 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008740 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008741
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008742 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008743 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008744 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008745
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008746 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008747 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008748
8749 trans = btrfs_start_transaction(root, 2);
8750 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008751 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008752 trans = NULL;
8753 break;
8754 }
8755
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008756 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008757 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008758 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008759 BUG_ON(ret); /* shouldn't happen */
8760 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008761 }
8762
Josef Bacikddfae632017-10-19 14:16:02 -04008763 /*
8764 * We can't call btrfs_truncate_block inside a trans handle as we could
8765 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8766 * we've truncated everything except the last little bit, and can do
8767 * btrfs_truncate_block and then update the disk_i_size.
8768 */
8769 if (ret == NEED_TRUNCATE_BLOCK) {
8770 btrfs_end_transaction(trans);
8771 btrfs_btree_balance_dirty(fs_info);
8772
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008773 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008774 if (ret)
8775 goto out;
8776 trans = btrfs_start_transaction(root, 1);
8777 if (IS_ERR(trans)) {
8778 ret = PTR_ERR(trans);
8779 goto out;
8780 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008781 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008782 }
8783
Chris Mason917c16b2011-11-08 14:49:59 -05008784 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008785 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008786
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008787 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008788 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008789 if (ret2 && !ret)
8790 ret = ret2;
8791
8792 ret2 = btrfs_end_transaction(trans);
8793 if (ret2 && !ret)
8794 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008795 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008796 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008797out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008798 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008799
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008800 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008801}
8802
Sven Wegener3b963622008-06-09 21:57:42 -04008803/*
Chris Masond352ac62008-09-29 15:18:18 -04008804 * create a new subvolume directory/inode (helper for the ioctl).
8805 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008806int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008807 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008808 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008809{
Chris Mason39279cc2007-06-12 06:35:45 -04008810 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008811 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008812 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008813 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008814
Nikolay Borisov23125102020-12-07 17:32:36 +02008815 err = btrfs_get_free_objectid(new_root, &ino);
8816 if (err < 0)
8817 return err;
8818
8819 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008820 S_IFDIR | (~current_umask() & S_IRWXUGO),
8821 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008822 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008823 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008824 inode->i_op = &btrfs_dir_inode_operations;
8825 inode->i_fop = &btrfs_dir_file_operations;
8826
Miklos Szeredibfe86842011-10-28 14:13:29 +02008827 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008828 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008829 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008830
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008831 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8832 if (err)
8833 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008834 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008835 new_root->root_key.objectid, err);
8836
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008837 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008838
Yan, Zheng76dda932009-09-21 16:00:26 -04008839 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008840 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008841}
8842
Chris Mason39279cc2007-06-12 06:35:45 -04008843struct inode *btrfs_alloc_inode(struct super_block *sb)
8844{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008845 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008846 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008847 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008848
David Sterba712e36c2017-10-31 17:08:27 +01008849 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008850 if (!ei)
8851 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008852
8853 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008854 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008855 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008856 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008857 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008858 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008859 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008860 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008861 ei->disk_i_size = 0;
8862 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008863 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008864 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008865 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008866 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008867 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008868 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008869
Josef Bacik9e0baf62011-07-15 15:16:44 +00008870 spin_lock_init(&ei->lock);
8871 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008872 if (sb->s_magic != BTRFS_TEST_MAGIC)
8873 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8874 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008875 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008876 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008877 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008878
Miao Xie16cdcec2011-04-22 18:12:22 +08008879 ei->delayed_node = NULL;
8880
chandan r9cc97d62012-07-04 12:48:07 +05308881 ei->i_otime.tv_sec = 0;
8882 ei->i_otime.tv_nsec = 0;
8883
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008884 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008885 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008886 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8887 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8888 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008889 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8890 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008891 ei->io_tree.track_uptodate = true;
8892 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008893 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008894 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008896 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008897 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008898 RB_CLEAR_NODE(&ei->rb_node);
8899
8900 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008901}
8902
Josef Bacikaaedb552013-10-11 14:44:09 -04008903#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8904void btrfs_test_destroy_inode(struct inode *inode)
8905{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008906 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008907 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8908}
8909#endif
8910
Al Viro26602ca2019-04-10 15:14:41 -04008911void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008912{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008913 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8914}
8915
Nikolay Borisov633cc812020-09-18 12:15:49 +03008916void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008917{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008919 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8920 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008921
Nikolay Borisov633cc812020-09-18 12:15:49 +03008922 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8923 WARN_ON(vfs_inode->i_data.nrpages);
8924 WARN_ON(inode->block_rsv.reserved);
8925 WARN_ON(inode->block_rsv.size);
8926 WARN_ON(inode->outstanding_extents);
8927 WARN_ON(inode->delalloc_bytes);
8928 WARN_ON(inode->new_delalloc_bytes);
8929 WARN_ON(inode->csum_bytes);
8930 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008931
Chris Mason5a3f23d2009-03-31 13:27:11 -04008932 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008933 * This can happen where we create an inode, but somebody else also
8934 * created the same inode and we need to destroy the one we already
8935 * created.
8936 */
8937 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008938 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008939
Chris Masond3977122009-01-05 21:25:51 -05008940 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008941 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008942 if (!ordered)
8943 break;
8944 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008945 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008946 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008947 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008948 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949 btrfs_put_ordered_extent(ordered);
8950 btrfs_put_ordered_extent(ordered);
8951 }
8952 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008953 btrfs_qgroup_check_reserved_leak(inode);
8954 inode_tree_del(inode);
8955 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8956 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8957 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008958}
8959
Al Viro45321ac2010-06-07 13:43:19 -04008960int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008961{
8962 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008963
Naohiro Aota6379ef92013-06-06 09:56:34 +00008964 if (root == NULL)
8965 return 1;
8966
Liu Bofa6ac872013-02-20 14:10:23 +00008967 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008968 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008969 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008970 else
Al Viro45321ac2010-06-07 13:43:19 -04008971 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008972}
8973
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008974static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008975{
8976 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8977
8978 inode_init_once(&ei->vfs_inode);
8979}
8980
David Sterbae67c7182018-02-19 17:24:18 +01008981void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008982{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008983 /*
8984 * Make sure all delayed rcu free inodes are flushed before we
8985 * destroy cache.
8986 */
8987 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008988 kmem_cache_destroy(btrfs_inode_cachep);
8989 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008990 kmem_cache_destroy(btrfs_path_cachep);
8991 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008992 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008993}
8994
Liu Bof5c29bd2017-11-02 17:21:50 -06008995int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008996{
David Sterba837e1972012-09-07 03:00:48 -06008997 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008998 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008999 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9000 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009001 if (!btrfs_inode_cachep)
9002 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009003
David Sterba837e1972012-09-07 03:00:48 -06009004 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009005 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009006 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009007 if (!btrfs_trans_handle_cachep)
9008 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009009
David Sterba837e1972012-09-07 03:00:48 -06009010 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009011 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009012 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009013 if (!btrfs_path_cachep)
9014 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009015
David Sterba837e1972012-09-07 03:00:48 -06009016 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009017 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009018 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009019 if (!btrfs_free_space_cachep)
9020 goto fail;
9021
Christophe Leroy3acd4852019-08-21 15:05:55 +00009022 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9023 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009024 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009025 if (!btrfs_free_space_bitmap_cachep)
9026 goto fail;
9027
Chris Mason39279cc2007-06-12 06:35:45 -04009028 return 0;
9029fail:
9030 btrfs_destroy_cachep();
9031 return -ENOMEM;
9032}
9033
Christian Brauner549c7292021-01-21 14:19:43 +01009034static int btrfs_getattr(struct user_namespace *mnt_userns,
9035 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009036 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009037{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009038 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009039 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009040 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009041 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009042 u32 bi_flags = BTRFS_I(inode)->flags;
9043
9044 stat->result_mask |= STATX_BTIME;
9045 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9046 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9047 if (bi_flags & BTRFS_INODE_APPEND)
9048 stat->attributes |= STATX_ATTR_APPEND;
9049 if (bi_flags & BTRFS_INODE_COMPRESS)
9050 stat->attributes |= STATX_ATTR_COMPRESSED;
9051 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9052 stat->attributes |= STATX_ATTR_IMMUTABLE;
9053 if (bi_flags & BTRFS_INODE_NODUMP)
9054 stat->attributes |= STATX_ATTR_NODUMP;
9055
9056 stat->attributes_mask |= (STATX_ATTR_APPEND |
9057 STATX_ATTR_COMPRESSED |
9058 STATX_ATTR_IMMUTABLE |
9059 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009060
Christian Brauner0d56a452021-01-21 14:19:30 +01009061 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009062 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009063
9064 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009065 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009066 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009067 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009068 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009069 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009070 return 0;
9071}
9072
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009073static int btrfs_rename_exchange(struct inode *old_dir,
9074 struct dentry *old_dentry,
9075 struct inode *new_dir,
9076 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009077{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009078 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009079 struct btrfs_trans_handle *trans;
9080 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009081 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009082 struct inode *new_inode = new_dentry->d_inode;
9083 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009084 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009085 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9086 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009087 u64 old_idx = 0;
9088 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009089 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009090 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009091 bool root_log_pinned = false;
9092 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009093
9094 /* we only allow rename subvolume link between subvolumes */
9095 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9096 return -EXDEV;
9097
9098 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009099 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9100 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009101 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009102
9103 /*
9104 * We want to reserve the absolute worst case amount of items. So if
9105 * both inodes are subvols and we need to unlink them then that would
9106 * require 4 item modifications, but if they are both normal inodes it
9107 * would require 5 item modifications, so we'll assume their normal
9108 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9109 * should cover the worst case number of items we'll modify.
9110 */
9111 trans = btrfs_start_transaction(root, 12);
9112 if (IS_ERR(trans)) {
9113 ret = PTR_ERR(trans);
9114 goto out_notrans;
9115 }
9116
Josef Bacik3e174092019-11-15 15:43:06 -05009117 if (dest != root)
9118 btrfs_record_root_in_trans(trans, dest);
9119
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009120 /*
9121 * We need to find a free sequence number both in the source and
9122 * in the destination directory for the exchange.
9123 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009124 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009125 if (ret)
9126 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009127 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009128 if (ret)
9129 goto out_fail;
9130
9131 BTRFS_I(old_inode)->dir_index = 0ULL;
9132 BTRFS_I(new_inode)->dir_index = 0ULL;
9133
9134 /* Reference for the source. */
9135 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9136 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009137 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009138 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009139 btrfs_pin_log_trans(root);
9140 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009141 ret = btrfs_insert_inode_ref(trans, dest,
9142 new_dentry->d_name.name,
9143 new_dentry->d_name.len,
9144 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009145 btrfs_ino(BTRFS_I(new_dir)),
9146 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009147 if (ret)
9148 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009149 }
9150
9151 /* And now for the dest. */
9152 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9153 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009154 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009155 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009156 btrfs_pin_log_trans(dest);
9157 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009158 ret = btrfs_insert_inode_ref(trans, root,
9159 old_dentry->d_name.name,
9160 old_dentry->d_name.len,
9161 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009162 btrfs_ino(BTRFS_I(old_dir)),
9163 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009164 if (ret)
9165 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009166 }
9167
9168 /* Update inode version and ctime/mtime. */
9169 inode_inc_iversion(old_dir);
9170 inode_inc_iversion(new_dir);
9171 inode_inc_iversion(old_inode);
9172 inode_inc_iversion(new_inode);
9173 old_dir->i_ctime = old_dir->i_mtime = ctime;
9174 new_dir->i_ctime = new_dir->i_mtime = ctime;
9175 old_inode->i_ctime = ctime;
9176 new_inode->i_ctime = ctime;
9177
9178 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009179 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9180 BTRFS_I(old_inode), 1);
9181 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9182 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009183 }
9184
9185 /* src is a subvolume */
9186 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009187 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009188 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009189 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9190 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009191 old_dentry->d_name.name,
9192 old_dentry->d_name.len);
9193 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009194 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009195 }
9196 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009197 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009198 goto out_fail;
9199 }
9200
9201 /* dest is a subvolume */
9202 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009203 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009204 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009205 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9206 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009207 new_dentry->d_name.name,
9208 new_dentry->d_name.len);
9209 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009210 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009211 }
9212 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009213 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009214 goto out_fail;
9215 }
9216
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009217 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009218 new_dentry->d_name.name,
9219 new_dentry->d_name.len, 0, old_idx);
9220 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009221 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009222 goto out_fail;
9223 }
9224
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009225 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009226 old_dentry->d_name.name,
9227 old_dentry->d_name.len, 0, new_idx);
9228 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009229 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009230 goto out_fail;
9231 }
9232
9233 if (old_inode->i_nlink == 1)
9234 BTRFS_I(old_inode)->dir_index = old_idx;
9235 if (new_inode->i_nlink == 1)
9236 BTRFS_I(new_inode)->dir_index = new_idx;
9237
Filipe Manana86e8aa02016-05-05 02:02:27 +01009238 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009239 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9240 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009241 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009242 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009243 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009244 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009245 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9246 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009247 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009248 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009249 }
9250out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009251 /*
9252 * If we have pinned a log and an error happened, we unpin tasks
9253 * trying to sync the log and force them to fallback to a transaction
9254 * commit if the log currently contains any of the inodes involved in
9255 * this rename operation (to ensure we do not persist a log with an
9256 * inconsistent state for any of these inodes or leading to any
9257 * inconsistencies when replayed). If the transaction was aborted, the
9258 * abortion reason is propagated to userspace when attempting to commit
9259 * the transaction. If the log does not contain any of these inodes, we
9260 * allow the tasks to sync it.
9261 */
9262 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009263 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9264 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9265 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009266 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009267 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009268 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009269
9270 if (root_log_pinned) {
9271 btrfs_end_log_trans(root);
9272 root_log_pinned = false;
9273 }
9274 if (dest_log_pinned) {
9275 btrfs_end_log_trans(dest);
9276 dest_log_pinned = false;
9277 }
9278 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009279 ret2 = btrfs_end_transaction(trans);
9280 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009281out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009282 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9283 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009284 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009285
9286 return ret;
9287}
9288
9289static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9290 struct btrfs_root *root,
9291 struct inode *dir,
9292 struct dentry *dentry)
9293{
9294 int ret;
9295 struct inode *inode;
9296 u64 objectid;
9297 u64 index;
9298
Nikolay Borisov543068a2020-12-07 17:32:33 +02009299 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009300 if (ret)
9301 return ret;
9302
9303 inode = btrfs_new_inode(trans, root, dir,
9304 dentry->d_name.name,
9305 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009306 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009307 objectid,
9308 S_IFCHR | WHITEOUT_MODE,
9309 &index);
9310
9311 if (IS_ERR(inode)) {
9312 ret = PTR_ERR(inode);
9313 return ret;
9314 }
9315
9316 inode->i_op = &btrfs_special_inode_operations;
9317 init_special_inode(inode, inode->i_mode,
9318 WHITEOUT_DEV);
9319
9320 ret = btrfs_init_inode_security(trans, inode, dir,
9321 &dentry->d_name);
9322 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009323 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009324
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009325 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9326 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009327 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009328 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009329
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009330 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009331out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009332 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009333 if (ret)
9334 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009335 iput(inode);
9336
Filipe Mananac9901612016-05-05 01:41:57 +01009337 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009338}
9339
Chris Mason39279cc2007-06-12 06:35:45 -04009340static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341 struct inode *new_dir, struct dentry *new_dentry,
9342 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009344 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009345 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009346 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009347 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9348 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009349 struct inode *new_inode = d_inode(new_dentry);
9350 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009351 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009352 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009353 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009354 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009355 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009356
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009357 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009358 return -EPERM;
9359
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009360 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009361 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009362 return -EXDEV;
9363
Li Zefan33345d012011-04-20 10:31:50 +08009364 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009365 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009366 return -ENOTEMPTY;
9367
Chris Mason39279cc2007-06-12 06:35:45 -04009368 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009369 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009370 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009371
9372
9373 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009374 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009375 new_dentry->d_name.name,
9376 new_dentry->d_name.len);
9377
9378 if (ret) {
9379 if (ret == -EEXIST) {
9380 /* we shouldn't get
9381 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309382 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009383 return ret;
9384 }
9385 } else {
9386 /* maybe -EOVERFLOW */
9387 return ret;
9388 }
9389 }
9390 ret = 0;
9391
Chris Mason5a3f23d2009-03-31 13:27:11 -04009392 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009393 * we're using rename to replace one file with another. Start IO on it
9394 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009395 */
Chris Mason8d875f92014-08-12 10:47:42 -07009396 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009397 filemap_flush(old_inode->i_mapping);
9398
Yan, Zheng76dda932009-09-21 16:00:26 -04009399 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009400 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009401 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009402 /*
9403 * We want to reserve the absolute worst case amount of items. So if
9404 * both inodes are subvols and we need to unlink them then that would
9405 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009406 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009407 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9408 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009409 * If our rename has the whiteout flag, we need more 5 units for the
9410 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9411 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009412 */
Filipe Manana5062af32016-05-05 10:26:26 +01009413 trans_num_items = 11;
9414 if (flags & RENAME_WHITEOUT)
9415 trans_num_items += 5;
9416 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009417 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009418 ret = PTR_ERR(trans);
9419 goto out_notrans;
9420 }
Chris Mason5f39d392007-10-15 16:14:19 -04009421
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009422 if (dest != root)
9423 btrfs_record_root_in_trans(trans, dest);
9424
Nikolay Borisov877574e2017-02-20 13:50:33 +02009425 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009426 if (ret)
9427 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009428
Miao Xie67de1172013-12-26 13:07:06 +08009429 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009430 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009431 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009432 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009433 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009434 btrfs_pin_log_trans(root);
9435 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009436 ret = btrfs_insert_inode_ref(trans, dest,
9437 new_dentry->d_name.name,
9438 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009439 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009440 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009441 if (ret)
9442 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009443 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009444
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009445 inode_inc_iversion(old_dir);
9446 inode_inc_iversion(new_dir);
9447 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009448 old_dir->i_ctime = old_dir->i_mtime =
9449 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009450 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009451
Chris Mason12fcfd22009-03-24 10:24:20 -04009452 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009453 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9454 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009455
Li Zefan33345d012011-04-20 10:31:50 +08009456 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009457 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009458 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009459 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9460 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009461 old_dentry->d_name.name,
9462 old_dentry->d_name.len);
9463 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009464 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009466 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009467 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009468 goto out_fail;
9469 }
Chris Mason39279cc2007-06-12 06:35:45 -04009470
9471 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009472 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009473 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009474 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009475 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009476 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009477 BUG_ON(new_inode->i_nlink == 0);
9478 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009479 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9480 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009481 new_dentry->d_name.name,
9482 new_dentry->d_name.len);
9483 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009484 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009485 ret = btrfs_orphan_add(trans,
9486 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009487 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009488 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009489 goto out_fail;
9490 }
Chris Mason39279cc2007-06-12 06:35:45 -04009491 }
Josef Bacikaec74772008-07-24 12:12:38 -04009492
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009493 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009494 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009495 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009496 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009498 goto out_fail;
9499 }
Chris Mason39279cc2007-06-12 06:35:45 -04009500
Miao Xie67de1172013-12-26 13:07:06 +08009501 if (old_inode->i_nlink == 1)
9502 BTRFS_I(old_inode)->dir_index = index;
9503
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009504 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009505 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9506 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009507 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009508 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009509 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510
9511 if (flags & RENAME_WHITEOUT) {
9512 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9513 old_dentry);
9514
9515 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009516 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 goto out_fail;
9518 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009519 }
Chris Mason39279cc2007-06-12 06:35:45 -04009520out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009521 /*
9522 * If we have pinned the log and an error happened, we unpin tasks
9523 * trying to sync the log and force them to fallback to a transaction
9524 * commit if the log currently contains any of the inodes involved in
9525 * this rename operation (to ensure we do not persist a log with an
9526 * inconsistent state for any of these inodes or leading to any
9527 * inconsistencies when replayed). If the transaction was aborted, the
9528 * abortion reason is propagated to userspace when attempting to commit
9529 * the transaction. If the log does not contain any of these inodes, we
9530 * allow the tasks to sync it.
9531 */
9532 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009533 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9534 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9535 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009536 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009537 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009538 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009539
9540 btrfs_end_log_trans(root);
9541 log_pinned = false;
9542 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009543 ret2 = btrfs_end_transaction(trans);
9544 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009545out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009546 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009547 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009548
Chris Mason39279cc2007-06-12 06:35:45 -04009549 return ret;
9550}
9551
Christian Brauner549c7292021-01-21 14:19:43 +01009552static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9553 struct dentry *old_dentry, struct inode *new_dir,
9554 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009555{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009557 return -EINVAL;
9558
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009559 if (flags & RENAME_EXCHANGE)
9560 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9561 new_dentry);
9562
9563 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009564}
9565
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009566struct btrfs_delalloc_work {
9567 struct inode *inode;
9568 struct completion completion;
9569 struct list_head list;
9570 struct btrfs_work work;
9571};
9572
Miao Xie8ccf6f192012-10-25 09:28:04 +00009573static void btrfs_run_delalloc_work(struct btrfs_work *work)
9574{
9575 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009576 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009577
9578 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9579 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009580 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009581 filemap_flush(inode->i_mapping);
9582 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9583 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009584 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009585
Nikolay Borisov076da912018-04-23 10:54:16 +03009586 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009587 complete(&delalloc_work->completion);
9588}
9589
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009590static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009591{
9592 struct btrfs_delalloc_work *work;
9593
David Sterba100d5702015-12-08 14:39:32 +01009594 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009595 if (!work)
9596 return NULL;
9597
9598 init_completion(&work->completion);
9599 INIT_LIST_HEAD(&work->list);
9600 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009601 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009602
9603 return work;
9604}
9605
Chris Masond352ac62008-09-29 15:18:18 -04009606/*
9607 * some fairly slow code that needs optimization. This walks the list
9608 * of all the inodes with pending delalloc and forces them to disk.
9609 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009610static int start_delalloc_inodes(struct btrfs_root *root,
9611 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009612 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009613{
Chris Masonea8c2812008-08-04 23:17:27 -04009614 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009615 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009616 struct btrfs_delalloc_work *work, *next;
9617 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009618 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009619 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009620 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009621
Miao Xie8ccf6f192012-10-25 09:28:04 +00009622 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009623 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009624
Miao Xie573bfb72014-03-06 13:55:03 +08009625 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009626 spin_lock(&root->delalloc_lock);
9627 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009628 while (!list_empty(&splice)) {
9629 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009630 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009631
Miao Xieeb73c1b2013-05-15 07:48:22 +00009632 list_move_tail(&binode->delalloc_inodes,
9633 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009634
9635 if (in_reclaim_context &&
9636 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9637 continue;
9638
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009639 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009640 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009641 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009642 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009643 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009644 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009645
Ethan Lien3cd24c62018-11-01 14:49:03 +08009646 if (snapshot)
9647 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9648 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009649 if (full_flush) {
9650 work = btrfs_alloc_delalloc_work(inode);
9651 if (!work) {
9652 iput(inode);
9653 ret = -ENOMEM;
9654 goto out;
9655 }
9656 list_add_tail(&work->list, &works);
9657 btrfs_queue_work(root->fs_info->flush_workers,
9658 &work->work);
9659 } else {
9660 ret = sync_inode(inode, wbc);
9661 if (!ret &&
9662 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9663 &BTRFS_I(inode)->runtime_flags))
9664 ret = sync_inode(inode, wbc);
9665 btrfs_add_delayed_iput(inode);
9666 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009667 goto out;
9668 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009669 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009670 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009671 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009672 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009673
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009674out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009675 list_for_each_entry_safe(work, next, &works, list) {
9676 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009677 wait_for_completion(&work->completion);
9678 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009679 }
9680
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009681 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009682 spin_lock(&root->delalloc_lock);
9683 list_splice_tail(&splice, &root->delalloc_inodes);
9684 spin_unlock(&root->delalloc_lock);
9685 }
Miao Xie573bfb72014-03-06 13:55:03 +08009686 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009687 return ret;
9688}
9689
Ethan Lien3cd24c62018-11-01 14:49:03 +08009690int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009691{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009692 struct writeback_control wbc = {
9693 .nr_to_write = LONG_MAX,
9694 .sync_mode = WB_SYNC_NONE,
9695 .range_start = 0,
9696 .range_end = LLONG_MAX,
9697 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009698 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009699
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009700 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009701 return -EROFS;
9702
Josef Bacike076ab2a2021-01-07 17:08:30 -05009703 return start_delalloc_inodes(root, &wbc, true, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009704}
9705
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009706int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009707 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009708{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009709 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009710 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009711 .sync_mode = WB_SYNC_NONE,
9712 .range_start = 0,
9713 .range_end = LLONG_MAX,
9714 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009715 struct btrfs_root *root;
9716 struct list_head splice;
9717 int ret;
9718
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009719 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009720 return -EROFS;
9721
9722 INIT_LIST_HEAD(&splice);
9723
Miao Xie573bfb72014-03-06 13:55:03 +08009724 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009725 spin_lock(&fs_info->delalloc_root_lock);
9726 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009727 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009728 /*
9729 * Reset nr_to_write here so we know that we're doing a full
9730 * flush.
9731 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009732 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009733 wbc.nr_to_write = LONG_MAX;
9734
Miao Xieeb73c1b2013-05-15 07:48:22 +00009735 root = list_first_entry(&splice, struct btrfs_root,
9736 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009737 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009738 BUG_ON(!root);
9739 list_move_tail(&root->delalloc_root,
9740 &fs_info->delalloc_roots);
9741 spin_unlock(&fs_info->delalloc_root_lock);
9742
Josef Bacike076ab2a2021-01-07 17:08:30 -05009743 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009744 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009745 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009746 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009747 spin_lock(&fs_info->delalloc_root_lock);
9748 }
9749 spin_unlock(&fs_info->delalloc_root_lock);
9750
Miao Xie6c255e62014-03-06 13:55:01 +08009751 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009752out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009753 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009754 spin_lock(&fs_info->delalloc_root_lock);
9755 list_splice_tail(&splice, &fs_info->delalloc_roots);
9756 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009757 }
Miao Xie573bfb72014-03-06 13:55:03 +08009758 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009759 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009760}
9761
Christian Brauner549c7292021-01-21 14:19:43 +01009762static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9763 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009765 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009766 struct btrfs_trans_handle *trans;
9767 struct btrfs_root *root = BTRFS_I(dir)->root;
9768 struct btrfs_path *path;
9769 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009770 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009771 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009772 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309773 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009774 int name_len;
9775 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009776 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009777 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009778 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009779
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009780 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009781 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009782 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009783
Josef Bacik9ed74f22009-09-11 16:12:44 -04009784 /*
9785 * 2 items for inode item and ref
9786 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009787 * 1 item for updating parent inode item
9788 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009789 * 1 item for xattr if selinux is on
9790 */
Filipe Manana9269d122015-12-31 18:16:29 +00009791 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009792 if (IS_ERR(trans))
9793 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009794
Nikolay Borisov543068a2020-12-07 17:32:33 +02009795 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009796 if (err)
9797 goto out_unlock;
9798
Josef Bacikaec74772008-07-24 12:12:38 -04009799 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009800 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9801 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009802 if (IS_ERR(inode)) {
9803 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009804 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009805 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009806 }
Chris Mason39279cc2007-06-12 06:35:45 -04009807
Casey Schauflerad19db72011-12-15 10:09:07 -05009808 /*
9809 * If the active LSM wants to access the inode during
9810 * d_instantiate it needs these. Smack checks to see
9811 * if the filesystem supports xattrs by looking at the
9812 * ops vector.
9813 */
9814 inode->i_fop = &btrfs_file_operations;
9815 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009816 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009817
9818 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9819 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009820 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009821
Chris Mason39279cc2007-06-12 06:35:45 -04009822 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009823 if (!path) {
9824 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009825 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009826 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009827 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009828 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009829 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009830 datasize = btrfs_file_extent_calc_inline_size(name_len);
9831 err = btrfs_insert_empty_item(trans, root, path, &key,
9832 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009833 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009834 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009835 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009836 }
Chris Mason5f39d392007-10-15 16:14:19 -04009837 leaf = path->nodes[0];
9838 ei = btrfs_item_ptr(leaf, path->slots[0],
9839 struct btrfs_file_extent_item);
9840 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9841 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009842 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009843 btrfs_set_file_extent_encryption(leaf, ei, 0);
9844 btrfs_set_file_extent_compression(leaf, ei, 0);
9845 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9846 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9847
Chris Mason39279cc2007-06-12 06:35:45 -04009848 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009849 write_extent_buffer(leaf, symname, ptr, name_len);
9850 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009851 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009852
Chris Mason39279cc2007-06-12 06:35:45 -04009853 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009854 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009855 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009856 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009857 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009858 /*
9859 * Last step, add directory indexes for our symlink inode. This is the
9860 * last step to avoid extra cleanup of these indexes if an error happens
9861 * elsewhere above.
9862 */
9863 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009864 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9865 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009866 if (err)
9867 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009868
Al Viro1e2e5472018-05-04 08:23:01 -04009869 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009870
9871out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009872 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009873 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009874 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009875 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009876 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009877 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009878 return err;
9879}
Chris Mason16432982008-04-10 10:23:21 -04009880
Filipe Manana8fccebf2020-09-08 11:27:20 +01009881static struct btrfs_trans_handle *insert_prealloc_file_extent(
9882 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009883 struct btrfs_inode *inode,
9884 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009885 u64 file_offset)
9886{
9887 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009888 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009889 struct btrfs_trans_handle *trans = trans_in;
9890 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009891 u64 start = ins->objectid;
9892 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009893 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009894 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009895
9896 memset(&stack_fi, 0, sizeof(stack_fi));
9897
9898 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9899 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9900 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9901 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9902 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9903 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9904 /* Encryption and other encoding is reserved and all 0 */
9905
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009906 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9907 if (qgroup_released < 0)
9908 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009909
9910 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009911 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009912 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009913 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009914 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009915 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009916 return trans;
9917 }
9918
9919 extent_info.disk_offset = start;
9920 extent_info.disk_len = len;
9921 extent_info.data_offset = 0;
9922 extent_info.data_len = len;
9923 extent_info.file_offset = file_offset;
9924 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009925 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009926 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009927 extent_info.insertions = 0;
9928
9929 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009930 if (!path) {
9931 ret = -ENOMEM;
9932 goto free_qgroup;
9933 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009934
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009935 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009936 file_offset + len - 1, &extent_info,
9937 &trans);
9938 btrfs_free_path(path);
9939 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009940 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009941 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009942
9943free_qgroup:
9944 /*
9945 * We have released qgroup data range at the beginning of the function,
9946 * and normally qgroup_released bytes will be freed when committing
9947 * transaction.
9948 * But if we error out early, we have to free what we have released
9949 * or we leak qgroup data reservation.
9950 */
9951 btrfs_qgroup_free_refroot(inode->root->fs_info,
9952 inode->root->root_key.objectid, qgroup_released,
9953 BTRFS_QGROUP_RSV_DATA);
9954 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009955}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009956
Josef Bacik0af3d002010-06-21 14:48:16 -04009957static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9958 u64 start, u64 num_bytes, u64 min_size,
9959 loff_t actual_len, u64 *alloc_hint,
9960 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009963 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9964 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009965 struct btrfs_root *root = BTRFS_I(inode)->root;
9966 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009967 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009968 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009969 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009970 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009971 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009972 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009973 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009974 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009975
Josef Bacik0af3d002010-06-21 14:48:16 -04009976 if (trans)
9977 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009978 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009979 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009980 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009981 /*
9982 * If we are severely fragmented we could end up with really
9983 * small allocations, so if the allocator is returning small
9984 * chunks lets make its job easier by only searching for those
9985 * sized chunks.
9986 */
9987 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009988 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9989 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009990 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009991 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009992
9993 /*
9994 * We've reserved this space, and thus converted it from
9995 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9996 * from here on out we will only need to clear our reservation
9997 * for the remaining unreserved area, so advance our
9998 * clear_offset by our extent size.
9999 */
10000 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010001
Josef Bacik0b670dc2015-09-23 17:11:16 -040010002 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010003 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10004 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010005 /*
10006 * Now that we inserted the prealloc extent we can finally
10007 * decrement the number of reservations in the block group.
10008 * If we did it before, we could race with relocation and have
10009 * relocation miss the reserved extent, making it fail later.
10010 */
10011 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010012 if (IS_ERR(trans)) {
10013 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010014 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010015 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010016 break;
10017 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010018
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010019 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010020 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010021
Josef Bacik5dc562c2012-08-17 13:14:17 -040010022 em = alloc_extent_map();
10023 if (!em) {
10024 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10025 &BTRFS_I(inode)->runtime_flags);
10026 goto next;
10027 }
10028
10029 em->start = cur_offset;
10030 em->orig_start = cur_offset;
10031 em->len = ins.offset;
10032 em->block_start = ins.objectid;
10033 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010034 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010035 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010036 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10037 em->generation = trans->transid;
10038
10039 while (1) {
10040 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010041 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010042 write_unlock(&em_tree->lock);
10043 if (ret != -EEXIST)
10044 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010045 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010046 cur_offset + ins.offset - 1,
10047 0);
10048 }
10049 free_extent_map(em);
10050next:
Yan Zhengd899e052008-10-30 14:25:28 -040010051 num_bytes -= ins.offset;
10052 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010053 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010054
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010055 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010056 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010057 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010058 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010059 (actual_len > inode->i_size) &&
10060 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010061 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010062 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010063 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010064 i_size = cur_offset;
10065 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010066 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010067 }
10068
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010069 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010070
10071 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010072 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010073 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010074 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010075 break;
10076 }
Yan Zhengd899e052008-10-30 14:25:28 -040010077
Filipe Manana8fccebf2020-09-08 11:27:20 +010010078 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010079 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010080 trans = NULL;
10081 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010082 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010083 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010084 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010085 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010086 return ret;
10087}
10088
Josef Bacik0af3d002010-06-21 14:48:16 -040010089int btrfs_prealloc_file_range(struct inode *inode, int mode,
10090 u64 start, u64 num_bytes, u64 min_size,
10091 loff_t actual_len, u64 *alloc_hint)
10092{
10093 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10094 min_size, actual_len, alloc_hint,
10095 NULL);
10096}
10097
10098int btrfs_prealloc_file_range_trans(struct inode *inode,
10099 struct btrfs_trans_handle *trans, int mode,
10100 u64 start, u64 num_bytes, u64 min_size,
10101 loff_t actual_len, u64 *alloc_hint)
10102{
10103 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10104 min_size, actual_len, alloc_hint, trans);
10105}
10106
Chris Masone6dcd2d2008-07-17 12:53:50 -040010107static int btrfs_set_page_dirty(struct page *page)
10108{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010109 return __set_page_dirty_nobuffers(page);
10110}
10111
Christian Brauner549c7292021-01-21 14:19:43 +010010112static int btrfs_permission(struct user_namespace *mnt_userns,
10113 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010114{
Li Zefanb83cc962010-12-20 16:04:08 +080010115 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010116 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010117
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010118 if (mask & MAY_WRITE &&
10119 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10120 if (btrfs_root_readonly(root))
10121 return -EROFS;
10122 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10123 return -EACCES;
10124 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010125 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010126}
Chris Mason39279cc2007-06-12 06:35:45 -040010127
Christian Brauner549c7292021-01-21 14:19:43 +010010128static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10129 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010130{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010131 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010132 struct btrfs_trans_handle *trans;
10133 struct btrfs_root *root = BTRFS_I(dir)->root;
10134 struct inode *inode = NULL;
10135 u64 objectid;
10136 u64 index;
10137 int ret = 0;
10138
10139 /*
10140 * 5 units required for adding orphan entry
10141 */
10142 trans = btrfs_start_transaction(root, 5);
10143 if (IS_ERR(trans))
10144 return PTR_ERR(trans);
10145
Nikolay Borisov543068a2020-12-07 17:32:33 +020010146 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010147 if (ret)
10148 goto out;
10149
10150 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010151 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010152 if (IS_ERR(inode)) {
10153 ret = PTR_ERR(inode);
10154 inode = NULL;
10155 goto out;
10156 }
10157
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010158 inode->i_fop = &btrfs_file_operations;
10159 inode->i_op = &btrfs_file_inode_operations;
10160
10161 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010162
Chris Masonb0d5d102014-09-08 13:08:51 -070010163 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10164 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010165 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010166
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010167 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010168 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010169 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010170 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010171 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010172 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010173
Filipe Manana5762b5c2014-08-01 00:10:32 +010010174 /*
10175 * We set number of links to 0 in btrfs_new_inode(), and here we set
10176 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10177 * through:
10178 *
10179 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10180 */
10181 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010182 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010183 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010184 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010185out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010186 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010187 if (ret && inode)
10188 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010189 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010190 return ret;
10191}
10192
David Sterba5cdc84b2018-07-18 20:32:52 +020010193void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010194{
David Sterba5cdc84b2018-07-18 20:32:52 +020010195 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010196 unsigned long index = start >> PAGE_SHIFT;
10197 unsigned long end_index = end >> PAGE_SHIFT;
10198 struct page *page;
10199
10200 while (index <= end_index) {
10201 page = find_get_page(inode->i_mapping, index);
10202 ASSERT(page); /* Pages should be in the extent_io_tree */
10203 set_page_writeback(page);
10204 put_page(page);
10205 index++;
10206 }
10207}
10208
Omar Sandovaled46ff32016-11-03 10:28:14 -070010209#ifdef CONFIG_SWAP
10210/*
10211 * Add an entry indicating a block group or device which is pinned by a
10212 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10213 * negative errno on failure.
10214 */
10215static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10216 bool is_block_group)
10217{
10218 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10219 struct btrfs_swapfile_pin *sp, *entry;
10220 struct rb_node **p;
10221 struct rb_node *parent = NULL;
10222
10223 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10224 if (!sp)
10225 return -ENOMEM;
10226 sp->ptr = ptr;
10227 sp->inode = inode;
10228 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010229 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010230
10231 spin_lock(&fs_info->swapfile_pins_lock);
10232 p = &fs_info->swapfile_pins.rb_node;
10233 while (*p) {
10234 parent = *p;
10235 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10236 if (sp->ptr < entry->ptr ||
10237 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10238 p = &(*p)->rb_left;
10239 } else if (sp->ptr > entry->ptr ||
10240 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10241 p = &(*p)->rb_right;
10242 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010243 if (is_block_group)
10244 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010245 spin_unlock(&fs_info->swapfile_pins_lock);
10246 kfree(sp);
10247 return 1;
10248 }
10249 }
10250 rb_link_node(&sp->node, parent, p);
10251 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10252 spin_unlock(&fs_info->swapfile_pins_lock);
10253 return 0;
10254}
10255
10256/* Free all of the entries pinned by this swapfile. */
10257static void btrfs_free_swapfile_pins(struct inode *inode)
10258{
10259 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10260 struct btrfs_swapfile_pin *sp;
10261 struct rb_node *node, *next;
10262
10263 spin_lock(&fs_info->swapfile_pins_lock);
10264 node = rb_first(&fs_info->swapfile_pins);
10265 while (node) {
10266 next = rb_next(node);
10267 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10268 if (sp->inode == inode) {
10269 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010270 if (sp->is_block_group) {
10271 btrfs_dec_block_group_swap_extents(sp->ptr,
10272 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010273 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010274 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010275 kfree(sp);
10276 }
10277 node = next;
10278 }
10279 spin_unlock(&fs_info->swapfile_pins_lock);
10280}
10281
10282struct btrfs_swap_info {
10283 u64 start;
10284 u64 block_start;
10285 u64 block_len;
10286 u64 lowest_ppage;
10287 u64 highest_ppage;
10288 unsigned long nr_pages;
10289 int nr_extents;
10290};
10291
10292static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10293 struct btrfs_swap_info *bsi)
10294{
10295 unsigned long nr_pages;
10296 u64 first_ppage, first_ppage_reported, next_ppage;
10297 int ret;
10298
10299 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10300 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10301 PAGE_SIZE) >> PAGE_SHIFT;
10302
10303 if (first_ppage >= next_ppage)
10304 return 0;
10305 nr_pages = next_ppage - first_ppage;
10306
10307 first_ppage_reported = first_ppage;
10308 if (bsi->start == 0)
10309 first_ppage_reported++;
10310 if (bsi->lowest_ppage > first_ppage_reported)
10311 bsi->lowest_ppage = first_ppage_reported;
10312 if (bsi->highest_ppage < (next_ppage - 1))
10313 bsi->highest_ppage = next_ppage - 1;
10314
10315 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10316 if (ret < 0)
10317 return ret;
10318 bsi->nr_extents += ret;
10319 bsi->nr_pages += nr_pages;
10320 return 0;
10321}
10322
10323static void btrfs_swap_deactivate(struct file *file)
10324{
10325 struct inode *inode = file_inode(file);
10326
10327 btrfs_free_swapfile_pins(inode);
10328 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10329}
10330
10331static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10332 sector_t *span)
10333{
10334 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010335 struct btrfs_root *root = BTRFS_I(inode)->root;
10336 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010337 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10338 struct extent_state *cached_state = NULL;
10339 struct extent_map *em = NULL;
10340 struct btrfs_device *device = NULL;
10341 struct btrfs_swap_info bsi = {
10342 .lowest_ppage = (sector_t)-1ULL,
10343 };
10344 int ret = 0;
10345 u64 isize;
10346 u64 start;
10347
10348 /*
10349 * If the swap file was just created, make sure delalloc is done. If the
10350 * file changes again after this, the user is doing something stupid and
10351 * we don't really care.
10352 */
10353 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10354 if (ret)
10355 return ret;
10356
10357 /*
10358 * The inode is locked, so these flags won't change after we check them.
10359 */
10360 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10361 btrfs_warn(fs_info, "swapfile must not be compressed");
10362 return -EINVAL;
10363 }
10364 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10365 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10366 return -EINVAL;
10367 }
10368 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10369 btrfs_warn(fs_info, "swapfile must not be checksummed");
10370 return -EINVAL;
10371 }
10372
10373 /*
10374 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010375 * under us. The exclop protection makes sure they aren't running/won't
10376 * run concurrently while we are mapping the swap extents, and
10377 * fs_info->swapfile_pins prevents them from running while the swap
10378 * file is active and moving the extents. Note that this also prevents
10379 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010380 * really worth the trouble to allow it.
10381 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010382 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010383 btrfs_warn(fs_info,
10384 "cannot activate swapfile while exclusive operation is running");
10385 return -EBUSY;
10386 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010387
10388 /*
10389 * Prevent snapshot creation while we are activating the swap file.
10390 * We do not want to race with snapshot creation. If snapshot creation
10391 * already started before we bumped nr_swapfiles from 0 to 1 and
10392 * completes before the first write into the swap file after it is
10393 * activated, than that write would fallback to COW.
10394 */
10395 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10396 btrfs_exclop_finish(fs_info);
10397 btrfs_warn(fs_info,
10398 "cannot activate swapfile because snapshot creation is in progress");
10399 return -EINVAL;
10400 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010401 /*
10402 * Snapshots can create extents which require COW even if NODATACOW is
10403 * set. We use this counter to prevent snapshots. We must increment it
10404 * before walking the extents because we don't want a concurrent
10405 * snapshot to run after we've already checked the extents.
10406 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010407 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010408
10409 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10410
10411 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10412 start = 0;
10413 while (start < isize) {
10414 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010415 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010416 u64 len = isize - start;
10417
Omar Sandoval39b07b52019-12-02 17:34:23 -080010418 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010419 if (IS_ERR(em)) {
10420 ret = PTR_ERR(em);
10421 goto out;
10422 }
10423
10424 if (em->block_start == EXTENT_MAP_HOLE) {
10425 btrfs_warn(fs_info, "swapfile must not have holes");
10426 ret = -EINVAL;
10427 goto out;
10428 }
10429 if (em->block_start == EXTENT_MAP_INLINE) {
10430 /*
10431 * It's unlikely we'll ever actually find ourselves
10432 * here, as a file small enough to fit inline won't be
10433 * big enough to store more than the swap header, but in
10434 * case something changes in the future, let's catch it
10435 * here rather than later.
10436 */
10437 btrfs_warn(fs_info, "swapfile must not be inline");
10438 ret = -EINVAL;
10439 goto out;
10440 }
10441 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10442 btrfs_warn(fs_info, "swapfile must not be compressed");
10443 ret = -EINVAL;
10444 goto out;
10445 }
10446
10447 logical_block_start = em->block_start + (start - em->start);
10448 len = min(len, em->len - (start - em->start));
10449 free_extent_map(em);
10450 em = NULL;
10451
Boris Burkova84d5d42020-08-18 11:00:05 -070010452 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010453 if (ret < 0) {
10454 goto out;
10455 } else if (ret) {
10456 ret = 0;
10457 } else {
10458 btrfs_warn(fs_info,
10459 "swapfile must not be copy-on-write");
10460 ret = -EINVAL;
10461 goto out;
10462 }
10463
10464 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10465 if (IS_ERR(em)) {
10466 ret = PTR_ERR(em);
10467 goto out;
10468 }
10469
10470 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10471 btrfs_warn(fs_info,
10472 "swapfile must have single data profile");
10473 ret = -EINVAL;
10474 goto out;
10475 }
10476
10477 if (device == NULL) {
10478 device = em->map_lookup->stripes[0].dev;
10479 ret = btrfs_add_swapfile_pin(inode, device, false);
10480 if (ret == 1)
10481 ret = 0;
10482 else if (ret)
10483 goto out;
10484 } else if (device != em->map_lookup->stripes[0].dev) {
10485 btrfs_warn(fs_info, "swapfile must be on one device");
10486 ret = -EINVAL;
10487 goto out;
10488 }
10489
10490 physical_block_start = (em->map_lookup->stripes[0].physical +
10491 (logical_block_start - em->start));
10492 len = min(len, em->len - (logical_block_start - em->start));
10493 free_extent_map(em);
10494 em = NULL;
10495
10496 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10497 if (!bg) {
10498 btrfs_warn(fs_info,
10499 "could not find block group containing swapfile");
10500 ret = -EINVAL;
10501 goto out;
10502 }
10503
Filipe Manana195a49e2021-02-05 12:55:37 +000010504 if (!btrfs_inc_block_group_swap_extents(bg)) {
10505 btrfs_warn(fs_info,
10506 "block group for swapfile at %llu is read-only%s",
10507 bg->start,
10508 atomic_read(&fs_info->scrubs_running) ?
10509 " (scrub running)" : "");
10510 btrfs_put_block_group(bg);
10511 ret = -EINVAL;
10512 goto out;
10513 }
10514
Omar Sandovaled46ff32016-11-03 10:28:14 -070010515 ret = btrfs_add_swapfile_pin(inode, bg, true);
10516 if (ret) {
10517 btrfs_put_block_group(bg);
10518 if (ret == 1)
10519 ret = 0;
10520 else
10521 goto out;
10522 }
10523
10524 if (bsi.block_len &&
10525 bsi.block_start + bsi.block_len == physical_block_start) {
10526 bsi.block_len += len;
10527 } else {
10528 if (bsi.block_len) {
10529 ret = btrfs_add_swap_extent(sis, &bsi);
10530 if (ret)
10531 goto out;
10532 }
10533 bsi.start = start;
10534 bsi.block_start = physical_block_start;
10535 bsi.block_len = len;
10536 }
10537
10538 start += len;
10539 }
10540
10541 if (bsi.block_len)
10542 ret = btrfs_add_swap_extent(sis, &bsi);
10543
10544out:
10545 if (!IS_ERR_OR_NULL(em))
10546 free_extent_map(em);
10547
10548 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10549
10550 if (ret)
10551 btrfs_swap_deactivate(file);
10552
Filipe Mananadd0734f2021-02-05 12:55:38 +000010553 btrfs_drew_write_unlock(&root->snapshot_lock);
10554
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010555 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010556
10557 if (ret)
10558 return ret;
10559
10560 if (device)
10561 sis->bdev = device->bdev;
10562 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10563 sis->max = bsi.nr_pages;
10564 sis->pages = bsi.nr_pages - 1;
10565 sis->highest_bit = bsi.nr_pages - 1;
10566 return bsi.nr_extents;
10567}
10568#else
10569static void btrfs_swap_deactivate(struct file *file)
10570{
10571}
10572
10573static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10574 sector_t *span)
10575{
10576 return -EOPNOTSUPP;
10577}
10578#endif
10579
Filipe Manana2766ff62020-11-04 11:07:34 +000010580/*
10581 * Update the number of bytes used in the VFS' inode. When we replace extents in
10582 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10583 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10584 * always get a correct value.
10585 */
10586void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10587 const u64 add_bytes,
10588 const u64 del_bytes)
10589{
10590 if (add_bytes == del_bytes)
10591 return;
10592
10593 spin_lock(&inode->lock);
10594 if (del_bytes > 0)
10595 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10596 if (add_bytes > 0)
10597 inode_add_bytes(&inode->vfs_inode, add_bytes);
10598 spin_unlock(&inode->lock);
10599}
10600
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010601static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010602 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010603 .lookup = btrfs_lookup,
10604 .create = btrfs_create,
10605 .unlink = btrfs_unlink,
10606 .link = btrfs_link,
10607 .mkdir = btrfs_mkdir,
10608 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010609 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010610 .symlink = btrfs_symlink,
10611 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010612 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010613 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010614 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010615 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010616 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010617 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010618 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010619};
Yan, Zheng76dda932009-09-21 16:00:26 -040010620
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010621static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010622 .llseek = generic_file_llseek,
10623 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010624 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010625 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010626 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010627#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010628 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010629#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010630 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010631 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010632};
10633
Chris Mason35054392009-01-21 13:11:13 -050010634/*
10635 * btrfs doesn't support the bmap operation because swapfiles
10636 * use bmap to make a mapping of extents in the file. They assume
10637 * these extents won't change over the life of the file and they
10638 * use the bmap result to do IO directly to the drive.
10639 *
10640 * the btrfs bmap call would return logical addresses that aren't
10641 * suitable for IO and they also will change frequently as COW
10642 * operations happen. So, swapfile + btrfs == corruption.
10643 *
10644 * For now we're avoiding this by dropping bmap.
10645 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010646static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010647 .readpage = btrfs_readpage,
10648 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010649 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010650 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010651 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010652 .invalidatepage = btrfs_invalidatepage,
10653 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010654#ifdef CONFIG_MIGRATION
10655 .migratepage = btrfs_migratepage,
10656#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010657 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010658 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010659 .swap_activate = btrfs_swap_activate,
10660 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010661};
10662
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010663static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010664 .getattr = btrfs_getattr,
10665 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010666 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010667 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010668 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010669 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010670 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010671 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010672};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010673static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010674 .getattr = btrfs_getattr,
10675 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010676 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010677 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010678 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010679 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010680 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010681};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010682static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010683 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010684 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010685 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010686 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010687 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010688 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010689};
Yan, Zheng76dda932009-09-21 16:00:26 -040010690
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010691const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010692 .d_delete = btrfs_dentry_delete,
10693};