blob: 7d27d6626888f1ca0a370110d5594eba030a682b [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>
Boris Burkov14605402021-06-30 13:01:49 -070035#include <linux/fsverity.h>
David Sterba602cbe92019-08-21 18:48:25 +020036#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040042#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040043#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040044#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020045#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040046#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050047#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050048#include "free-space-cache.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Naohiro Aotad8e3fb12021-02-04 19:22:05 +090054#include "zoned.h"
Qu Wenruob945a462021-05-31 16:50:46 +080055#include "subpage.h"
Chris Mason39279cc2007-06-12 06:35:45 -040056
57struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020058 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040059 struct btrfs_root *root;
60};
61
Filipe Mananaf28a4922015-12-08 19:23:20 +000062struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000063 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050064 loff_t length;
65 ssize_t submitted;
66 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000067};
68
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_dir_inode_operations;
70static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070071static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070074static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
77struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000080struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
Eric Sandeen3972f262013-01-12 02:57:22 +000082static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000083static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040084static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030085static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050086 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030087 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030088 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030089static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080091 u64 block_len, u64 orig_block_len,
92 u64 ram_bytes, int compress_type,
93 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030095static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080096 const u64 offset, const u64 bytes,
97 const bool uptodate);
98
99/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500100 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
101 *
102 * ilock_flags can have the following bit set:
103 *
104 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
105 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
106 * return -EAGAIN
Josef Bacik8318ba72021-02-10 17:14:33 -0500107 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500108 */
109int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
110{
111 if (ilock_flags & BTRFS_ILOCK_SHARED) {
112 if (ilock_flags & BTRFS_ILOCK_TRY) {
113 if (!inode_trylock_shared(inode))
114 return -EAGAIN;
115 else
116 return 0;
117 }
118 inode_lock_shared(inode);
119 } else {
120 if (ilock_flags & BTRFS_ILOCK_TRY) {
121 if (!inode_trylock(inode))
122 return -EAGAIN;
123 else
124 return 0;
125 }
126 inode_lock(inode);
127 }
Josef Bacik8318ba72021-02-10 17:14:33 -0500128 if (ilock_flags & BTRFS_ILOCK_MMAP)
129 down_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500130 return 0;
131}
132
133/*
134 * btrfs_inode_unlock - unock inode i_rwsem
135 *
136 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
137 * to decide whether the lock acquired is shared or exclusive.
138 */
139void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
140{
Josef Bacik8318ba72021-02-10 17:14:33 -0500141 if (ilock_flags & BTRFS_ILOCK_MMAP)
142 up_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500143 if (ilock_flags & BTRFS_ILOCK_SHARED)
144 inode_unlock_shared(inode);
145 else
146 inode_unlock(inode);
147}
148
149/*
Qu Wenruo524272602017-03-08 10:25:52 +0800150 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100151 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800152 *
153 * NOTE: caller must ensure that when an error happens, it can not call
154 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
155 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
156 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200157 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800158 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300159static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200160 struct page *locked_page,
161 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800162{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900163 unsigned long index = offset >> PAGE_SHIFT;
164 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200165 u64 page_start = page_offset(locked_page);
166 u64 page_end = page_start + PAGE_SIZE - 1;
167
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900168 struct page *page;
169
170 while (index <= end_index) {
Qu Wenruo968f2562021-05-18 15:09:41 +0800171 /*
172 * For locked page, we will call end_extent_writepage() on it
173 * in run_delalloc_range() for the error handling. That
174 * end_extent_writepage() function will call
175 * btrfs_mark_ordered_io_finished() to clear page Ordered and
176 * run the ordered extent accounting.
177 *
178 * Here we can't just clear the Ordered bit, or
179 * btrfs_mark_ordered_io_finished() would skip the accounting
180 * for the page range, and the ordered extent will never finish.
181 */
182 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
183 index++;
184 continue;
185 }
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300186 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900187 index++;
188 if (!page)
189 continue;
Qu Wenruo968f2562021-05-18 15:09:41 +0800190
191 /*
192 * Here we just clear all Ordered bits for every page in the
193 * range, then __endio_write_update_ordered() will handle
194 * the ordered extent accounting for the range.
195 */
Qu Wenruob945a462021-05-31 16:50:46 +0800196 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
197 offset, bytes);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900198 put_page(page);
199 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200200
Qu Wenruob945a462021-05-31 16:50:46 +0800201 /* The locked page covers the full range, nothing needs to be done */
202 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
203 return;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200204 /*
205 * In case this page belongs to the delalloc range being instantiated
206 * then skip it, since the first page of a range is going to be
207 * properly cleaned up by the caller of run_delalloc_range
208 */
209 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
Qu Wenruob945a462021-05-31 16:50:46 +0800210 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
211 offset = page_offset(locked_page) + PAGE_SIZE;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200212 }
213
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300214 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800215}
216
Eric Sandeen48a3b632013-04-25 20:41:01 +0000217static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400218
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000219static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500220 struct inode *inode, struct inode *dir,
221 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500222{
223 int err;
224
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000225 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500226 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500227 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500228 return err;
229}
230
Chris Masond352ac62008-09-29 15:18:18 -0400231/*
Chris Masonc8b97812008-10-29 14:49:59 -0400232 * this does all the hard work for inserting an inline extent into
233 * the btree. The caller should have done a btrfs_drop_extents so that
234 * no overlapping inline items exist in the btree
235 */
Chris Mason40f76582014-05-21 13:35:51 -0700236static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000237 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400238 struct btrfs_root *root, struct inode *inode,
239 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000240 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400241 struct page **compressed_pages)
242{
Chris Masonc8b97812008-10-29 14:49:59 -0400243 struct extent_buffer *leaf;
244 struct page *page = NULL;
245 char *kaddr;
246 unsigned long ptr;
247 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400248 int ret;
249 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400250 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400251
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800252 ASSERT((compressed_size > 0 && compressed_pages) ||
253 (compressed_size == 0 && !compressed_pages));
254
Li Zefanfe3f5662011-03-28 08:30:38 +0000255 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400256 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000258 if (!extent_inserted) {
259 struct btrfs_key key;
260 size_t datasize;
261
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200262 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000263 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200264 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000265
266 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000267 ret = btrfs_insert_empty_item(trans, root, path, &key,
268 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200269 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000270 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400271 }
272 leaf = path->nodes[0];
273 ei = btrfs_item_ptr(leaf, path->slots[0],
274 struct btrfs_file_extent_item);
275 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
276 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
277 btrfs_set_file_extent_encryption(leaf, ei, 0);
278 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
279 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
280 ptr = btrfs_file_extent_inline_start(ei);
281
Li Zefan261507a02010-12-17 14:21:50 +0800282 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400283 struct page *cpage;
284 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500285 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400286 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500287 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300288 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400289
David Sterba4c2bf272021-06-15 17:15:38 +0200290 kaddr = page_address(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonc8b97812008-10-29 14:49:59 -0400292
293 i++;
294 ptr += cur_size;
295 compressed_size -= cur_size;
296 }
297 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800298 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400299 } else {
300 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300301 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400302 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800303 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100304 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400305 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800306 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300307 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400308 }
309 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400311
Yan, Zhengc2167752009-11-12 09:34:21 +0000312 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500313 * We align size to sectorsize for inline extents just for simplicity
314 * sake.
315 */
316 size = ALIGN(size, root->fs_info->sectorsize);
317 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
318 if (ret)
319 goto fail;
320
321 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000322 * we're an inline extent, so nobody can
323 * extend the file past i_size without locking
324 * a page we already have locked.
325 *
326 * We must do any isize and inode updates
327 * before we unlock the pages. Otherwise we
328 * could end up racing with unlink.
329 */
Chris Masonc8b97812008-10-29 14:49:59 -0400330 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400331fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200332 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400333}
334
335
336/*
337 * conditionally insert an inline extent into the file. This
338 * does the checks required to make sure the data is small enough
339 * to fit as an inline extent.
340 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300341static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400342 u64 end, size_t compressed_size,
343 int compress_type,
344 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400345{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000346 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300347 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400348 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400349 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300350 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400351 u64 actual_end = min(end + 1, isize);
352 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400353 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400354 u64 data_len = inline_len;
355 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000356 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400357
358 if (compressed_size)
359 data_len = compressed_size;
360
361 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400362 actual_end > fs_info->sectorsize ||
363 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400364 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400365 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400366 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400367 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400368 return 1;
369 }
370
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000371 path = btrfs_alloc_path();
372 if (!path)
373 return -ENOMEM;
374
Josef Bacik00361582013-08-14 14:02:47 -0400375 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000376 if (IS_ERR(trans)) {
377 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400378 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000379 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300380 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400381
Filipe Manana5893dfb2020-11-04 11:07:32 +0000382 drop_args.path = path;
383 drop_args.start = start;
384 drop_args.end = aligned_end;
385 drop_args.drop_cache = true;
386 drop_args.replace_extent = true;
387
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000388 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000389 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000390 compressed_size);
391 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000392 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000393 inline_len);
394
Filipe Manana5893dfb2020-11-04 11:07:32 +0000395 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400396 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400397 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400398 goto out;
399 }
Chris Masonc8b97812008-10-29 14:49:59 -0400400
401 if (isize > actual_end)
402 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000403 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300404 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400405 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000406 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400407 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400408 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400409 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400410 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400411 ret = 1;
412 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100413 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400414
Filipe Manana2766ff62020-11-04 11:07:34 +0000415 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200416 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000417 if (ret && ret != -ENOSPC) {
418 btrfs_abort_transaction(trans, ret);
419 goto out;
420 } else if (ret == -ENOSPC) {
421 ret = 1;
422 goto out;
423 }
424
Nikolay Borisova0349402020-06-03 08:55:12 +0300425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400426out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800427 /*
428 * Don't forget to free the reserved space, as for inlined extent
429 * it won't count as data extent, free them directly here.
430 * And at reserve time, it's always aligned to page size, so
431 * just free one page here.
432 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300433 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000434 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400435 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400436 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400437}
438
Chris Mason771ed682008-11-06 22:02:51 -0500439struct async_extent {
440 u64 start;
441 u64 ram_size;
442 u64 compressed_size;
443 struct page **pages;
444 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800445 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500446 struct list_head list;
447};
448
Nikolay Borisov97db1202019-03-12 17:20:24 +0200449struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500450 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500451 struct page *locked_page;
452 u64 start;
453 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600454 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500455 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700456 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500457 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200458 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500459};
460
Nikolay Borisov97db1202019-03-12 17:20:24 +0200461struct async_cow {
462 /* Number of chunks in flight; must be first in the structure */
463 atomic_t num_chunks;
464 struct async_chunk chunks[];
465};
466
467static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500468 u64 start, u64 ram_size,
469 u64 compressed_size,
470 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800471 unsigned long nr_pages,
472 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500473{
474 struct async_extent *async_extent;
475
476 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100477 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500478 async_extent->start = start;
479 async_extent->ram_size = ram_size;
480 async_extent->compressed_size = compressed_size;
481 async_extent->pages = pages;
482 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800483 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500484 list_add_tail(&async_extent->list, &cow->extents);
485 return 0;
486}
487
Qu Wenruo42c16da2019-07-01 05:12:46 +0000488/*
489 * Check if the inode has flags compatible with compression
490 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300491static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000492{
Qu Wenruo95ea0482021-07-26 14:35:06 +0800493 /* Subpage doesn't support compression yet */
494 if (inode->root->fs_info->sectorsize < PAGE_SIZE)
495 return false;
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300496 if (inode->flags & BTRFS_INODE_NODATACOW ||
497 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000498 return false;
499 return true;
500}
501
502/*
503 * Check if the inode needs to be submitted to compression, based on mount
504 * options, defragmentation, properties or heuristics.
505 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300506static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
507 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800508{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300509 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800510
Nikolay Borisov808a1292020-06-03 08:55:27 +0300511 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000512 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
513 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300514 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000515 return 0;
516 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800517 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400518 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800519 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200520 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300521 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200522 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800523 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300524 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800525 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400526 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300527 inode->flags & BTRFS_INODE_COMPRESS ||
528 inode->prop_compress)
529 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800530 return 0;
531}
532
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200533static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800534 u64 start, u64 end, u64 num_bytes, u64 small_write)
535{
536 /* If this is a small write inside eof, kick off a defrag */
537 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200538 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800539 btrfs_add_inode_defrag(NULL, inode);
540}
541
Chris Masonc8b97812008-10-29 14:49:59 -0400542/*
Chris Mason771ed682008-11-06 22:02:51 -0500543 * we create compressed extents in two phases. The first
544 * phase compresses a range of pages that have already been
545 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400546 *
Chris Mason771ed682008-11-06 22:02:51 -0500547 * This is done inside an ordered work queue, and the compression
548 * is spread across many cpus. The actual IO submission is step
549 * two, and the ordered work queue takes care of making sure that
550 * happens in the same order things were put onto the queue by
551 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400552 *
Chris Mason771ed682008-11-06 22:02:51 -0500553 * If this code finds it can't get good compression, it puts an
554 * entry onto the work queue to write the uncompressed bytes. This
555 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300556 * are written in the same order that the flusher thread sent them
557 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400558 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300559static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400560{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200561 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400562 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400563 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200564 u64 start = async_chunk->start;
565 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400566 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400567 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400568 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400569 struct page **pages = NULL;
570 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400571 unsigned long total_compressed = 0;
572 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400573 int i;
574 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400575 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300576 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400577 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400578
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200579 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
580 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400581
Josef Bacikd98da492019-10-11 09:03:54 -0400582 /*
583 * We need to save i_size before now because it could change in between
584 * us evaluating the size and assigning it. This is because we lock and
585 * unlock the page in truncate and fallocate, and then modify the i_size
586 * later on.
587 *
588 * The barriers are to emulate READ_ONCE, remove that once i_size_read
589 * does that for us.
590 */
591 barrier();
592 i_size = i_size_read(inode);
593 barrier();
594 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400595again:
596 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300597 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100598 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
599 nr_pages = min_t(unsigned long, nr_pages,
600 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400601
Chris Masonf03d9301f2009-02-04 09:31:06 -0500602 /*
603 * we don't want to send crud past the end of i_size through
604 * compression, that's just a waste of CPU time. So, if the
605 * end of the file is before the start of our current
606 * requested range of bytes, we bail out to the uncompressed
607 * cleanup code that can deal with all of this.
608 *
609 * It isn't really the fastest way to fix things, but this is a
610 * very uncommon corner.
611 */
612 if (actual_end <= start)
613 goto cleanup_and_bail_uncompressed;
614
Chris Masonc8b97812008-10-29 14:49:59 -0400615 total_compressed = actual_end - start;
616
Shilong Wang4bcbb332014-10-07 18:44:35 -0400617 /*
618 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400619 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400620 */
621 if (total_compressed <= blocksize &&
622 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
623 goto cleanup_and_bail_uncompressed;
624
David Sterba069eac72017-02-14 19:36:54 +0100625 total_compressed = min_t(unsigned long, total_compressed,
626 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400627 total_in = 0;
628 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400629
Chris Mason771ed682008-11-06 22:02:51 -0500630 /*
631 * we do compression for mount -o compress and when the
632 * inode has not been flagged as nocompress. This flag can
633 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400634 */
Qu Wenruo4e965572021-08-25 13:41:42 +0800635 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400636 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100637 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800638 if (!pages) {
639 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100640 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800641 goto cont;
642 }
Chris Mason179e29e2007-11-01 11:28:41 -0400643
David Sterbaeec63c62017-07-17 19:41:31 +0200644 if (BTRFS_I(inode)->defrag_compress)
645 compress_type = BTRFS_I(inode)->defrag_compress;
646 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200647 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800648
Chris Mason4adaa612013-03-26 13:07:00 -0400649 /*
650 * we need to call clear_page_dirty_for_io on each
651 * page in the range. Otherwise applications with the file
652 * mmap'd can wander in and change the page contents while
653 * we are compressing them.
654 *
655 * If the compression fails for any reason, we set the pages
656 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300657 *
658 * Note that the remaining part is redirtied, the start pointer
659 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400660 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300661 if (!redirty) {
662 extent_range_clear_dirty_for_io(inode, start, end);
663 redirty = 1;
664 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200665
666 /* Compression level is applied here and only here */
667 ret = btrfs_compress_pages(
668 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800669 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100670 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100671 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800672 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100673 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400674
675 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100676 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100677 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400678
679 /* zero the tail end of the last page, we might be
680 * sending it down to disk
681 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700682 if (offset)
683 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400684 will_compress = 1;
685 }
686 }
Li Zefan560f7d72011-09-08 10:22:01 +0800687cont:
Qu Wenruo73672532021-07-26 14:34:59 +0800688 /*
689 * Check cow_file_range() for why we don't even try to create inline
690 * extent for subpage case.
691 */
692 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400693 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300694 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400695 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500696 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400697 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300698 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
699 0, BTRFS_COMPRESS_NONE,
700 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400701 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500702 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300703 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000704 total_compressed,
705 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400706 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400708 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400709 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
710 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100711 unsigned long page_error_op;
712
Filipe Mananae6eb4312014-10-10 10:45:12 +0100713 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400714
Chris Mason771ed682008-11-06 22:02:51 -0500715 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100716 * inline extent creation worked or returned error,
717 * we don't need to create any more async work items.
718 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400719 *
720 * We use DO_ACCOUNTING here because we need the
721 * delalloc_release_metadata to be done _after_ we drop
722 * our outstanding extent for clearing delalloc for this
723 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500724 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300725 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
726 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300727 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800728 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800729 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100730 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400731 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300732
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800733 /*
734 * Ensure we only free the compressed pages if we have
735 * them allocated, as we can still reach here with
736 * inode_need_compress() == false.
737 */
738 if (pages) {
739 for (i = 0; i < nr_pages; i++) {
740 WARN_ON(pages[i]->mapping);
741 put_page(pages[i]);
742 }
743 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300744 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300745 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400746 }
747 }
748
749 if (will_compress) {
750 /*
751 * we aren't doing an inline extent round the compressed size
752 * up to a block size boundary so the allocator does sane
753 * things
754 */
Qu Wenruofda28322013-02-26 08:10:22 +0000755 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400756
757 /*
758 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300759 * win, compare the page count read with the blocks on disk,
760 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400761 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300762 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300763 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300764 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700765
766 /*
767 * The async work queues will take care of doing actual
768 * allocation on disk for these compressed pages, and
769 * will submit them to the elevator.
770 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200771 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100772 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700773 compress_type);
774
Timofey Titovets11708622017-10-03 18:06:01 +0300775 if (start + total_in < end) {
776 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700777 pages = NULL;
778 cond_resched();
779 goto again;
780 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300781 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400782 }
783 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400785 /*
786 * the compression code ran but failed to make things smaller,
787 * free any pages it allocated and our page pointer array
788 */
David Sterba4d3a8002017-02-14 19:04:07 +0100789 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400790 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300791 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400792 }
793 kfree(pages);
794 pages = NULL;
795 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100796 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400797
798 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400799 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200800 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500801 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500802 }
Chris Masonc8b97812008-10-29 14:49:59 -0400803 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500804cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700805 /*
806 * No compression, but we still need to write the pages in the file
807 * we've been given so far. redirty the locked page if it corresponds
808 * to our extent and set things up for the async work queue to run
809 * cow_file_range to do the normal delalloc dance.
810 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700811 if (async_chunk->locked_page &&
812 (page_offset(async_chunk->locked_page) >= start &&
813 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200814 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700815 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700816 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700817
818 if (redirty)
819 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200820 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700821 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300822 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500823
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300824 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500825}
Chris Mason771ed682008-11-06 22:02:51 -0500826
Filipe Manana40ae8372014-10-06 22:14:24 +0100827static void free_async_extent_pages(struct async_extent *async_extent)
828{
829 int i;
830
831 if (!async_extent->pages)
832 return;
833
834 for (i = 0; i < async_extent->nr_pages; i++) {
835 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300836 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100837 }
838 kfree(async_extent->pages);
839 async_extent->nr_pages = 0;
840 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500841}
842
843/*
844 * phase two of compressed writeback. This is the ordered portion
845 * of the code, which only gets called in the order the work was
846 * queued. We walk all the async extents created by compress_file_range
847 * and send them down to the disk.
848 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200849static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500850{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300851 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
852 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500853 struct async_extent *async_extent;
854 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500855 struct btrfs_key ins;
856 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300857 struct btrfs_root *root = inode->root;
858 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500859 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500860
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200862 while (!list_empty(&async_chunk->extents)) {
863 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500864 struct async_extent, list);
865 list_del(&async_extent->list);
866
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500867retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200868 lock_extent(io_tree, async_extent->start,
869 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500870 /* did the compression code fall back to uncompressed IO? */
871 if (!async_extent->pages) {
872 int page_started = 0;
873 unsigned long nr_written = 0;
874
Chris Mason771ed682008-11-06 22:02:51 -0500875 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300876 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500877 async_extent->start,
878 async_extent->start +
879 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300880 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500881
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100882 /* JDM XXX */
883
Chris Mason771ed682008-11-06 22:02:51 -0500884 /*
885 * if page_started, cow_file_range inserted an
886 * inline extent and took care of all the unlocking
887 * and IO for us. Otherwise, we need to submit
888 * all those pages down to the drive.
889 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500890 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300891 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200892 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500893 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500894 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500895 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700896 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200897 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500898 kfree(async_extent);
899 cond_resched();
900 continue;
901 }
902
Wang Xiaoguang18513092016-07-25 15:51:40 +0800903 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500904 async_extent->compressed_size,
905 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800906 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500907 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100908 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500909
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400910 if (ret == -ENOSPC) {
911 unlock_extent(io_tree, async_extent->start,
912 async_extent->start +
913 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800914
915 /*
916 * we need to redirty the pages if we decide to
917 * fallback to uncompressed IO, otherwise we
918 * will not submit these pages down to lower
919 * layers.
920 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300921 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800922 async_extent->start,
923 async_extent->start +
924 async_extent->ram_size - 1);
925
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100926 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400927 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500929 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000930 /*
931 * here we're doing allocation and writeback of the
932 * compressed pages
933 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300934 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800935 async_extent->ram_size, /* len */
936 async_extent->start, /* orig_start */
937 ins.objectid, /* block_start */
938 ins.offset, /* block_len */
939 ins.offset, /* orig_block_len */
940 async_extent->ram_size, /* ram_bytes */
941 async_extent->compress_type,
942 BTRFS_ORDERED_COMPRESSED);
943 if (IS_ERR(em))
944 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500945 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800946 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500947
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300948 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800949 async_extent->start,
950 ins.objectid,
951 async_extent->ram_size,
952 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800953 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100954 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300955 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100956 async_extent->start +
957 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500958 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100959 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400960 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500961
Chris Mason771ed682008-11-06 22:02:51 -0500962 /*
963 * clear dirty, set writeback and unlock the pages.
964 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300965 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400966 async_extent->start +
967 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400968 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800969 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300970 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500971 async_extent->ram_size,
972 ins.objectid,
973 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600974 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700975 async_chunk->write_flags,
976 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100977 struct page *p = async_extent->pages[0];
978 const u64 start = async_extent->start;
979 const u64 end = start + async_extent->ram_size - 1;
980
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300981 p->mapping = inode->vfs_inode.i_mapping;
Qu Wenruo38a39ac72021-04-08 20:32:27 +0800982 btrfs_writepage_endio_finish_ordered(inode, p, start,
David Sterba25c12522021-07-26 14:15:08 +0200983 end, false);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200984
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100985 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300986 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100987 PAGE_END_WRITEBACK |
988 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100989 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100990 }
Chris Mason771ed682008-11-06 22:02:51 -0500991 alloc_hint = ins.objectid + ins.offset;
992 kfree(async_extent);
993 cond_resched();
994 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100995 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500996out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400997 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400998 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +03001000 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001001 async_extent->start +
1002 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001003 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001004 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -04001005 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001006 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1007 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +01001008 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001010 goto again;
Chris Mason771ed682008-11-06 22:02:51 -05001011}
1012
Nikolay Borisov43c69842020-06-03 08:55:02 +03001013static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04001014 u64 num_bytes)
1015{
Nikolay Borisov43c69842020-06-03 08:55:02 +03001016 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -04001017 struct extent_map *em;
1018 u64 alloc_hint = 0;
1019
1020 read_lock(&em_tree->lock);
1021 em = search_extent_mapping(em_tree, start, num_bytes);
1022 if (em) {
1023 /*
1024 * if block start isn't an actual block number then find the
1025 * first block in this inode and use that as a hint. If that
1026 * block is also bogus then just don't worry about it.
1027 */
1028 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1029 free_extent_map(em);
1030 em = search_extent_mapping(em_tree, 0, 0);
1031 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1032 alloc_hint = em->block_start;
1033 if (em)
1034 free_extent_map(em);
1035 } else {
1036 alloc_hint = em->block_start;
1037 free_extent_map(em);
1038 }
1039 }
1040 read_unlock(&em_tree->lock);
1041
1042 return alloc_hint;
1043}
1044
Chris Mason771ed682008-11-06 22:02:51 -05001045/*
1046 * when extent_io.c finds a delayed allocation range in the file,
1047 * the call backs end up in this code. The basic idea is to
1048 * allocate extents on disk for the range, and create ordered data structs
1049 * in ram to track those extents.
1050 *
1051 * locked_page is the page that writepage had locked already. We use
1052 * it to make sure we don't do extra locks or unlocks.
1053 *
1054 * *page_started is set to one if we unlock locked_page and do everything
1055 * required to start IO on it. It may be clean and already done with
1056 * IO when we return.
1057 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001058static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001059 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001060 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001061 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001062{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001063 struct btrfs_root *root = inode->root;
1064 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001065 u64 alloc_hint = 0;
1066 u64 num_bytes;
1067 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001068 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001069 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001070 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001071 struct btrfs_key ins;
1072 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001073 unsigned clear_bits;
1074 unsigned long page_ops;
1075 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001076 int ret = 0;
1077
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001078 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001079 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001080 ret = -EINVAL;
1081 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001082 }
Chris Mason771ed682008-11-06 22:02:51 -05001083
Qu Wenruofda28322013-02-26 08:10:22 +00001084 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001085 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001086 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001087
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001088 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001089
Qu Wenruo73672532021-07-26 14:34:59 +08001090 /*
1091 * Due to the page size limit, for subpage we can only trigger the
1092 * writeback for the dirty sectors of page, that means data writeback
1093 * is doing more writeback than what we want.
1094 *
1095 * This is especially unexpected for some call sites like fallocate,
1096 * where we only increase i_size after everything is done.
1097 * This means we can trigger inline extent even if we didn't want to.
1098 * So here we skip inline extent creation completely.
1099 */
1100 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Mason771ed682008-11-06 22:02:51 -05001101 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001102 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001103 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001104 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001105 /*
1106 * We use DO_ACCOUNTING here because we need the
1107 * delalloc_release_metadata to be run _after_ we drop
1108 * our outstanding extent for clearing delalloc for this
1109 * range.
1110 */
Qu Wenruo4750af32021-05-31 16:50:48 +08001111 extent_clear_unlock_delalloc(inode, start, end,
1112 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001113 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001114 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1115 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001116 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001117 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001118 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001119 *page_started = 1;
Qu Wenruo4750af32021-05-31 16:50:48 +08001120 /*
1121 * locked_page is locked by the caller of
1122 * writepage_delalloc(), not locked by
1123 * __process_pages_contig().
1124 *
1125 * We can't let __process_pages_contig() to unlock it,
1126 * as it doesn't have any subpage::writers recorded.
1127 *
1128 * Here we manually unlock the page, since the caller
1129 * can't use page_started to determine if it's an
1130 * inline extent or a compressed extent.
1131 */
1132 unlock_page(locked_page);
Chris Mason771ed682008-11-06 22:02:51 -05001133 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001136 }
1137 }
Chris Masonc8b97812008-10-29 14:49:59 -04001138
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001139 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1140 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001141
Filipe Manana432cd2a2020-06-08 13:32:55 +01001142 /*
1143 * Relocation relies on the relocated extents to have exactly the same
1144 * size as the original extents. Normally writeback for relocation data
1145 * extents follows a NOCOW path because relocation preallocates the
1146 * extents. However, due to an operation such as scrub turning a block
1147 * group to RO mode, it may fallback to COW mode, so we must make sure
1148 * an extent allocated during COW has exactly the requested size and can
1149 * not be split into smaller extents, otherwise relocation breaks and
1150 * fails during the stage where it updates the bytenr of file extent
1151 * items.
1152 */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001153 if (btrfs_is_data_reloc_root(root))
Filipe Manana432cd2a2020-06-08 13:32:55 +01001154 min_alloc_size = num_bytes;
1155 else
1156 min_alloc_size = fs_info->sectorsize;
1157
Anand Jain3752d222018-02-15 12:29:38 +08001158 while (num_bytes > 0) {
1159 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001160 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001161 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001162 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001163 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001164 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001165 cur_alloc_size = ins.offset;
1166 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001167
Chris Mason771ed682008-11-06 22:02:51 -05001168 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001169 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001170 start, /* orig_start */
1171 ins.objectid, /* block_start */
1172 ins.offset, /* block_len */
1173 ins.offset, /* orig_block_len */
1174 ram_size, /* ram_bytes */
1175 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001176 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001177 if (IS_ERR(em)) {
1178 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001179 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001180 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001181 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001182
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001183 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001184 ram_size, cur_alloc_size,
1185 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001186 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001187 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001188
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001189 if (btrfs_is_data_reloc_root(root)) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001190 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001191 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001192 /*
1193 * Only drop cache here, and process as normal.
1194 *
1195 * We must not allow extent_clear_unlock_delalloc()
1196 * at out_unlock label to free meta of this ordered
1197 * extent, as its meta should be freed by
1198 * btrfs_finish_ordered_io().
1199 *
1200 * So we must continue until @start is increased to
1201 * skip current ordered extent.
1202 */
Josef Bacik00361582013-08-14 14:02:47 -04001203 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001204 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001205 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001206 }
1207
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001208 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001209
Qu Wenruof57ad932021-04-07 19:22:13 +08001210 /*
1211 * We're not doing compressed IO, don't unlock the first page
1212 * (which the caller expects to stay locked), don't clear any
1213 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001214 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001215 * Do set the Ordered (Private2) bit so we know this page was
1216 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001217 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001218 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001219 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001220
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001221 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001222 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001223 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001224 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001225 if (num_bytes < cur_alloc_size)
1226 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001227 else
Anand Jain3752d222018-02-15 12:29:38 +08001228 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001229 alloc_hint = ins.objectid + ins.offset;
1230 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001231 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001232
1233 /*
1234 * btrfs_reloc_clone_csums() error, since start is increased
1235 * extent_clear_unlock_delalloc() at out_unlock label won't
1236 * free metadata of current ordered extent, we're OK to exit.
1237 */
1238 if (ret)
1239 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001240 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001241out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001242 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001243
Filipe Mananad9f85962014-08-25 10:43:00 +01001244out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001245 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001246out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001247 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001248 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001250 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1251 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001252 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001253 /*
1254 * If we reserved an extent for our delalloc range (or a subrange) and
1255 * failed to create the respective ordered extent, then it means that
1256 * when we reserved the extent we decremented the extent's size from
1257 * the data space_info's bytes_may_use counter and incremented the
1258 * space_info's bytes_reserved counter by the same amount. We must make
1259 * sure extent_clear_unlock_delalloc() does not try to decrement again
1260 * the data space_info's bytes_may_use counter, therefore we do not pass
1261 * it the flag EXTENT_CLEAR_DATA_RESV.
1262 */
1263 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001264 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001265 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001266 locked_page,
1267 clear_bits,
1268 page_ops);
1269 start += cur_alloc_size;
1270 if (start >= end)
1271 goto out;
1272 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001273 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001274 clear_bits | EXTENT_CLEAR_DATA_RESV,
1275 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001277}
Chris Masonc8b97812008-10-29 14:49:59 -04001278
Chris Mason771ed682008-11-06 22:02:51 -05001279/*
1280 * work queue call back to started compression on a file and pages
1281 */
1282static noinline void async_cow_start(struct btrfs_work *work)
1283{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001284 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001285 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001286
Nikolay Borisovb5326272019-03-12 17:20:25 +02001287 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001288
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001289 compressed_extents = compress_file_range(async_chunk);
1290 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001291 btrfs_add_delayed_iput(async_chunk->inode);
1292 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001293 }
Chris Mason771ed682008-11-06 22:02:51 -05001294}
1295
1296/*
1297 * work queue call back to submit previously compressed pages
1298 */
1299static noinline void async_cow_submit(struct btrfs_work *work)
1300{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001301 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1302 work);
1303 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001304 unsigned long nr_pages;
1305
Nikolay Borisovb5326272019-03-12 17:20:25 +02001306 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001307 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001308
Nikolay Borisov4546d172019-01-03 10:50:03 +02001309 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001310 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001311 * in which case we don't have anything to submit, yet we need to
1312 * always adjust ->async_delalloc_pages as its paired with the init
1313 * happening in cow_file_range_async
1314 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001315 if (async_chunk->inode)
1316 submit_compressed_extents(async_chunk);
Josef Bacikac981412021-07-14 14:47:17 -04001317
1318 /* atomic_sub_return implies a barrier */
1319 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1320 5 * SZ_1M)
1321 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001322}
Chris Masonc8b97812008-10-29 14:49:59 -04001323
Chris Mason771ed682008-11-06 22:02:51 -05001324static noinline void async_cow_free(struct btrfs_work *work)
1325{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001326 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001327
Nikolay Borisovb5326272019-03-12 17:20:25 +02001328 async_chunk = container_of(work, struct async_chunk, work);
1329 if (async_chunk->inode)
1330 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001331 if (async_chunk->blkcg_css)
1332 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001333 /*
1334 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001335 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001336 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001337 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001338 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001339}
1340
Nikolay Borisov751b6432020-06-03 08:55:22 +03001341static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001342 struct writeback_control *wbc,
1343 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001344 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001345 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001346{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001347 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001348 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001349 struct async_cow *ctx;
1350 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001351 unsigned long nr_pages;
1352 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001353 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1354 int i;
1355 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001356 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001357 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001358
Nikolay Borisov751b6432020-06-03 08:55:22 +03001359 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001360
Nikolay Borisov751b6432020-06-03 08:55:22 +03001361 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001362 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1363 num_chunks = 1;
1364 should_compress = false;
1365 } else {
1366 should_compress = true;
1367 }
1368
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001369 nofs_flag = memalloc_nofs_save();
1370 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1371 memalloc_nofs_restore(nofs_flag);
1372
Nikolay Borisov97db1202019-03-12 17:20:24 +02001373 if (!ctx) {
1374 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1375 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1376 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001377 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1378 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001379
Nikolay Borisov751b6432020-06-03 08:55:22 +03001380 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1381 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001382 return -ENOMEM;
1383 }
1384
1385 async_chunk = ctx->chunks;
1386 atomic_set(&ctx->num_chunks, num_chunks);
1387
1388 for (i = 0; i < num_chunks; i++) {
1389 if (should_compress)
1390 cur_end = min(end, start + SZ_512K - 1);
1391 else
1392 cur_end = end;
1393
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001394 /*
1395 * igrab is called higher up in the call chain, take only the
1396 * lightweight reference for the callback lifetime
1397 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001398 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001399 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001400 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001401 async_chunk[i].start = start;
1402 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001403 async_chunk[i].write_flags = write_flags;
1404 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001405
Chris Mason1d53c9e2019-07-10 12:28:16 -07001406 /*
1407 * The locked_page comes all the way from writepage and its
1408 * the original page we were actually given. As we spread
1409 * this large delalloc region across multiple async_chunk
1410 * structs, only the first struct needs a pointer to locked_page
1411 *
1412 * This way we don't need racey decisions about who is supposed
1413 * to unlock it.
1414 */
1415 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001416 /*
1417 * Depending on the compressibility, the pages might or
1418 * might not go through async. We want all of them to
1419 * be accounted against wbc once. Let's do it here
1420 * before the paths diverge. wbc accounting is used
1421 * only for foreign writeback detection and doesn't
1422 * need full accuracy. Just account the whole thing
1423 * against the first page.
1424 */
1425 wbc_account_cgroup_owner(wbc, locked_page,
1426 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001427 async_chunk[i].locked_page = locked_page;
1428 locked_page = NULL;
1429 } else {
1430 async_chunk[i].locked_page = NULL;
1431 }
1432
Chris Masonec39f762019-07-10 12:28:17 -07001433 if (blkcg_css != blkcg_root_css) {
1434 css_get(blkcg_css);
1435 async_chunk[i].blkcg_css = blkcg_css;
1436 } else {
1437 async_chunk[i].blkcg_css = NULL;
1438 }
1439
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001440 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1441 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001442
Nikolay Borisov97db1202019-03-12 17:20:24 +02001443 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001444 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001445
Nikolay Borisov97db1202019-03-12 17:20:24 +02001446 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001447
Chris Mason771ed682008-11-06 22:02:51 -05001448 *nr_written += nr_pages;
1449 start = cur_end + 1;
1450 }
1451 *page_started = 1;
1452 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001453}
1454
Naohiro Aota42c01102021-02-04 19:22:07 +09001455static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1456 struct page *locked_page, u64 start,
1457 u64 end, int *page_started,
1458 unsigned long *nr_written)
1459{
1460 int ret;
1461
1462 ret = cow_file_range(inode, locked_page, start, end, page_started,
1463 nr_written, 0);
1464 if (ret)
1465 return ret;
1466
1467 if (*page_started)
1468 return 0;
1469
1470 __set_page_dirty_nobuffers(locked_page);
1471 account_page_redirty(locked_page);
1472 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1473 *page_started = 1;
1474
1475 return 0;
1476}
1477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001478static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001479 u64 bytenr, u64 num_bytes)
1480{
1481 int ret;
1482 struct btrfs_ordered_sum *sums;
1483 LIST_HEAD(list);
1484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001485 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001486 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001487 if (ret == 0 && list_empty(&list))
1488 return 0;
1489
1490 while (!list_empty(&list)) {
1491 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1492 list_del(&sums->list);
1493 kfree(sums);
1494 }
Liu Bo58113752018-01-31 17:09:13 -07001495 if (ret < 0)
1496 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001497 return 1;
1498}
1499
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001500static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001501 const u64 start, const u64 end,
1502 int *page_started, unsigned long *nr_written)
1503{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001504 const bool is_space_ino = btrfs_is_free_space_inode(inode);
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001505 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001506 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001507 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001508 u64 range_start = start;
1509 u64 count;
1510
1511 /*
1512 * If EXTENT_NORESERVE is set it means that when the buffered write was
1513 * made we had not enough available data space and therefore we did not
1514 * reserve data space for it, since we though we could do NOCOW for the
1515 * respective file range (either there is prealloc extent or the inode
1516 * has the NOCOW bit set).
1517 *
1518 * However when we need to fallback to COW mode (because for example the
1519 * block group for the corresponding extent was turned to RO mode by a
1520 * scrub or relocation) we need to do the following:
1521 *
1522 * 1) We increment the bytes_may_use counter of the data space info.
1523 * If COW succeeds, it allocates a new data extent and after doing
1524 * that it decrements the space info's bytes_may_use counter and
1525 * increments its bytes_reserved counter by the same amount (we do
1526 * this at btrfs_add_reserved_bytes()). So we need to increment the
1527 * bytes_may_use counter to compensate (when space is reserved at
1528 * buffered write time, the bytes_may_use counter is incremented);
1529 *
1530 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1531 * that if the COW path fails for any reason, it decrements (through
1532 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1533 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001534 *
1535 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001536 * space cache inode or an inode of the data relocation tree, we must
1537 * also increment bytes_may_use of the data space_info for the same
1538 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001539 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001540 * group that contains that extent to RO mode and therefore force COW
1541 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001542 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001543 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001544 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001545 if (count > 0 || is_space_ino || is_reloc_ino) {
1546 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001547 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001548 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1549
Filipe Manana6bd335b2020-06-08 13:33:05 +01001550 if (is_space_ino || is_reloc_ino)
1551 bytes = range_bytes;
1552
Filipe Manana467dc472020-05-27 11:16:07 +01001553 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001554 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001555 spin_unlock(&sinfo->lock);
1556
Filipe Manana2166e5e2020-05-27 11:16:19 +01001557 if (count > 0)
1558 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1559 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001560 }
1561
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001562 return cow_file_range(inode, locked_page, start, end, page_started,
1563 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001564}
1565
Chris Masond352ac62008-09-29 15:18:18 -04001566/*
1567 * when nowcow writeback call back. This checks for snapshots or COW copies
1568 * of the extents that exist in the file, and COWs the file as required.
1569 *
1570 * If no cow copies or snapshots exist, we write directly to the existing
1571 * blocks on disk
1572 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001573static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001574 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001575 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001576 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001577 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001578{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001579 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1580 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001581 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001582 u64 cow_start = (u64)-1;
1583 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001584 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001585 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001586 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1587 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001588 bool nocow = false;
1589 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001590 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001591
1592 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001593 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001594 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001595 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001596 EXTENT_DO_ACCOUNTING |
1597 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001598 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001599 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001600 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001601 }
Li Zefan82d59022011-04-20 10:33:24 +08001602
Yan Zheng80ff3852008-10-30 14:20:02 -04001603 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001604 struct btrfs_key found_key;
1605 struct btrfs_file_extent_item *fi;
1606 struct extent_buffer *leaf;
1607 u64 extent_end;
1608 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001609 u64 num_bytes = 0;
1610 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001611 u64 ram_bytes;
1612 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001613
1614 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001615
Liu Boe4c3b2d2017-01-30 12:25:28 -08001616 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001617 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001618 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001619 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001620
1621 /*
1622 * If there is no extent for our range when doing the initial
1623 * search, then go back to the previous slot as it will be the
1624 * one containing the search offset
1625 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001626 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1627 leaf = path->nodes[0];
1628 btrfs_item_key_to_cpu(leaf, &found_key,
1629 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001630 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001631 found_key.type == BTRFS_EXTENT_DATA_KEY)
1632 path->slots[0]--;
1633 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001634 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001635next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001636 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001637 leaf = path->nodes[0];
1638 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1639 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001640 if (ret < 0) {
1641 if (cow_start != (u64)-1)
1642 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001643 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001644 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001645 if (ret > 0)
1646 break;
1647 leaf = path->nodes[0];
1648 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001649
Yan Zheng80ff3852008-10-30 14:20:02 -04001650 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001651
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001652 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001653 if (found_key.objectid > ino)
1654 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001655 /*
1656 * Keep searching until we find an EXTENT_ITEM or there are no
1657 * more extents for this inode
1658 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001659 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1660 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1661 path->slots[0]++;
1662 goto next_slot;
1663 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001664
1665 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001666 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001667 found_key.offset > end)
1668 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001669
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001670 /*
1671 * If the found extent starts after requested offset, then
1672 * adjust extent_end to be right before this extent begins
1673 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001674 if (found_key.offset > cur_offset) {
1675 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001676 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001677 goto out_check;
1678 }
Chris Masonc31f8832008-01-08 15:46:31 -05001679
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001680 /*
1681 * Found extent which begins before our range and potentially
1682 * intersect it
1683 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001684 fi = btrfs_item_ptr(leaf, path->slots[0],
1685 struct btrfs_file_extent_item);
1686 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001687
Josef Bacikcc95bef2013-04-04 14:31:27 -04001688 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001689 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1690 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001691 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001692 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001693 extent_end = found_key.offset +
1694 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001695 disk_num_bytes =
1696 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001697 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001698 * If the extent we got ends before our current offset,
1699 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001700 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001701 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001702 path->slots[0]++;
1703 goto next_slot;
1704 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001705 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001706 if (disk_bytenr == 0)
1707 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001708 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001709 if (btrfs_file_extent_compression(leaf, fi) ||
1710 btrfs_file_extent_encryption(leaf, fi) ||
1711 btrfs_file_extent_other_encoding(leaf, fi))
1712 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001713 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001714 * If extent is created before the last volume's snapshot
1715 * this implies the extent is shared, hence we can't do
1716 * nocow. This is the same check as in
1717 * btrfs_cross_ref_exist but without calling
1718 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001719 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001720 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001721 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001722 btrfs_root_last_snapshot(&root->root_item))
1723 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001724 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1725 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001726
1727 /*
1728 * The following checks can be expensive, as they need to
1729 * take other locks and do btree or rbtree searches, so
1730 * release the path to avoid blocking other tasks for too
1731 * long.
1732 */
1733 btrfs_release_path(path);
1734
Liu Bo58113752018-01-31 17:09:13 -07001735 ret = btrfs_cross_ref_exist(root, ino,
1736 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001737 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001738 if (ret) {
1739 /*
1740 * ret could be -EIO if the above fails to read
1741 * metadata.
1742 */
1743 if (ret < 0) {
1744 if (cow_start != (u64)-1)
1745 cur_offset = cow_start;
1746 goto error;
1747 }
1748
Nikolay Borisov3e024842019-08-21 10:42:03 +03001749 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001750 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001751 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001752 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001753 disk_bytenr += cur_offset - found_key.offset;
1754 num_bytes = min(end + 1, extent_end) - cur_offset;
1755 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001756 * If there are pending snapshots for this root, we
1757 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001758 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001759 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001760 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001761 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001762 * force cow if csum exists in the range.
1763 * this ensure that csum for a given extent are
1764 * either valid or do not exist.
1765 */
Liu Bo58113752018-01-31 17:09:13 -07001766 ret = csum_exist_in_range(fs_info, disk_bytenr,
1767 num_bytes);
1768 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001769 /*
1770 * ret could be -EIO if the above fails to read
1771 * metadata.
1772 */
1773 if (ret < 0) {
1774 if (cow_start != (u64)-1)
1775 cur_offset = cow_start;
1776 goto error;
1777 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001778 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001779 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001780 }
Filipe Manana20903032021-02-05 12:55:36 +00001781 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001782 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001783 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001784 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001785 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001786 extent_end = found_key.offset + ram_bytes;
1787 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001788 /* Skip extents outside of our requested range */
1789 if (extent_end <= start) {
1790 path->slots[0]++;
1791 goto next_slot;
1792 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001793 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001794 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001795 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001796 }
1797out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001798 /*
1799 * If nocow is false then record the beginning of the range
1800 * that needs to be COWed
1801 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001802 if (!nocow) {
1803 if (cow_start == (u64)-1)
1804 cow_start = cur_offset;
1805 cur_offset = extent_end;
1806 if (cur_offset > end)
1807 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001808 if (!path->nodes[0])
1809 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001810 path->slots[0]++;
1811 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001812 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001813
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001814 /*
1815 * COW range from cow_start to found_key.offset - 1. As the key
1816 * will contain the beginning of the first extent that can be
1817 * NOCOW, following one which needs to be COW'ed
1818 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001819 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001820 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001821 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001822 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001823 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001824 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001825 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001826 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001827
Yan Zhengd899e052008-10-30 14:25:28 -04001828 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001829 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001830 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001831
Nikolay Borisov968322c2020-06-03 08:55:21 +03001832 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001833 orig_start,
1834 disk_bytenr, /* block_start */
1835 num_bytes, /* block_len */
1836 disk_num_bytes, /* orig_block_len */
1837 ram_bytes, BTRFS_COMPRESS_NONE,
1838 BTRFS_ORDERED_PREALLOC);
1839 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001840 ret = PTR_ERR(em);
1841 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001842 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001843 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001844 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001845 disk_bytenr, num_bytes,
1846 num_bytes,
1847 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001848 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001849 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001850 cur_offset + num_bytes - 1,
1851 0);
1852 goto error;
1853 }
Yan Zhengd899e052008-10-30 14:25:28 -04001854 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001855 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001856 disk_bytenr, num_bytes,
1857 num_bytes,
1858 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001859 if (ret)
1860 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001861 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001862
Filipe Mananaf78c4362016-05-09 13:15:41 +01001863 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001864 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001865 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001866
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09001867 if (btrfs_is_data_reloc_root(root))
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001868 /*
1869 * Error handled later, as we must prevent
1870 * extent_clear_unlock_delalloc() in error handler
1871 * from freeing metadata of created ordered extent.
1872 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001873 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001874 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001875
Nikolay Borisov968322c2020-06-03 08:55:21 +03001876 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001877 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001878 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001879 EXTENT_DELALLOC |
1880 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001881 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001882
Yan Zheng80ff3852008-10-30 14:20:02 -04001883 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001884
1885 /*
1886 * btrfs_reloc_clone_csums() error, now we're OK to call error
1887 * handler, as metadata for created ordered extent will only
1888 * be freed by btrfs_finish_ordered_io().
1889 */
1890 if (ret)
1891 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001892 if (cur_offset > end)
1893 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001894 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001895 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001896
Robbie Ko506481b2018-10-30 18:04:04 +08001897 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001898 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001899
Yan Zheng80ff3852008-10-30 14:20:02 -04001900 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001901 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001902 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1903 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001904 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001906 }
1907
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001909 if (nocow)
1910 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1911
Josef Bacik17ca04a2012-05-31 15:58:55 -04001912 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001913 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001914 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001915 EXTENT_DELALLOC | EXTENT_DEFRAG |
1916 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001917 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001918 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001919 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001921}
1922
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001923static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001924{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001925 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1926 if (inode->defrag_bytes &&
1927 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1928 0, NULL))
1929 return false;
1930 return true;
1931 }
1932 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001933}
1934
Chris Masond352ac62008-09-29 15:18:18 -04001935/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001936 * Function to process delayed allocation (create CoW) for ranges which are
1937 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001938 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001939int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001940 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1941 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001942{
Chris Masonbe20aa92007-12-17 20:14:01 -05001943 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001944 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001945
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001946 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001947 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001948 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001949 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001950 } else if (!inode_can_compress(inode) ||
1951 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001952 if (zoned)
1953 ret = run_delalloc_zoned(inode, locked_page, start, end,
1954 page_started, nr_written);
1955 else
1956 ret = cow_file_range(inode, locked_page, start, end,
1957 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001958 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001959 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1960 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001961 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001962 }
Qu Wenruo7361b4a2021-07-28 14:05:05 +08001963 ASSERT(ret <= 0);
Qu Wenruo524272602017-03-08 10:25:52 +08001964 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001965 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001966 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001967 return ret;
1968}
1969
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001970void btrfs_split_delalloc_extent(struct inode *inode,
1971 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001972{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001973 u64 size;
1974
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001975 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001976 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001977 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001978
Josef Bacikdcab6a32015-02-11 15:08:59 -05001979 size = orig->end - orig->start + 1;
1980 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001981 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001982 u64 new_size;
1983
1984 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001985 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001986 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001987 */
1988 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001989 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001990 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001991 num_extents += count_max_extents(new_size);
1992 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001993 return;
1994 }
1995
Josef Bacik9e0baf62011-07-15 15:16:44 +00001996 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001997 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001998 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001999}
2000
2001/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002002 * Handle merged delayed allocation extents so we can keep track of new extents
2003 * that are just merged onto old extents, such as when we are doing sequential
2004 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04002005 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02002006void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2007 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002008{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002009 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01002010 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002011
Josef Bacik9ed74f22009-09-11 16:12:44 -04002012 /* not delalloc, ignore it */
2013 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002014 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002015
Josef Bacik8461a3d2015-03-13 15:12:08 -04002016 if (new->start > other->start)
2017 new_size = new->end - other->start + 1;
2018 else
2019 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002020
2021 /* we're not bigger than the max, unreserve the space and go */
2022 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2023 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002024 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002025 spin_unlock(&BTRFS_I(inode)->lock);
2026 return;
2027 }
2028
2029 /*
Josef Bacikba117212015-03-13 15:01:24 -04002030 * We have to add up either side to figure out how many extents were
2031 * accounted for before we merged into one big extent. If the number of
2032 * extents we accounted for is <= the amount we need for the new range
2033 * then we can return, otherwise drop. Think of it like this
2034 *
2035 * [ 4k][MAX_SIZE]
2036 *
2037 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2038 * need 2 outstanding extents, on one side we have 1 and the other side
2039 * we have 1 so they are == and we can return. But in this case
2040 *
2041 * [MAX_SIZE+4k][MAX_SIZE+4k]
2042 *
2043 * Each range on their own accounts for 2 extents, but merged together
2044 * they are only 3 extents worth of accounting, so we need to drop in
2045 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002046 */
Josef Bacikba117212015-03-13 15:01:24 -04002047 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002048 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002049 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002050 num_extents += count_max_extents(old_size);
2051 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002052 return;
2053
Josef Bacik9e0baf62011-07-15 15:16:44 +00002054 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002055 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002056 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002057}
2058
Miao Xieeb73c1b2013-05-15 07:48:22 +00002059static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2060 struct inode *inode)
2061{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2063
Miao Xieeb73c1b2013-05-15 07:48:22 +00002064 spin_lock(&root->delalloc_lock);
2065 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2066 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2067 &root->delalloc_inodes);
2068 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2069 &BTRFS_I(inode)->runtime_flags);
2070 root->nr_delalloc_inodes++;
2071 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002073 BUG_ON(!list_empty(&root->delalloc_root));
2074 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002075 &fs_info->delalloc_roots);
2076 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002077 }
2078 }
2079 spin_unlock(&root->delalloc_lock);
2080}
2081
Nikolay Borisov2b877332018-04-27 12:21:51 +03002082
2083void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2084 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002085{
David Sterba3ffbd682018-06-29 10:56:42 +02002086 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002087
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002088 if (!list_empty(&inode->delalloc_inodes)) {
2089 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002090 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002091 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002092 root->nr_delalloc_inodes--;
2093 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002094 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002095 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002096 BUG_ON(list_empty(&root->delalloc_root));
2097 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002098 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002099 }
2100 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002101}
2102
2103static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2104 struct btrfs_inode *inode)
2105{
2106 spin_lock(&root->delalloc_lock);
2107 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002108 spin_unlock(&root->delalloc_lock);
2109}
2110
Chris Masond352ac62008-09-29 15:18:18 -04002111/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002112 * Properly track delayed allocation bytes in the inode and to maintain the
2113 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002114 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002115void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2116 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002117{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2119
Wang Shilong47059d92014-07-03 18:22:07 +08002120 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2121 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002122 /*
2123 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002124 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002125 * bit, which is only set or cleared with irqs on
2126 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002127 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002128 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002129 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002130 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002131 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002132
Josef Bacik8b62f872017-10-19 14:15:55 -04002133 spin_lock(&BTRFS_I(inode)->lock);
2134 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2135 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002136
Josef Bacik6a3891c2015-03-16 17:38:52 -04002137 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002138 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002139 return;
2140
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002141 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2142 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002143 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002144 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002145 if (*bits & EXTENT_DEFRAG)
2146 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002147 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002148 &BTRFS_I(inode)->runtime_flags))
2149 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002150 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002151 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002152
2153 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2154 (*bits & EXTENT_DELALLOC_NEW)) {
2155 spin_lock(&BTRFS_I(inode)->lock);
2156 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2157 state->start;
2158 spin_unlock(&BTRFS_I(inode)->lock);
2159 }
Chris Mason291d6732008-01-29 15:55:23 -05002160}
2161
Chris Masond352ac62008-09-29 15:18:18 -04002162/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002163 * Once a range is no longer delalloc this function ensures that proper
2164 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002165 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002166void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2167 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002168{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002169 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2170 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002171 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002172 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002173
Filipe Manana4a4b9642017-07-27 19:52:55 +01002174 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2175 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002176 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002177 spin_unlock(&inode->lock);
2178 }
Wang Shilong47059d92014-07-03 18:22:07 +08002179
Chris Mason75eff682008-12-15 15:54:40 -05002180 /*
2181 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002182 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002183 * bit, which is only set or cleared with irqs on
2184 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002185 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002186 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002187 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002188
Josef Bacik8b62f872017-10-19 14:15:55 -04002189 spin_lock(&inode->lock);
2190 btrfs_mod_outstanding_extents(inode, -num_extents);
2191 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002192
Josef Bacikb6d08f02013-09-27 14:57:43 -04002193 /*
2194 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002195 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002196 * error.
2197 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002198 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002200 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002201
Josef Bacik6a3891c2015-03-16 17:38:52 -04002202 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002203 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002204 return;
2205
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002206 if (!btrfs_is_data_reloc_root(root) &&
Filipe Mananaa315e682017-03-06 23:04:20 +00002207 do_list && !(state->state & EXTENT_NORESERVE) &&
2208 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002209 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002210
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002211 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2212 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002213 spin_lock(&inode->lock);
2214 inode->delalloc_bytes -= len;
2215 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002216 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002217 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002218 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002219 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002220 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002221
2222 if ((state->state & EXTENT_DELALLOC_NEW) &&
2223 (*bits & EXTENT_DELALLOC_NEW)) {
2224 spin_lock(&inode->lock);
2225 ASSERT(inode->new_delalloc_bytes >= len);
2226 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002227 if (*bits & EXTENT_ADD_INODE_BYTES)
2228 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002229 spin_unlock(&inode->lock);
2230 }
Chris Mason291d6732008-01-29 15:55:23 -05002231}
2232
Chris Masond352ac62008-09-29 15:18:18 -04002233/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002234 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2235 * in a chunk's stripe. This function ensures that bios do not span a
2236 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002237 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002238 * @page - The page we are about to add to the bio
2239 * @size - size we want to add to the bio
2240 * @bio - bio we want to ensure is smaller than a stripe
2241 * @bio_flags - flags of the bio
2242 *
2243 * return 1 if page cannot be added to the bio
2244 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002245 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002246 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002247int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2248 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002249{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002250 struct inode *inode = page->mapping->host;
2251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002252 u64 logical = bio->bi_iter.bi_sector << 9;
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002253 u32 bio_len = bio->bi_iter.bi_size;
Michal Rostecki42034312021-01-27 14:57:27 +01002254 struct extent_map *em;
Michal Rostecki42034312021-01-27 14:57:27 +01002255 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002256 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002257
Chris Mason771ed682008-11-06 22:02:51 -05002258 if (bio_flags & EXTENT_BIO_COMPRESSED)
2259 return 0;
2260
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002261 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
Michal Rostecki42034312021-01-27 14:57:27 +01002262 if (IS_ERR(em))
2263 return PTR_ERR(em);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002264 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002265 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002266 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002267
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002268 if (geom.len < bio_len + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002269 ret = 1;
2270out:
2271 free_extent_map(em);
2272 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002273}
2274
Chris Masond352ac62008-09-29 15:18:18 -04002275/*
2276 * in order to insert checksums into the metadata in large chunks,
2277 * we wait until bio submission time. All the pages in the bio are
2278 * checksummed and sums are attached onto the ordered extent record.
2279 *
2280 * At IO completion time the cums attached on the ordered extent record
2281 * are inserted into the btree
2282 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002283static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002284 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002285{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002286 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002287}
Chris Masone0156402008-04-16 11:15:20 -04002288
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002289/*
2290 * Split an extent_map at [start, start + len]
2291 *
2292 * This function is intended to be used only for extract_ordered_extent().
2293 */
2294static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2295 u64 pre, u64 post)
2296{
2297 struct extent_map_tree *em_tree = &inode->extent_tree;
2298 struct extent_map *em;
2299 struct extent_map *split_pre = NULL;
2300 struct extent_map *split_mid = NULL;
2301 struct extent_map *split_post = NULL;
2302 int ret = 0;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002303 unsigned long flags;
2304
2305 /* Sanity check */
2306 if (pre == 0 && post == 0)
2307 return 0;
2308
2309 split_pre = alloc_extent_map();
2310 if (pre)
2311 split_mid = alloc_extent_map();
2312 if (post)
2313 split_post = alloc_extent_map();
2314 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2315 ret = -ENOMEM;
2316 goto out;
2317 }
2318
2319 ASSERT(pre + post < len);
2320
2321 lock_extent(&inode->io_tree, start, start + len - 1);
2322 write_lock(&em_tree->lock);
2323 em = lookup_extent_mapping(em_tree, start, len);
2324 if (!em) {
2325 ret = -EIO;
2326 goto out_unlock;
2327 }
2328
2329 ASSERT(em->len == len);
2330 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2331 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
Naohiro Aota63fb5872021-08-09 09:29:18 +09002332 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2333 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2334 ASSERT(!list_empty(&em->list));
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002335
2336 flags = em->flags;
2337 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002338
2339 /* First, replace the em with a new extent_map starting from * em->start */
2340 split_pre->start = em->start;
2341 split_pre->len = (pre ? pre : em->len - post);
2342 split_pre->orig_start = split_pre->start;
2343 split_pre->block_start = em->block_start;
2344 split_pre->block_len = split_pre->len;
2345 split_pre->orig_block_len = split_pre->block_len;
2346 split_pre->ram_bytes = split_pre->len;
2347 split_pre->flags = flags;
2348 split_pre->compress_type = em->compress_type;
2349 split_pre->generation = em->generation;
2350
Naohiro Aota63fb5872021-08-09 09:29:18 +09002351 replace_extent_mapping(em_tree, em, split_pre, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002352
2353 /*
2354 * Now we only have an extent_map at:
2355 * [em->start, em->start + pre] if pre != 0
2356 * [em->start, em->start + em->len - post] if pre == 0
2357 */
2358
2359 if (pre) {
2360 /* Insert the middle extent_map */
2361 split_mid->start = em->start + pre;
2362 split_mid->len = em->len - pre - post;
2363 split_mid->orig_start = split_mid->start;
2364 split_mid->block_start = em->block_start + pre;
2365 split_mid->block_len = split_mid->len;
2366 split_mid->orig_block_len = split_mid->block_len;
2367 split_mid->ram_bytes = split_mid->len;
2368 split_mid->flags = flags;
2369 split_mid->compress_type = em->compress_type;
2370 split_mid->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002371 add_extent_mapping(em_tree, split_mid, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002372 }
2373
2374 if (post) {
2375 split_post->start = em->start + em->len - post;
2376 split_post->len = post;
2377 split_post->orig_start = split_post->start;
2378 split_post->block_start = em->block_start + em->len - post;
2379 split_post->block_len = split_post->len;
2380 split_post->orig_block_len = split_post->block_len;
2381 split_post->ram_bytes = split_post->len;
2382 split_post->flags = flags;
2383 split_post->compress_type = em->compress_type;
2384 split_post->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002385 add_extent_mapping(em_tree, split_post, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002386 }
2387
2388 /* Once for us */
2389 free_extent_map(em);
2390 /* Once for the tree */
2391 free_extent_map(em);
2392
2393out_unlock:
2394 write_unlock(&em_tree->lock);
2395 unlock_extent(&inode->io_tree, start, start + len - 1);
2396out:
2397 free_extent_map(split_pre);
2398 free_extent_map(split_mid);
2399 free_extent_map(split_post);
2400
2401 return ret;
2402}
2403
Naohiro Aotad22002f2021-02-04 19:22:00 +09002404static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2405 struct bio *bio, loff_t file_offset)
2406{
2407 struct btrfs_ordered_extent *ordered;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002408 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002409 u64 file_len;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002410 u64 len = bio->bi_iter.bi_size;
2411 u64 end = start + len;
2412 u64 ordered_end;
2413 u64 pre, post;
2414 int ret = 0;
2415
2416 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2417 if (WARN_ON_ONCE(!ordered))
2418 return BLK_STS_IOERR;
2419
2420 /* No need to split */
2421 if (ordered->disk_num_bytes == len)
2422 goto out;
2423
2424 /* We cannot split once end_bio'd ordered extent */
2425 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2426 ret = -EINVAL;
2427 goto out;
2428 }
2429
2430 /* We cannot split a compressed ordered extent */
2431 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2432 ret = -EINVAL;
2433 goto out;
2434 }
2435
2436 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2437 /* bio must be in one ordered extent */
2438 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2439 ret = -EINVAL;
2440 goto out;
2441 }
2442
2443 /* Checksum list should be empty */
2444 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2445 ret = -EINVAL;
2446 goto out;
2447 }
2448
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002449 file_len = ordered->num_bytes;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002450 pre = start - ordered->disk_bytenr;
2451 post = ordered_end - end;
2452
2453 ret = btrfs_split_ordered_extent(ordered, pre, post);
2454 if (ret)
2455 goto out;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002456 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
Naohiro Aotad22002f2021-02-04 19:22:00 +09002457
2458out:
Naohiro Aotad22002f2021-02-04 19:22:00 +09002459 btrfs_put_ordered_extent(ordered);
2460
2461 return errno_to_blk_status(ret);
2462}
2463
Chris Mason4a69a412008-11-06 22:03:00 -05002464/*
Chris Masoncad321a2008-12-17 14:51:42 -05002465 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002466 * on write, or reading the csums from the tree before a read.
2467 *
2468 * Rules about async/sync submit,
2469 * a) read: sync submit
2470 *
2471 * b) write without checksum: sync submit
2472 *
2473 * c) write with checksum:
2474 * c-1) if bio is issued by fsync: sync submit
2475 * (sync_writers != 0)
2476 *
2477 * c-2) if root is reloc root: sync submit
2478 * (only in case of buffered IO)
2479 *
2480 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002481 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002482blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2483 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002484
Chris Mason44b8bd72008-04-16 11:14:51 -04002485{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002487 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302488 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002489 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002490 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002491 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002492
Josef Bacik42437a62020-10-16 11:29:18 -04002493 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2494 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002495
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002496 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302497 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002498
Naohiro Aotad22002f2021-02-04 19:22:00 +09002499 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2500 struct page *page = bio_first_bvec_all(bio)->bv_page;
2501 loff_t file_offset = page_offset(page);
2502
2503 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2504 if (ret)
2505 goto out;
2506 }
2507
Naohiro Aotacfe94442021-02-04 19:21:59 +09002508 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002510 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002511 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002512
Chris Masond20f7042008-12-08 16:58:54 -05002513 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002514 ret = btrfs_submit_compressed_read(inode, bio,
2515 mirror_num,
2516 bio_flags);
2517 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002518 } else {
2519 /*
2520 * Lookup bio sums does extra checks around whether we
2521 * need to csum or not, which is why we ignore skip_sum
2522 * here.
2523 */
Qu Wenruo62751932020-12-02 14:48:06 +08002524 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002525 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002526 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002527 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002528 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002529 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002530 /* csum items have already been cloned */
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09002531 if (btrfs_is_data_reloc_root(root))
Yan Zheng17d217f2008-12-12 10:03:38 -05002532 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002533 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002534 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2535 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002536 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002537 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002538 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002539 if (ret)
2540 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002541 }
2542
Chris Mason0b86a832008-03-24 15:01:56 -04002543mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002544 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002545
2546out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002547 if (ret) {
2548 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002549 bio_endio(bio);
2550 }
Stefan Behrens61891922012-11-05 18:51:52 +01002551 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002552}
Chris Mason6885f302008-02-20 16:11:05 -05002553
Chris Masond352ac62008-09-29 15:18:18 -04002554/*
2555 * given a list of ordered sums record them in the inode. This happens
2556 * at IO completion time based on sums calculated at bio submission time.
2557 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002558static int add_pending_csums(struct btrfs_trans_handle *trans,
2559 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002560{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002561 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002562 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002563
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002564 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002565 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002566 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002567 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002568 if (ret)
2569 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002570 }
2571 return 0;
2572}
2573
Filipe Mananac3347302020-11-04 11:07:31 +00002574static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2575 const u64 start,
2576 const u64 len,
2577 struct extent_state **cached_state)
2578{
2579 u64 search_start = start;
2580 const u64 end = start + len - 1;
2581
2582 while (search_start < end) {
2583 const u64 search_len = end - search_start + 1;
2584 struct extent_map *em;
2585 u64 em_len;
2586 int ret = 0;
2587
2588 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2589 if (IS_ERR(em))
2590 return PTR_ERR(em);
2591
2592 if (em->block_start != EXTENT_MAP_HOLE)
2593 goto next;
2594
2595 em_len = em->len;
2596 if (em->start < search_start)
2597 em_len -= search_start - em->start;
2598 if (em_len > search_len)
2599 em_len = search_len;
2600
2601 ret = set_extent_bit(&inode->io_tree, search_start,
2602 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002603 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2604 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002605next:
2606 search_start = extent_map_end(em);
2607 free_extent_map(em);
2608 if (ret)
2609 return ret;
2610 }
2611 return 0;
2612}
2613
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002614int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002615 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002616 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002617{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002618 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002619
2620 if (start >= i_size_read(&inode->vfs_inode) &&
2621 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2622 /*
2623 * There can't be any extents following eof in this case so just
2624 * set the delalloc new bit for the range directly.
2625 */
2626 extra_bits |= EXTENT_DELALLOC_NEW;
2627 } else {
2628 int ret;
2629
2630 ret = btrfs_find_new_delalloc_bytes(inode, start,
2631 end + 1 - start,
2632 cached_state);
2633 if (ret)
2634 return ret;
2635 }
2636
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002637 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2638 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002639}
2640
Chris Masond352ac62008-09-29 15:18:18 -04002641/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002642struct btrfs_writepage_fixup {
2643 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002644 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002645 struct btrfs_work work;
2646};
2647
Christoph Hellwigb2950862008-12-02 09:54:17 -05002648static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002649{
2650 struct btrfs_writepage_fixup *fixup;
2651 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002652 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002653 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002654 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002655 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002656 u64 page_start;
2657 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002658 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002659 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002660
2661 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2662 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002663 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002664 page_start = page_offset(page);
2665 page_end = page_offset(page) + PAGE_SIZE - 1;
2666
2667 /*
2668 * This is similar to page_mkwrite, we need to reserve the space before
2669 * we take the page lock.
2670 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002671 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2672 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002673again:
Chris Mason247e7432008-07-17 12:53:51 -04002674 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002675
Chris Mason25f3c502020-01-21 11:51:42 -05002676 /*
2677 * Before we queued this fixup, we took a reference on the page.
2678 * page->mapping may go NULL, but it shouldn't be moved to a different
2679 * address space.
2680 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002681 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2682 /*
2683 * Unfortunately this is a little tricky, either
2684 *
2685 * 1) We got here and our page had already been dealt with and
2686 * we reserved our space, thus ret == 0, so we need to just
2687 * drop our space reservation and bail. This can happen the
2688 * first time we come into the fixup worker, or could happen
2689 * while waiting for the ordered extent.
2690 * 2) Our page was already dealt with, but we happened to get an
2691 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2692 * this case we obviously don't have anything to release, but
2693 * because the page was already dealt with we don't want to
2694 * mark the page with an error, so make sure we're resetting
2695 * ret to 0. This is why we have this check _before_ the ret
2696 * check, because we do not want to have a surprise ENOSPC
2697 * when the page was already properly dealt with.
2698 */
2699 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002700 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2701 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002702 page_start, PAGE_SIZE,
2703 true);
2704 }
2705 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002706 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002707 }
Chris Mason25f3c502020-01-21 11:51:42 -05002708
2709 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002710 * We can't mess with the page state unless it is locked, so now that
2711 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002712 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002713 if (ret)
2714 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002715
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002716 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002717
2718 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002719 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002720 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002721
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002722 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002723 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002724 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2725 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002726 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002727 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002728 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002729 goto again;
2730 }
Chris Mason247e7432008-07-17 12:53:51 -04002731
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002732 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002733 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002734 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002735 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002736
Chris Mason25f3c502020-01-21 11:51:42 -05002737 /*
2738 * Everything went as planned, we're now the owner of a dirty page with
2739 * delayed allocation bits set and space reserved for our COW
2740 * destination.
2741 *
2742 * The page was dirty when we started, nothing should have cleaned it.
2743 */
2744 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002745 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002746out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002747 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002748 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002749 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2750 PAGE_SIZE, true);
2751 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002752 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002753out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002754 if (ret) {
2755 /*
2756 * We hit ENOSPC or other errors. Update the mapping and page
2757 * to reflect the errors and clean the page.
2758 */
2759 mapping_set_error(page->mapping, ret);
2760 end_extent_writepage(page, ret, page_start, page_end);
2761 clear_page_dirty_for_io(page);
2762 SetPageError(page);
2763 }
2764 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002765 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002766 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002767 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002768 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002769 /*
2770 * As a precaution, do a delayed iput in case it would be the last iput
2771 * that could need flushing space. Recursing back to fixup worker would
2772 * deadlock.
2773 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002774 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002775}
2776
2777/*
2778 * There are a few paths in the higher layers of the kernel that directly
2779 * set the page dirty bit without asking the filesystem if it is a
2780 * good idea. This causes problems because we want to make sure COW
2781 * properly happens and the data=ordered rules are followed.
2782 *
Chris Masonc8b97812008-10-29 14:49:59 -04002783 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002784 * hasn't been properly setup for IO. We kick off an async process
2785 * to fix it up. The async helper will wait for ordered extents, set
2786 * the delalloc bit and make it safe to write the page.
2787 */
Qu Wenruoa129ffb2021-07-27 13:41:32 +08002788int btrfs_writepage_cow_fixup(struct page *page)
Chris Mason247e7432008-07-17 12:53:51 -04002789{
2790 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002792 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002793
Qu Wenruof57ad932021-04-07 19:22:13 +08002794 /* This page has ordered extent covering it already */
2795 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002796 return 0;
2797
Chris Mason25f3c502020-01-21 11:51:42 -05002798 /*
2799 * PageChecked is set below when we create a fixup worker for this page,
2800 * don't try to create another one if we're already PageChecked()
2801 *
2802 * The extent_io writepage code will redirty the page if we send back
2803 * EAGAIN.
2804 */
Chris Mason247e7432008-07-17 12:53:51 -04002805 if (PageChecked(page))
2806 return -EAGAIN;
2807
2808 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2809 if (!fixup)
2810 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002811
Josef Bacikf4b13632020-01-21 14:34:52 -05002812 /*
2813 * We are already holding a reference to this inode from
2814 * write_cache_pages. We need to hold it because the space reservation
2815 * takes place outside of the page lock, and we can't trust
2816 * page->mapping outside of the page lock.
2817 */
2818 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002819 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002820 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002821 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002822 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002823 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002825
2826 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002827}
2828
Yan Zhengd899e052008-10-30 14:25:28 -04002829static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002830 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002831 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002832 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002833 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002834{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002835 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002836 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002837 struct btrfs_path *path;
2838 struct extent_buffer *leaf;
2839 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002840 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2841 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2842 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2843 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002844 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002845 int ret;
2846
2847 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002848 if (!path)
2849 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002850
Chris Masona1ed8352009-09-11 12:27:37 -04002851 /*
2852 * we may be replacing one extent in the tree with another.
2853 * The new extent is pinned in the extent map, and we don't want
2854 * to drop it from the cache until it is completely in the btree.
2855 *
2856 * So, tell btrfs_drop_extents to leave this extent in the cache.
2857 * the caller is expected to unpin it and allow it to be merged
2858 * with the others.
2859 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002860 drop_args.path = path;
2861 drop_args.start = file_pos;
2862 drop_args.end = file_pos + num_bytes;
2863 drop_args.replace_extent = true;
2864 drop_args.extent_item_size = sizeof(*stack_fi);
2865 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 if (ret)
2867 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002868
Filipe Manana5893dfb2020-11-04 11:07:32 +00002869 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002870 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002871 ins.offset = file_pos;
2872 ins.type = BTRFS_EXTENT_DATA_KEY;
2873
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002874 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002875 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002876 if (ret)
2877 goto out;
2878 }
Yan Zhengd899e052008-10-30 14:25:28 -04002879 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002880 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2881 write_extent_buffer(leaf, stack_fi,
2882 btrfs_item_ptr_offset(leaf, path->slots[0]),
2883 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002884
Yan Zhengd899e052008-10-30 14:25:28 -04002885 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002886 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002887
Filipe Manana2766ff62020-11-04 11:07:34 +00002888 /*
2889 * If we dropped an inline extent here, we know the range where it is
2890 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
David Sterba1a9fd412021-05-21 17:42:23 +02002891 * number of bytes only for that range containing the inline extent.
Filipe Manana2766ff62020-11-04 11:07:34 +00002892 * The remaining of the range will be processed when clearning the
2893 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2894 */
2895 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2896 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2897
2898 inline_size = drop_args.bytes_found - inline_size;
2899 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2900 drop_args.bytes_found -= inline_size;
2901 num_bytes -= sectorsize;
2902 }
2903
2904 if (update_inode_bytes)
2905 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002906
2907 ins.objectid = disk_bytenr;
2908 ins.offset = disk_num_bytes;
2909 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002910
Nikolay Borisovc553f942020-06-03 08:55:19 +03002911 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002912 if (ret)
2913 goto out;
2914
Nikolay Borisovc553f942020-06-03 08:55:19 +03002915 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002916 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002917out:
Yan Zhengd899e052008-10-30 14:25:28 -04002918 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002919
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002920 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002921}
2922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002923static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002924 u64 start, u64 len)
2925{
David Sterba32da53862019-10-29 19:20:18 +01002926 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002927
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002929 ASSERT(cache);
2930
2931 spin_lock(&cache->lock);
2932 cache->delalloc_bytes -= len;
2933 spin_unlock(&cache->lock);
2934
2935 btrfs_put_block_group(cache);
2936}
2937
Qu Wenruo203f44c52020-06-10 09:04:40 +08002938static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002939 struct btrfs_ordered_extent *oe)
2940{
2941 struct btrfs_file_extent_item stack_fi;
2942 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002943 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002944
2945 memset(&stack_fi, 0, sizeof(stack_fi));
2946 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2947 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2948 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2949 oe->disk_num_bytes);
2950 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2951 logical_len = oe->truncated_len;
2952 else
2953 logical_len = oe->num_bytes;
2954 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2955 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2956 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2957 /* Encryption and other encoding is reserved and all 0 */
2958
Filipe Manana2766ff62020-11-04 11:07:34 +00002959 /*
2960 * For delalloc, when completing an ordered extent we update the inode's
2961 * bytes when clearing the range in the inode's io tree, so pass false
2962 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2963 * except if the ordered extent was truncated.
2964 */
2965 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2966 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2967
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002968 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2969 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002970 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002971}
2972
2973/*
2974 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002975 * an ordered extent if the range of bytes in the file it covers are
2976 * fully written.
2977 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002978static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002979{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002980 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2981 struct btrfs_root *root = inode->root;
2982 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002983 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002984 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002985 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002986 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002987 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002988 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002989 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002990 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002991 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002992 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002993 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002994
Omar Sandovalbffe6332019-12-02 17:34:19 -08002995 start = ordered_extent->file_offset;
2996 end = start + ordered_extent->num_bytes - 1;
2997
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002998 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2999 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3000 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00003001 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003002
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003003 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003004
Josef Bacik5fd02042012-05-02 14:00:54 -04003005 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3006 ret = -EIO;
3007 goto out;
3008 }
3009
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003010 /* A valid bdev implies a write on a sequential zone */
3011 if (ordered_extent->bdev) {
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003012 btrfs_rewrite_logical_zoned(ordered_extent);
Naohiro Aotabe1a1d72021-08-19 21:19:23 +09003013 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3014 ordered_extent->disk_num_bytes);
3015 }
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003016
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003017 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08003018
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3020 truncated = true;
3021 logical_len = ordered_extent->truncated_len;
3022 /* Truncated the entire extent, don't bother adding */
3023 if (!logical_len)
3024 goto out;
3025 }
3026
Yan, Zhengc2167752009-11-12 09:34:21 +00003027 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003029
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003030 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003031 if (freespace_inode)
3032 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003033 else
3034 trans = btrfs_join_transaction(root);
3035 if (IS_ERR(trans)) {
3036 ret = PTR_ERR(trans);
3037 trans = NULL;
3038 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003039 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003040 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02003041 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003042 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003043 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003044 goto out;
3045 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046
Filipe Manana2766ff62020-11-04 11:07:34 +00003047 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003048 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049
Nikolay Borisov8d510122019-10-08 20:43:06 +03003050 if (freespace_inode)
3051 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003052 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003053 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003054 if (IS_ERR(trans)) {
3055 ret = PTR_ERR(trans);
3056 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003057 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 }
Chris Masona79b7d42014-05-22 16:18:52 -07003059
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003060 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003061
Chris Masonc8b97812008-10-29 14:49:59 -04003062 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003063 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003064 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003065 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003066 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04003067 ordered_extent->file_offset,
3068 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003069 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003070 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003071 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03003072 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04003073 if (!ret) {
3074 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003075 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003076 ordered_extent->disk_bytenr,
3077 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04003078 }
Yan Zhengd899e052008-10-30 14:25:28 -04003079 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003080 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003081 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003083 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003084 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003086
Nikolay Borisov510f85e2020-09-18 12:15:52 +03003087 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02003088 if (ret) {
3089 btrfs_abort_transaction(trans, ret);
3090 goto out;
3091 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092
Filipe Manana2766ff62020-11-04 11:07:34 +00003093 /*
3094 * If this is a new delalloc range, clear its new delalloc flag to
3095 * update the inode's number of bytes. This needs to be done first
3096 * before updating the inode item.
3097 */
3098 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3099 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003100 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00003101 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3102 0, 0, &cached_state);
3103
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003104 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003105 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003106 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003107 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003108 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003109 }
3110 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003111out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003112 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003113 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003114 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003115
Miao Xiea698d0752012-09-20 01:51:59 -06003116 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003117 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003118
Josef Bacik77cef2e2013-08-29 13:57:21 -04003119 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003120 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003121
Josef Bacikd61bec02021-05-19 09:38:27 -04003122 /*
3123 * If we failed to finish this ordered extent for any reason we
3124 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3125 * extent, and mark the inode with the error if it wasn't
3126 * already set. Any error during writeback would have already
3127 * set the mapping error, so we need to set it if we're the ones
3128 * marking this ordered extent as failed.
3129 */
3130 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3131 &ordered_extent->flags))
3132 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3133
Josef Bacik77cef2e2013-08-29 13:57:21 -04003134 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003135 unwritten_start += logical_len;
3136 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003137
3138 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003139 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003140
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003141 /*
3142 * If the ordered extent had an IOERR or something else went
3143 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003144 * back to the allocator. We only free the extent in the
3145 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003146 *
3147 * If we made it past insert_reserved_file_extent before we
3148 * errored out then we don't need to do this as the accounting
3149 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003150 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003151 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003152 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003153 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003154 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3155 /*
3156 * Discard the range before returning it back to the
3157 * free space pool
3158 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003159 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003160 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003161 ordered_extent->disk_bytenr,
3162 ordered_extent->disk_num_bytes,
3163 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003164 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003165 ordered_extent->disk_bytenr,
3166 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003167 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003168 }
3169
Josef Bacik5fd02042012-05-02 14:00:54 -04003170 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003171 * This needs to be done to make sure anybody waiting knows we are done
3172 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003173 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003174 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003175
Chris Masone6dcd2d2008-07-17 12:53:50 -04003176 /* once for us */
3177 btrfs_put_ordered_extent(ordered_extent);
3178 /* once for the tree */
3179 btrfs_put_ordered_extent(ordered_extent);
3180
Josef Bacik5fd02042012-05-02 14:00:54 -04003181 return ret;
3182}
3183
3184static void finish_ordered_fn(struct btrfs_work *work)
3185{
3186 struct btrfs_ordered_extent *ordered_extent;
3187 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3188 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003189}
3190
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003191void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3192 struct page *page, u64 start,
David Sterba25c12522021-07-26 14:15:08 +02003193 u64 end, bool uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003194{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003195 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003196
Qu Wenruoe65f1522021-04-01 15:15:06 +08003197 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3198 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003199}
3200
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003201/*
3202 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003203 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003204 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003205 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003206 * @page: page where is the data to be verified
3207 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003208 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003209 *
3210 * The length of such check is always one sector size.
3211 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003212static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003213 u32 bio_offset, struct page *page, u32 pgoff,
3214 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003215{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3217 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003218 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003219 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003220 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003221 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003222 u8 *csum_expected;
3223 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003224
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003225 ASSERT(pgoff + len <= PAGE_SIZE);
3226
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003227 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3228 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003229
3230 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003231 shash->tfm = fs_info->csum_shash;
3232
Eric Biggersfd080012020-04-30 23:51:59 -07003233 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003234
3235 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003236 goto zeroit;
3237
3238 kunmap_atomic(kaddr);
3239 return 0;
3240zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003241 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3242 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003243 if (io_bio->device)
3244 btrfs_dev_stat_inc_and_print(io_bio->device,
3245 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003246 memset(kaddr + pgoff, 1, len);
3247 flush_dcache_page(page);
3248 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 return -EIO;
3250}
3251
Chris Masond352ac62008-09-29 15:18:18 -04003252/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003253 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003254 * if there's a match, we allow the bio to finish. If not, the code in
3255 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003256 *
3257 * @bio_offset: offset to the beginning of the bio (in bytes)
3258 * @start: file offset of the range start
3259 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003260 *
3261 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3262 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003263 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003264unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3265 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003266{
Chris Mason07157aa2007-08-30 08:50:51 -04003267 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003268 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003269 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003270 const u32 sectorsize = root->fs_info->sectorsize;
3271 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003272 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003273
Chris Masond20f7042008-12-08 16:58:54 -05003274 if (PageChecked(page)) {
3275 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003276 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003277 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003278
Qu Wenruo3670e642021-07-26 14:34:51 +08003279 /*
3280 * For subpage case, above PageChecked is not safe as it's not subpage
3281 * compatible.
3282 * But for now only cow fixup and compressed read utilize PageChecked
3283 * flag, while in this context we can easily use io_bio->csum to
3284 * determine if we really need to do csum verification.
3285 *
3286 * So for now, just exit if io_bio->csum is NULL, as it means it's
3287 * compressed read, and its compressed data csum has already been
3288 * verified.
3289 */
3290 if (io_bio->csum == NULL)
3291 return 0;
3292
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003293 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003294 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003295
Josef Bacik42437a62020-10-16 11:29:18 -04003296 if (!root->fs_info->csum_root)
3297 return 0;
3298
Qu Wenruof44cf412020-12-02 14:47:59 +08003299 ASSERT(page_offset(page) <= start &&
3300 end <= page_offset(page) + PAGE_SIZE - 1);
3301 for (pg_off = offset_in_page(start);
3302 pg_off < offset_in_page(end);
3303 pg_off += sectorsize, bio_offset += sectorsize) {
Qu Wenruoe3c62322021-07-26 14:35:04 +08003304 u64 file_offset = pg_off + page_offset(page);
Qu Wenruof44cf412020-12-02 14:47:59 +08003305 int ret;
3306
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09003307 if (btrfs_is_data_reloc_root(root) &&
Qu Wenruoe3c62322021-07-26 14:35:04 +08003308 test_range_bit(io_tree, file_offset,
3309 file_offset + sectorsize - 1,
3310 EXTENT_NODATASUM, 1, NULL)) {
3311 /* Skip the range without csum for data reloc inode */
3312 clear_extent_bits(io_tree, file_offset,
3313 file_offset + sectorsize - 1,
3314 EXTENT_NODATASUM);
3315 continue;
3316 }
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003317 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3318 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003319 if (ret < 0) {
3320 const int nr_bit = (pg_off - offset_in_page(start)) >>
3321 root->fs_info->sectorsize_bits;
3322
3323 result |= (1U << nr_bit);
3324 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003325 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003326 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003327}
Chris Masonb888db22007-08-27 16:49:44 -04003328
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003329/*
3330 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3331 *
3332 * @inode: The inode we want to perform iput on
3333 *
3334 * This function uses the generic vfs_inode::i_count to track whether we should
3335 * just decrement it (in case it's > 1) or if this is the last iput then link
3336 * the inode to the delayed iput machinery. Delayed iputs are processed at
3337 * transaction commit time/superblock commit/cleaner kthread.
3338 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003339void btrfs_add_delayed_iput(struct inode *inode)
3340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003341 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003342 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003343
3344 if (atomic_add_unless(&inode->i_count, -1, 1))
3345 return;
3346
Josef Bacik034f7842018-12-03 11:06:52 -05003347 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003348 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003349 ASSERT(list_empty(&binode->delayed_iput));
3350 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003351 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003352 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3353 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003354}
3355
Josef Bacik63611e72019-06-18 10:59:18 -04003356static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3357 struct btrfs_inode *inode)
3358{
3359 list_del_init(&inode->delayed_iput);
3360 spin_unlock(&fs_info->delayed_iput_lock);
3361 iput(&inode->vfs_inode);
3362 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3363 wake_up(&fs_info->delayed_iputs_wait);
3364 spin_lock(&fs_info->delayed_iput_lock);
3365}
3366
3367static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3368 struct btrfs_inode *inode)
3369{
3370 if (!list_empty(&inode->delayed_iput)) {
3371 spin_lock(&fs_info->delayed_iput_lock);
3372 if (!list_empty(&inode->delayed_iput))
3373 run_delayed_iput_locked(fs_info, inode);
3374 spin_unlock(&fs_info->delayed_iput_lock);
3375 }
3376}
3377
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003378void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003379{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003380
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003381 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003382 while (!list_empty(&fs_info->delayed_iputs)) {
3383 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003384
David Sterba8089fe62015-11-19 14:15:51 +01003385 inode = list_first_entry(&fs_info->delayed_iputs,
3386 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003387 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003388 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003389 }
David Sterba8089fe62015-11-19 14:15:51 +01003390 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003391}
3392
Josef Bacik034f7842018-12-03 11:06:52 -05003393/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003394 * Wait for flushing all delayed iputs
3395 *
3396 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003397 *
3398 * This will wait on any delayed iputs that are currently running with KILLABLE
3399 * set. Once they are all done running we will return, unless we are killed in
3400 * which case we return EINTR. This helps in user operations like fallocate etc
3401 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003402 *
3403 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003404 */
3405int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3406{
3407 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3408 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3409 if (ret)
3410 return -EINTR;
3411 return 0;
3412}
3413
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003415 * This creates an orphan entry for the given inode in case something goes wrong
3416 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003417 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003418int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003419 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003420{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003421 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003422
Omar Sandoval27919062018-05-11 13:13:37 -07003423 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3424 if (ret && ret != -EEXIST) {
3425 btrfs_abort_transaction(trans, ret);
3426 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003427 }
3428
Yan, Zhengd68fc572010-05-16 10:49:58 -04003429 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003430}
3431
3432/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003433 * We have done the delete so we can go ahead and remove the orphan item for
3434 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003435 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003436static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003437 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003438{
Omar Sandoval27919062018-05-11 13:13:37 -07003439 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003440}
3441
3442/*
3443 * this cleans up any orphans that may be left on the list from the last use
3444 * of this root.
3445 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003446int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003448 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003449 struct btrfs_path *path;
3450 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003451 struct btrfs_key key, found_key;
3452 struct btrfs_trans_handle *trans;
3453 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003454 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003455 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003456
Yan, Zhengd68fc572010-05-16 10:49:58 -04003457 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003458 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003459
3460 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003461 if (!path) {
3462 ret = -ENOMEM;
3463 goto out;
3464 }
David Sterbae4058b52015-11-27 16:31:35 +01003465 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003466
3467 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003468 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003469 key.offset = (u64)-1;
3470
Josef Bacik7b128762008-07-24 12:17:14 -04003471 while (1) {
3472 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 if (ret < 0)
3474 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003475
3476 /*
3477 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003478 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003479 * find the key and see if we have stuff that matches
3480 */
3481 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003482 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003483 if (path->slots[0] == 0)
3484 break;
3485 path->slots[0]--;
3486 }
3487
3488 /* pull out the item */
3489 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003490 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3491
3492 /* make sure the item matches what we want */
3493 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3494 break;
David Sterba962a2982014-06-04 18:41:45 +02003495 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003496 break;
3497
3498 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003499 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003500
3501 /*
3502 * this is where we are basically btrfs_lookup, without the
3503 * crossing root thing. we store the inode number in the
3504 * offset of the orphan item.
3505 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003506
3507 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 btrfs_err(fs_info,
3509 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003510 ret = -EINVAL;
3511 goto out;
3512 }
3513
3514 last_objectid = found_key.offset;
3515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003516 found_key.objectid = found_key.offset;
3517 found_key.type = BTRFS_INODE_ITEM_KEY;
3518 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003519 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303520 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003521 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003524 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003525 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003526 int is_dead_root = 0;
3527
3528 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003529 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003530 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003531 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003532 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003533 * We need to distinguish those two, as the orphan item
3534 * for a root must not get deleted before the deletion
3535 * of the snapshot/subvolume's tree completes.
3536 *
3537 * btrfs_find_orphan_roots() ran before us, which has
3538 * found all deleted roots and loaded them into
3539 * fs_info->fs_roots_radix. So here we can find if an
3540 * orphan item corresponds to a deleted root by looking
3541 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003542 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003543
3544 spin_lock(&fs_info->fs_roots_radix_lock);
3545 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3546 (unsigned long)found_key.objectid);
3547 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3548 is_dead_root = 1;
3549 spin_unlock(&fs_info->fs_roots_radix_lock);
3550
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003551 if (is_dead_root) {
3552 /* prevent this orphan from being found again */
3553 key.offset = found_key.objectid - 1;
3554 continue;
3555 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003556
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003557 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003558
Josef Bacika8c9e572011-09-21 16:55:59 -04003559 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003560 * If we have an inode with links, there are a couple of
Boris Burkov70524252021-06-30 13:01:50 -07003561 * possibilities:
3562 *
3563 * 1. We were halfway through creating fsverity metadata for the
3564 * file. In that case, the orphan item represents incomplete
3565 * fsverity metadata which must be cleaned up with
3566 * btrfs_drop_verity_items and deleting the orphan item.
3567
3568 * 2. Old kernels (before v3.12) used to create an
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003569 * orphan item for truncate indicating that there were possibly
3570 * extent items past i_size that needed to be deleted. In v3.12,
3571 * truncate was changed to update i_size in sync with the extent
3572 * items, but the (useless) orphan item was still created. Since
3573 * v4.18, we don't create the orphan item for truncate at all.
3574 *
3575 * So, this item could mean that we need to do a truncate, but
3576 * only if this filesystem was last used on a pre-v3.12 kernel
3577 * and was not cleanly unmounted. The odds of that are quite
3578 * slim, and it's a pain to do the truncate now, so just delete
3579 * the orphan item.
3580 *
3581 * It's also possible that this orphan item was supposed to be
3582 * deleted but wasn't. The inode number may have been reused,
3583 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003584 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003585 if (ret == -ENOENT || inode->i_nlink) {
Boris Burkov70524252021-06-30 13:01:50 -07003586 if (!ret) {
3587 ret = btrfs_drop_verity_items(BTRFS_I(inode));
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003588 iput(inode);
Boris Burkov70524252021-06-30 13:01:50 -07003589 if (ret)
3590 goto out;
3591 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003592 trans = btrfs_start_transaction(root, 1);
3593 if (IS_ERR(trans)) {
3594 ret = PTR_ERR(trans);
3595 goto out;
3596 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003597 btrfs_debug(fs_info, "auto deleting %Lu",
3598 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003599 ret = btrfs_del_orphan_item(trans, root,
3600 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003601 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003602 if (ret)
3603 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003604 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003605 }
Josef Bacik7b128762008-07-24 12:17:14 -04003606
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003607 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003608
3609 /* this will do delete_inode and everything for us */
3610 iput(inode);
3611 }
Miao Xie3254c872011-11-10 20:45:05 -05003612 /* release the path since we're done with it */
3613 btrfs_release_path(path);
3614
Yan, Zhengd68fc572010-05-16 10:49:58 -04003615 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3616
Omar Sandovala575cee2018-05-11 13:13:38 -07003617 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003618 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003619 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003620 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003621 }
Josef Bacik7b128762008-07-24 12:17:14 -04003622
3623 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003625
3626out:
3627 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003628 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003629 btrfs_free_path(path);
3630 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003631}
3632
Chris Masond352ac62008-09-29 15:18:18 -04003633/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003634 * very simple check to peek ahead in the leaf looking for xattrs. If we
3635 * don't find any xattrs, we know there can't be any acls.
3636 *
3637 * slot is the slot the inode is in, objectid is the objectid of the inode
3638 */
3639static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003640 int slot, u64 objectid,
3641 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003642{
3643 u32 nritems = btrfs_header_nritems(leaf);
3644 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003645 static u64 xattr_access = 0;
3646 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 int scanned = 0;
3648
Josef Bacikf23b5a52013-06-19 10:16:26 -04003649 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003650 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3651 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3652 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3653 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003654 }
3655
Chris Mason46a53cc2009-04-27 11:47:50 -04003656 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003657 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 while (slot < nritems) {
3659 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3660
3661 /* we found a different objectid, there must not be acls */
3662 if (found_key.objectid != objectid)
3663 return 0;
3664
3665 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003667 if (*first_xattr_slot == -1)
3668 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003669 if (found_key.offset == xattr_access ||
3670 found_key.offset == xattr_default)
3671 return 1;
3672 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003673
3674 /*
3675 * we found a key greater than an xattr key, there can't
3676 * be any acls later on
3677 */
3678 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3679 return 0;
3680
3681 slot++;
3682 scanned++;
3683
3684 /*
3685 * it goes inode, inode backrefs, xattrs, extents,
3686 * so if there are a ton of hard links to an inode there can
3687 * be a lot of backrefs. Don't waste time searching too hard,
3688 * this is just an optimization
3689 */
3690 if (scanned >= 8)
3691 break;
3692 }
3693 /* we hit the end of the leaf before we found an xattr or
3694 * something larger than an xattr. We have to assume the inode
3695 * has acls
3696 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697 if (*first_xattr_slot == -1)
3698 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003699 return 1;
3700}
3701
3702/*
Chris Masond352ac62008-09-29 15:18:18 -04003703 * read an inode from the btree into the in-memory inode
3704 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003705static int btrfs_read_locked_inode(struct inode *inode,
3706 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003707{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003709 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003710 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003711 struct btrfs_inode_item *inode_item;
3712 struct btrfs_root *root = BTRFS_I(inode)->root;
3713 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003714 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003715 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003716 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003717 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003718 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003719 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003720
3721 ret = btrfs_fill_inode(inode, &rdev);
3722 if (!ret)
3723 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003724
Filipe Manana4222ea72018-10-24 10:13:03 +01003725 if (!path) {
3726 path = btrfs_alloc_path();
3727 if (!path)
3728 return -ENOMEM;
3729 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003730
Chris Mason39279cc2007-06-12 06:35:45 -04003731 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003732
Chris Mason39279cc2007-06-12 06:35:45 -04003733 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003734 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003735 if (path != in_path)
3736 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003737 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003738 }
Chris Mason39279cc2007-06-12 06:35:45 -04003739
Chris Mason5f39d392007-10-15 16:14:19 -04003740 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003741
3742 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003743 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003744
Chris Mason5f39d392007-10-15 16:14:19 -04003745 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3746 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003747 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003748 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003749 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3750 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003751 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003752 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3753 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003754
David Sterbaa937b972014-12-12 17:39:12 +01003755 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3756 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003757
David Sterbaa937b972014-12-12 17:39:12 +01003758 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3759 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
David Sterbaa937b972014-12-12 17:39:12 +01003761 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3762 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003763
chandan r9cc97d62012-07-04 12:48:07 +05303764 BTRFS_I(inode)->i_otime.tv_sec =
3765 btrfs_timespec_sec(leaf, &inode_item->otime);
3766 BTRFS_I(inode)->i_otime.tv_nsec =
3767 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003768
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003769 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003770 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003771 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3772
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003773 inode_set_iversion_queried(inode,
3774 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003775 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003776 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003777 rdev = btrfs_inode_rdev(leaf, inode_item);
3778
Josef Bacikaec74772008-07-24 12:12:38 -04003779 BTRFS_I(inode)->index_cnt = (u64)-1;
Boris Burkov77eea052021-06-30 13:01:48 -07003780 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3781 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
Miao Xie67de1172013-12-26 13:07:06 +08003782
3783cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003784 /*
3785 * If we were modified in the current generation and evicted from memory
3786 * and then re-read we need to do a full sync since we don't have any
3787 * idea about which extents were modified before we were evicted from
3788 * cache.
3789 *
3790 * This is required for both inode re-read from disk and delayed inode
3791 * in delayed_nodes_tree.
3792 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003793 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003794 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3795 &BTRFS_I(inode)->runtime_flags);
3796
Filipe Mananabde6c242015-07-24 00:00:19 +01003797 /*
3798 * We don't persist the id of the transaction where an unlink operation
3799 * against the inode was last made. So here we assume the inode might
3800 * have been evicted, and therefore the exact value of last_unlink_trans
3801 * lost, and set it to last_trans to avoid metadata inconsistencies
3802 * between the inode and its parent if the inode is fsync'ed and the log
3803 * replayed. For example, in the scenario:
3804 *
3805 * touch mydir/foo
3806 * ln mydir/foo mydir/bar
3807 * sync
3808 * unlink mydir/bar
3809 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3810 * xfs_io -c fsync mydir/foo
3811 * <power failure>
3812 * mount fs, triggers fsync log replay
3813 *
3814 * We must make sure that when we fsync our inode foo we also log its
3815 * parent inode, otherwise after log replay the parent still has the
3816 * dentry with the "bar" name but our inode foo has a link count of 1
3817 * and doesn't have an inode ref with the name "bar" anymore.
3818 *
3819 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003820 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003821 * transaction commits on fsync if our inode is a directory, or if our
3822 * inode is not a directory, logging its parent unnecessarily.
3823 */
3824 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3825
Filipe Manana3ebac172020-07-15 12:30:43 +01003826 /*
3827 * Same logic as for last_unlink_trans. We don't persist the generation
3828 * of the last transaction where this inode was used for a reflink
3829 * operation, so after eviction and reloading the inode we must be
3830 * pessimistic and assume the last transaction that modified the inode.
3831 */
3832 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3833
Miao Xie67de1172013-12-26 13:07:06 +08003834 path->slots[0]++;
3835 if (inode->i_nlink != 1 ||
3836 path->slots[0] >= btrfs_header_nritems(leaf))
3837 goto cache_acl;
3838
3839 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003840 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003841 goto cache_acl;
3842
3843 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3844 if (location.type == BTRFS_INODE_REF_KEY) {
3845 struct btrfs_inode_ref *ref;
3846
3847 ref = (struct btrfs_inode_ref *)ptr;
3848 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3849 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3850 struct btrfs_inode_extref *extref;
3851
3852 extref = (struct btrfs_inode_extref *)ptr;
3853 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3854 extref);
3855 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003856cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003857 /*
3858 * try to precache a NULL acl entry for files that don't have
3859 * any xattrs or acls
3860 */
Li Zefan33345d012011-04-20 10:31:50 +08003861 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003862 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003863 if (first_xattr_slot != -1) {
3864 path->slots[0] = first_xattr_slot;
3865 ret = btrfs_load_inode_props(inode, path);
3866 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003867 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003868 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003869 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003870 root->root_key.objectid, ret);
3871 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003872 if (path != in_path)
3873 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003874
Al Viro72c04902009-06-24 16:58:48 -04003875 if (!maybe_acls)
3876 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003877
Chris Mason39279cc2007-06-12 06:35:45 -04003878 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003879 case S_IFREG:
3880 inode->i_mapping->a_ops = &btrfs_aops;
3881 inode->i_fop = &btrfs_file_operations;
3882 inode->i_op = &btrfs_file_inode_operations;
3883 break;
3884 case S_IFDIR:
3885 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003886 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 break;
3888 case S_IFLNK:
3889 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003890 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003891 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003893 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003894 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003895 init_special_inode(inode, inode->i_mode, rdev);
3896 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003897 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003898
David Sterba7b6a2212018-03-26 18:40:21 +02003899 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003900 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003901}
3902
Chris Masond352ac62008-09-29 15:18:18 -04003903/*
3904 * given a leaf and an inode, copy the inode fields into the leaf
3905 */
Chris Masone02119d2008-09-05 16:13:11 -04003906static void fill_inode_item(struct btrfs_trans_handle *trans,
3907 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003908 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003909 struct inode *inode)
3910{
Liu Bo51fab692012-12-27 09:01:21 +00003911 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07003912 u64 flags;
Chris Mason5f39d392007-10-15 16:14:19 -04003913
David Sterbac82f8232019-08-09 17:48:21 +02003914 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003915
David Sterbacc4c13d2020-04-29 02:15:56 +02003916 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3917 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3918 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3919 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3920 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003921
David Sterbacc4c13d2020-04-29 02:15:56 +02003922 btrfs_set_token_timespec_sec(&token, &item->atime,
3923 inode->i_atime.tv_sec);
3924 btrfs_set_token_timespec_nsec(&token, &item->atime,
3925 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003926
David Sterbacc4c13d2020-04-29 02:15:56 +02003927 btrfs_set_token_timespec_sec(&token, &item->mtime,
3928 inode->i_mtime.tv_sec);
3929 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3930 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003931
David Sterbacc4c13d2020-04-29 02:15:56 +02003932 btrfs_set_token_timespec_sec(&token, &item->ctime,
3933 inode->i_ctime.tv_sec);
3934 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3935 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003936
David Sterbacc4c13d2020-04-29 02:15:56 +02003937 btrfs_set_token_timespec_sec(&token, &item->otime,
3938 BTRFS_I(inode)->i_otime.tv_sec);
3939 btrfs_set_token_timespec_nsec(&token, &item->otime,
3940 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303941
David Sterbacc4c13d2020-04-29 02:15:56 +02003942 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3943 btrfs_set_token_inode_generation(&token, item,
3944 BTRFS_I(inode)->generation);
3945 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3946 btrfs_set_token_inode_transid(&token, item, trans->transid);
3947 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07003948 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3949 BTRFS_I(inode)->ro_flags);
3950 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02003951 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003952}
3953
Chris Masond352ac62008-09-29 15:18:18 -04003954/*
3955 * copy everything in the in-memory inode into the btree.
3956 */
Chris Mason21151332011-11-10 20:39:08 -05003957static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003958 struct btrfs_root *root,
3959 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003960{
3961 struct btrfs_inode_item *inode_item;
3962 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003964 int ret;
3965
3966 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003967 if (!path)
3968 return -ENOMEM;
3969
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003970 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003971 if (ret) {
3972 if (ret > 0)
3973 ret = -ENOENT;
3974 goto failed;
3975 }
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 leaf = path->nodes[0];
3978 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003979 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003980
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003981 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003982 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003983 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003984 ret = 0;
3985failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003986 btrfs_free_path(path);
3987 return ret;
3988}
3989
Chris Masond352ac62008-09-29 15:18:18 -04003990/*
Chris Mason21151332011-11-10 20:39:08 -05003991 * copy everything in the in-memory inode into the btree.
3992 */
3993noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003994 struct btrfs_root *root,
3995 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003996{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003997 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003998 int ret;
3999
4000 /*
4001 * If the inode is a free space inode, we can deadlock during commit
4002 * if we put it into the delayed code.
4003 *
4004 * The data relocation inode should also be directly updated
4005 * without delay
4006 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004007 if (!btrfs_is_free_space_inode(inode)
Johannes Thumshirn37f00a62021-09-09 01:19:25 +09004008 && !btrfs_is_data_reloc_root(root)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004009 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004010 btrfs_update_root_times(trans, root);
4011
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004012 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004013 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004014 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05004015 return ret;
4016 }
4017
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004018 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004019}
4020
Nikolay Borisov729f7962020-11-02 16:49:06 +02004021int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4022 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004023{
4024 int ret;
4025
Nikolay Borisov729f7962020-11-02 16:49:06 +02004026 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004027 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02004028 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004029 return ret;
4030}
4031
4032/*
Chris Masond352ac62008-09-29 15:18:18 -04004033 * unlink helper that gets used here in inode.c and in the tree logging
4034 * recovery code. It remove a link in a directory with a given name, and
4035 * also drops the back refs in the inode to the directory
4036 */
Al Viro92986792011-03-04 17:14:37 +00004037static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4038 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004039 struct btrfs_inode *dir,
4040 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004041 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004042{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004045 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004047 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004048 u64 ino = btrfs_ino(inode);
4049 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004050
4051 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004052 if (!path) {
4053 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004054 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004055 }
4056
Li Zefan33345d012011-04-20 10:31:50 +08004057 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004058 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004059 if (IS_ERR_OR_NULL(di)) {
4060 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004061 goto err;
4062 }
Chris Mason39279cc2007-06-12 06:35:45 -04004063 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004064 if (ret)
4065 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004066 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004067
Miao Xie67de1172013-12-26 13:07:06 +08004068 /*
4069 * If we don't have dir index, we have to get it by looking up
4070 * the inode ref, since we get the inode ref, remove it directly,
4071 * it is unnecessary to do delayed deletion.
4072 *
4073 * But if we have dir index, needn't search inode ref to get it.
4074 * Since the inode ref is close to the inode item, it is better
4075 * that we delay to delete it, and just do this deletion when
4076 * we update the inode item.
4077 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004078 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004079 ret = btrfs_delayed_delete_inode_ref(inode);
4080 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004081 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004082 goto skip_backref;
4083 }
4084 }
4085
Li Zefan33345d012011-04-20 10:31:50 +08004086 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4087 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004088 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004090 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004091 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004092 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004093 goto err;
4094 }
Miao Xie67de1172013-12-26 13:07:06 +08004095skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004096 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004098 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004099 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 }
Chris Mason39279cc2007-06-12 06:35:45 -04004101
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004102 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4103 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 goto err;
4107 }
Chris Masone02119d2008-09-05 16:13:11 -04004108
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004109 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4110 index);
Chris Mason6418c962010-10-30 07:34:24 -04004111 if (ret == -ENOENT)
4112 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004113 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004114 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004115
4116 /*
4117 * If we have a pending delayed iput we could end up with the final iput
4118 * being run in btrfs-cleaner context. If we have enough of these built
4119 * up we can end up burning a lot of time in btrfs-cleaner without any
4120 * way to throttle the unlinks. Since we're currently holding a ref on
4121 * the inode we can run the delayed iput here without any issues as the
4122 * final iput won't be done until after we drop the ref we're currently
4123 * holding.
4124 */
4125 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004126err:
4127 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004128 if (ret)
4129 goto out;
4130
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004131 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 inode_inc_iversion(&inode->vfs_inode);
4133 inode_inc_iversion(&dir->vfs_inode);
4134 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4135 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004136 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004137out:
Chris Mason39279cc2007-06-12 06:35:45 -04004138 return ret;
4139}
4140
Al Viro92986792011-03-04 17:14:37 +00004141int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4142 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004143 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004144 const char *name, int name_len)
4145{
4146 int ret;
4147 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4148 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004149 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004150 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004151 }
4152 return ret;
4153}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154
4155/*
4156 * helper to start transaction for unlink and rmdir.
4157 *
Josef Bacikd52be812013-05-29 14:54:47 -04004158 * unlink and rmdir are special in btrfs, they do not always free space, so
4159 * if we cannot make our reservations the normal way try and see if there is
4160 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4161 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162 */
Josef Bacikd52be812013-05-29 14:54:47 -04004163static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166
Josef Bacike70bea52011-10-11 14:18:24 -04004167 /*
4168 * 1 for the possible orphan item
4169 * 1 for the dir item
4170 * 1 for the dir index
4171 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004172 * 1 for the inode
4173 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004174 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175}
4176
Chris Mason39279cc2007-06-12 06:35:45 -04004177static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4178{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004180 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004181 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004182 int ret;
4183
Josef Bacikd52be812013-05-29 14:54:47 -04004184 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004185 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004186 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004187
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004188 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4189 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004190
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004191 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4192 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4193 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004194 if (ret)
4195 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004196
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004198 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004199 if (ret)
4200 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201 }
Josef Bacik7b128762008-07-24 12:17:14 -04004202
Tsutomu Itohb5324022011-07-19 07:27:20 +00004203out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004204 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004205 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004206 return ret;
4207}
4208
Misono Tomohirof60a2362018-04-18 11:34:52 +09004209static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004210 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004212 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004213 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 struct btrfs_path *path;
4215 struct extent_buffer *leaf;
4216 struct btrfs_dir_item *di;
4217 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004218 const char *name = dentry->d_name.name;
4219 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004220 u64 index;
4221 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004222 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004223 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224
Josef Bacik045d3962019-12-18 17:20:27 -05004225 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4226 objectid = inode->root->root_key.objectid;
4227 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4228 objectid = inode->location.objectid;
4229 } else {
4230 WARN_ON(1);
4231 return -EINVAL;
4232 }
4233
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234 path = btrfs_alloc_path();
4235 if (!path)
4236 return -ENOMEM;
4237
Li Zefan33345d012011-04-20 10:31:50 +08004238 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004239 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004241 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 goto out;
4243 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244
4245 leaf = path->nodes[0];
4246 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4247 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4248 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004250 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004251 goto out;
4252 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004253 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004254
Josef Bacikd49d3282019-12-18 17:20:28 -05004255 /*
4256 * This is a placeholder inode for a subvolume we didn't have a
4257 * reference to at the time of the snapshot creation. In the meantime
4258 * we could have renamed the real subvol link into our snapshot, so
David Sterba1a9fd412021-05-21 17:42:23 +02004259 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
Josef Bacikd49d3282019-12-18 17:20:28 -05004260 * Instead simply lookup the dir_index_item for this entry so we can
4261 * remove it. Otherwise we know we have a ref to the root and we can
4262 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4263 */
4264 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004265 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004266 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267 if (IS_ERR_OR_NULL(di)) {
4268 if (!di)
4269 ret = -ENOENT;
4270 else
4271 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004272 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 goto out;
4274 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275
4276 leaf = path->nodes[0];
4277 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004279 btrfs_release_path(path);
4280 } else {
4281 ret = btrfs_del_root_ref(trans, objectid,
4282 root->root_key.objectid, dir_ino,
4283 &index, name, name_len);
4284 if (ret) {
4285 btrfs_abort_transaction(trans, ret);
4286 goto out;
4287 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289
Lu Fengqi9add2942018-08-01 11:32:26 +08004290 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004291 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004292 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004293 goto out;
4294 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004295
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004296 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004297 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004298 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004299 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004301 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004302out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004303 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004305}
4306
Misono Tomohiroec42f162018-04-18 11:34:13 +09004307/*
4308 * Helper to check if the subvolume references other subvolumes or if it's
4309 * default.
4310 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004311static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004312{
4313 struct btrfs_fs_info *fs_info = root->fs_info;
4314 struct btrfs_path *path;
4315 struct btrfs_dir_item *di;
4316 struct btrfs_key key;
4317 u64 dir_id;
4318 int ret;
4319
4320 path = btrfs_alloc_path();
4321 if (!path)
4322 return -ENOMEM;
4323
4324 /* Make sure this root isn't set as the default subvol */
4325 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4326 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4327 dir_id, "default", 7, 0);
4328 if (di && !IS_ERR(di)) {
4329 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4330 if (key.objectid == root->root_key.objectid) {
4331 ret = -EPERM;
4332 btrfs_err(fs_info,
4333 "deleting default subvolume %llu is not allowed",
4334 key.objectid);
4335 goto out;
4336 }
4337 btrfs_release_path(path);
4338 }
4339
4340 key.objectid = root->root_key.objectid;
4341 key.type = BTRFS_ROOT_REF_KEY;
4342 key.offset = (u64)-1;
4343
4344 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4345 if (ret < 0)
4346 goto out;
4347 BUG_ON(ret == 0);
4348
4349 ret = 0;
4350 if (path->slots[0] > 0) {
4351 path->slots[0]--;
4352 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4353 if (key.objectid == root->root_key.objectid &&
4354 key.type == BTRFS_ROOT_REF_KEY)
4355 ret = -ENOTEMPTY;
4356 }
4357out:
4358 btrfs_free_path(path);
4359 return ret;
4360}
4361
Nikolay Borisov20a68002018-04-27 14:36:24 +03004362/* Delete all dentries for inodes belonging to the root */
4363static void btrfs_prune_dentries(struct btrfs_root *root)
4364{
4365 struct btrfs_fs_info *fs_info = root->fs_info;
4366 struct rb_node *node;
4367 struct rb_node *prev;
4368 struct btrfs_inode *entry;
4369 struct inode *inode;
4370 u64 objectid = 0;
4371
4372 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4373 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4374
4375 spin_lock(&root->inode_lock);
4376again:
4377 node = root->inode_tree.rb_node;
4378 prev = NULL;
4379 while (node) {
4380 prev = node;
4381 entry = rb_entry(node, struct btrfs_inode, rb_node);
4382
David Sterba37508512018-06-29 10:56:40 +02004383 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004384 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004385 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004386 node = node->rb_right;
4387 else
4388 break;
4389 }
4390 if (!node) {
4391 while (prev) {
4392 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004393 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004394 node = prev;
4395 break;
4396 }
4397 prev = rb_next(prev);
4398 }
4399 }
4400 while (node) {
4401 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004402 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004403 inode = igrab(&entry->vfs_inode);
4404 if (inode) {
4405 spin_unlock(&root->inode_lock);
4406 if (atomic_read(&inode->i_count) > 1)
4407 d_prune_aliases(inode);
4408 /*
4409 * btrfs_drop_inode will have it removed from the inode
4410 * cache when its usage count hits zero.
4411 */
4412 iput(inode);
4413 cond_resched();
4414 spin_lock(&root->inode_lock);
4415 goto again;
4416 }
4417
4418 if (cond_resched_lock(&root->inode_lock))
4419 goto again;
4420
4421 node = rb_next(node);
4422 }
4423 spin_unlock(&root->inode_lock);
4424}
4425
Misono Tomohirof60a2362018-04-18 11:34:52 +09004426int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4427{
4428 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4429 struct btrfs_root *root = BTRFS_I(dir)->root;
4430 struct inode *inode = d_inode(dentry);
4431 struct btrfs_root *dest = BTRFS_I(inode)->root;
4432 struct btrfs_trans_handle *trans;
4433 struct btrfs_block_rsv block_rsv;
4434 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004435 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004436
4437 /*
4438 * Don't allow to delete a subvolume with send in progress. This is
4439 * inside the inode lock so the error handling that has to drop the bit
4440 * again is not run concurrently.
4441 */
4442 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004443 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004444 spin_unlock(&dest->root_item_lock);
4445 btrfs_warn(fs_info,
4446 "attempt to delete subvolume %llu during send",
4447 dest->root_key.objectid);
4448 return -EPERM;
4449 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004450 root_flags = btrfs_root_flags(&dest->root_item);
4451 btrfs_set_root_flags(&dest->root_item,
4452 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4453 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004454
4455 down_write(&fs_info->subvol_sem);
4456
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004457 ret = may_destroy_subvol(dest);
4458 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004459 goto out_up_write;
4460
4461 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4462 /*
4463 * One for dir inode,
4464 * two for dir entries,
4465 * two for root ref/backref.
4466 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004467 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4468 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004469 goto out_up_write;
4470
4471 trans = btrfs_start_transaction(root, 0);
4472 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004473 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004474 goto out_release;
4475 }
4476 trans->block_rsv = &block_rsv;
4477 trans->bytes_reserved = block_rsv.size;
4478
4479 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4480
Josef Bacik045d3962019-12-18 17:20:27 -05004481 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004482 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004483 btrfs_abort_transaction(trans, ret);
4484 goto out_end_trans;
4485 }
4486
Josef Bacik2731f512021-03-12 15:25:04 -05004487 ret = btrfs_record_root_in_trans(trans, dest);
4488 if (ret) {
4489 btrfs_abort_transaction(trans, ret);
4490 goto out_end_trans;
4491 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004492
4493 memset(&dest->root_item.drop_progress, 0,
4494 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004495 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004496 btrfs_set_root_refs(&dest->root_item, 0);
4497
4498 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4499 ret = btrfs_insert_orphan_item(trans,
4500 fs_info->tree_root,
4501 dest->root_key.objectid);
4502 if (ret) {
4503 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004504 goto out_end_trans;
4505 }
4506 }
4507
Lu Fengqid1957792018-05-29 15:01:54 +08004508 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004509 BTRFS_UUID_KEY_SUBVOL,
4510 dest->root_key.objectid);
4511 if (ret && ret != -ENOENT) {
4512 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004513 goto out_end_trans;
4514 }
4515 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004516 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004517 dest->root_item.received_uuid,
4518 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4519 dest->root_key.objectid);
4520 if (ret && ret != -ENOENT) {
4521 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004522 goto out_end_trans;
4523 }
4524 }
4525
Qu Wenruo082b6c92020-06-16 10:17:37 +08004526 free_anon_bdev(dest->anon_dev);
4527 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004528out_end_trans:
4529 trans->block_rsv = NULL;
4530 trans->bytes_reserved = 0;
4531 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004532 inode->i_flags |= S_DEAD;
4533out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004534 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004535out_up_write:
4536 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004537 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004538 spin_lock(&dest->root_item_lock);
4539 root_flags = btrfs_root_flags(&dest->root_item);
4540 btrfs_set_root_flags(&dest->root_item,
4541 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4542 spin_unlock(&dest->root_item_lock);
4543 } else {
4544 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004545 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004546 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004547 }
4548
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004549 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004550}
4551
Chris Mason39279cc2007-06-12 06:35:45 -04004552static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4553{
David Howells2b0143b2015-03-17 22:25:59 +00004554 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004555 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004556 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004557 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004558 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004559
David Sterbab3ae2442012-09-13 16:04:34 -06004560 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004561 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004562 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004563 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004564
Josef Bacikd52be812013-05-29 14:54:47 -04004565 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004566 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004567 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004568
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004569 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004570 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004571 goto out;
4572 }
4573
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004574 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004575 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004576 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004577
Filipe Manana44f714d2016-06-06 16:11:13 +01004578 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4579
Chris Mason39279cc2007-06-12 06:35:45 -04004580 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004581 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4582 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4583 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004584 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004585 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004586 /*
4587 * Propagate the last_unlink_trans value of the deleted dir to
4588 * its parent directory. This is to prevent an unrecoverable
4589 * log tree in the case we do something like this:
4590 * 1) create dir foo
4591 * 2) create snapshot under dir foo
4592 * 3) delete the snapshot
4593 * 4) rmdir foo
4594 * 5) mkdir foo
4595 * 6) fsync foo or some file inside foo
4596 */
4597 if (last_unlink_trans >= trans->transid)
4598 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4599 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004600out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004601 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004602 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004603
Chris Mason39279cc2007-06-12 06:35:45 -04004604 return err;
4605}
4606
Josef Bacikddfae632017-10-19 14:16:02 -04004607/*
4608 * Return this if we need to call truncate_block for the last bit of the
4609 * truncate.
4610 */
4611#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004612
Chris Mason323ac952008-10-01 19:05:46 -04004613/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004614 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004615 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004616 * @trans: A transaction handle.
4617 * @root: The root from which to remove items.
4618 * @inode: The inode whose items we want to remove.
4619 * @new_size: The new i_size for the inode. This is only applicable when
4620 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4621 * @min_type: The minimum key type to remove. All keys with a type
4622 * greater than this value are removed and all keys with
4623 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004624 * @extents_found: Output parameter that will contain the number of file
4625 * extent items that were removed or adjusted to the new
4626 * inode i_size. The caller is responsible for initializing
4627 * the counter. Also, it can be NULL if the caller does not
4628 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004629 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004630 * Remove all keys associated with the inode from the given root that have a key
4631 * with a type greater than or equals to @min_type. When @min_type has a value of
4632 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4633 * greater than or equals to @new_size. If a file extent item that starts before
4634 * @new_size and ends after it is found, its length is adjusted.
4635 *
4636 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4637 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004638 */
Yan, Zheng80825102009-11-12 09:35:36 +00004639int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4640 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004641 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004642 u64 new_size, u32 min_type,
4643 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004644{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004645 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004646 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004647 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004648 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004649 struct btrfs_key key;
4650 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004651 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004652 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004653 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004654 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004655 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004656 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004657 int found_extent;
4658 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004659 int pending_del_nr = 0;
4660 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004661 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004662 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004663 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004664 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004665 bool be_nice = false;
4666 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004667 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4668 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004669
4670 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004671
Chris Mason28ed1342014-12-17 09:41:04 -08004672 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004673 * For non-free space inodes and non-shareable roots, we want to back
4674 * off from time to time. This means all inodes in subvolume roots,
4675 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004676 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004677 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004678 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004679 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004680
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004681 path = btrfs_alloc_path();
4682 if (!path)
4683 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004684 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004685
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004686 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004687 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004688 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004689
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004690 /*
4691 * We want to drop from the next block forward in case this
4692 * new size is not block aligned since we will be keeping the
4693 * last block of the extent just the way it is.
4694 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004695 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004696 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004697 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004698 }
Yan, Zheng80825102009-11-12 09:35:36 +00004699
Miao Xie16cdcec2011-04-22 18:12:22 +08004700 /*
4701 * This function is also used to drop the items in the log tree before
4702 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004703 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004704 * items.
4705 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004706 if (min_type == 0 && root == inode->root)
4707 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004708
Li Zefan33345d012011-04-20 10:31:50 +08004709 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004710 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004711 key.type = (u8)-1;
4712
Chris Mason85e21ba2008-01-29 15:11:36 -05004713search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004714 /*
4715 * with a 16K leaf size and 128MB extents, you can actually queue
4716 * up a huge file in a single leaf. Most of the time that
4717 * bytes_deleted is > 0, it will be huge by the time we get here
4718 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004719 if (be_nice && bytes_deleted > SZ_32M &&
4720 btrfs_should_end_transaction(trans)) {
4721 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004722 goto out;
4723 }
Chris Masond3977122009-01-05 21:25:51 -05004724
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004725 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4726 if (ret < 0)
4727 goto out;
4728
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004730 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004731 /* there are no items in the tree for us to truncate, we're
4732 * done
4733 */
Yan, Zheng80825102009-11-12 09:35:36 +00004734 if (path->slots[0] == 0)
4735 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004736 path->slots[0]--;
4737 }
4738
Chris Masond3977122009-01-05 21:25:51 -05004739 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004740 u64 clear_start = 0, clear_len = 0;
4741
Chris Mason39279cc2007-06-12 06:35:45 -04004742 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004743 leaf = path->nodes[0];
4744 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004745 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004746
Li Zefan33345d012011-04-20 10:31:50 +08004747 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004748 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004749
Chris Mason85e21ba2008-01-29 15:11:36 -05004750 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004751 break;
4752
Chris Mason5f39d392007-10-15 16:14:19 -04004753 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004754 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004755 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004756 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004757 extent_type = btrfs_file_extent_type(leaf, fi);
4758 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004759 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004760 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004761
4762 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004763 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004764 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004765 item_end += btrfs_file_extent_ram_bytes(leaf,
4766 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004767
4768 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004769 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004770 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004771 }
Yan008630c2007-11-07 13:31:09 -05004772 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004773 }
Yan, Zheng80825102009-11-12 09:35:36 +00004774 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004775 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004776 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004777 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004778 break;
4779 if (found_key.offset >= new_size)
4780 del_item = 1;
4781 else
4782 del_item = 0;
4783 }
Chris Mason39279cc2007-06-12 06:35:45 -04004784 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004785 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004786 if (found_type != BTRFS_EXTENT_DATA_KEY)
4787 goto delete;
4788
Filipe Manana0d7d3162021-05-24 11:35:55 +01004789 if (extents_found != NULL)
4790 (*extents_found)++;
4791
Chris Mason179e29e2007-11-01 11:28:41 -04004792 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004793 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004794
4795 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004796 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004797 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004798 u64 orig_num_bytes =
4799 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004800 extent_num_bytes = ALIGN(new_size -
4801 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004802 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004803 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004804 btrfs_set_file_extent_num_bytes(leaf, fi,
4805 extent_num_bytes);
4806 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004807 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004808 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004809 &root->state) &&
4810 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004811 inode_sub_bytes(&inode->vfs_inode,
4812 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004813 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004814 } else {
Chris Masondb945352007-10-15 16:15:53 -04004815 extent_num_bytes =
4816 btrfs_file_extent_disk_num_bytes(leaf,
4817 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004818 extent_offset = found_key.offset -
4819 btrfs_file_extent_offset(leaf, fi);
4820
Chris Mason39279cc2007-06-12 06:35:45 -04004821 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004822 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004823 if (extent_start != 0) {
4824 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004825 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004826 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004827 inode_sub_bytes(&inode->vfs_inode,
4828 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004829 }
4830 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004831 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004832 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004833 /*
4834 * we can't truncate inline items that have had
4835 * special encodings
4836 */
4837 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004838 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004839 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4840 btrfs_file_extent_compression(leaf, fi) == 0) {
4841 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004842
Josef Bacikddfae632017-10-19 14:16:02 -04004843 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4844 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004845 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004846 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004847 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004848 * We have to bail so the last_size is set to
4849 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004850 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004851 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004852 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004853 } else {
4854 /*
4855 * Inline extents are special, we just treat
4856 * them as a full sector worth in the file
4857 * extent tree just for simplicity sake.
4858 */
4859 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004860 }
Josef Bacikddfae632017-10-19 14:16:02 -04004861
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004862 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004863 inode_sub_bytes(&inode->vfs_inode,
4864 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004865 }
Chris Mason179e29e2007-11-01 11:28:41 -04004866delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004867 /*
4868 * We use btrfs_truncate_inode_items() to clean up log trees for
4869 * multiple fsyncs, and in this case we don't want to clear the
4870 * file extent range because it's just the log.
4871 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004872 if (root == inode->root) {
4873 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004874 clear_start, clear_len);
4875 if (ret) {
4876 btrfs_abort_transaction(trans, ret);
4877 break;
4878 }
4879 }
4880
Josef Bacikddfae632017-10-19 14:16:02 -04004881 if (del_item)
4882 last_size = found_key.offset;
4883 else
4884 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004885 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004886 if (!pending_del_nr) {
4887 /* no pending yet, add ourselves */
4888 pending_del_slot = path->slots[0];
4889 pending_del_nr = 1;
4890 } else if (pending_del_nr &&
4891 path->slots[0] + 1 == pending_del_slot) {
4892 /* hop on the pending chunk */
4893 pending_del_nr++;
4894 pending_del_slot = path->slots[0];
4895 } else {
Chris Masond3977122009-01-05 21:25:51 -05004896 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004897 }
Chris Mason39279cc2007-06-12 06:35:45 -04004898 } else {
4899 break;
4900 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004901 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004902
Miao Xie27cdeb72014-04-02 19:51:05 +08004903 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004904 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004905 struct btrfs_ref ref = { 0 };
4906
Chris Mason28ed1342014-12-17 09:41:04 -08004907 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004908
4909 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4910 extent_start, extent_num_bytes, 0);
4911 ref.real_root = root->root_key.objectid;
4912 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4913 ino, extent_offset);
4914 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004915 if (ret) {
4916 btrfs_abort_transaction(trans, ret);
4917 break;
4918 }
Chris Mason28f75a02015-02-04 06:59:29 -08004919 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004920 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004921 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004922 }
Chris Mason39279cc2007-06-12 06:35:45 -04004923 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004924
Yan, Zheng80825102009-11-12 09:35:36 +00004925 if (found_type == BTRFS_INODE_ITEM_KEY)
4926 break;
4927
4928 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004929 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004930 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004931 if (pending_del_nr) {
4932 ret = btrfs_del_items(trans, root, path,
4933 pending_del_slot,
4934 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004935 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004936 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004937 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004938 }
Yan, Zheng80825102009-11-12 09:35:36 +00004939 pending_del_nr = 0;
4940 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004941 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004942
Chris Mason28f75a02015-02-04 06:59:29 -08004943 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004944 * We can generate a lot of delayed refs, so we need to
4945 * throttle every once and a while and make sure we're
4946 * adding enough space to keep up with the work we are
4947 * generating. Since we hold a transaction here we
4948 * can't flush, and we don't want to FLUSH_LIMIT because
4949 * we could have generated too many delayed refs to
4950 * actually allocate, so just bail if we're short and
4951 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004952 */
Josef Bacik28bad212018-12-03 10:20:38 -05004953 if (should_throttle) {
4954 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4955 BTRFS_RESERVE_NO_FLUSH);
4956 if (ret) {
4957 ret = -EAGAIN;
4958 break;
4959 }
Chris Mason28f75a02015-02-04 06:59:29 -08004960 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004961 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004962 } else {
4963 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004964 }
Chris Mason39279cc2007-06-12 06:35:45 -04004965 }
Yan, Zheng80825102009-11-12 09:35:36 +00004966out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004967 if (ret >= 0 && pending_del_nr) {
4968 int err;
4969
4970 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004971 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004972 if (err) {
4973 btrfs_abort_transaction(trans, err);
4974 ret = err;
4975 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004976 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004977 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4978 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004979 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004980 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004981 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4982 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4983 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004984 }
Chris Mason28ed1342014-12-17 09:41:04 -08004985
Chris Mason39279cc2007-06-12 06:35:45 -04004986 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004987 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004988}
4989
Chris Masona52d9a82007-08-27 16:49:44 -04004990/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304991 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004992 * @inode - inode that we're zeroing
4993 * @from - the offset to start zeroing
4994 * @len - the length to zero, 0 to zero the entire range respective to the
4995 * offset
4996 * @front - zero up to the offset instead of from the offset on
4997 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304998 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004999 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04005000 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005001int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
5002 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04005003{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005004 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5005 struct address_space *mapping = inode->vfs_inode.i_mapping;
5006 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005007 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005008 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08005009 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005010 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005012 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305013 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04005014 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005015 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005016 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04005017 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305018 u64 block_start;
5019 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04005020
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02005021 if (IS_ALIGNED(offset, blocksize) &&
5022 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04005023 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305024
Josef Bacik8b62f872017-10-19 14:15:55 -04005025 block_start = round_down(from, blocksize);
5026 block_end = block_start + blocksize - 1;
5027
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005028 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5029 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005030 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005031 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005032 /* For nocow case, no need to reserve data space */
5033 only_release_metadata = true;
5034 } else {
5035 goto out;
5036 }
5037 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005038 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005039 if (ret < 0) {
5040 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005041 btrfs_free_reserved_data_space(inode, data_reserved,
5042 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005043 goto out;
5044 }
Chris Mason211c17f2008-05-15 09:13:45 -04005045again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005046 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005047 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005048 btrfs_delalloc_release_space(inode, data_reserved, block_start,
5049 blocksize, true);
5050 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005051 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005052 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005053 }
Qu Wenruo32443de2021-01-26 16:34:00 +08005054 ret = set_page_extent_mapped(page);
5055 if (ret < 0)
5056 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005057
Chris Masona52d9a82007-08-27 16:49:44 -04005058 if (!PageUptodate(page)) {
5059 ret = btrfs_readpage(NULL, page);
5060 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005061 if (page->mapping != mapping) {
5062 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005063 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005064 goto again;
5065 }
Chris Masona52d9a82007-08-27 16:49:44 -04005066 if (!PageUptodate(page)) {
5067 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005068 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005069 }
5070 }
Chris Mason211c17f2008-05-15 09:13:45 -04005071 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005072
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305073 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005074
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005075 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005076 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305077 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005078 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005079 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005080 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03005081 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005082 btrfs_put_ordered_extent(ordered);
5083 goto again;
5084 }
5085
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005086 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005087 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5088 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005089
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005090 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005091 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005092 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305093 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005094 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005095 goto out_unlock;
5096 }
5097
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305098 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005099 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305100 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04005101 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07005102 memzero_page(page, (block_start - page_offset(page)),
5103 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005104 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07005105 memzero_page(page, (block_start - page_offset(page)) + offset,
5106 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005107 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005108 }
Chris Mason247e7432008-07-17 12:53:51 -04005109 ClearPageChecked(page);
Qu Wenruo6c9ac8b2021-05-31 16:50:51 +08005110 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
David Sterbae43bbe52017-12-12 21:43:52 +01005111 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005112
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005113 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005114 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02005115 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005116
Chris Mason89642222008-07-24 09:41:53 -04005117out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005118 if (ret) {
5119 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005120 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005121 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005122 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005123 block_start, blocksize, true);
5124 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005125 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005126 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005127 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005128out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005129 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005130 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08005131 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005132 return ret;
5133}
5134
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005135static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04005136 u64 offset, u64 len)
5137{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005138 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04005139 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00005140 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04005141 int ret;
5142
5143 /*
Filipe Mananacceaa892021-07-20 16:03:40 +01005144 * If NO_HOLES is enabled, we don't need to do anything.
5145 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5146 * or btrfs_update_inode() will be called, which guarantee that the next
5147 * fsync will know this inode was changed and needs to be logged.
Josef Bacik16e75492013-10-22 12:18:51 -04005148 */
Filipe Mananacceaa892021-07-20 16:03:40 +01005149 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04005150 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005151
5152 /*
5153 * 1 - for the one we're dropping
5154 * 1 - for the one we're adding
5155 * 1 - for updating the inode.
5156 */
5157 trans = btrfs_start_transaction(root, 3);
5158 if (IS_ERR(trans))
5159 return PTR_ERR(trans);
5160
Filipe Manana5893dfb2020-11-04 11:07:32 +00005161 drop_args.start = offset;
5162 drop_args.end = offset + len;
5163 drop_args.drop_cache = true;
5164
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005165 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005166 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005167 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005168 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005169 return ret;
5170 }
5171
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005172 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005173 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005174 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005175 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005176 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005177 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5178 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005179 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005180 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005181 return ret;
5182}
5183
Josef Bacik695a0d02011-03-04 15:46:53 -05005184/*
5185 * This function puts in dummy file extents for the area we're creating a hole
5186 * for. So if we are truncating this file to a larger size we need to insert
5187 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5188 * the range between oldsize and size
5189 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005190int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005191{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005192 struct btrfs_root *root = inode->root;
5193 struct btrfs_fs_info *fs_info = root->fs_info;
5194 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005195 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005196 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005197 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005198 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5199 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005200 u64 last_byte;
5201 u64 cur_offset;
5202 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005203 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005204
Josef Bacika71754f2013-06-17 17:14:39 -04005205 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305206 * If our size started in the middle of a block we need to zero out the
5207 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005208 * expose stale data.
5209 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005210 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005211 if (err)
5212 return err;
5213
Yan Zheng9036c102008-10-30 14:19:41 -04005214 if (size <= hole_start)
5215 return 0;
5216
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005217 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5218 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005219 cur_offset = hole_start;
5220 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005221 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005222 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005223 if (IS_ERR(em)) {
5224 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005225 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005226 break;
5227 }
Yan Zheng9036c102008-10-30 14:19:41 -04005228 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005229 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005230 hole_size = last_byte - cur_offset;
5231
Yan, Zheng80825102009-11-12 09:35:36 +00005232 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005233 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005234
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005235 err = maybe_insert_hole(root, inode, cur_offset,
5236 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005237 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005238 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005239
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005240 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005241 cur_offset, hole_size);
5242 if (err)
5243 break;
5244
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005245 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005246 cur_offset + hole_size - 1, 0);
5247 hole_em = alloc_extent_map();
5248 if (!hole_em) {
5249 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005250 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005251 goto next;
5252 }
5253 hole_em->start = cur_offset;
5254 hole_em->len = hole_size;
5255 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005256
Josef Bacik5dc562c2012-08-17 13:14:17 -04005257 hole_em->block_start = EXTENT_MAP_HOLE;
5258 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005259 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005260 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005261 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005262 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005263
5264 while (1) {
5265 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005266 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005267 write_unlock(&em_tree->lock);
5268 if (err != -EEXIST)
5269 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005270 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005271 cur_offset +
5272 hole_size - 1, 0);
5273 }
5274 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005275 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005276 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005277 cur_offset, hole_size);
5278 if (err)
5279 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005280 }
Josef Bacik16e75492013-10-22 12:18:51 -04005281next:
Yan Zheng9036c102008-10-30 14:19:41 -04005282 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005283 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005284 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005285 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005286 break;
5287 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005288 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005289 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005290 return err;
5291}
5292
Eric Sandeen3972f262013-01-12 02:57:22 +00005293static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005294{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005295 struct btrfs_root *root = BTRFS_I(inode)->root;
5296 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005297 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005298 loff_t newsize = attr->ia_size;
5299 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005300 int ret;
5301
Eric Sandeen3972f262013-01-12 02:57:22 +00005302 /*
5303 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5304 * special case where we need to update the times despite not having
5305 * these flags set. For all other operations the VFS set these flags
5306 * explicitly if it wants a timestamp update.
5307 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005308 if (newsize != oldsize) {
5309 inode_inc_iversion(inode);
5310 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5311 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005312 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005313 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005314
Josef Bacika41ad392011-01-31 15:30:16 -05005315 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005316 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005317 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005318 * This is to ensure the snapshot captures a fully consistent
5319 * state of this file - if the snapshot captures this expanding
5320 * truncation, it must capture all writes that happened before
5321 * this truncation.
5322 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005323 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005324 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005325 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005326 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005327 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005328 }
Yan, Zheng80825102009-11-12 09:35:36 +00005329
Miao Xief4a2f4c2011-12-14 20:12:01 -05005330 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005331 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005332 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005333 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005334 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005335
5336 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005337 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305338 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005339 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005340 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005341 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005342 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005343 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5344
5345 if (btrfs_is_zoned(fs_info)) {
5346 ret = btrfs_wait_ordered_range(inode,
5347 ALIGN(newsize, fs_info->sectorsize),
5348 (u64)-1);
5349 if (ret)
5350 return ret;
5351 }
Yan, Zheng80825102009-11-12 09:35:36 +00005352
Josef Bacika41ad392011-01-31 15:30:16 -05005353 /*
5354 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005355 * zero. Make sure any new writes to the file get on disk
5356 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005357 */
5358 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005359 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005360 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005361
Josef Bacika41ad392011-01-31 15:30:16 -05005362 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005363
Miao Xie2e60a512013-02-08 07:01:08 +00005364 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005365
Filipe Manana213e8c52018-02-06 20:40:31 +00005366 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005367 if (ret && inode->i_nlink) {
5368 int err;
5369
5370 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005371 * Truncate failed, so fix up the in-memory size. We
5372 * adjusted disk_i_size down as we removed extents, so
5373 * wait for disk_i_size to be stable and then update the
5374 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005375 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005376 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005377 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005378 return err;
5379 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005380 }
Yan, Zheng80825102009-11-12 09:35:36 +00005381 }
5382
Josef Bacika41ad392011-01-31 15:30:16 -05005383 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005384}
5385
Christian Brauner549c7292021-01-21 14:19:43 +01005386static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5387 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005388{
David Howells2b0143b2015-03-17 22:25:59 +00005389 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005390 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005391 int err;
5392
Li Zefanb83cc962010-12-20 16:04:08 +08005393 if (btrfs_root_readonly(root))
5394 return -EROFS;
5395
Christian Braunerd4d09462021-07-27 12:48:49 +02005396 err = setattr_prepare(mnt_userns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005397 if (err)
5398 return err;
5399
Chris Mason5a3f23d2009-03-31 13:27:11 -04005400 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005401 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005402 if (err)
5403 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005404 }
Yan Zheng9036c102008-10-30 14:19:41 -04005405
Christoph Hellwig10257742010-06-04 11:30:02 +02005406 if (attr->ia_valid) {
Christian Braunerd4d09462021-07-27 12:48:49 +02005407 setattr_copy(mnt_userns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005408 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005409 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005410
Josef Bacik22c44fe2011-11-30 10:45:38 -05005411 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunerd4d09462021-07-27 12:48:49 +02005412 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005413 }
5414
Chris Mason39279cc2007-06-12 06:35:45 -04005415 return err;
5416}
Chris Mason61295eb2008-01-14 16:24:38 -05005417
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005418/*
5419 * While truncating the inode pages during eviction, we get the VFS calling
5420 * btrfs_invalidatepage() against each page of the inode. This is slow because
5421 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5422 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5423 * extent_state structures over and over, wasting lots of time.
5424 *
5425 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5426 * those expensive operations on a per page basis and do only the ordered io
5427 * finishing, while we release here the extent_map and extent_state structures,
5428 * without the excessive merging and splitting.
5429 */
5430static void evict_inode_truncate_pages(struct inode *inode)
5431{
5432 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5433 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5434 struct rb_node *node;
5435
5436 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005437 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005438
5439 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005440 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005441 struct extent_map *em;
5442
Liu Bo07e1ce02018-08-23 03:51:52 +08005443 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005444 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005445 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5446 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005447 remove_extent_mapping(map_tree, em);
5448 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005449 if (need_resched()) {
5450 write_unlock(&map_tree->lock);
5451 cond_resched();
5452 write_lock(&map_tree->lock);
5453 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005454 }
5455 write_unlock(&map_tree->lock);
5456
Filipe Manana6ca07092015-05-26 00:55:42 +01005457 /*
5458 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005459 * We can have ongoing bios started by readahead that have
5460 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005461 * still in progress (unlocked the pages in the bio but did not yet
5462 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005463 * ranges can still be locked and eviction started because before
5464 * submitting those bios, which are executed by a separate task (work
5465 * queue kthread), inode references (inode->i_count) were not taken
5466 * (which would be dropped in the end io callback of each bio).
5467 * Therefore here we effectively end up waiting for those bios and
5468 * anyone else holding locked ranges without having bumped the inode's
5469 * reference count - if we don't do it, when they access the inode's
5470 * io_tree to unlock a range it may be too late, leading to an
5471 * use-after-free issue.
5472 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005473 spin_lock(&io_tree->lock);
5474 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5475 struct extent_state *state;
5476 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005477 u64 start;
5478 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005479 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005480
5481 node = rb_first(&io_tree->state);
5482 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005483 start = state->start;
5484 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005485 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005486 spin_unlock(&io_tree->lock);
5487
David Sterbaff13db42015-12-03 14:30:40 +01005488 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005489
5490 /*
5491 * If still has DELALLOC flag, the extent didn't reach disk,
5492 * and its reserved space won't be freed by delayed_ref.
5493 * So we need to free its reserved space here.
5494 * (Refer to comment in btrfs_invalidatepage, case 2)
5495 *
5496 * Note, end is the bytenr of last byte, so we need + 1 here.
5497 */
Filipe Manana421f0922018-10-12 13:02:48 +01005498 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005499 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5500 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005501
Filipe Manana6ca07092015-05-26 00:55:42 +01005502 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005503 EXTENT_LOCKED | EXTENT_DELALLOC |
5504 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5505 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005506
Filipe Manana7064dd52014-08-08 02:47:05 +01005507 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005508 spin_lock(&io_tree->lock);
5509 }
5510 spin_unlock(&io_tree->lock);
5511}
5512
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005513static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005514 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005515{
5516 struct btrfs_fs_info *fs_info = root->fs_info;
5517 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005518 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005519 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005520 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005521
Josef Bacikd3984c92019-08-01 18:19:37 -04005522 /*
5523 * Eviction should be taking place at some place safe because of our
5524 * delayed iputs. However the normal flushing code will run delayed
5525 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5526 *
5527 * We reserve the delayed_refs_extra here again because we can't use
5528 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5529 * above. We reserve our extra bit here because we generate a ton of
5530 * delayed refs activity by truncating.
5531 *
5532 * If we cannot make our reservation we'll attempt to steal from the
5533 * global reserve, because we really want to be able to free up space.
5534 */
5535 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5536 BTRFS_RESERVE_FLUSH_EVICT);
5537 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005538 /*
5539 * Try to steal from the global reserve if there is space for
5540 * it.
5541 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005542 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5543 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5544 btrfs_warn(fs_info,
5545 "could not allocate space for delete; will truncate on mount");
5546 return ERR_PTR(-ENOSPC);
5547 }
5548 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005549 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005550
5551 trans = btrfs_join_transaction(root);
5552 if (IS_ERR(trans))
5553 return trans;
5554
5555 if (delayed_refs_extra) {
5556 trans->block_rsv = &fs_info->trans_block_rsv;
5557 trans->bytes_reserved = delayed_refs_extra;
5558 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5559 delayed_refs_extra, 1);
5560 }
5561 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005562}
5563
Al Virobd555972010-06-07 11:35:40 -04005564void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005565{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005566 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005567 struct btrfs_trans_handle *trans;
5568 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005569 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 int ret;
5571
liubo1abe9b82011-03-24 11:18:59 +00005572 trace_btrfs_inode_evict(inode);
5573
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005574 if (!root) {
Boris Burkov14605402021-06-30 13:01:49 -07005575 fsverity_cleanup_inode(inode);
Liu Boe8f1bc12018-01-25 11:02:53 -07005576 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005577 return;
5578 }
5579
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005580 evict_inode_truncate_pages(inode);
5581
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005582 if (inode->i_nlink &&
5583 ((btrfs_root_refs(&root->root_item) != 0 &&
5584 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005585 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005586 goto no_delete;
5587
Omar Sandoval27919062018-05-11 13:13:37 -07005588 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005589 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005590
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005591 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005592
Omar Sandoval7b40b692018-05-11 13:13:33 -07005593 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005594 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005595
Yan, Zheng76dda932009-09-21 16:00:26 -04005596 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005597 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5598 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 goto no_delete;
5600 }
5601
Nikolay Borisovaa790212017-01-10 20:35:40 +02005602 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005603 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005604 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005606 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005607 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005608 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005609 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005610 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005611
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005612 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005613
Yan, Zheng80825102009-11-12 09:35:36 +00005614 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005615 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005616 if (IS_ERR(trans))
5617 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005618
5619 trans->block_rsv = rsv;
5620
Nikolay Borisov50743392020-11-02 16:48:55 +02005621 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005622 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005623 trans->block_rsv = &fs_info->trans_block_rsv;
5624 btrfs_end_transaction(trans);
5625 btrfs_btree_balance_dirty(fs_info);
5626 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5627 goto free_rsv;
5628 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005629 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005630 }
5631
Josef Bacik4ef31a42013-08-13 14:10:08 -04005632 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005633 * Errors here aren't a big deal, it just means we leave orphan items in
5634 * the tree. They will be cleaned up on the next mount. If the inode
5635 * number gets reused, cleanup deletes the orphan item without doing
5636 * anything, and unlink reuses the existing orphan item.
5637 *
5638 * If it turns out that we are dropping too many of these, we might want
5639 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005640 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005641 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005642 if (!IS_ERR(trans)) {
5643 trans->block_rsv = rsv;
5644 btrfs_orphan_del(trans, BTRFS_I(inode));
5645 trans->block_rsv = &fs_info->trans_block_rsv;
5646 btrfs_end_transaction(trans);
5647 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005648
Omar Sandoval27919062018-05-11 13:13:37 -07005649free_rsv:
5650 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005651no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005652 /*
5653 * If we didn't successfully delete, the orphan item will still be in
5654 * the tree and we'll retry on the next mount. Again, we might also want
5655 * to retry these periodically in the future.
5656 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005657 btrfs_remove_delayed_node(BTRFS_I(inode));
Boris Burkov14605402021-06-30 13:01:49 -07005658 fsverity_cleanup_inode(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005659 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005660}
5661
5662/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005663 * Return the key found in the dir entry in the location pointer, fill @type
5664 * with BTRFS_FT_*, and return 0.
5665 *
Su Yue005d6712018-03-05 17:13:37 +08005666 * If no dir entries were found, returns -ENOENT.
5667 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005668 */
5669static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005670 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005671{
5672 const char *name = dentry->d_name.name;
5673 int namelen = dentry->d_name.len;
5674 struct btrfs_dir_item *di;
5675 struct btrfs_path *path;
5676 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005677 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005678
5679 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005680 if (!path)
5681 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005682
David Sterbaf85b7372017-01-20 14:54:07 +01005683 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5684 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005685 if (IS_ERR_OR_NULL(di)) {
5686 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005687 goto out;
5688 }
Chris Masond3977122009-01-05 21:25:51 -05005689
Chris Mason5f39d392007-10-15 16:14:19 -04005690 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005691 if (location->type != BTRFS_INODE_ITEM_KEY &&
5692 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005693 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005694 btrfs_warn(root->fs_info,
5695"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5696 __func__, name, btrfs_ino(BTRFS_I(dir)),
5697 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005698 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005699 if (!ret)
5700 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005701out:
Chris Mason39279cc2007-06-12 06:35:45 -04005702 btrfs_free_path(path);
5703 return ret;
5704}
5705
5706/*
5707 * when we hit a tree root in a directory, the btrfs part of the inode
5708 * needs to be changed to reflect the root directory of the tree root. This
5709 * is kind of like crossing a mount point.
5710 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005711static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712 struct inode *dir,
5713 struct dentry *dentry,
5714 struct btrfs_key *location,
5715 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005716{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717 struct btrfs_path *path;
5718 struct btrfs_root *new_root;
5719 struct btrfs_root_ref *ref;
5720 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005721 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 int ret;
5723 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005724
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725 path = btrfs_alloc_path();
5726 if (!path) {
5727 err = -ENOMEM;
5728 goto out;
5729 }
Chris Mason39279cc2007-06-12 06:35:45 -04005730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005732 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5733 key.type = BTRFS_ROOT_REF_KEY;
5734 key.offset = location->objectid;
5735
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005736 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737 if (ret) {
5738 if (ret < 0)
5739 err = ret;
5740 goto out;
5741 }
Chris Mason39279cc2007-06-12 06:35:45 -04005742
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 leaf = path->nodes[0];
5744 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005745 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5747 goto out;
5748
5749 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5750 (unsigned long)(ref + 1),
5751 dentry->d_name.len);
5752 if (ret)
5753 goto out;
5754
David Sterbab3b4aa72011-04-21 01:20:15 +02005755 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
David Sterba56e93572020-05-15 19:35:55 +02005757 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 if (IS_ERR(new_root)) {
5759 err = PTR_ERR(new_root);
5760 goto out;
5761 }
5762
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763 *sub_root = new_root;
5764 location->objectid = btrfs_root_dirid(&new_root->root_item);
5765 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005766 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 err = 0;
5768out:
5769 btrfs_free_path(path);
5770 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005771}
5772
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005773static void inode_tree_add(struct inode *inode)
5774{
5775 struct btrfs_root *root = BTRFS_I(inode)->root;
5776 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005777 struct rb_node **p;
5778 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005779 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005780 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005781
Al Viro1d3382cb2010-10-23 15:19:20 -04005782 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005783 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005784 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005785 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005786 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005787 while (*p) {
5788 parent = *p;
5789 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5790
David Sterba37508512018-06-29 10:56:40 +02005791 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005792 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005793 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005794 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005795 else {
5796 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005797 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005798 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005799 RB_CLEAR_NODE(parent);
5800 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005801 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005802 }
5803 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005804 rb_link_node(new, parent, p);
5805 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005806 spin_unlock(&root->inode_lock);
5807}
5808
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005809static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005810{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005811 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005812 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005813
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005814 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005815 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5816 rb_erase(&inode->rb_node, &root->inode_tree);
5817 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005818 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005819 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005820 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005821
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005822 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005823 spin_lock(&root->inode_lock);
5824 empty = RB_EMPTY_ROOT(&root->inode_tree);
5825 spin_unlock(&root->inode_lock);
5826 if (empty)
5827 btrfs_add_dead_root(root);
5828 }
5829}
5830
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005831
Chris Masone02119d2008-09-05 16:13:11 -04005832static int btrfs_init_locked_inode(struct inode *inode, void *p)
5833{
5834 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005835
5836 inode->i_ino = args->ino;
5837 BTRFS_I(inode)->location.objectid = args->ino;
5838 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5839 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005840 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5841 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 return 0;
5843}
5844
5845static int btrfs_find_actor(struct inode *inode, void *opaque)
5846{
5847 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005848
5849 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005850 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005851}
5852
David Sterba0202e832020-05-15 19:35:59 +02005853static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005854 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005855{
5856 struct inode *inode;
5857 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005858 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005859
David Sterba0202e832020-05-15 19:35:59 +02005860 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005861 args.root = root;
5862
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005863 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005864 btrfs_init_locked_inode,
5865 (void *)&args);
5866 return inode;
5867}
5868
David Sterba4c66e0d2019-10-03 19:09:35 +02005869/*
David Sterba0202e832020-05-15 19:35:59 +02005870 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005871 * Path can be preallocated to prevent recursing back to iget through
5872 * allocator. NULL is also valid but may require an additional allocation
5873 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305874 */
David Sterba0202e832020-05-15 19:35:59 +02005875struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005876 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305877{
5878 struct inode *inode;
5879
David Sterba0202e832020-05-15 19:35:59 +02005880 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305881 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005882 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305883
5884 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005885 int ret;
5886
Filipe Manana4222ea72018-10-24 10:13:03 +01005887 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005888 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005889 inode_tree_add(inode);
5890 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005891 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005892 iget_failed(inode);
5893 /*
5894 * ret > 0 can come from btrfs_search_slot called by
5895 * btrfs_read_locked_inode, this means the inode item
5896 * was not found.
5897 */
5898 if (ret > 0)
5899 ret = -ENOENT;
5900 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005901 }
5902 }
5903
Balaji Rao1a54ef82008-07-21 02:01:04 +05305904 return inode;
5905}
5906
David Sterba0202e832020-05-15 19:35:59 +02005907struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005908{
David Sterba0202e832020-05-15 19:35:59 +02005909 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005910}
5911
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005912static struct inode *new_simple_dir(struct super_block *s,
5913 struct btrfs_key *key,
5914 struct btrfs_root *root)
5915{
5916 struct inode *inode = new_inode(s);
5917
5918 if (!inode)
5919 return ERR_PTR(-ENOMEM);
5920
Josef Bacik5c8fd992020-02-14 16:11:43 -05005921 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005922 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005923 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005924
5925 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005926 /*
5927 * We only need lookup, the rest is read-only and there's no inode
5928 * associated with the dentry
5929 */
5930 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005931 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005932 inode->i_fop = &simple_dir_operations;
5933 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005934 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305935 inode->i_atime = inode->i_mtime;
5936 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005937 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005938
5939 return inode;
5940}
5941
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005942static inline u8 btrfs_inode_type(struct inode *inode)
5943{
5944 /*
5945 * Compile-time asserts that generic FT_* types still match
5946 * BTRFS_FT_* types
5947 */
5948 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5949 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5950 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5951 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5952 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5953 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5954 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5955 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5956
5957 return fs_umode_to_ftype(inode->i_mode);
5958}
5959
Chris Mason3de45862008-11-17 21:02:50 -05005960struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005962 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005963 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005964 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005965 struct btrfs_root *sub_root = root;
5966 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005967 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005968 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005969
5970 if (dentry->d_name.len > BTRFS_NAME_LEN)
5971 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005972
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005973 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005974 if (ret < 0)
5975 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005976
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005977 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005978 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005979 if (IS_ERR(inode))
5980 return inode;
5981
5982 /* Do extra check against inode mode with di_type */
5983 if (btrfs_inode_type(inode) != di_type) {
5984 btrfs_crit(fs_info,
5985"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5986 inode->i_mode, btrfs_inode_type(inode),
5987 di_type);
5988 iput(inode);
5989 return ERR_PTR(-EUCLEAN);
5990 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005991 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005993
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005994 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 &location, &sub_root);
5996 if (ret < 0) {
5997 if (ret != -ENOENT)
5998 inode = ERR_PTR(ret);
5999 else
6000 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6001 } else {
David Sterba0202e832020-05-15 19:35:59 +02006002 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04006003 }
Josef Bacik87270022020-01-24 09:32:31 -05006004 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05006005 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04006006
Julia Lawall34d19ba2011-01-24 19:55:19 +00006007 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006008 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006009 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006010 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006012 if (ret) {
6013 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006014 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006015 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006016 }
6017
Chris Mason3de45862008-11-17 21:02:50 -05006018 return inode;
6019}
6020
Nick Pigginfe15ce42011-01-07 17:49:23 +11006021static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006022{
6023 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006024 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006025
Li Zefan848cce02012-02-21 17:04:28 +08006026 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006027 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006028
Li Zefan848cce02012-02-21 17:04:28 +08006029 if (inode) {
6030 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006031 if (btrfs_root_refs(&root->root_item) == 0)
6032 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006033
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006034 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006035 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006036 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006037 return 0;
6038}
6039
Chris Mason3de45862008-11-17 21:02:50 -05006040static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006041 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006042{
Al Viro3837d202018-10-10 16:38:27 -04006043 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04006044
Al Viro3837d202018-10-10 16:38:27 -04006045 if (inode == ERR_PTR(-ENOENT))
6046 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04006047 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006048}
6049
Josef Bacik23b5ec72017-07-24 15:14:25 -04006050/*
6051 * All this infrastructure exists because dir_emit can fault, and we are holding
6052 * the tree lock when doing readdir. For now just allocate a buffer and copy
6053 * our information into that, and then dir_emit from the buffer. This is
6054 * similar to what NFS does, only we don't keep the buffer around in pagecache
6055 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6056 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6057 * tree lock.
6058 */
6059static int btrfs_opendir(struct inode *inode, struct file *file)
6060{
6061 struct btrfs_file_private *private;
6062
6063 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6064 if (!private)
6065 return -ENOMEM;
6066 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6067 if (!private->filldir_buf) {
6068 kfree(private);
6069 return -ENOMEM;
6070 }
6071 file->private_data = private;
6072 return 0;
6073}
6074
6075struct dir_entry {
6076 u64 ino;
6077 u64 offset;
6078 unsigned type;
6079 int name_len;
6080};
6081
6082static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6083{
6084 while (entries--) {
6085 struct dir_entry *entry = addr;
6086 char *name = (char *)(entry + 1);
6087
David Sterba92d32172018-04-16 21:10:14 +02006088 ctx->pos = get_unaligned(&entry->offset);
6089 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6090 get_unaligned(&entry->ino),
6091 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006092 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006093 addr += sizeof(struct dir_entry) +
6094 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006095 ctx->pos++;
6096 }
6097 return 0;
6098}
6099
Al Viro9cdda8d2013-05-22 16:48:09 -04006100static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006101{
Al Viro9cdda8d2013-05-22 16:48:09 -04006102 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006103 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006104 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006105 struct btrfs_dir_item *di;
6106 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006107 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006108 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006109 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006110 struct list_head ins_list;
6111 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006112 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006113 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006114 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006115 char *name_ptr;
6116 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006117 int entries = 0;
6118 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006119 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006120 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006121
Al Viro9cdda8d2013-05-22 16:48:09 -04006122 if (!dir_emit_dots(file, ctx))
6123 return 0;
6124
David Woodhouse49593bf2008-08-17 17:08:36 +01006125 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006126 if (!path)
6127 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006128
Josef Bacik23b5ec72017-07-24 15:14:25 -04006129 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006130 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006131
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006132 INIT_LIST_HEAD(&ins_list);
6133 INIT_LIST_HEAD(&del_list);
6134 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006135
Josef Bacik23b5ec72017-07-24 15:14:25 -04006136again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006137 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006138 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006139 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006140
Chris Mason39279cc2007-06-12 06:35:45 -04006141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6142 if (ret < 0)
6143 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006144
6145 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006146 struct dir_entry *entry;
6147
Chris Mason5f39d392007-10-15 16:14:19 -04006148 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006149 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006150 if (slot >= btrfs_header_nritems(leaf)) {
6151 ret = btrfs_next_leaf(root, path);
6152 if (ret < 0)
6153 goto err;
6154 else if (ret > 0)
6155 break;
6156 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006157 }
Chris Mason3de45862008-11-17 21:02:50 -05006158
Chris Mason5f39d392007-10-15 16:14:19 -04006159 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6160
6161 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006162 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006163 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006164 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006165 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006166 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006167 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006168 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006169 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006170 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006171 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6172 PAGE_SIZE) {
6173 btrfs_release_path(path);
6174 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6175 if (ret)
6176 goto nopos;
6177 addr = private->filldir_buf;
6178 entries = 0;
6179 total_len = 0;
6180 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006181 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006182
6183 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006184 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006185 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006186 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6187 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006188 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006189 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006190 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006191 put_unaligned(location.objectid, &entry->ino);
6192 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006193 entries++;
6194 addr += sizeof(struct dir_entry) + name_len;
6195 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006196next:
6197 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006198 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006199 btrfs_release_path(path);
6200
6201 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6202 if (ret)
6203 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006204
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006205 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006206 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006207 goto nopos;
6208
Zach Browndb62efb2013-07-11 16:19:42 -07006209 /*
6210 * Stop new entries from being returned after we return the last
6211 * entry.
6212 *
6213 * New directory entries are assigned a strictly increasing
6214 * offset. This means that new entries created during readdir
6215 * are *guaranteed* to be seen in the future by that readdir.
6216 * This has broken buggy programs which operate on names as
6217 * they're returned by readdir. Until we re-use freed offsets
6218 * we have this hack to stop new entries from being returned
6219 * under the assumption that they'll never reach this huge
6220 * offset.
6221 *
6222 * This is being careful not to overflow 32bit loff_t unless the
6223 * last entry requires it because doing so has broken 32bit apps
6224 * in the past.
6225 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006226 if (ctx->pos >= INT_MAX)
6227 ctx->pos = LLONG_MAX;
6228 else
6229 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006230nopos:
6231 ret = 0;
6232err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006233 if (put)
6234 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006235 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006236 return ret;
6237}
6238
Chris Mason39279cc2007-06-12 06:35:45 -04006239/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006240 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006241 * inode changes. But, it is most likely to find the inode in cache.
6242 * FIXME, needs more benchmarking...there are no reasons other than performance
6243 * to keep or drop this code.
6244 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006245static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006246{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006248 struct btrfs_root *root = BTRFS_I(inode)->root;
6249 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006250 int ret;
6251
Josef Bacik72ac3c02012-05-23 14:13:11 -04006252 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006253 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006254
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006255 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006256 if (IS_ERR(trans))
6257 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006258
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006259 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006260 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006261 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006262 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006263 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006264 if (IS_ERR(trans))
6265 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006266
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006267 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006268 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006269 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006270 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006271 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006272
6273 return ret;
6274}
6275
6276/*
6277 * This is a copy of file_update_time. We need this so we can return error on
6278 * ENOSPC for updating the inode in the case of file write and mmap writes.
6279 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006280static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006281 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006282{
Alexander Block2bc5565282012-06-15 09:49:33 +02006283 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006284 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006285
6286 if (btrfs_root_readonly(root))
6287 return -EROFS;
6288
Josef Bacike41f9412012-03-26 09:46:47 -04006289 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006290 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006291 if (flags & S_CTIME)
6292 inode->i_ctime = *now;
6293 if (flags & S_MTIME)
6294 inode->i_mtime = *now;
6295 if (flags & S_ATIME)
6296 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006297 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006298}
6299
Chris Masond352ac62008-09-29 15:18:18 -04006300/*
6301 * find the highest existing sequence number in a directory
6302 * and then set the in-memory index_cnt variable to reflect
6303 * free sequence numbers
6304 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006305static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006306{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006307 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006308 struct btrfs_key key, found_key;
6309 struct btrfs_path *path;
6310 struct extent_buffer *leaf;
6311 int ret;
6312
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006313 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006314 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006315 key.offset = (u64)-1;
6316
6317 path = btrfs_alloc_path();
6318 if (!path)
6319 return -ENOMEM;
6320
6321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6322 if (ret < 0)
6323 goto out;
6324 /* FIXME: we should be able to handle this */
6325 if (ret == 0)
6326 goto out;
6327 ret = 0;
6328
6329 /*
6330 * MAGIC NUMBER EXPLANATION:
6331 * since we search a directory based on f_pos we have to start at 2
6332 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6333 * else has to start at 2
6334 */
6335 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006336 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006337 goto out;
6338 }
6339
6340 path->slots[0]--;
6341
6342 leaf = path->nodes[0];
6343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6344
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006345 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006346 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006347 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006348 goto out;
6349 }
6350
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006351 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006352out:
6353 btrfs_free_path(path);
6354 return ret;
6355}
6356
Chris Masond352ac62008-09-29 15:18:18 -04006357/*
6358 * helper to find a free sequence number in a given directory. This current
6359 * code is very simple, later versions will do smarter things in the btree
6360 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006361int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006362{
6363 int ret = 0;
6364
Nikolay Borisov877574e2017-02-20 13:50:33 +02006365 if (dir->index_cnt == (u64)-1) {
6366 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006367 if (ret) {
6368 ret = btrfs_set_inode_index_count(dir);
6369 if (ret)
6370 return ret;
6371 }
Josef Bacikaec74772008-07-24 12:12:38 -04006372 }
6373
Nikolay Borisov877574e2017-02-20 13:50:33 +02006374 *index = dir->index_cnt;
6375 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006376
6377 return ret;
6378}
6379
Chris Masonb0d5d102014-09-08 13:08:51 -07006380static int btrfs_insert_inode_locked(struct inode *inode)
6381{
6382 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006383
6384 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 args.root = BTRFS_I(inode)->root;
6386
6387 return insert_inode_locked4(inode,
6388 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6389 btrfs_find_actor, &args);
6390}
6391
Anand Jain19aee8d2017-07-18 17:37:05 +08006392/*
6393 * Inherit flags from the parent inode.
6394 *
6395 * Currently only the compression flags and the cow flags are inherited.
6396 */
6397static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6398{
6399 unsigned int flags;
6400
6401 if (!dir)
6402 return;
6403
6404 flags = BTRFS_I(dir)->flags;
6405
6406 if (flags & BTRFS_INODE_NOCOMPRESS) {
6407 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6408 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6409 } else if (flags & BTRFS_INODE_COMPRESS) {
6410 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6411 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6412 }
6413
6414 if (flags & BTRFS_INODE_NODATACOW) {
6415 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6416 if (S_ISREG(inode->i_mode))
6417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6418 }
6419
David Sterba7b6a2212018-03-26 18:40:21 +02006420 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006421}
6422
Chris Mason39279cc2007-06-12 06:35:45 -04006423static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6424 struct btrfs_root *root,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006425 struct user_namespace *mnt_userns,
Josef Bacikaec74772008-07-24 12:12:38 -04006426 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006427 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006428 u64 ref_objectid, u64 objectid,
6429 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006431 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006432 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006433 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006435 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006436 struct btrfs_inode_ref *ref;
6437 struct btrfs_key key[2];
6438 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006439 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006440 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006441 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006442 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006443
Chris Mason5f39d392007-10-15 16:14:19 -04006444 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006445 if (!path)
6446 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006447
Josef Bacik11a19a92019-09-09 10:12:04 -04006448 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006449 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006450 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006451 if (!inode) {
6452 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006454 }
Chris Mason39279cc2007-06-12 06:35:45 -04006455
Li Zefan581bb052011-04-20 10:06:11 +08006456 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006457 * O_TMPFILE, set link count to 0, so that after this point,
6458 * we fill in an inode item with the correct link count.
6459 */
6460 if (!name)
6461 set_nlink(inode, 0);
6462
6463 /*
Li Zefan581bb052011-04-20 10:06:11 +08006464 * we have to initialize this early, so we can reclaim the inode
6465 * number if we fail afterwards in this function.
6466 */
6467 inode->i_ino = objectid;
6468
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006469 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006470 trace_btrfs_inode_request(dir);
6471
Nikolay Borisov877574e2017-02-20 13:50:33 +02006472 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006473 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006474 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006475 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006476 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006477 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006478 } else if (dir) {
6479 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006480 }
6481 /*
6482 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006483 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006484 * number
6485 */
6486 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006487 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006488 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006489 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006490 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006491
Josef Bacik5dc562c2012-08-17 13:14:17 -04006492 /*
6493 * We could have gotten an inode number from somebody who was fsynced
6494 * and then removed in this same transaction, so let's just set full
6495 * sync since it will be a full sync anyway and this will blow away the
6496 * old info in the log.
6497 */
6498 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6499
Chris Mason9c583092008-01-29 15:15:18 -05006500 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006501 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006502 key[0].offset = 0;
6503
Chris Mason9c583092008-01-29 15:15:18 -05006504 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006505
6506 if (name) {
6507 /*
6508 * Start new inodes with an inode_ref. This is slightly more
6509 * efficient for small numbers of hard links since they will
6510 * be packed into one item. Extended refs will kick in if we
6511 * add more hard links than can fit in the ref item.
6512 */
6513 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006514 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006515 key[1].offset = ref_objectid;
6516
6517 sizes[1] = name_len + sizeof(*ref);
6518 }
Chris Mason9c583092008-01-29 15:15:18 -05006519
Chris Masonb0d5d102014-09-08 13:08:51 -07006520 location = &BTRFS_I(inode)->location;
6521 location->objectid = objectid;
6522 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006523 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006524
6525 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006526 if (ret < 0) {
6527 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006528 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006529 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006530
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006531 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006532 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006533 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006534
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006535 inode_init_owner(mnt_userns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006536 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306537
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006538 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306539 inode->i_atime = inode->i_mtime;
6540 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006541 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306542
Chris Mason5f39d392007-10-15 16:14:19 -04006543 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6544 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006545 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006546 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006547 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006548
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006549 if (name) {
6550 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6551 struct btrfs_inode_ref);
6552 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6553 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6554 ptr = (unsigned long)(ref + 1);
6555 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6556 }
Chris Mason9c583092008-01-29 15:15:18 -05006557
Chris Mason5f39d392007-10-15 16:14:19 -04006558 btrfs_mark_buffer_dirty(path->nodes[0]);
6559 btrfs_free_path(path);
6560
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006561 btrfs_inherit_iflags(inode, dir);
6562
Al Viro569254b2011-07-24 17:08:40 -04006563 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006564 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006565 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006566 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006567 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6568 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006569 }
6570
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006571 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006572
6573 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006574 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006575
Alexander Block8ea05e32012-07-25 17:35:53 +02006576 btrfs_update_root_times(trans, root);
6577
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006578 ret = btrfs_inode_inherit_props(trans, inode, dir);
6579 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006580 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006581 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006582 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006583
Chris Mason39279cc2007-06-12 06:35:45 -04006584 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006585
6586fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006587 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006588fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006589 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006590 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006591 btrfs_free_path(path);
6592 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006593}
6594
Chris Masond352ac62008-09-29 15:18:18 -04006595/*
6596 * utility function to add 'inode' into 'parent_inode' with
6597 * a give name and a given sequence number.
6598 * if 'add_backref' is true, also insert a backref from the
6599 * inode to the parent directory.
6600 */
Chris Masone02119d2008-09-05 16:13:11 -04006601int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006602 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006603 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006604{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006605 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006606 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006607 struct btrfs_root *root = parent_inode->root;
6608 u64 ino = btrfs_ino(inode);
6609 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006610
Li Zefan33345d012011-04-20 10:31:50 +08006611 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006612 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006613 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006614 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006615 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006616 key.offset = 0;
6617 }
Chris Mason39279cc2007-06-12 06:35:45 -04006618
Li Zefan33345d012011-04-20 10:31:50 +08006619 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006620 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006621 root->root_key.objectid, parent_ino,
6622 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006623 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006624 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6625 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006626 }
6627
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006628 /* Nothing to clean up yet */
6629 if (ret)
6630 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006631
Lu Fengqi684572d2018-08-04 21:10:57 +08006632 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006633 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006634 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006635 goto fail_dir_item;
6636 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006637 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006638 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006639 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006640
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006641 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006642 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006643 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006644 /*
6645 * If we are replaying a log tree, we do not want to update the mtime
6646 * and ctime of the parent directory with the current time, since the
6647 * log replay procedure is responsible for setting them to their correct
6648 * values (the ones it had when the fsync was done).
6649 */
6650 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6651 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6652
6653 parent_inode->vfs_inode.i_mtime = now;
6654 parent_inode->vfs_inode.i_ctime = now;
6655 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006656 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006657 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006658 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006659 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006660
6661fail_dir_item:
6662 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6663 u64 local_index;
6664 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006665 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006666 root->root_key.objectid, parent_ino,
6667 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006668 if (err)
6669 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006670 } else if (add_backref) {
6671 u64 local_index;
6672 int err;
6673
6674 err = btrfs_del_inode_ref(trans, root, name, name_len,
6675 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006676 if (err)
6677 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006678 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006679
6680 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006681 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006682}
6683
6684static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006685 struct btrfs_inode *dir, struct dentry *dentry,
6686 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006687{
Josef Bacika1b075d2010-11-19 20:36:11 +00006688 int err = btrfs_add_link(trans, dir, inode,
6689 dentry->d_name.name, dentry->d_name.len,
6690 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006691 if (err > 0)
6692 err = -EEXIST;
6693 return err;
6694}
6695
Christian Brauner549c7292021-01-21 14:19:43 +01006696static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6697 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006698{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006699 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006700 struct btrfs_trans_handle *trans;
6701 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006702 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006703 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006704 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006705 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006706
Josef Bacik9ed74f22009-09-11 16:12:44 -04006707 /*
6708 * 2 for inode item and ref
6709 * 2 for dir items
6710 * 1 for xattr if selinux is on
6711 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 trans = btrfs_start_transaction(root, 5);
6713 if (IS_ERR(trans))
6714 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006715
Nikolay Borisov543068a2020-12-07 17:32:33 +02006716 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006717 if (err)
6718 goto out_unlock;
6719
Christian Brauner72105272021-07-27 12:48:44 +02006720 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006721 dentry->d_name.name, dentry->d_name.len,
6722 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006723 if (IS_ERR(inode)) {
6724 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006725 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006726 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006727 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006728
Casey Schauflerad19db72011-12-15 10:09:07 -05006729 /*
6730 * If the active LSM wants to access the inode during
6731 * d_instantiate it needs these. Smack checks to see
6732 * if the filesystem supports xattrs by looking at the
6733 * ops vector.
6734 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006735 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006736 init_special_inode(inode, inode->i_mode, rdev);
6737
6738 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006739 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006740 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006741
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006742 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6743 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006744 if (err)
6745 goto out_unlock;
6746
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006747 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006748 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006749
Josef Bacik618e21d2007-07-11 10:18:17 -04006750out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006751 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006752 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006753 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006754 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006755 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006756 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006757 return err;
6758}
6759
Christian Brauner549c7292021-01-21 14:19:43 +01006760static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6761 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006762{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006763 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006764 struct btrfs_trans_handle *trans;
6765 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006766 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006767 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006768 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006769 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006770
Josef Bacik9ed74f22009-09-11 16:12:44 -04006771 /*
6772 * 2 for inode item and ref
6773 * 2 for dir items
6774 * 1 for xattr if selinux is on
6775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006776 trans = btrfs_start_transaction(root, 5);
6777 if (IS_ERR(trans))
6778 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006779
Nikolay Borisov543068a2020-12-07 17:32:33 +02006780 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006781 if (err)
6782 goto out_unlock;
6783
Christian Braunere93ca492021-07-27 12:48:45 +02006784 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006785 dentry->d_name.name, dentry->d_name.len,
6786 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006787 if (IS_ERR(inode)) {
6788 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006789 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006790 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006791 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006792 /*
6793 * If the active LSM wants to access the inode during
6794 * d_instantiate it needs these. Smack checks to see
6795 * if the filesystem supports xattrs by looking at the
6796 * ops vector.
6797 */
6798 inode->i_fop = &btrfs_file_operations;
6799 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006800 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006801
6802 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6803 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006804 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006805
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006806 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006807 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006808 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006809
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006810 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6811 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006812 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006813 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006814
Al Viro1e2e5472018-05-04 08:23:01 -04006815 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006816
Chris Mason39279cc2007-06-12 06:35:45 -04006817out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006818 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006819 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006820 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006821 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006822 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006823 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006824 return err;
6825}
6826
6827static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6828 struct dentry *dentry)
6829{
Filipe Manana271dba452016-01-05 16:24:05 +00006830 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006831 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006832 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006834 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006835 int err;
6836 int drop_inode = 0;
6837
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006838 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006839 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006840 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006841
Mark Fashehf1863732012-08-08 11:32:27 -07006842 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006843 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006844
Nikolay Borisov877574e2017-02-20 13:50:33 +02006845 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006846 if (err)
6847 goto fail;
6848
Yan, Zhenga22285a2010-05-16 10:48:46 -04006849 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006850 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006851 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006852 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006853 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006854 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006855 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006856 if (IS_ERR(trans)) {
6857 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006858 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006859 goto fail;
6860 }
Chris Mason5f39d392007-10-15 16:14:19 -04006861
Miao Xie67de1172013-12-26 13:07:06 +08006862 /* There are several dir indexes for this inode, clear the cache. */
6863 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006864 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006865 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006866 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006867 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006868 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006869
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006870 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6871 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006872
Yan, Zhenga5719522009-09-24 09:17:31 -04006873 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006874 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006875 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006876 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006877
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006878 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006879 if (err)
6880 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006881 if (inode->i_nlink == 1) {
6882 /*
6883 * If new hard link count is 1, it's a file created
6884 * with open(2) O_TMPFILE flag.
6885 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006886 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006887 if (err)
6888 goto fail;
6889 }
Al Viro08c422c2011-12-23 07:58:13 -05006890 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006891 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006892 }
Chris Mason39279cc2007-06-12 06:35:45 -04006893
Chris Mason1832a6d2007-12-21 16:27:21 -05006894fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006895 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006896 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006897 if (drop_inode) {
6898 inode_dec_link_count(inode);
6899 iput(inode);
6900 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006901 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006902 return err;
6903}
6904
Christian Brauner549c7292021-01-21 14:19:43 +01006905static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6906 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006907{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006908 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006909 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006910 struct btrfs_trans_handle *trans;
6911 struct btrfs_root *root = BTRFS_I(dir)->root;
6912 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006913 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006914 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006915
Josef Bacik9ed74f22009-09-11 16:12:44 -04006916 /*
6917 * 2 items for inode and ref
6918 * 2 items for dir items
6919 * 1 for xattr if selinux is on
6920 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006921 trans = btrfs_start_transaction(root, 5);
6922 if (IS_ERR(trans))
6923 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006924
Nikolay Borisov543068a2020-12-07 17:32:33 +02006925 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006926 if (err)
6927 goto out_fail;
6928
Christian Braunerb0b3e442021-07-27 12:48:46 +02006929 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006930 dentry->d_name.name, dentry->d_name.len,
6931 btrfs_ino(BTRFS_I(dir)), objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01006932 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006933 if (IS_ERR(inode)) {
6934 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006935 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006936 goto out_fail;
6937 }
Chris Mason5f39d392007-10-15 16:14:19 -04006938
Chris Masonb0d5d102014-09-08 13:08:51 -07006939 /* these must be set before we unlock the inode */
6940 inode->i_op = &btrfs_dir_inode_operations;
6941 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006942
Eric Paris2a7dba32011-02-01 11:05:39 -05006943 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006944 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006945 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006946
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006947 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006948 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006949 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006950 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006951
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006952 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6953 dentry->d_name.name,
6954 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006955 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006956 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006957
Al Viro1e2e5472018-05-04 08:23:01 -04006958 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006959
6960out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006961 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006962 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006963 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006964 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006965 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006966 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006967 return err;
6968}
6969
Chris Masonc8b97812008-10-29 14:49:59 -04006970static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006971 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006972 size_t pg_offset, u64 extent_offset,
6973 struct btrfs_file_extent_item *item)
6974{
6975 int ret;
6976 struct extent_buffer *leaf = path->nodes[0];
6977 char *tmp;
6978 size_t max_size;
6979 unsigned long inline_size;
6980 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006981 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006982
6983 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006984 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006985 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6986 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006987 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006988 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006989 if (!tmp)
6990 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006991 ptr = btrfs_file_extent_inline_start(item);
6992
6993 read_extent_buffer(leaf, tmp, ptr, inline_size);
6994
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006995 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006996 ret = btrfs_decompress(compress_type, tmp, page,
6997 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006998
6999 /*
7000 * decompression code contains a memset to fill in any space between the end
7001 * of the uncompressed data and the end of max_size in case the decompressed
7002 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7003 * the end of an inline extent and the beginning of the next block, so we
7004 * cover that region here.
7005 */
7006
Ira Weinyd048b9c2021-05-04 18:40:07 -07007007 if (max_size + pg_offset < PAGE_SIZE)
7008 memzero_page(page, pg_offset + max_size,
7009 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04007010 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007011 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007012}
7013
Omar Sandoval39b07b52019-12-02 17:34:23 -08007014/**
7015 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
7016 * @inode: file to search in
7017 * @page: page to read extent data into if the extent is inline
7018 * @pg_offset: offset into @page to copy to
7019 * @start: file offset
7020 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04007021 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08007022 * This returns the first &struct extent_map which overlaps with the given
7023 * range, reading it from the B-tree and caching it if necessary. Note that
7024 * there may be more extents which overlap the given range after the returned
7025 * extent_map.
7026 *
7027 * If @page is not NULL and the extent is inline, this also reads the extent
7028 * data directly into the page and marks the extent up to date in the io_tree.
7029 *
7030 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04007031 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007032struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08007033 struct page *page, size_t pg_offset,
7034 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04007035{
David Sterba3ffbd682018-06-29 10:56:42 +02007036 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007037 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007038 u64 extent_start = 0;
7039 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007040 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07007041 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04007042 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007043 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007044 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007045 struct extent_buffer *leaf;
7046 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007047 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007048 struct extent_map_tree *em_tree = &inode->extent_tree;
7049 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007050
Chris Mason890871b2009-09-02 16:24:52 -04007051 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007052 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007053 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007054
Chris Masona52d9a82007-08-27 16:49:44 -04007055 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007056 if (em->start > start || em->start + em->len <= start)
7057 free_extent_map(em);
7058 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007059 free_extent_map(em);
7060 else
7061 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007062 }
David Sterba172ddd62011-04-21 00:48:27 +02007063 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007064 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007065 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05007066 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Masond1310b22008-01-24 16:13:08 -05007068 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007069 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007070 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007071 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007072
Liu Bobee6ec82018-08-17 05:05:28 +08007073 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04007074 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007075 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08007076 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04007077 }
7078
Liu Bobee6ec82018-08-17 05:05:28 +08007079 /* Chances are we'll be called again, so go ahead and do readahead */
7080 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04007081
7082 /*
7083 * The same explanation in load_free_space_cache applies here as well,
7084 * we only read when we're loading the free space cache, and at that
7085 * point the commit_root has everything we need.
7086 */
7087 if (btrfs_is_free_space_inode(inode)) {
7088 path->search_commit_root = 1;
7089 path->skip_locking = 1;
7090 }
Josef Bacik51899412020-08-20 11:46:01 -04007091
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007092 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007093 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007094 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007095 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007096 if (path->slots[0] == 0)
7097 goto not_found;
7098 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007099 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007100 }
7101
Chris Mason5f39d392007-10-15 16:14:19 -04007102 leaf = path->nodes[0];
7103 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007104 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007106 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007107 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007108 /*
7109 * If we backup past the first extent we want to move forward
7110 * and see if there is an extent in front of us, otherwise we'll
7111 * say there is a hole for our whole search range which can
7112 * cause problems.
7113 */
7114 extent_end = start;
7115 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007116 }
7117
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007118 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007119 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00007120 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007121 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7122 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007123 /* Only regular file could have regular/prealloc extent */
7124 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007125 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007126 btrfs_crit(fs_info,
7127 "regular/prealloc extent found for non-regular inode %llu",
7128 btrfs_ino(inode));
7129 goto out;
7130 }
Liu Bo09ed2f12017-03-10 11:09:48 -08007131 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7132 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007133 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08007134 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7135 path->slots[0],
7136 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007137 }
Josef Bacik25a50342013-10-14 12:08:38 -04007138next:
Yan Zheng9036c102008-10-30 14:19:41 -04007139 if (start >= extent_end) {
7140 path->slots[0]++;
7141 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7142 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007143 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007144 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007145 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007146 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007147
Yan Zheng9036c102008-10-30 14:19:41 -04007148 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007149 }
Yan Zheng9036c102008-10-30 14:19:41 -04007150 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7151 if (found_key.objectid != objectid ||
7152 found_key.type != BTRFS_EXTENT_DATA_KEY)
7153 goto not_found;
7154 if (start + len <= found_key.offset)
7155 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007156 if (start > found_key.offset)
7157 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007158
7159 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007160 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007161 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007162 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007163 em->block_start = EXTENT_MAP_HOLE;
7164 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007165 }
7166
Omar Sandoval39b07b52019-12-02 17:34:23 -08007167 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007168
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007169 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7170 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007171 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007172 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007173 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007174 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007175 size_t size;
7176 size_t extent_offset;
7177 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007178
Omar Sandoval39b07b52019-12-02 17:34:23 -08007179 if (!page)
Yan689f9342007-10-29 11:41:07 -04007180 goto out;
Yan689f9342007-10-29 11:41:07 -04007181
Qu Wenruoe41ca582018-06-06 15:41:49 +08007182 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007183 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007184 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7185 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007186 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007188 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007189 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007190 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007191
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007192 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007193 if (btrfs_file_extent_compression(leaf, item) !=
7194 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007195 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007196 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007197 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007198 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007199 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007200 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007201 read_extent_buffer(leaf, map + pg_offset, ptr,
7202 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007203 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007204 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007205 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007206 copy_size);
7207 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007208 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007209 }
Chris Mason179e29e2007-11-01 11:28:41 -04007210 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007211 }
Chris Masond1310b22008-01-24 16:13:08 -05007212 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007213 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007214 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007215 }
7216not_found:
7217 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007218 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007219 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007220 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007221insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007222 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007223 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007224 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007225 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007226 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7227 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007228 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007229 goto out;
7230 }
Chris Masond1310b22008-01-24 16:13:08 -05007231
Chris Mason890871b2009-09-02 16:24:52 -04007232 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007233 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007234 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007235out:
Liu Boc6414282018-08-23 07:36:17 +08007236 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007237
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007238 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007239
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007240 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007241 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007242 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007243 }
7244 return em;
7245}
7246
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007247struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007248 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007249{
7250 struct extent_map *em;
7251 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007252 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007253 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007254 u64 delalloc_len;
7255 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007256 int err = 0;
7257
Omar Sandoval39b07b52019-12-02 17:34:23 -08007258 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007259 if (IS_ERR(em))
7260 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007261 /*
7262 * If our em maps to:
7263 * - a hole or
7264 * - a pre-alloc extent,
7265 * there might actually be delalloc bytes behind it.
7266 */
7267 if (em->block_start != EXTENT_MAP_HOLE &&
7268 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7269 return em;
7270 else
7271 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007272
7273 /* check to see if we've wrapped (len == -1 or similar) */
7274 end = start + len;
7275 if (end < start)
7276 end = (u64)-1;
7277 else
7278 end -= 1;
7279
7280 em = NULL;
7281
7282 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007283 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007284 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007285 delalloc_end = delalloc_start + delalloc_len;
7286 if (delalloc_end < delalloc_start)
7287 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007288
7289 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007290 * We didn't find anything useful, return the original results from
7291 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007292 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007293 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007294 em = hole_em;
7295 hole_em = NULL;
7296 goto out;
7297 }
7298
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007299 /*
7300 * Adjust the delalloc_start to make sure it doesn't go backwards from
7301 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007302 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007303 delalloc_start = max(start, delalloc_start);
7304 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007305
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007306 if (delalloc_len > 0) {
7307 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007308 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007309 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007310
David Sterba172ddd62011-04-21 00:48:27 +02007311 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007312 if (!em) {
7313 err = -ENOMEM;
7314 goto out;
7315 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007316
7317 ASSERT(hole_em);
7318 /*
7319 * When btrfs_get_extent can't find anything it returns one
7320 * huge hole
7321 *
7322 * Make sure what it found really fits our range, and adjust to
7323 * make sure it is based on the start from the caller
7324 */
7325 if (hole_end <= start || hole_em->start > end) {
7326 free_extent_map(hole_em);
7327 hole_em = NULL;
7328 } else {
7329 hole_start = max(hole_em->start, start);
7330 hole_len = hole_end - hole_start;
7331 }
7332
7333 if (hole_em && delalloc_start > hole_start) {
7334 /*
7335 * Our hole starts before our delalloc, so we have to
7336 * return just the parts of the hole that go until the
7337 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007338 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007339 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007340 em->start = hole_start;
7341 em->orig_start = hole_start;
7342 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007343 * Don't adjust block start at all, it is fixed at
7344 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007345 */
7346 em->block_start = hole_em->block_start;
7347 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007348 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7349 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007350 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007351 /*
7352 * Hole is out of passed range or it starts after
7353 * delalloc range
7354 */
7355 em->start = delalloc_start;
7356 em->len = delalloc_len;
7357 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007358 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007359 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007360 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007361 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007362 return hole_em;
7363 }
7364out:
7365
7366 free_extent_map(hole_em);
7367 if (err) {
7368 free_extent_map(em);
7369 return ERR_PTR(err);
7370 }
7371 return em;
7372}
7373
Nikolay Borisov64f54182020-06-03 08:55:31 +03007374static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007375 const u64 start,
7376 const u64 len,
7377 const u64 orig_start,
7378 const u64 block_start,
7379 const u64 block_len,
7380 const u64 orig_block_len,
7381 const u64 ram_bytes,
7382 const int type)
7383{
7384 struct extent_map *em = NULL;
7385 int ret;
7386
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007387 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007388 em = create_io_em(inode, start, len, orig_start, block_start,
7389 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007390 BTRFS_COMPRESS_NONE, /* compress_type */
7391 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007392 if (IS_ERR(em))
7393 goto out;
7394 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007395 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7396 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007397 if (ret) {
7398 if (em) {
7399 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007400 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007401 }
7402 em = ERR_PTR(ret);
7403 }
7404 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007405
7406 return em;
7407}
7408
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007409static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007410 u64 start, u64 len)
7411{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007412 struct btrfs_root *root = inode->root;
7413 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007414 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007415 struct btrfs_key ins;
7416 u64 alloc_hint;
7417 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007418
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007419 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007420 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007421 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007422 if (ret)
7423 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007424
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007425 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007426 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007427 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007428 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007429 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007430 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7431 1);
Josef Bacik00361582013-08-14 14:02:47 -04007432
Josef Bacik4b46fce2010-05-23 11:00:55 -04007433 return em;
7434}
7435
Anand Jainf4639632021-02-10 21:25:16 -08007436static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007437{
7438 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007439 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007440
7441 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7442 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007443 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007444 if (block_group)
7445 btrfs_put_block_group(block_group);
7446 return readonly;
7447}
7448
Chris Mason46bfbb52010-05-26 11:04:10 -04007449/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007450 * Check if we can do nocow write into the range [@offset, @offset + @len)
7451 *
7452 * @offset: File offset
7453 * @len: The length to write, will be updated to the nocow writeable
7454 * range
7455 * @orig_start: (optional) Return the original file offset of the file extent
7456 * @orig_len: (optional) Return the original on-disk length of the file extent
7457 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007458 * @strict: if true, omit optimizations that might force us into unnecessary
7459 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007460 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007461 * Return:
7462 * >0 and update @len if we can do nocow write
7463 * 0 if we can't do nocow write
7464 * <0 if error happened
7465 *
7466 * NOTE: This only checks the file extents, caller is responsible to wait for
7467 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 */
Josef Bacik00361582013-08-14 14:02:47 -04007469noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007470 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007471 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007472{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 struct btrfs_path *path;
7475 int ret;
7476 struct extent_buffer *leaf;
7477 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007478 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 struct btrfs_file_extent_item *fi;
7480 struct btrfs_key key;
7481 u64 disk_bytenr;
7482 u64 backref_offset;
7483 u64 extent_end;
7484 u64 num_bytes;
7485 int slot;
7486 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007487 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007488
Chris Mason46bfbb52010-05-26 11:04:10 -04007489 path = btrfs_alloc_path();
7490 if (!path)
7491 return -ENOMEM;
7492
David Sterbaf85b7372017-01-20 14:54:07 +01007493 ret = btrfs_lookup_file_extent(NULL, root, path,
7494 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007495 if (ret < 0)
7496 goto out;
7497
7498 slot = path->slots[0];
7499 if (ret == 1) {
7500 if (slot == 0) {
7501 /* can't find the item, must cow */
7502 ret = 0;
7503 goto out;
7504 }
7505 slot--;
7506 }
7507 ret = 0;
7508 leaf = path->nodes[0];
7509 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007510 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007511 key.type != BTRFS_EXTENT_DATA_KEY) {
7512 /* not our file or wrong item type, must cow */
7513 goto out;
7514 }
7515
7516 if (key.offset > offset) {
7517 /* Wrong offset, must cow */
7518 goto out;
7519 }
7520
7521 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7522 found_type = btrfs_file_extent_type(leaf, fi);
7523 if (found_type != BTRFS_FILE_EXTENT_REG &&
7524 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7525 /* not a regular extent, must cow */
7526 goto out;
7527 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007528
7529 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7530 goto out;
7531
Miao Xiee77751a2013-12-27 21:11:50 +08007532 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7533 if (extent_end <= offset)
7534 goto out;
7535
Chris Mason46bfbb52010-05-26 11:04:10 -04007536 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007537 if (disk_bytenr == 0)
7538 goto out;
7539
7540 if (btrfs_file_extent_compression(leaf, fi) ||
7541 btrfs_file_extent_encryption(leaf, fi) ||
7542 btrfs_file_extent_other_encoding(leaf, fi))
7543 goto out;
7544
Ethan Lien78d42952018-05-17 14:58:29 +08007545 /*
7546 * Do the same check as in btrfs_cross_ref_exist but without the
7547 * unnecessary search.
7548 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007549 if (!strict &&
7550 (btrfs_file_extent_generation(leaf, fi) <=
7551 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007552 goto out;
7553
Chris Mason46bfbb52010-05-26 11:04:10 -04007554 backref_offset = btrfs_file_extent_offset(leaf, fi);
7555
Josef Bacik7ee9e442013-06-21 16:37:03 -04007556 if (orig_start) {
7557 *orig_start = key.offset - backref_offset;
7558 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7559 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7560 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007561
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007562 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007563 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007564
7565 num_bytes = min(offset + *len, extent_end) - offset;
7566 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7567 u64 range_end;
7568
Jeff Mahoneyda170662016-06-15 09:22:56 -04007569 range_end = round_up(offset + num_bytes,
7570 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007571 ret = test_range_bit(io_tree, offset, range_end,
7572 EXTENT_DELALLOC, 0, NULL);
7573 if (ret) {
7574 ret = -EAGAIN;
7575 goto out;
7576 }
7577 }
7578
Josef Bacik1bda19e2013-10-18 12:10:36 -04007579 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007580
7581 /*
7582 * look for other files referencing this extent, if we
7583 * find any we must cow
7584 */
Josef Bacik00361582013-08-14 14:02:47 -04007585
Liu Boe4c3b2d2017-01-30 12:25:28 -08007586 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007587 key.offset - backref_offset, disk_bytenr,
7588 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007589 if (ret) {
7590 ret = 0;
7591 goto out;
7592 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007593
7594 /*
7595 * adjust disk_bytenr and num_bytes to cover just the bytes
7596 * in this extent we are about to write. If there
7597 * are any csums in that range we have to cow in order
7598 * to keep the csums correct
7599 */
7600 disk_bytenr += backref_offset;
7601 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007602 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7603 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007604 /*
7605 * all of the above have passed, it is safe to overwrite this extent
7606 * without cow
7607 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007608 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007609 ret = 1;
7610out:
7611 btrfs_free_path(path);
7612 return ret;
7613}
7614
Josef Bacikeb838e72012-07-31 16:28:48 -04007615static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007616 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007617{
7618 struct btrfs_ordered_extent *ordered;
7619 int ret = 0;
7620
7621 while (1) {
7622 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007623 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 /*
7625 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007626 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 * extents in this range.
7628 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007629 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 lockend - lockstart + 1);
7631
7632 /*
7633 * We need to make sure there are no buffered pages in this
7634 * range either, we could have raced between the invalidate in
7635 * generic_file_direct_write and locking the extent. The
7636 * invalidate needs to happen so that reads after a write do not
7637 * get stale data.
7638 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007639 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007640 (!writing || !filemap_range_has_page(inode->i_mapping,
7641 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007642 break;
7643
7644 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007645 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007646
7647 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007648 /*
7649 * If we are doing a DIO read and the ordered extent we
7650 * found is for a buffered write, we can not wait for it
7651 * to complete and retry, because if we do so we can
7652 * deadlock with concurrent buffered writes on page
7653 * locks. This happens only if our DIO read covers more
7654 * than one extent map, if at this point has already
7655 * created an ordered extent for a previous extent map
7656 * and locked its range in the inode's io tree, and a
7657 * concurrent write against that previous extent map's
7658 * range and this range started (we unlock the ranges
7659 * in the io tree only when the bios complete and
7660 * buffered writes always lock pages before attempting
7661 * to lock range in the io tree).
7662 */
7663 if (writing ||
7664 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007665 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007666 else
7667 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007668 btrfs_put_ordered_extent(ordered);
7669 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007671 * We could trigger writeback for this range (and wait
7672 * for it to complete) and then invalidate the pages for
7673 * this range (through invalidate_inode_pages2_range()),
7674 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007675 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007676 * triggered a readahead) on a page lock due to an
7677 * ordered dio extent we created before but did not have
7678 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007679 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007680 * ordered extent to complete while holding a lock on
7681 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007683 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007684 }
7685
Filipe Mananaade77022016-02-18 14:28:55 +00007686 if (ret)
7687 break;
7688
Josef Bacikeb838e72012-07-31 16:28:48 -04007689 cond_resched();
7690 }
7691
7692 return ret;
7693}
7694
Liu Bo6f9994d2017-01-31 07:50:22 -08007695/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007696static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7697 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007698 u64 block_len, u64 orig_block_len,
7699 u64 ram_bytes, int compress_type,
7700 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007701{
7702 struct extent_map_tree *em_tree;
7703 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007704 int ret;
7705
Liu Bo6f9994d2017-01-31 07:50:22 -08007706 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7707 type == BTRFS_ORDERED_COMPRESSED ||
7708 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007709 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007710
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007711 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007712 em = alloc_extent_map();
7713 if (!em)
7714 return ERR_PTR(-ENOMEM);
7715
7716 em->start = start;
7717 em->orig_start = orig_start;
7718 em->len = len;
7719 em->block_len = block_len;
7720 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007721 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007722 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007723 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007724 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007725 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007726 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007727 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007728 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7729 em->compress_type = compress_type;
7730 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007731
7732 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007733 btrfs_drop_extent_cache(inode, em->start,
7734 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007736 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007737 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007738 /*
7739 * The caller has taken lock_extent(), who could race with us
7740 * to add em?
7741 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 } while (ret == -EEXIST);
7743
7744 if (ret) {
7745 free_extent_map(em);
7746 return ERR_PTR(ret);
7747 }
7748
Liu Bo6f9994d2017-01-31 07:50:22 -08007749 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007750 return em;
7751}
7752
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007753
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007754static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007755 struct inode *inode,
7756 struct btrfs_dio_data *dio_data,
7757 u64 start, u64 len)
7758{
7759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7760 struct extent_map *em = *map;
7761 int ret = 0;
7762
7763 /*
7764 * We don't allocate a new extent in the following cases
7765 *
7766 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7767 * existing extent.
7768 * 2) The extent is marked as PREALLOC. We're good to go here and can
7769 * just use the extent.
7770 *
7771 */
7772 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7773 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7774 em->block_start != EXTENT_MAP_HOLE)) {
7775 int type;
7776 u64 block_start, orig_start, orig_block_len, ram_bytes;
7777
7778 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7779 type = BTRFS_ORDERED_PREALLOC;
7780 else
7781 type = BTRFS_ORDERED_NOCOW;
7782 len = min(len, em->len - (start - em->start));
7783 block_start = em->block_start + (start - em->start);
7784
7785 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007786 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007787 btrfs_inc_nocow_writers(fs_info, block_start)) {
7788 struct extent_map *em2;
7789
Nikolay Borisov64f54182020-06-03 08:55:31 +03007790 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007791 orig_start, block_start,
7792 len, orig_block_len,
7793 ram_bytes, type);
7794 btrfs_dec_nocow_writers(fs_info, block_start);
7795 if (type == BTRFS_ORDERED_PREALLOC) {
7796 free_extent_map(em);
7797 *map = em = em2;
7798 }
7799
7800 if (em2 && IS_ERR(em2)) {
7801 ret = PTR_ERR(em2);
7802 goto out;
7803 }
7804 /*
7805 * For inode marked NODATACOW or extent marked PREALLOC,
7806 * use the existing or preallocated extent, so does not
7807 * need to adjust btrfs_space_info's bytes_may_use.
7808 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007809 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007810 goto skip_cow;
7811 }
7812 }
7813
7814 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007815 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007816 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007817 if (IS_ERR(em)) {
7818 ret = PTR_ERR(em);
7819 goto out;
7820 }
7821
7822 len = min(len, em->len - (start - em->start));
7823
7824skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007825 /*
7826 * Need to update the i_size under the extent lock so buffered
7827 * readers will get the updated i_size when we unlock.
7828 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007829 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007830 i_size_write(inode, start + len);
7831
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007832 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007833out:
7834 return ret;
7835}
7836
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007837static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7838 loff_t length, unsigned int flags, struct iomap *iomap,
7839 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007843 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307844 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007845 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007846 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007847 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007848 u64 len = length;
7849 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007850
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007851 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007852 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007853
Josef Bacikc3298612012-08-03 16:49:19 -04007854 lockstart = start;
7855 lockend = start + len - 1;
7856
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007857 /*
7858 * The generic stuff only does filemap_write_and_wait_range, which
7859 * isn't enough if we've written compressed pages to this area, so we
7860 * need to flush the dirty pages again to make absolutely sure that any
7861 * outstanding dirty pages are on disk.
7862 */
7863 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7864 &BTRFS_I(inode)->runtime_flags)) {
7865 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7866 start + length - 1);
7867 if (ret)
7868 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007869 }
7870
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007871 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7872 if (!dio_data)
7873 return -ENOMEM;
7874
7875 dio_data->length = length;
7876 if (write) {
7877 dio_data->reserve = round_up(length, fs_info->sectorsize);
7878 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7879 &dio_data->data_reserved,
7880 start, dio_data->reserve);
7881 if (ret) {
7882 extent_changeset_free(dio_data->data_reserved);
7883 kfree(dio_data);
7884 return ret;
7885 }
7886 }
7887 iomap->private = dio_data;
7888
7889
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 /*
7891 * If this errors out it's because we couldn't invalidate pagecache for
7892 * this range and we need to fallback to buffered.
7893 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007894 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007895 ret = -ENOTBLK;
7896 goto err;
7897 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007898
Omar Sandoval39b07b52019-12-02 17:34:23 -08007899 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007900 if (IS_ERR(em)) {
7901 ret = PTR_ERR(em);
7902 goto unlock_err;
7903 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904
7905 /*
7906 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7907 * io. INLINE is special, and we could probably kludge it in here, but
7908 * it's still buffered so for safety lets just fall back to the generic
7909 * buffered path.
7910 *
7911 * For COMPRESSED we _have_ to read the entire extent in so we can
7912 * decompress it, so there will be buffering required no matter what we
7913 * do, so go ahead and fallback to buffered.
7914 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007915 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916 * to buffered IO. Don't blame me, this is the price we pay for using
7917 * the generic code.
7918 */
7919 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7920 em->block_start == EXTENT_MAP_INLINE) {
7921 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007922 ret = -ENOTBLK;
7923 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 }
7925
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007926 len = min(len, em->len - (start - em->start));
7927 if (write) {
7928 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7929 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007930 if (ret < 0)
7931 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007932 unlock_extents = true;
7933 /* Recalc len in case the new em is smaller than requested */
7934 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007935 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007936 /*
7937 * We need to unlock only the end area that we aren't using.
7938 * The rest is going to be unlocked by the endio routine.
7939 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007940 lockstart = start + len;
7941 if (lockstart < lockend)
7942 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007943 }
7944
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007945 if (unlock_extents)
7946 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7947 lockstart, lockend, &cached_state);
7948 else
7949 free_extent_state(cached_state);
7950
7951 /*
7952 * Translate extent map information to iomap.
7953 * We trim the extents (and move the addr) even though iomap code does
7954 * that, since we have locked only the parts we are performing I/O in.
7955 */
7956 if ((em->block_start == EXTENT_MAP_HOLE) ||
7957 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7958 iomap->addr = IOMAP_NULL_ADDR;
7959 iomap->type = IOMAP_HOLE;
7960 } else {
7961 iomap->addr = em->block_start + (start - em->start);
7962 iomap->type = IOMAP_MAPPED;
7963 }
7964 iomap->offset = start;
Anand Jaind24fa5c2021-08-24 13:05:19 +08007965 iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007966 iomap->length = len;
7967
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007968 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007969 iomap->flags |= IOMAP_F_ZONE_APPEND;
7970
Josef Bacik4b46fce2010-05-23 11:00:55 -04007971 free_extent_map(em);
7972
7973 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007974
7975unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007976 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7977 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007978err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007979 if (dio_data) {
7980 btrfs_delalloc_release_space(BTRFS_I(inode),
7981 dio_data->data_reserved, start,
7982 dio_data->reserve, true);
7983 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7984 extent_changeset_free(dio_data->data_reserved);
7985 kfree(dio_data);
7986 }
7987 return ret;
7988}
7989
7990static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7991 ssize_t written, unsigned int flags, struct iomap *iomap)
7992{
7993 int ret = 0;
7994 struct btrfs_dio_data *dio_data = iomap->private;
7995 size_t submitted = dio_data->submitted;
7996 const bool write = !!(flags & IOMAP_WRITE);
7997
7998 if (!write && (iomap->type == IOMAP_HOLE)) {
7999 /* If reading from a hole, unlock and return */
8000 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
8001 goto out;
8002 }
8003
8004 if (submitted < length) {
8005 pos += submitted;
8006 length -= submitted;
8007 if (write)
8008 __endio_write_update_ordered(BTRFS_I(inode), pos,
8009 length, false);
8010 else
8011 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
8012 pos + length - 1);
8013 ret = -ENOTBLK;
8014 }
8015
8016 if (write) {
8017 if (dio_data->reserve)
8018 btrfs_delalloc_release_space(BTRFS_I(inode),
8019 dio_data->data_reserved, pos,
8020 dio_data->reserve, true);
8021 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
8022 extent_changeset_free(dio_data->data_reserved);
8023 }
8024out:
8025 kfree(dio_data);
8026 iomap->private = NULL;
8027
Josef Bacikeb838e72012-07-31 16:28:48 -04008028 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008029}
8030
Omar Sandoval769b4f22020-04-16 14:46:22 -07008031static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8032{
8033 /*
8034 * This implies a barrier so that stores to dio_bio->bi_status before
8035 * this and loads of dio_bio->bi_status after this are fully ordered.
8036 */
8037 if (!refcount_dec_and_test(&dip->refs))
8038 return;
8039
Naohiro Aotacfe94442021-02-04 19:21:59 +09008040 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008041 __endio_write_update_ordered(BTRFS_I(dip->inode),
8042 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07008043 dip->bytes,
8044 !dip->dio_bio->bi_status);
8045 } else {
8046 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
8047 dip->logical_offset,
8048 dip->logical_offset + dip->bytes - 1);
8049 }
8050
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008051 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008052 kfree(dip);
8053}
8054
Omar Sandoval77d5d682020-04-16 14:46:25 -07008055static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8056 int mirror_num,
8057 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08008058{
Omar Sandoval77d5d682020-04-16 14:46:25 -07008059 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008060 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008061 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008062
Mike Christie37226b22016-06-05 14:31:52 -05008063 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008064
Omar Sandoval5c047a62020-04-16 14:46:24 -07008065 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08008066 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02008067 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008068
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008069 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07008070 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07008071 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008072 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08008073 return ret;
8074}
8075
Omar Sandoval769b4f22020-04-16 14:46:22 -07008076static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
8077 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008078 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08008079{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008080 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8081 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04008082 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008083 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8084 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07008085 struct bio_vec bvec;
8086 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008087 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008088 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008089 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008090
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008091 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
8092 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
Liu Bo17347ce2017-05-15 15:33:27 -07008094 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8095 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008096 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07008097 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008098 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008099 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07008100 bio_offset, bvec.bv_page,
8101 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008102 clean_io_failure(fs_info, failure_tree, io_tree,
8103 start, bvec.bv_page,
8104 btrfs_ino(BTRFS_I(inode)),
8105 pgoff);
8106 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08008107 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008108
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008109 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08008110 ret = btrfs_repair_one_sector(inode,
8111 &io_bio->bio,
8112 start - io_bio->logical,
8113 bvec.bv_page, pgoff,
8114 start, io_bio->mirror_num,
8115 submit_dio_repair_bio);
8116 if (ret)
8117 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008118 }
8119 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008120 ASSERT(bio_offset + sectorsize > bio_offset);
8121 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008124 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008125 return err;
8126}
8127
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008128static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08008129 const u64 offset, const u64 bytes,
8130 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008131{
Qu Wenruoe65f1522021-04-01 15:15:06 +08008132 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8133 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00008134}
8135
Qu Wenruo8896a082020-10-21 14:24:53 +08008136static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008137 struct bio *bio,
8138 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008139{
Qu Wenruo1941b642020-12-02 14:47:57 +08008140 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008141}
8142
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008143static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008144{
8145 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008146 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008147
Miao Xie8b110e32014-09-12 18:44:03 +08008148 if (err)
8149 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008150 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008151 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008152 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008153 bio->bi_iter.bi_size, err);
8154
Omar Sandoval769b4f22020-04-16 14:46:22 -07008155 if (bio_op(bio) == REQ_OP_READ) {
8156 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008157 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008158 }
8159
Omar Sandoval769b4f22020-04-16 14:46:22 -07008160 if (err)
8161 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008162
Naohiro Aota544d24f2021-02-04 19:22:06 +09008163 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8164
Miao Xiee65e1532010-11-22 03:04:43 +00008165 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008166 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008167}
8168
David Sterbad0ee3932018-03-08 14:35:48 +01008169static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8170 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008173 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008174 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008175 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008176
Liu Bo4c274bc2017-11-01 17:19:27 -06008177 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008178 if (async_submit)
8179 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8180
Josef Bacik5fd02042012-05-02 14:00:54 -04008181 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008182 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008183 if (ret)
8184 goto err;
8185 }
Miao Xiee65e1532010-11-22 03:04:43 +00008186
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008187 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008188 goto map;
8189
8190 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008191 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008192 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008193 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008194 } else if (write) {
8195 /*
8196 * If we aren't doing async submit, calculate the csum of the
8197 * bio now.
8198 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008199 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008200 if (ret)
8201 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008202 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008203 u64 csum_offset;
8204
8205 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008206 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008207 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008208 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008209 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008210map:
Chris Mason08635ba2019-07-10 12:28:14 -07008211 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008212err:
Miao Xiee65e1532010-11-22 03:04:43 +00008213 return ret;
8214}
8215
Omar Sandovalc36cac22020-04-16 14:46:13 -07008216/*
8217 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8218 * or ordered extents whether or not we submit any bios.
8219 */
8220static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8221 struct inode *inode,
8222 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008223{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008224 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008225 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8226 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008227 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008228
Omar Sandoval85879572020-04-16 14:46:21 -07008229 dip_size = sizeof(*dip);
8230 if (!write && csum) {
8231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008232 size_t nblocks;
8233
David Sterba265fdfa2020-07-01 21:19:09 +02008234 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008235 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008236 }
8237
8238 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008239 if (!dip)
8240 return NULL;
8241
Omar Sandovalc36cac22020-04-16 14:46:13 -07008242 dip->inode = inode;
8243 dip->logical_offset = file_offset;
8244 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008245 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008246 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008247 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008248 return dip;
8249}
8250
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07008251static blk_qc_t btrfs_submit_direct(const struct iomap_iter *iter,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008252 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008253{
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07008254 struct inode *inode = iter->inode;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008255 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008257 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8258 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008259 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008260 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008261 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008262 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008263 u64 submit_len;
Naohiro Aota42b5d732021-07-21 21:43:34 +09008264 u64 clone_offset = 0;
8265 u64 clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008266 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308267 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008268 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008269 struct btrfs_io_geometry geom;
Christoph Hellwiga6d3d492021-08-10 18:33:10 -07008270 struct btrfs_dio_data *dio_data = iter->iomap.private;
Michal Rostecki42034312021-01-27 14:57:27 +01008271 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008272
Omar Sandovalc36cac22020-04-16 14:46:13 -07008273 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8274 if (!dip) {
8275 if (!write) {
8276 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8277 file_offset + dio_bio->bi_iter.bi_size - 1);
8278 }
8279 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008280 bio_endio(dio_bio);
8281 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008282 }
8283
Josef Bacik334c16d2020-10-16 11:29:14 -04008284 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008285 /*
8286 * Load the csums up front to reduce csum tree searches and
8287 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008288 *
8289 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008290 */
Qu Wenruo62751932020-12-02 14:48:06 +08008291 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008292 if (status != BLK_STS_OK)
8293 goto out_err;
8294 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008295
Omar Sandoval769b4f22020-04-16 14:46:22 -07008296 start_sector = dio_bio->bi_iter.bi_sector;
8297 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008298
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008299 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008300 logical = start_sector << 9;
8301 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8302 if (IS_ERR(em)) {
8303 status = errno_to_blk_status(PTR_ERR(em));
8304 em = NULL;
8305 goto out_err_em;
8306 }
8307 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008308 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008309 if (ret) {
8310 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008311 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008312 }
Omar Sandoval769b4f22020-04-16 14:46:22 -07008313
Naohiro Aota42b5d732021-07-21 21:43:34 +09008314 clone_len = min(submit_len, geom.len);
8315 ASSERT(clone_len <= UINT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008316
Liu Bo725130b2017-05-16 09:51:39 -07008317 /*
8318 * This will never fail as it's passing GPF_NOFS and
8319 * the allocation is backed by btrfs_bioset.
8320 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008321 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008322 bio->bi_private = dip;
8323 bio->bi_end_io = btrfs_end_dio_bio;
8324 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008325
Naohiro Aota544d24f2021-02-04 19:22:06 +09008326 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8327 status = extract_ordered_extent(BTRFS_I(inode), bio,
8328 file_offset);
8329 if (status) {
8330 bio_put(bio);
8331 goto out_err;
8332 }
8333 }
8334
Liu Bo725130b2017-05-16 09:51:39 -07008335 ASSERT(submit_len >= clone_len);
8336 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008337
Liu Bo725130b2017-05-16 09:51:39 -07008338 /*
8339 * Increase the count before we submit the bio so we know
8340 * the end IO handler won't happen before we increase the
8341 * count. Otherwise, the dip might get freed before we're
8342 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008343 *
8344 * We transfer the initial reference to the last bio, so we
8345 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008346 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008347 if (submit_len > 0) {
8348 refcount_inc(&dip->refs);
8349 /*
8350 * If we are submitting more than one bio, submit them
8351 * all asynchronously. The exception is RAID 5 or 6, as
8352 * asynchronous checksums make it difficult to collect
8353 * full stripe writes.
8354 */
8355 if (!raid56)
8356 async_submit = 1;
8357 }
Miao Xiee65e1532010-11-22 03:04:43 +00008358
David Sterbad0ee3932018-03-08 14:35:48 +01008359 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008360 async_submit);
8361 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008362 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008363 if (submit_len > 0)
8364 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008365 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008366 }
Liu Bo725130b2017-05-16 09:51:39 -07008367
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008368 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008369 clone_offset += clone_len;
8370 start_sector += clone_len >> 9;
8371 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008372
8373 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008374 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008375 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008376
Michal Rostecki42034312021-01-27 14:57:27 +01008377out_err_em:
8378 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008379out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008380 dip->dio_bio->bi_status = status;
8381 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008382
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008383 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008384}
8385
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008386const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008387 .iomap_begin = btrfs_dio_iomap_begin,
8388 .iomap_end = btrfs_dio_iomap_end,
8389};
8390
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008391const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008392 .submit_io = btrfs_submit_direct,
8393};
8394
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008395static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008396 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008397{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008398 int ret;
8399
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008400 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008401 if (ret)
8402 return ret;
8403
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008404 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008405}
8406
Chris Mason9ebefb182007-06-15 13:50:00 -04008407int btrfs_readpage(struct file *file, struct page *page)
8408{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008409 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8410 u64 start = page_offset(page);
8411 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008412 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008413 int ret;
8414
Nikolay Borisov0f208812020-09-14 14:39:16 +03008415 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8416
Qu Wenruo390ed292021-04-14 16:42:15 +08008417 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8418 if (bio_ctrl.bio)
8419 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008420 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008421}
Chris Mason1832a6d2007-12-21 16:27:21 -05008422
Chris Mason39279cc2007-06-12 06:35:45 -04008423static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8424{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008425 struct inode *inode = page->mapping->host;
8426 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008427
8428 if (current->flags & PF_MEMALLOC) {
8429 redirty_page_for_writepage(wbc, page);
8430 unlock_page(page);
8431 return 0;
8432 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008433
8434 /*
8435 * If we are under memory pressure we will call this directly from the
8436 * VM, we need to make sure we have the inode referenced for the ordered
8437 * extent. If not just return like we didn't do anything.
8438 */
8439 if (!igrab(inode)) {
8440 redirty_page_for_writepage(wbc, page);
8441 return AOP_WRITEPAGE_ACTIVATE;
8442 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008443 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008444 btrfs_add_delayed_iput(inode);
8445 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008446}
Chris Mason39279cc2007-06-12 06:35:45 -04008447
Eric Sandeen48a3b632013-04-25 20:41:01 +00008448static int btrfs_writepages(struct address_space *mapping,
8449 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008450{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008451 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008452}
8453
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008454static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008455{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008456 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008457}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008458
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008459/*
8460 * For releasepage() and invalidatepage() we have a race window where
8461 * end_page_writeback() is called but the subpage spinlock is not yet released.
8462 * If we continue to release/invalidate the page, we could cause use-after-free
8463 * for subpage spinlock. So this function is to spin and wait for subpage
8464 * spinlock.
8465 */
8466static void wait_subpage_spinlock(struct page *page)
8467{
8468 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8469 struct btrfs_subpage *subpage;
8470
8471 if (fs_info->sectorsize == PAGE_SIZE)
8472 return;
8473
8474 ASSERT(PagePrivate(page) && page->private);
8475 subpage = (struct btrfs_subpage *)page->private;
8476
8477 /*
8478 * This may look insane as we just acquire the spinlock and release it,
8479 * without doing anything. But we just want to make sure no one is
8480 * still holding the subpage spinlock.
8481 * And since the page is not dirty nor writeback, and we have page
8482 * locked, the only possible way to hold a spinlock is from the endio
8483 * function to clear page writeback.
8484 *
8485 * Here we just acquire the spinlock so that all existing callers
8486 * should exit and we're safe to release/invalidate the page.
8487 */
8488 spin_lock_irq(&subpage->lock);
8489 spin_unlock_irq(&subpage->lock);
8490}
8491
Chris Masone6dcd2d2008-07-17 12:53:50 -04008492static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008493{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008494 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008495
8496 if (ret == 1) {
8497 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008498 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008499 }
Chris Masona52d9a82007-08-27 16:49:44 -04008500 return ret;
8501}
Chris Mason39279cc2007-06-12 06:35:45 -04008502
Chris Masone6dcd2d2008-07-17 12:53:50 -04008503static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8504{
Chris Mason98509cf2008-09-11 15:51:43 -04008505 if (PageWriteback(page) || PageDirty(page))
8506 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008507 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008508}
8509
Roman Gushchinf8e66082020-03-04 16:57:35 -08008510#ifdef CONFIG_MIGRATION
8511static int btrfs_migratepage(struct address_space *mapping,
8512 struct page *newpage, struct page *page,
8513 enum migrate_mode mode)
8514{
8515 int ret;
8516
8517 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8518 if (ret != MIGRATEPAGE_SUCCESS)
8519 return ret;
8520
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008521 if (page_has_private(page))
8522 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008523
Qu Wenruof57ad932021-04-07 19:22:13 +08008524 if (PageOrdered(page)) {
8525 ClearPageOrdered(page);
8526 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008527 }
8528
8529 if (mode != MIGRATE_SYNC_NO_COPY)
8530 migrate_page_copy(newpage, page);
8531 else
8532 migrate_page_states(newpage, page);
8533 return MIGRATEPAGE_SUCCESS;
8534}
8535#endif
8536
Lukas Czernerd47992f2013-05-21 23:17:23 -04008537static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8538 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008539{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008540 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008541 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008542 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008543 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008544 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008545 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008546 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008547 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008548
Chris Mason8b62b722009-09-02 16:53:46 -04008549 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008550 * We have page locked so no new ordered extent can be created on this
8551 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008552 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008553 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008554 * Furthermore, endio function won't skip page which has Ordered
8555 * (Private2) already cleared, so it's possible for endio and
8556 * invalidatepage to do the same ordered extent accounting twice
8557 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008558 *
8559 * So here we wait for any submitted bios to finish, so that we won't
8560 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008561 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008562 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008563 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008564
Qu Wenruobcd77452021-05-31 16:50:55 +08008565 /*
8566 * For subpage case, we have call sites like
8567 * btrfs_punch_hole_lock_range() which passes range not aligned to
8568 * sectorsize.
8569 * If the range doesn't cover the full page, we don't need to and
8570 * shouldn't clear page extent mapped, as page->private can still
8571 * record subpage dirty bits for other part of the range.
8572 *
8573 * For cases that can invalidate the full even the range doesn't
8574 * cover the full page, like invalidating the last page, we're
8575 * still safe to wait for ordered extent to finish.
8576 */
8577 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008578 btrfs_releasepage(page, GFP_NOFS);
8579 return;
8580 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008581
8582 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008583 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008584
Qu Wenruo3b835842021-04-06 19:54:53 +08008585 cur = page_start;
8586 while (cur < page_end) {
8587 struct btrfs_ordered_extent *ordered;
8588 bool delete_states;
8589 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008590 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008591
8592 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8593 page_end + 1 - cur);
8594 if (!ordered) {
8595 range_end = page_end;
8596 /*
8597 * No ordered extent covering this range, we are safe
8598 * to delete all extent states in the range.
8599 */
8600 delete_states = true;
8601 goto next;
8602 }
8603 if (ordered->file_offset > cur) {
8604 /*
8605 * There is a range between [cur, oe->file_offset) not
8606 * covered by any ordered extent.
8607 * We are safe to delete all extent states, and handle
8608 * the ordered extent in the next iteration.
8609 */
8610 range_end = ordered->file_offset - 1;
8611 delete_states = true;
8612 goto next;
8613 }
8614
8615 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8616 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008617 ASSERT(range_end + 1 - cur < U32_MAX);
8618 range_len = range_end + 1 - cur;
8619 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008620 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008621 * If Ordered (Private2) is cleared, it means endio has
8622 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008623 * We can't delete the extent states as
8624 * btrfs_finish_ordered_io() may still use some of them.
8625 */
8626 delete_states = false;
8627 goto next;
8628 }
Qu Wenruob945a462021-05-31 16:50:46 +08008629 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008630
Chris Masoneb84ae02008-07-17 13:53:27 -04008631 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008632 * IO on this page will never be started, so we need to account
8633 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8634 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008635 *
8636 * This will also unlock the range for incoming
8637 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008638 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008639 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008640 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008641 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008642 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008643 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008644
8645 spin_lock_irq(&inode->ordered_tree.lock);
8646 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8647 ordered->truncated_len = min(ordered->truncated_len,
8648 cur - ordered->file_offset);
8649 spin_unlock_irq(&inode->ordered_tree.lock);
8650
8651 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008652 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008653 btrfs_finish_ordered_io(ordered);
8654 /*
8655 * The ordered extent has finished, now we're again
8656 * safe to delete all extent states of the range.
8657 */
8658 delete_states = true;
8659 } else {
8660 /*
8661 * btrfs_finish_ordered_io() will get executed by endio
8662 * of other pages, thus we can't delete extent states
8663 * anymore
8664 */
8665 delete_states = false;
8666 }
8667next:
8668 if (ordered)
8669 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008670 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008671 * Qgroup reserved space handler
8672 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008673 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008674 * 1) Already written to disk or bio already finished
8675 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8676 * Qgroup will be handled by its qgroup_record then.
8677 * btrfs_qgroup_free_data() call will do nothing here.
8678 *
8679 * 2) Not written to disk yet
8680 * Then btrfs_qgroup_free_data() call will clear the
8681 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8682 * reserved data space.
8683 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008684 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008685 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008686 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008687 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008688 EXTENT_DELALLOC | EXTENT_UPTODATE |
8689 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008690 delete_states, &cached_state);
8691 }
8692 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008693 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008694 /*
8695 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008696 * should not have Ordered (Private2) anymore, or the above iteration
8697 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008698 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008699 ASSERT(!PageOrdered(page));
Qu Wenruo3b835842021-04-06 19:54:53 +08008700 if (!inode_evicting)
8701 __btrfs_releasepage(page, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04008702 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008703 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008704}
8705
Chris Mason9ebefb182007-06-15 13:50:00 -04008706/*
8707 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8708 * called from a page fault handler when a page is first dirtied. Hence we must
8709 * be careful to check for EOF conditions here. We set the page up correctly
8710 * for a written page which means we get ENOSPC checking when writing into
8711 * holes and correct delalloc and unwritten extent mapping on filesystems that
8712 * support these features.
8713 *
8714 * We are not allowed to take the i_mutex here so we have to play games to
8715 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008716 * truncate_setsize() writes the inode size before removing pages, once we have
8717 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008718 * beyond EOF, then the page is guaranteed safe against truncation until we
8719 * unlock the page.
8720 */
Souptick Joardera528a242018-06-06 19:54:44 +05308721vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008722{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008723 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008724 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008726 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8727 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008728 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008729 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008730 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008731 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308732 vm_fault_t ret;
8733 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008734 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308735 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008736 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308738 u64 end;
8739
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008740 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008741
Jan Karab2b5ef52012-06-12 16:20:45 +02008742 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008743 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008744 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308745 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008746
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308747 /*
8748 * Reserving delalloc space after obtaining the page lock can lead to
8749 * deadlock. For example, if a dirty page is locked by this function
8750 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8751 * dirty page write out, then the btrfs_writepage() function could
8752 * end up waiting indefinitely to get a lock on the page currently
8753 * being processed by btrfs_page_mkwrite() function.
8754 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008755 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8756 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308757 if (!ret2) {
8758 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008759 reserved = 1;
8760 }
Souptick Joardera528a242018-06-06 19:54:44 +05308761 if (ret2) {
8762 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008763 if (reserved)
8764 goto out;
8765 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008766 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008767
Nick Piggin56a76f82009-03-31 15:23:23 -07008768 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008769again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008770 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008771 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008772 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008773
Chris Mason9ebefb182007-06-15 13:50:00 -04008774 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008776 /* page got truncated out from underneath us */
8777 goto out_unlock;
8778 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779 wait_on_page_writeback(page);
8780
David Sterbaff13db42015-12-03 14:30:40 +01008781 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008782 ret2 = set_page_extent_mapped(page);
8783 if (ret2 < 0) {
8784 ret = vmf_error(ret2);
8785 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8786 goto out_unlock;
8787 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008788
Chris Masoneb84ae02008-07-17 13:53:27 -04008789 /*
8790 * we can't set the delalloc bits if there are pending ordered
8791 * extents. Drop our locks and wait for them to finish
8792 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008793 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8794 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008795 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008796 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008797 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008798 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008799 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008800 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801 btrfs_put_ordered_extent(ordered);
8802 goto again;
8803 }
8804
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008805 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008806 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008807 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008808 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308809 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008810 btrfs_delalloc_release_space(BTRFS_I(inode),
8811 data_reserved, page_start,
8812 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308813 }
8814 }
8815
Josef Bacikfbf19082009-10-01 17:10:23 -04008816 /*
Liu Bo54160342016-12-13 12:15:19 -08008817 * page_mkwrite gets called when the page is firstly dirtied after it's
8818 * faulted in, but write(2) could also dirty a page and set delalloc
8819 * bits, thus in this case for space account reason, we still need to
8820 * clear any delalloc bits within this page range since we have to
8821 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008822 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308823 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008824 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8825 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008826
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008827 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008828 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308829 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008830 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008831 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008832 ret = VM_FAULT_SIGBUS;
8833 goto out_unlock;
8834 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008835
8836 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008837 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008838 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008839 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008840 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008841
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008842 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008843 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 }
Chris Mason247e7432008-07-17 12:53:51 -04008846 ClearPageChecked(page);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008847 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8848 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008849
Filipe Mananabc0939f2021-02-23 12:08:48 +00008850 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008851
David Sterbae43bbe52017-12-12 21:43:52 +01008852 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008853 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008854
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008855 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8856 sb_end_pagefault(inode->i_sb);
8857 extent_changeset_free(data_reserved);
8858 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008859
8860out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008861 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008862 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008863out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008864 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008865 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008866 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008867out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008868 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008869 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008870 return ret;
8871}
8872
Filipe Manana213e8c52018-02-06 20:40:31 +00008873static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008874{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008876 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008877 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008878 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008879 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008880 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008881 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008882 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008883
Filipe Manana213e8c52018-02-06 20:40:31 +00008884 if (!skip_writeback) {
8885 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8886 (u64)-1);
8887 if (ret)
8888 return ret;
8889 }
Chris Mason39279cc2007-06-12 06:35:45 -04008890
Josef Bacikfcb80c22011-05-03 10:40:22 -04008891 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008892 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8893 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008894 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008895 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008896 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008897 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008898 * be free'd up by the truncate operation, but also have some slack
8899 * space reserved in case it uses space during the truncate (thank you
8900 * very much snapshotting).
8901 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008902 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008903 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008904 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008905 * doesn't end up using space reserved for updating the inode. We also
8906 * need to be able to stop the transaction and start a new one, which
8907 * means we need to be able to update the inode several times, and we
8908 * have no idea of knowing how many times that will be, so we can't just
8909 * reserve 1 item for the entirety of the operation, so that has to be
8910 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008911 *
8912 * So that leaves us with
8913 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008914 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008915 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008916 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008917 * updating the inode.
8918 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008919 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008920 if (!rsv)
8921 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008922 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008923 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008924
Josef Bacik907cbce2011-08-08 13:46:15 -04008925 /*
Josef Bacik07127182011-08-19 10:29:59 -04008926 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008927 * 1 for updating the inode.
8928 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008929 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008930 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008931 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008932 goto out;
8933 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008934
Josef Bacik907cbce2011-08-08 13:46:15 -04008935 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008936 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008937 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008938 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008939
Josef Bacikca7e70f2012-08-27 17:48:15 -04008940 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008941
Yan, Zheng80825102009-11-12 09:35:36 +00008942 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008943 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008944 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008945 BTRFS_EXTENT_DATA_KEY,
8946 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008947 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008948 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008949 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008950
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008951 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008952 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008953 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008954
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008955 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008956 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008957
8958 trans = btrfs_start_transaction(root, 2);
8959 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008960 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008961 trans = NULL;
8962 break;
8963 }
8964
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008965 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008966 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008967 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008968 BUG_ON(ret); /* shouldn't happen */
8969 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008970 }
8971
Josef Bacikddfae632017-10-19 14:16:02 -04008972 /*
8973 * We can't call btrfs_truncate_block inside a trans handle as we could
8974 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8975 * we've truncated everything except the last little bit, and can do
8976 * btrfs_truncate_block and then update the disk_i_size.
8977 */
8978 if (ret == NEED_TRUNCATE_BLOCK) {
8979 btrfs_end_transaction(trans);
8980 btrfs_btree_balance_dirty(fs_info);
8981
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008982 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008983 if (ret)
8984 goto out;
8985 trans = btrfs_start_transaction(root, 1);
8986 if (IS_ERR(trans)) {
8987 ret = PTR_ERR(trans);
8988 goto out;
8989 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008990 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008991 }
8992
Chris Mason917c16b2011-11-08 14:49:59 -05008993 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008994 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008995
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008996 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008997 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008998 if (ret2 && !ret)
8999 ret = ret2;
9000
9001 ret2 = btrfs_end_transaction(trans);
9002 if (ret2 && !ret)
9003 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009004 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009005 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009006out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009007 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01009008 /*
9009 * So if we truncate and then write and fsync we normally would just
9010 * write the extents that changed, which is a problem if we need to
9011 * first truncate that entire inode. So set this flag so we write out
9012 * all of the extents in the inode to the sync log so we're completely
9013 * safe.
9014 *
9015 * If no extents were dropped or trimmed we don't need to force the next
9016 * fsync to truncate all the inode's items from the log and re-log them
9017 * all. This means the truncate operation did not change the file size,
9018 * or changed it to a smaller size but there was only an implicit hole
9019 * between the old i_size and the new i_size, and there were no prealloc
9020 * extents beyond i_size to drop.
9021 */
9022 if (extents_found > 0)
9023 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009024
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009025 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009026}
9027
Sven Wegener3b963622008-06-09 21:57:42 -04009028/*
Chris Masond352ac62008-09-29 15:18:18 -04009029 * create a new subvolume directory/inode (helper for the ioctl).
9030 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009031int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009032 struct btrfs_root *new_root,
Christian Brauner4d4340c2021-07-27 12:48:52 +02009033 struct btrfs_root *parent_root,
9034 struct user_namespace *mnt_userns)
Chris Mason39279cc2007-06-12 06:35:45 -04009035{
Chris Mason39279cc2007-06-12 06:35:45 -04009036 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009037 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009038 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02009039 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04009040
Nikolay Borisov23125102020-12-07 17:32:36 +02009041 err = btrfs_get_free_objectid(new_root, &ino);
9042 if (err < 0)
9043 return err;
9044
Christian Brauner4d4340c2021-07-27 12:48:52 +02009045 inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02009046 ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009047 S_IFDIR | (~current_umask() & S_IRWXUGO),
9048 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009049 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009050 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009051 inode->i_op = &btrfs_dir_inode_operations;
9052 inode->i_fop = &btrfs_dir_file_operations;
9053
Miklos Szeredibfe86842011-10-28 14:13:29 +02009054 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009055 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009056 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009057
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009058 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9059 if (err)
9060 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009061 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009062 new_root->root_key.objectid, err);
9063
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009064 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009065
Yan, Zheng76dda932009-09-21 16:00:26 -04009066 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009067 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009068}
9069
Chris Mason39279cc2007-06-12 06:35:45 -04009070struct inode *btrfs_alloc_inode(struct super_block *sb)
9071{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009072 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009073 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009074 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009075
David Sterba712e36c2017-10-31 17:08:27 +01009076 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009077 if (!ei)
9078 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009079
9080 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009081 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009082 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009083 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009084 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009085 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009086 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009087 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009088 ei->disk_i_size = 0;
9089 ei->flags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -07009090 ei->ro_flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009091 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009092 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009093 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009094 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01009095 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009096 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009097
Josef Bacik9e0baf62011-07-15 15:16:44 +00009098 spin_lock_init(&ei->lock);
9099 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009100 if (sb->s_magic != BTRFS_TEST_MAGIC)
9101 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9102 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009103 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009104 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009105 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009106
Miao Xie16cdcec2011-04-22 18:12:22 +08009107 ei->delayed_node = NULL;
9108
chandan r9cc97d62012-07-04 12:48:07 +05309109 ei->i_otime.tv_sec = 0;
9110 ei->i_otime.tv_nsec = 0;
9111
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009112 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009113 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009114 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9115 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9116 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05009117 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9118 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01009119 ei->io_tree.track_uptodate = true;
9120 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009121 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009122 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009123 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009124 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009125 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009126 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05009127 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009128
9129 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009130}
9131
Josef Bacikaaedb552013-10-11 14:44:09 -04009132#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9133void btrfs_test_destroy_inode(struct inode *inode)
9134{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009135 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009136 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9137}
9138#endif
9139
Al Viro26602ca2019-04-10 15:14:41 -04009140void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009141{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009142 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9143}
9144
Nikolay Borisov633cc812020-09-18 12:15:49 +03009145void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009146{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009147 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03009148 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9149 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009150
Nikolay Borisov633cc812020-09-18 12:15:49 +03009151 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9152 WARN_ON(vfs_inode->i_data.nrpages);
9153 WARN_ON(inode->block_rsv.reserved);
9154 WARN_ON(inode->block_rsv.size);
9155 WARN_ON(inode->outstanding_extents);
9156 WARN_ON(inode->delalloc_bytes);
9157 WARN_ON(inode->new_delalloc_bytes);
9158 WARN_ON(inode->csum_bytes);
9159 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009160
Chris Mason5a3f23d2009-03-31 13:27:11 -04009161 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009162 * This can happen where we create an inode, but somebody else also
9163 * created the same inode and we need to destroy the one we already
9164 * created.
9165 */
9166 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009167 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009168
Chris Masond3977122009-01-05 21:25:51 -05009169 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009170 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009171 if (!ordered)
9172 break;
9173 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009174 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009175 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08009176 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03009177 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009178 btrfs_put_ordered_extent(ordered);
9179 btrfs_put_ordered_extent(ordered);
9180 }
9181 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009182 btrfs_qgroup_check_reserved_leak(inode);
9183 inode_tree_del(inode);
9184 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9185 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9186 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04009187}
9188
Al Viro45321ac2010-06-07 13:43:19 -04009189int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009190{
9191 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009192
Naohiro Aota6379ef92013-06-06 09:56:34 +00009193 if (root == NULL)
9194 return 1;
9195
Liu Bofa6ac872013-02-20 14:10:23 +00009196 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009197 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009198 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009199 else
Al Viro45321ac2010-06-07 13:43:19 -04009200 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009201}
9202
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009203static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009204{
9205 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9206
9207 inode_init_once(&ei->vfs_inode);
9208}
9209
David Sterbae67c7182018-02-19 17:24:18 +01009210void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009211{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009212 /*
9213 * Make sure all delayed rcu free inodes are flushed before we
9214 * destroy cache.
9215 */
9216 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009217 kmem_cache_destroy(btrfs_inode_cachep);
9218 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009219 kmem_cache_destroy(btrfs_path_cachep);
9220 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009221 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009222}
9223
Liu Bof5c29bd2017-11-02 17:21:50 -06009224int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009225{
David Sterba837e1972012-09-07 03:00:48 -06009226 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009227 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009228 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9229 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009230 if (!btrfs_inode_cachep)
9231 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009232
David Sterba837e1972012-09-07 03:00:48 -06009233 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009234 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009235 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009236 if (!btrfs_trans_handle_cachep)
9237 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009238
David Sterba837e1972012-09-07 03:00:48 -06009239 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009240 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009241 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009242 if (!btrfs_path_cachep)
9243 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009244
David Sterba837e1972012-09-07 03:00:48 -06009245 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009246 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009247 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009248 if (!btrfs_free_space_cachep)
9249 goto fail;
9250
Christophe Leroy3acd4852019-08-21 15:05:55 +00009251 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9252 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009253 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009254 if (!btrfs_free_space_bitmap_cachep)
9255 goto fail;
9256
Chris Mason39279cc2007-06-12 06:35:45 -04009257 return 0;
9258fail:
9259 btrfs_destroy_cachep();
9260 return -ENOMEM;
9261}
9262
Christian Brauner549c7292021-01-21 14:19:43 +01009263static int btrfs_getattr(struct user_namespace *mnt_userns,
9264 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009265 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009266{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009267 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009268 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009269 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009270 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009271 u32 bi_flags = BTRFS_I(inode)->flags;
Boris Burkov14605402021-06-30 13:01:49 -07009272 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
Yonghong Song04a87e32017-05-12 15:07:43 -07009273
9274 stat->result_mask |= STATX_BTIME;
9275 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9276 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9277 if (bi_flags & BTRFS_INODE_APPEND)
9278 stat->attributes |= STATX_ATTR_APPEND;
9279 if (bi_flags & BTRFS_INODE_COMPRESS)
9280 stat->attributes |= STATX_ATTR_COMPRESSED;
9281 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9282 stat->attributes |= STATX_ATTR_IMMUTABLE;
9283 if (bi_flags & BTRFS_INODE_NODUMP)
9284 stat->attributes |= STATX_ATTR_NODUMP;
Boris Burkov14605402021-06-30 13:01:49 -07009285 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9286 stat->attributes |= STATX_ATTR_VERITY;
Yonghong Song04a87e32017-05-12 15:07:43 -07009287
9288 stat->attributes_mask |= (STATX_ATTR_APPEND |
9289 STATX_ATTR_COMPRESSED |
9290 STATX_ATTR_IMMUTABLE |
9291 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009292
Christian Braunerc020d2e2021-07-27 12:48:43 +02009293 generic_fillattr(mnt_userns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009294 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009295
9296 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009297 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009298 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009299 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009300 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009301 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009302 return 0;
9303}
9304
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009305static int btrfs_rename_exchange(struct inode *old_dir,
9306 struct dentry *old_dentry,
9307 struct inode *new_dir,
9308 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009310 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009311 struct btrfs_trans_handle *trans;
9312 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009313 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009314 struct inode *new_inode = new_dentry->d_inode;
9315 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009316 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009317 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9318 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009319 u64 old_idx = 0;
9320 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009321 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009322 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009323 bool root_log_pinned = false;
9324 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009325 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009326
NeilBrown3f79f6f2021-08-06 14:26:24 +10009327 /*
9328 * For non-subvolumes allow exchange only within one subvolume, in the
9329 * same inode namespace. Two subvolumes (represented as directory) can
9330 * be exchanged as they're a logical link and have a fixed inode number.
9331 */
9332 if (root != dest &&
9333 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9334 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009335 return -EXDEV;
9336
9337 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009338 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9339 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009340 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341
9342 /*
9343 * We want to reserve the absolute worst case amount of items. So if
9344 * both inodes are subvols and we need to unlink them then that would
9345 * require 4 item modifications, but if they are both normal inodes it
9346 * would require 5 item modifications, so we'll assume their normal
9347 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9348 * should cover the worst case number of items we'll modify.
9349 */
9350 trans = btrfs_start_transaction(root, 12);
9351 if (IS_ERR(trans)) {
9352 ret = PTR_ERR(trans);
9353 goto out_notrans;
9354 }
9355
Josef Bacik00aa8e82021-03-12 15:25:02 -05009356 if (dest != root) {
9357 ret = btrfs_record_root_in_trans(trans, dest);
9358 if (ret)
9359 goto out_fail;
9360 }
Josef Bacik3e174092019-11-15 15:43:06 -05009361
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009362 /*
9363 * We need to find a free sequence number both in the source and
9364 * in the destination directory for the exchange.
9365 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009366 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009367 if (ret)
9368 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009369 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009370 if (ret)
9371 goto out_fail;
9372
9373 BTRFS_I(old_inode)->dir_index = 0ULL;
9374 BTRFS_I(new_inode)->dir_index = 0ULL;
9375
9376 /* Reference for the source. */
9377 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9378 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009379 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009380 } else {
9381 ret = btrfs_insert_inode_ref(trans, dest,
9382 new_dentry->d_name.name,
9383 new_dentry->d_name.len,
9384 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009385 btrfs_ino(BTRFS_I(new_dir)),
9386 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009387 if (ret)
9388 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009389 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009390 }
9391
9392 /* And now for the dest. */
9393 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9394 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009395 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009396 } else {
9397 ret = btrfs_insert_inode_ref(trans, root,
9398 old_dentry->d_name.name,
9399 old_dentry->d_name.len,
9400 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009401 btrfs_ino(BTRFS_I(old_dir)),
9402 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009403 if (ret) {
9404 if (need_abort)
9405 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009406 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009407 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009408 }
9409
9410 /* Update inode version and ctime/mtime. */
9411 inode_inc_iversion(old_dir);
9412 inode_inc_iversion(new_dir);
9413 inode_inc_iversion(old_inode);
9414 inode_inc_iversion(new_inode);
9415 old_dir->i_ctime = old_dir->i_mtime = ctime;
9416 new_dir->i_ctime = new_dir->i_mtime = ctime;
9417 old_inode->i_ctime = ctime;
9418 new_inode->i_ctime = ctime;
9419
9420 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009421 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9422 BTRFS_I(old_inode), 1);
9423 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9424 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009425 }
9426
Filipe Mananabd54f382021-07-27 11:24:45 +01009427 /*
9428 * Now pin the logs of the roots. We do it to ensure that no other task
9429 * can sync the logs while we are in progress with the rename, because
9430 * that could result in an inconsistency in case any of the inodes that
9431 * are part of this rename operation were logged before.
9432 *
9433 * We pin the logs even if at this precise moment none of the inodes was
9434 * logged before. This is because right after we checked for that, some
9435 * other task fsyncing some other inode not involved with this rename
9436 * operation could log that one of our inodes exists.
9437 *
9438 * We don't need to pin the logs before the above calls to
9439 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9440 */
9441 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9442 btrfs_pin_log_trans(root);
9443 root_log_pinned = true;
9444 }
9445 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9446 btrfs_pin_log_trans(dest);
9447 dest_log_pinned = true;
9448 }
9449
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 /* src is a subvolume */
9451 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009452 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009453 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009454 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9455 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009456 old_dentry->d_name.name,
9457 old_dentry->d_name.len);
9458 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009459 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 }
9461 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009462 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009463 goto out_fail;
9464 }
9465
9466 /* dest is a subvolume */
9467 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009468 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009470 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9471 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009472 new_dentry->d_name.name,
9473 new_dentry->d_name.len);
9474 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009475 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009476 }
9477 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009478 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009479 goto out_fail;
9480 }
9481
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009482 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 new_dentry->d_name.name,
9484 new_dentry->d_name.len, 0, old_idx);
9485 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009486 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009487 goto out_fail;
9488 }
9489
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009490 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009491 old_dentry->d_name.name,
9492 old_dentry->d_name.len, 0, new_idx);
9493 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009494 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009495 goto out_fail;
9496 }
9497
9498 if (old_inode->i_nlink == 1)
9499 BTRFS_I(old_inode)->dir_index = old_idx;
9500 if (new_inode->i_nlink == 1)
9501 BTRFS_I(new_inode)->dir_index = new_idx;
9502
Filipe Manana86e8aa02016-05-05 02:02:27 +01009503 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009504 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9505 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009507 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009509 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009510 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9511 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009513 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 }
9515out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009516 /*
9517 * If we have pinned a log and an error happened, we unpin tasks
9518 * trying to sync the log and force them to fallback to a transaction
9519 * commit if the log currently contains any of the inodes involved in
9520 * this rename operation (to ensure we do not persist a log with an
9521 * inconsistent state for any of these inodes or leading to any
9522 * inconsistencies when replayed). If the transaction was aborted, the
9523 * abortion reason is propagated to userspace when attempting to commit
9524 * the transaction. If the log does not contain any of these inodes, we
9525 * allow the tasks to sync it.
9526 */
9527 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009528 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9529 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9530 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana1c167b82021-07-29 15:28:34 +01009531 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
David Sterba90787762019-03-20 13:28:05 +01009532 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009533
9534 if (root_log_pinned) {
9535 btrfs_end_log_trans(root);
9536 root_log_pinned = false;
9537 }
9538 if (dest_log_pinned) {
9539 btrfs_end_log_trans(dest);
9540 dest_log_pinned = false;
9541 }
9542 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009543 ret2 = btrfs_end_transaction(trans);
9544 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009546 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9547 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009548 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549
9550 return ret;
9551}
9552
9553static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9554 struct btrfs_root *root,
Christian Braunerca072742021-07-27 12:48:42 +02009555 struct user_namespace *mnt_userns,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 struct inode *dir,
9557 struct dentry *dentry)
9558{
9559 int ret;
9560 struct inode *inode;
9561 u64 objectid;
9562 u64 index;
9563
Nikolay Borisov543068a2020-12-07 17:32:33 +02009564 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565 if (ret)
9566 return ret;
9567
Christian Braunerca072742021-07-27 12:48:42 +02009568 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 dentry->d_name.name,
9570 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009571 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 objectid,
9573 S_IFCHR | WHITEOUT_MODE,
9574 &index);
9575
9576 if (IS_ERR(inode)) {
9577 ret = PTR_ERR(inode);
9578 return ret;
9579 }
9580
9581 inode->i_op = &btrfs_special_inode_operations;
9582 init_special_inode(inode, inode->i_mode,
9583 WHITEOUT_DEV);
9584
9585 ret = btrfs_init_inode_security(trans, inode, dir,
9586 &dentry->d_name);
9587 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009588 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009590 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9591 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009593 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009595 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009596out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009598 if (ret)
9599 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 iput(inode);
9601
Filipe Mananac9901612016-05-05 01:41:57 +01009602 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603}
9604
Christian Braunerca072742021-07-27 12:48:42 +02009605static int btrfs_rename(struct user_namespace *mnt_userns,
9606 struct inode *old_dir, struct dentry *old_dentry,
9607 struct inode *new_dir, struct dentry *new_dentry,
9608 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009609{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009610 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009611 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009612 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009613 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9614 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009615 struct inode *new_inode = d_inode(new_dentry);
9616 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009617 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009618 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009619 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009620 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009621 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009622
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009623 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009624 return -EPERM;
9625
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009626 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009627 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009628 return -EXDEV;
9629
Li Zefan33345d012011-04-20 10:31:50 +08009630 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009631 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009632 return -ENOTEMPTY;
9633
Chris Mason39279cc2007-06-12 06:35:45 -04009634 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009635 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009636 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009637
9638
9639 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009640 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009641 new_dentry->d_name.name,
9642 new_dentry->d_name.len);
9643
9644 if (ret) {
9645 if (ret == -EEXIST) {
9646 /* we shouldn't get
9647 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309648 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009649 return ret;
9650 }
9651 } else {
9652 /* maybe -EOVERFLOW */
9653 return ret;
9654 }
9655 }
9656 ret = 0;
9657
Chris Mason5a3f23d2009-03-31 13:27:11 -04009658 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009659 * we're using rename to replace one file with another. Start IO on it
9660 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009661 */
Chris Mason8d875f92014-08-12 10:47:42 -07009662 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009663 filemap_flush(old_inode->i_mapping);
9664
Yan, Zheng76dda932009-09-21 16:00:26 -04009665 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009666 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009667 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009668 /*
9669 * We want to reserve the absolute worst case amount of items. So if
9670 * both inodes are subvols and we need to unlink them then that would
9671 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009673 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9674 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009675 * If our rename has the whiteout flag, we need more 5 units for the
9676 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9677 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009678 */
Filipe Manana5062af32016-05-05 10:26:26 +01009679 trans_num_items = 11;
9680 if (flags & RENAME_WHITEOUT)
9681 trans_num_items += 5;
9682 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009683 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 ret = PTR_ERR(trans);
9685 goto out_notrans;
9686 }
Chris Mason5f39d392007-10-15 16:14:19 -04009687
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009688 if (dest != root) {
9689 ret = btrfs_record_root_in_trans(trans, dest);
9690 if (ret)
9691 goto out_fail;
9692 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009693
Nikolay Borisov877574e2017-02-20 13:50:33 +02009694 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009695 if (ret)
9696 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009697
Miao Xie67de1172013-12-26 13:07:06 +08009698 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009699 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009700 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009701 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009702 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009703 ret = btrfs_insert_inode_ref(trans, dest,
9704 new_dentry->d_name.name,
9705 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009706 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009707 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009708 if (ret)
9709 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009710 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009711
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009712 inode_inc_iversion(old_dir);
9713 inode_inc_iversion(new_dir);
9714 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009715 old_dir->i_ctime = old_dir->i_mtime =
9716 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009717 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009718
Chris Mason12fcfd22009-03-24 10:24:20 -04009719 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009720 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9721 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009722
Li Zefan33345d012011-04-20 10:31:50 +08009723 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009724 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009725 } else {
Filipe Mananabd54f382021-07-27 11:24:45 +01009726 /*
9727 * Now pin the log. We do it to ensure that no other task can
9728 * sync the log while we are in progress with the rename, as
9729 * that could result in an inconsistency in case any of the
9730 * inodes that are part of this rename operation were logged
9731 * before.
9732 *
9733 * We pin the log even if at this precise moment none of the
9734 * inodes was logged before. This is because right after we
9735 * checked for that, some other task fsyncing some other inode
9736 * not involved with this rename operation could log that one of
9737 * our inodes exists.
9738 *
9739 * We don't need to pin the logs before the above call to
9740 * btrfs_insert_inode_ref(), since that does not need to change
9741 * a log.
9742 */
9743 btrfs_pin_log_trans(root);
9744 log_pinned = true;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009745 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9746 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009747 old_dentry->d_name.name,
9748 old_dentry->d_name.len);
9749 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009750 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009751 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009752 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009753 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009754 goto out_fail;
9755 }
Chris Mason39279cc2007-06-12 06:35:45 -04009756
9757 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009758 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009759 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009760 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009761 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009762 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009763 BUG_ON(new_inode->i_nlink == 0);
9764 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009765 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9766 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009767 new_dentry->d_name.name,
9768 new_dentry->d_name.len);
9769 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009770 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009771 ret = btrfs_orphan_add(trans,
9772 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009773 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009774 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009775 goto out_fail;
9776 }
Chris Mason39279cc2007-06-12 06:35:45 -04009777 }
Josef Bacikaec74772008-07-24 12:12:38 -04009778
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009779 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009780 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009781 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009782 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009783 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009784 goto out_fail;
9785 }
Chris Mason39279cc2007-06-12 06:35:45 -04009786
Miao Xie67de1172013-12-26 13:07:06 +08009787 if (old_inode->i_nlink == 1)
9788 BTRFS_I(old_inode)->dir_index = index;
9789
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009790 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009791 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9792 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009794 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009795 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009796
9797 if (flags & RENAME_WHITEOUT) {
Christian Braunerca072742021-07-27 12:48:42 +02009798 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9799 old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800
9801 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009802 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009803 goto out_fail;
9804 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009805 }
Chris Mason39279cc2007-06-12 06:35:45 -04009806out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009807 /*
9808 * If we have pinned the log and an error happened, we unpin tasks
9809 * trying to sync the log and force them to fallback to a transaction
9810 * commit if the log currently contains any of the inodes involved in
9811 * this rename operation (to ensure we do not persist a log with an
9812 * inconsistent state for any of these inodes or leading to any
9813 * inconsistencies when replayed). If the transaction was aborted, the
9814 * abortion reason is propagated to userspace when attempting to commit
9815 * the transaction. If the log does not contain any of these inodes, we
9816 * allow the tasks to sync it.
9817 */
9818 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009819 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9820 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9821 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009822 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009823 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009824 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009825
9826 btrfs_end_log_trans(root);
9827 log_pinned = false;
9828 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009829 ret2 = btrfs_end_transaction(trans);
9830 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009831out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009832 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009833 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009834
Chris Mason39279cc2007-06-12 06:35:45 -04009835 return ret;
9836}
9837
Christian Brauner549c7292021-01-21 14:19:43 +01009838static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9839 struct dentry *old_dentry, struct inode *new_dir,
9840 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009841{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009843 return -EINVAL;
9844
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 if (flags & RENAME_EXCHANGE)
9846 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9847 new_dentry);
9848
Christian Braunerca072742021-07-27 12:48:42 +02009849 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9850 new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009851}
9852
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009853struct btrfs_delalloc_work {
9854 struct inode *inode;
9855 struct completion completion;
9856 struct list_head list;
9857 struct btrfs_work work;
9858};
9859
Miao Xie8ccf6f192012-10-25 09:28:04 +00009860static void btrfs_run_delalloc_work(struct btrfs_work *work)
9861{
9862 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009863 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009864
9865 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9866 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009867 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009868 filemap_flush(inode->i_mapping);
9869 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9870 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009871 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009872
Nikolay Borisov076da912018-04-23 10:54:16 +03009873 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009874 complete(&delalloc_work->completion);
9875}
9876
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009877static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009878{
9879 struct btrfs_delalloc_work *work;
9880
David Sterba100d5702015-12-08 14:39:32 +01009881 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009882 if (!work)
9883 return NULL;
9884
9885 init_completion(&work->completion);
9886 INIT_LIST_HEAD(&work->list);
9887 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009888 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009889
9890 return work;
9891}
9892
Chris Masond352ac62008-09-29 15:18:18 -04009893/*
9894 * some fairly slow code that needs optimization. This walks the list
9895 * of all the inodes with pending delalloc and forces them to disk.
9896 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009897static int start_delalloc_inodes(struct btrfs_root *root,
9898 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009899 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009900{
Chris Masonea8c2812008-08-04 23:17:27 -04009901 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009902 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009903 struct btrfs_delalloc_work *work, *next;
9904 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009905 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009906 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009907 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009908
Miao Xie8ccf6f192012-10-25 09:28:04 +00009909 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009910 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009911
Miao Xie573bfb72014-03-06 13:55:03 +08009912 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009913 spin_lock(&root->delalloc_lock);
9914 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009915 while (!list_empty(&splice)) {
9916 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009917 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009918
Miao Xieeb73c1b2013-05-15 07:48:22 +00009919 list_move_tail(&binode->delalloc_inodes,
9920 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009921
9922 if (in_reclaim_context &&
9923 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9924 continue;
9925
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009926 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009927 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009928 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009929 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009930 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009931 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009932
Ethan Lien3cd24c62018-11-01 14:49:03 +08009933 if (snapshot)
9934 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9935 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009936 if (full_flush) {
9937 work = btrfs_alloc_delalloc_work(inode);
9938 if (!work) {
9939 iput(inode);
9940 ret = -ENOMEM;
9941 goto out;
9942 }
9943 list_add_tail(&work->list, &works);
9944 btrfs_queue_work(root->fs_info->flush_workers,
9945 &work->work);
9946 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009947 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009948 btrfs_add_delayed_iput(inode);
9949 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009950 goto out;
9951 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009952 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009953 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009954 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009955 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009956
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009957out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009958 list_for_each_entry_safe(work, next, &works, list) {
9959 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009960 wait_for_completion(&work->completion);
9961 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009962 }
9963
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009964 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009965 spin_lock(&root->delalloc_lock);
9966 list_splice_tail(&splice, &root->delalloc_inodes);
9967 spin_unlock(&root->delalloc_lock);
9968 }
Miao Xie573bfb72014-03-06 13:55:03 +08009969 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009970 return ret;
9971}
9972
Filipe Mananaf9baa502021-04-22 12:08:05 +01009973int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009974{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009975 struct writeback_control wbc = {
9976 .nr_to_write = LONG_MAX,
9977 .sync_mode = WB_SYNC_NONE,
9978 .range_start = 0,
9979 .range_end = LLONG_MAX,
9980 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009981 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009983 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009984 return -EROFS;
9985
Filipe Mananaf9baa502021-04-22 12:08:05 +01009986 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009987}
9988
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009989int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009990 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009991{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009992 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009993 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009994 .sync_mode = WB_SYNC_NONE,
9995 .range_start = 0,
9996 .range_end = LLONG_MAX,
9997 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009998 struct btrfs_root *root;
9999 struct list_head splice;
10000 int ret;
10001
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010002 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010003 return -EROFS;
10004
10005 INIT_LIST_HEAD(&splice);
10006
Miao Xie573bfb72014-03-06 13:55:03 +080010007 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010008 spin_lock(&fs_info->delalloc_root_lock);
10009 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +020010010 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -050010011 /*
10012 * Reset nr_to_write here so we know that we're doing a full
10013 * flush.
10014 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010015 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -050010016 wbc.nr_to_write = LONG_MAX;
10017
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 root = list_first_entry(&splice, struct btrfs_root,
10019 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -050010020 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 BUG_ON(!root);
10022 list_move_tail(&root->delalloc_root,
10023 &fs_info->delalloc_roots);
10024 spin_unlock(&fs_info->delalloc_root_lock);
10025
Josef Bacike076ab2a2021-01-07 17:08:30 -050010026 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -050010027 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -050010028 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010029 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010030 spin_lock(&fs_info->delalloc_root_lock);
10031 }
10032 spin_unlock(&fs_info->delalloc_root_lock);
10033
Miao Xie6c255e62014-03-06 13:55:01 +080010034 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010035out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010036 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010037 spin_lock(&fs_info->delalloc_root_lock);
10038 list_splice_tail(&splice, &fs_info->delalloc_roots);
10039 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010040 }
Miao Xie573bfb72014-03-06 13:55:03 +080010041 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010042 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010043}
10044
Christian Brauner549c7292021-01-21 14:19:43 +010010045static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
10046 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -040010047{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010048 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010049 struct btrfs_trans_handle *trans;
10050 struct btrfs_root *root = BTRFS_I(dir)->root;
10051 struct btrfs_path *path;
10052 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010053 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010054 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010055 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010056 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010057 int name_len;
10058 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010059 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010060 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010061 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010062
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010063 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010064 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010065 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010066
Josef Bacik9ed74f22009-09-11 16:12:44 -040010067 /*
10068 * 2 items for inode item and ref
10069 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010070 * 1 item for updating parent inode item
10071 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010072 * 1 item for xattr if selinux is on
10073 */
Filipe Manana9269d122015-12-31 18:16:29 +000010074 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010075 if (IS_ERR(trans))
10076 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010077
Nikolay Borisov543068a2020-12-07 17:32:33 +020010078 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +080010079 if (err)
10080 goto out_unlock;
10081
Christian Brauner5a052102021-07-27 12:48:47 +020010082 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +020010083 dentry->d_name.name, dentry->d_name.len,
10084 btrfs_ino(BTRFS_I(dir)), objectid,
10085 S_IFLNK | S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010086 if (IS_ERR(inode)) {
10087 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010088 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010089 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010090 }
Chris Mason39279cc2007-06-12 06:35:45 -040010091
Casey Schauflerad19db72011-12-15 10:09:07 -050010092 /*
10093 * If the active LSM wants to access the inode during
10094 * d_instantiate it needs these. Smack checks to see
10095 * if the filesystem supports xattrs by looking at the
10096 * ops vector.
10097 */
10098 inode->i_fop = &btrfs_file_operations;
10099 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010100 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010101
10102 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10103 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010104 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010105
Chris Mason39279cc2007-06-12 06:35:45 -040010106 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010107 if (!path) {
10108 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010109 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010110 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010111 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010112 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010113 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010114 datasize = btrfs_file_extent_calc_inline_size(name_len);
10115 err = btrfs_insert_empty_item(trans, root, path, &key,
10116 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010117 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010118 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010119 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010120 }
Chris Mason5f39d392007-10-15 16:14:19 -040010121 leaf = path->nodes[0];
10122 ei = btrfs_item_ptr(leaf, path->slots[0],
10123 struct btrfs_file_extent_item);
10124 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10125 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010126 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010127 btrfs_set_file_extent_encryption(leaf, ei, 0);
10128 btrfs_set_file_extent_compression(leaf, ei, 0);
10129 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10130 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10131
Chris Mason39279cc2007-06-12 06:35:45 -040010132 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010133 write_extent_buffer(leaf, symname, ptr, name_len);
10134 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010135 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010136
Chris Mason39279cc2007-06-12 06:35:45 -040010137 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010138 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010139 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010140 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010141 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +000010142 /*
10143 * Last step, add directory indexes for our symlink inode. This is the
10144 * last step to avoid extra cleanup of these indexes if an error happens
10145 * elsewhere above.
10146 */
10147 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010148 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10149 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010150 if (err)
10151 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010152
Al Viro1e2e5472018-05-04 08:23:01 -040010153 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010154
10155out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010156 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010157 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010158 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010159 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010160 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010161 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010162 return err;
10163}
Chris Mason16432982008-04-10 10:23:21 -040010164
Filipe Manana8fccebf2020-09-08 11:27:20 +010010165static struct btrfs_trans_handle *insert_prealloc_file_extent(
10166 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010167 struct btrfs_inode *inode,
10168 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +080010169 u64 file_offset)
10170{
10171 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +010010172 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010173 struct btrfs_trans_handle *trans = trans_in;
10174 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010175 u64 start = ins->objectid;
10176 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010177 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +080010178 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010179
10180 memset(&stack_fi, 0, sizeof(stack_fi));
10181
10182 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10183 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10184 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10185 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10186 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10187 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10188 /* Encryption and other encoding is reserved and all 0 */
10189
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010190 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10191 if (qgroup_released < 0)
10192 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010193
10194 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010195 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +000010196 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010197 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010198 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010199 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010200 return trans;
10201 }
10202
10203 extent_info.disk_offset = start;
10204 extent_info.disk_len = len;
10205 extent_info.data_offset = 0;
10206 extent_info.data_len = len;
10207 extent_info.file_offset = file_offset;
10208 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010209 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010210 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010211 extent_info.insertions = 0;
10212
10213 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010214 if (!path) {
10215 ret = -ENOMEM;
10216 goto free_qgroup;
10217 }
Filipe Manana8fccebf2020-09-08 11:27:20 +010010218
Nikolay Borisovbfc78472021-02-17 15:12:47 +020010219 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +010010220 file_offset + len - 1, &extent_info,
10221 &trans);
10222 btrfs_free_path(path);
10223 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010224 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010225 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010226
10227free_qgroup:
10228 /*
10229 * We have released qgroup data range at the beginning of the function,
10230 * and normally qgroup_released bytes will be freed when committing
10231 * transaction.
10232 * But if we error out early, we have to free what we have released
10233 * or we leak qgroup data reservation.
10234 */
10235 btrfs_qgroup_free_refroot(inode->root->fs_info,
10236 inode->root->root_key.objectid, qgroup_released,
10237 BTRFS_QGROUP_RSV_DATA);
10238 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +080010239}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010240
Josef Bacik0af3d002010-06-21 14:48:16 -040010241static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10242 u64 start, u64 num_bytes, u64 min_size,
10243 loff_t actual_len, u64 *alloc_hint,
10244 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010245{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010246 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010247 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10248 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010249 struct btrfs_root *root = BTRFS_I(inode)->root;
10250 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010251 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010252 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010253 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010254 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010255 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010256 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010257 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010258 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010259
Josef Bacik0af3d002010-06-21 14:48:16 -040010260 if (trans)
10261 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010262 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010263 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010264 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010265 /*
10266 * If we are severely fragmented we could end up with really
10267 * small allocations, so if the allocator is returning small
10268 * chunks lets make its job easier by only searching for those
10269 * sized chunks.
10270 */
10271 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010272 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10273 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010274 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010275 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010276
10277 /*
10278 * We've reserved this space, and thus converted it from
10279 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10280 * from here on out we will only need to clear our reservation
10281 * for the remaining unreserved area, so advance our
10282 * clear_offset by our extent size.
10283 */
10284 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010285
Josef Bacik0b670dc2015-09-23 17:11:16 -040010286 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010287 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10288 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010289 /*
10290 * Now that we inserted the prealloc extent we can finally
10291 * decrement the number of reservations in the block group.
10292 * If we did it before, we could race with relocation and have
10293 * relocation miss the reserved extent, making it fail later.
10294 */
10295 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010296 if (IS_ERR(trans)) {
10297 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010298 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010299 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010300 break;
10301 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010302
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010303 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010304 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010305
Josef Bacik5dc562c2012-08-17 13:14:17 -040010306 em = alloc_extent_map();
10307 if (!em) {
10308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10309 &BTRFS_I(inode)->runtime_flags);
10310 goto next;
10311 }
10312
10313 em->start = cur_offset;
10314 em->orig_start = cur_offset;
10315 em->len = ins.offset;
10316 em->block_start = ins.objectid;
10317 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010318 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010319 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010320 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10321 em->generation = trans->transid;
10322
10323 while (1) {
10324 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010325 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010326 write_unlock(&em_tree->lock);
10327 if (ret != -EEXIST)
10328 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010329 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010330 cur_offset + ins.offset - 1,
10331 0);
10332 }
10333 free_extent_map(em);
10334next:
Yan Zhengd899e052008-10-30 14:25:28 -040010335 num_bytes -= ins.offset;
10336 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010337 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010339 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010340 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010341 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010342 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010343 (actual_len > inode->i_size) &&
10344 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010345 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010346 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010347 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010348 i_size = cur_offset;
10349 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010350 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010351 }
10352
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010353 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010354
10355 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010356 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010357 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010358 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010359 break;
10360 }
Yan Zhengd899e052008-10-30 14:25:28 -040010361
Filipe Manana8fccebf2020-09-08 11:27:20 +010010362 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010363 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010364 trans = NULL;
10365 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010366 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010367 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010368 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010369 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010370 return ret;
10371}
10372
Josef Bacik0af3d002010-06-21 14:48:16 -040010373int btrfs_prealloc_file_range(struct inode *inode, int mode,
10374 u64 start, u64 num_bytes, u64 min_size,
10375 loff_t actual_len, u64 *alloc_hint)
10376{
10377 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10378 min_size, actual_len, alloc_hint,
10379 NULL);
10380}
10381
10382int btrfs_prealloc_file_range_trans(struct inode *inode,
10383 struct btrfs_trans_handle *trans, int mode,
10384 u64 start, u64 num_bytes, u64 min_size,
10385 loff_t actual_len, u64 *alloc_hint)
10386{
10387 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10388 min_size, actual_len, alloc_hint, trans);
10389}
10390
Chris Masone6dcd2d2008-07-17 12:53:50 -040010391static int btrfs_set_page_dirty(struct page *page)
10392{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010393 return __set_page_dirty_nobuffers(page);
10394}
10395
Christian Brauner549c7292021-01-21 14:19:43 +010010396static int btrfs_permission(struct user_namespace *mnt_userns,
10397 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010398{
Li Zefanb83cc962010-12-20 16:04:08 +080010399 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010400 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010401
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010402 if (mask & MAY_WRITE &&
10403 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10404 if (btrfs_root_readonly(root))
10405 return -EROFS;
10406 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10407 return -EACCES;
10408 }
Christian Brauner3bc71ba2021-07-27 12:48:50 +020010409 return generic_permission(mnt_userns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010410}
Chris Mason39279cc2007-06-12 06:35:45 -040010411
Christian Brauner549c7292021-01-21 14:19:43 +010010412static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10413 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010414{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010415 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010416 struct btrfs_trans_handle *trans;
10417 struct btrfs_root *root = BTRFS_I(dir)->root;
10418 struct inode *inode = NULL;
10419 u64 objectid;
10420 u64 index;
10421 int ret = 0;
10422
10423 /*
10424 * 5 units required for adding orphan entry
10425 */
10426 trans = btrfs_start_transaction(root, 5);
10427 if (IS_ERR(trans))
10428 return PTR_ERR(trans);
10429
Nikolay Borisov543068a2020-12-07 17:32:33 +020010430 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010431 if (ret)
10432 goto out;
10433
Christian Brauner98b6ab52021-07-27 12:48:48 +020010434 inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010435 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010436 if (IS_ERR(inode)) {
10437 ret = PTR_ERR(inode);
10438 inode = NULL;
10439 goto out;
10440 }
10441
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010442 inode->i_fop = &btrfs_file_operations;
10443 inode->i_op = &btrfs_file_inode_operations;
10444
10445 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010446
Chris Masonb0d5d102014-09-08 13:08:51 -070010447 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10448 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010449 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010450
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010451 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010452 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010453 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010454 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010455 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010456 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010457
Filipe Manana5762b5c2014-08-01 00:10:32 +010010458 /*
10459 * We set number of links to 0 in btrfs_new_inode(), and here we set
10460 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10461 * through:
10462 *
10463 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10464 */
10465 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010467 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010469out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010470 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010471 if (ret && inode)
10472 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010473 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010474 return ret;
10475}
10476
Qu Wenruod2a91062021-05-31 16:50:49 +080010477void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010478{
Qu Wenruod2a91062021-05-31 16:50:49 +080010479 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010480 unsigned long index = start >> PAGE_SHIFT;
10481 unsigned long end_index = end >> PAGE_SHIFT;
10482 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010483 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010484
Qu Wenruod2a91062021-05-31 16:50:49 +080010485 ASSERT(end + 1 - start <= U32_MAX);
10486 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010487 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010488 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010489 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010490
10491 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010492 put_page(page);
10493 index++;
10494 }
10495}
10496
Omar Sandovaled46ff32016-11-03 10:28:14 -070010497#ifdef CONFIG_SWAP
10498/*
10499 * Add an entry indicating a block group or device which is pinned by a
10500 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10501 * negative errno on failure.
10502 */
10503static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10504 bool is_block_group)
10505{
10506 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10507 struct btrfs_swapfile_pin *sp, *entry;
10508 struct rb_node **p;
10509 struct rb_node *parent = NULL;
10510
10511 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10512 if (!sp)
10513 return -ENOMEM;
10514 sp->ptr = ptr;
10515 sp->inode = inode;
10516 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010517 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010518
10519 spin_lock(&fs_info->swapfile_pins_lock);
10520 p = &fs_info->swapfile_pins.rb_node;
10521 while (*p) {
10522 parent = *p;
10523 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10524 if (sp->ptr < entry->ptr ||
10525 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10526 p = &(*p)->rb_left;
10527 } else if (sp->ptr > entry->ptr ||
10528 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10529 p = &(*p)->rb_right;
10530 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010531 if (is_block_group)
10532 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010533 spin_unlock(&fs_info->swapfile_pins_lock);
10534 kfree(sp);
10535 return 1;
10536 }
10537 }
10538 rb_link_node(&sp->node, parent, p);
10539 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10540 spin_unlock(&fs_info->swapfile_pins_lock);
10541 return 0;
10542}
10543
10544/* Free all of the entries pinned by this swapfile. */
10545static void btrfs_free_swapfile_pins(struct inode *inode)
10546{
10547 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10548 struct btrfs_swapfile_pin *sp;
10549 struct rb_node *node, *next;
10550
10551 spin_lock(&fs_info->swapfile_pins_lock);
10552 node = rb_first(&fs_info->swapfile_pins);
10553 while (node) {
10554 next = rb_next(node);
10555 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10556 if (sp->inode == inode) {
10557 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010558 if (sp->is_block_group) {
10559 btrfs_dec_block_group_swap_extents(sp->ptr,
10560 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010561 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010562 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010563 kfree(sp);
10564 }
10565 node = next;
10566 }
10567 spin_unlock(&fs_info->swapfile_pins_lock);
10568}
10569
10570struct btrfs_swap_info {
10571 u64 start;
10572 u64 block_start;
10573 u64 block_len;
10574 u64 lowest_ppage;
10575 u64 highest_ppage;
10576 unsigned long nr_pages;
10577 int nr_extents;
10578};
10579
10580static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10581 struct btrfs_swap_info *bsi)
10582{
10583 unsigned long nr_pages;
10584 u64 first_ppage, first_ppage_reported, next_ppage;
10585 int ret;
10586
10587 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10588 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10589 PAGE_SIZE) >> PAGE_SHIFT;
10590
10591 if (first_ppage >= next_ppage)
10592 return 0;
10593 nr_pages = next_ppage - first_ppage;
10594
10595 first_ppage_reported = first_ppage;
10596 if (bsi->start == 0)
10597 first_ppage_reported++;
10598 if (bsi->lowest_ppage > first_ppage_reported)
10599 bsi->lowest_ppage = first_ppage_reported;
10600 if (bsi->highest_ppage < (next_ppage - 1))
10601 bsi->highest_ppage = next_ppage - 1;
10602
10603 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10604 if (ret < 0)
10605 return ret;
10606 bsi->nr_extents += ret;
10607 bsi->nr_pages += nr_pages;
10608 return 0;
10609}
10610
10611static void btrfs_swap_deactivate(struct file *file)
10612{
10613 struct inode *inode = file_inode(file);
10614
10615 btrfs_free_swapfile_pins(inode);
10616 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10617}
10618
10619static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10620 sector_t *span)
10621{
10622 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010623 struct btrfs_root *root = BTRFS_I(inode)->root;
10624 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010625 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10626 struct extent_state *cached_state = NULL;
10627 struct extent_map *em = NULL;
10628 struct btrfs_device *device = NULL;
10629 struct btrfs_swap_info bsi = {
10630 .lowest_ppage = (sector_t)-1ULL,
10631 };
10632 int ret = 0;
10633 u64 isize;
10634 u64 start;
10635
10636 /*
10637 * If the swap file was just created, make sure delalloc is done. If the
10638 * file changes again after this, the user is doing something stupid and
10639 * we don't really care.
10640 */
10641 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10642 if (ret)
10643 return ret;
10644
10645 /*
10646 * The inode is locked, so these flags won't change after we check them.
10647 */
10648 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10649 btrfs_warn(fs_info, "swapfile must not be compressed");
10650 return -EINVAL;
10651 }
10652 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10653 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10654 return -EINVAL;
10655 }
10656 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10657 btrfs_warn(fs_info, "swapfile must not be checksummed");
10658 return -EINVAL;
10659 }
10660
10661 /*
10662 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010663 * under us. The exclop protection makes sure they aren't running/won't
10664 * run concurrently while we are mapping the swap extents, and
10665 * fs_info->swapfile_pins prevents them from running while the swap
10666 * file is active and moving the extents. Note that this also prevents
10667 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010668 * really worth the trouble to allow it.
10669 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010670 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010671 btrfs_warn(fs_info,
10672 "cannot activate swapfile while exclusive operation is running");
10673 return -EBUSY;
10674 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010675
10676 /*
10677 * Prevent snapshot creation while we are activating the swap file.
10678 * We do not want to race with snapshot creation. If snapshot creation
10679 * already started before we bumped nr_swapfiles from 0 to 1 and
10680 * completes before the first write into the swap file after it is
10681 * activated, than that write would fallback to COW.
10682 */
10683 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10684 btrfs_exclop_finish(fs_info);
10685 btrfs_warn(fs_info,
10686 "cannot activate swapfile because snapshot creation is in progress");
10687 return -EINVAL;
10688 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010689 /*
10690 * Snapshots can create extents which require COW even if NODATACOW is
10691 * set. We use this counter to prevent snapshots. We must increment it
10692 * before walking the extents because we don't want a concurrent
10693 * snapshot to run after we've already checked the extents.
10694 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010695 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010696
10697 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10698
10699 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10700 start = 0;
10701 while (start < isize) {
10702 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010703 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010704 u64 len = isize - start;
10705
Omar Sandoval39b07b52019-12-02 17:34:23 -080010706 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010707 if (IS_ERR(em)) {
10708 ret = PTR_ERR(em);
10709 goto out;
10710 }
10711
10712 if (em->block_start == EXTENT_MAP_HOLE) {
10713 btrfs_warn(fs_info, "swapfile must not have holes");
10714 ret = -EINVAL;
10715 goto out;
10716 }
10717 if (em->block_start == EXTENT_MAP_INLINE) {
10718 /*
10719 * It's unlikely we'll ever actually find ourselves
10720 * here, as a file small enough to fit inline won't be
10721 * big enough to store more than the swap header, but in
10722 * case something changes in the future, let's catch it
10723 * here rather than later.
10724 */
10725 btrfs_warn(fs_info, "swapfile must not be inline");
10726 ret = -EINVAL;
10727 goto out;
10728 }
10729 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10730 btrfs_warn(fs_info, "swapfile must not be compressed");
10731 ret = -EINVAL;
10732 goto out;
10733 }
10734
10735 logical_block_start = em->block_start + (start - em->start);
10736 len = min(len, em->len - (start - em->start));
10737 free_extent_map(em);
10738 em = NULL;
10739
Boris Burkova84d5d42020-08-18 11:00:05 -070010740 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010741 if (ret < 0) {
10742 goto out;
10743 } else if (ret) {
10744 ret = 0;
10745 } else {
10746 btrfs_warn(fs_info,
10747 "swapfile must not be copy-on-write");
10748 ret = -EINVAL;
10749 goto out;
10750 }
10751
10752 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10753 if (IS_ERR(em)) {
10754 ret = PTR_ERR(em);
10755 goto out;
10756 }
10757
10758 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10759 btrfs_warn(fs_info,
10760 "swapfile must have single data profile");
10761 ret = -EINVAL;
10762 goto out;
10763 }
10764
10765 if (device == NULL) {
10766 device = em->map_lookup->stripes[0].dev;
10767 ret = btrfs_add_swapfile_pin(inode, device, false);
10768 if (ret == 1)
10769 ret = 0;
10770 else if (ret)
10771 goto out;
10772 } else if (device != em->map_lookup->stripes[0].dev) {
10773 btrfs_warn(fs_info, "swapfile must be on one device");
10774 ret = -EINVAL;
10775 goto out;
10776 }
10777
10778 physical_block_start = (em->map_lookup->stripes[0].physical +
10779 (logical_block_start - em->start));
10780 len = min(len, em->len - (logical_block_start - em->start));
10781 free_extent_map(em);
10782 em = NULL;
10783
10784 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10785 if (!bg) {
10786 btrfs_warn(fs_info,
10787 "could not find block group containing swapfile");
10788 ret = -EINVAL;
10789 goto out;
10790 }
10791
Filipe Manana195a49e2021-02-05 12:55:37 +000010792 if (!btrfs_inc_block_group_swap_extents(bg)) {
10793 btrfs_warn(fs_info,
10794 "block group for swapfile at %llu is read-only%s",
10795 bg->start,
10796 atomic_read(&fs_info->scrubs_running) ?
10797 " (scrub running)" : "");
10798 btrfs_put_block_group(bg);
10799 ret = -EINVAL;
10800 goto out;
10801 }
10802
Omar Sandovaled46ff32016-11-03 10:28:14 -070010803 ret = btrfs_add_swapfile_pin(inode, bg, true);
10804 if (ret) {
10805 btrfs_put_block_group(bg);
10806 if (ret == 1)
10807 ret = 0;
10808 else
10809 goto out;
10810 }
10811
10812 if (bsi.block_len &&
10813 bsi.block_start + bsi.block_len == physical_block_start) {
10814 bsi.block_len += len;
10815 } else {
10816 if (bsi.block_len) {
10817 ret = btrfs_add_swap_extent(sis, &bsi);
10818 if (ret)
10819 goto out;
10820 }
10821 bsi.start = start;
10822 bsi.block_start = physical_block_start;
10823 bsi.block_len = len;
10824 }
10825
10826 start += len;
10827 }
10828
10829 if (bsi.block_len)
10830 ret = btrfs_add_swap_extent(sis, &bsi);
10831
10832out:
10833 if (!IS_ERR_OR_NULL(em))
10834 free_extent_map(em);
10835
10836 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10837
10838 if (ret)
10839 btrfs_swap_deactivate(file);
10840
Filipe Mananadd0734f2021-02-05 12:55:38 +000010841 btrfs_drew_write_unlock(&root->snapshot_lock);
10842
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010843 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010844
10845 if (ret)
10846 return ret;
10847
10848 if (device)
10849 sis->bdev = device->bdev;
10850 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10851 sis->max = bsi.nr_pages;
10852 sis->pages = bsi.nr_pages - 1;
10853 sis->highest_bit = bsi.nr_pages - 1;
10854 return bsi.nr_extents;
10855}
10856#else
10857static void btrfs_swap_deactivate(struct file *file)
10858{
10859}
10860
10861static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10862 sector_t *span)
10863{
10864 return -EOPNOTSUPP;
10865}
10866#endif
10867
Filipe Manana2766ff62020-11-04 11:07:34 +000010868/*
10869 * Update the number of bytes used in the VFS' inode. When we replace extents in
10870 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10871 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10872 * always get a correct value.
10873 */
10874void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10875 const u64 add_bytes,
10876 const u64 del_bytes)
10877{
10878 if (add_bytes == del_bytes)
10879 return;
10880
10881 spin_lock(&inode->lock);
10882 if (del_bytes > 0)
10883 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10884 if (add_bytes > 0)
10885 inode_add_bytes(&inode->vfs_inode, add_bytes);
10886 spin_unlock(&inode->lock);
10887}
10888
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010889static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010890 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010891 .lookup = btrfs_lookup,
10892 .create = btrfs_create,
10893 .unlink = btrfs_unlink,
10894 .link = btrfs_link,
10895 .mkdir = btrfs_mkdir,
10896 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010897 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010898 .symlink = btrfs_symlink,
10899 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010900 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010901 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010902 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010903 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010904 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010905 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010906 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010907 .fileattr_get = btrfs_fileattr_get,
10908 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010909};
Yan, Zheng76dda932009-09-21 16:00:26 -040010910
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010911static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010912 .llseek = generic_file_llseek,
10913 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010914 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010915 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010916 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010917#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010918 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010919#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010920 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010921 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010922};
10923
Chris Mason35054392009-01-21 13:11:13 -050010924/*
10925 * btrfs doesn't support the bmap operation because swapfiles
10926 * use bmap to make a mapping of extents in the file. They assume
10927 * these extents won't change over the life of the file and they
10928 * use the bmap result to do IO directly to the drive.
10929 *
10930 * the btrfs bmap call would return logical addresses that aren't
10931 * suitable for IO and they also will change frequently as COW
10932 * operations happen. So, swapfile + btrfs == corruption.
10933 *
10934 * For now we're avoiding this by dropping bmap.
10935 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010936static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010937 .readpage = btrfs_readpage,
10938 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010939 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010940 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010941 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010942 .invalidatepage = btrfs_invalidatepage,
10943 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010944#ifdef CONFIG_MIGRATION
10945 .migratepage = btrfs_migratepage,
10946#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010947 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010948 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010949 .swap_activate = btrfs_swap_activate,
10950 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010951};
10952
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010953static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010954 .getattr = btrfs_getattr,
10955 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010956 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010957 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010958 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010959 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010960 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010961 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010962 .fileattr_get = btrfs_fileattr_get,
10963 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010964};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010965static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010966 .getattr = btrfs_getattr,
10967 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010968 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010969 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010970 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010971 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010972 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010973};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010974static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010975 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010976 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010977 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010978 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010979 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010980 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010981};
Yan, Zheng76dda932009-09-21 16:00:26 -040010982
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010983const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010984 .d_delete = btrfs_dentry_delete,
10985};