blob: 88ac63f3b57bc43ccf1bc07a7120f21709e23214 [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 */
David Sterbaf2165622021-06-14 12:45:18 +0200635 if (nr_pages > 1 && 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 */
1153 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1154 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
Yan Zheng17d217f2008-12-12 10:03:38 -05001189 if (root->root_key.objectid ==
1190 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001191 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001192 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001193 /*
1194 * Only drop cache here, and process as normal.
1195 *
1196 * We must not allow extent_clear_unlock_delalloc()
1197 * at out_unlock label to free meta of this ordered
1198 * extent, as its meta should be freed by
1199 * btrfs_finish_ordered_io().
1200 *
1201 * So we must continue until @start is increased to
1202 * skip current ordered extent.
1203 */
Josef Bacik00361582013-08-14 14:02:47 -04001204 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001205 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001206 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001207 }
1208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001209 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001210
Qu Wenruof57ad932021-04-07 19:22:13 +08001211 /*
1212 * We're not doing compressed IO, don't unlock the first page
1213 * (which the caller expects to stay locked), don't clear any
1214 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001215 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001216 * Do set the Ordered (Private2) bit so we know this page was
1217 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001218 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001219 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001220 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001221
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001222 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001223 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001224 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001225 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001226 if (num_bytes < cur_alloc_size)
1227 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001228 else
Anand Jain3752d222018-02-15 12:29:38 +08001229 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001230 alloc_hint = ins.objectid + ins.offset;
1231 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001232 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001233
1234 /*
1235 * btrfs_reloc_clone_csums() error, since start is increased
1236 * extent_clear_unlock_delalloc() at out_unlock label won't
1237 * free metadata of current ordered extent, we're OK to exit.
1238 */
1239 if (ret)
1240 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001241 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001243 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001244
Filipe Mananad9f85962014-08-25 10:43:00 +01001245out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001246 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001247out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001248 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001249 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001251 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1252 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001253 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001254 /*
1255 * If we reserved an extent for our delalloc range (or a subrange) and
1256 * failed to create the respective ordered extent, then it means that
1257 * when we reserved the extent we decremented the extent's size from
1258 * the data space_info's bytes_may_use counter and incremented the
1259 * space_info's bytes_reserved counter by the same amount. We must make
1260 * sure extent_clear_unlock_delalloc() does not try to decrement again
1261 * the data space_info's bytes_may_use counter, therefore we do not pass
1262 * it the flag EXTENT_CLEAR_DATA_RESV.
1263 */
1264 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001265 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001266 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001267 locked_page,
1268 clear_bits,
1269 page_ops);
1270 start += cur_alloc_size;
1271 if (start >= end)
1272 goto out;
1273 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001274 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001275 clear_bits | EXTENT_CLEAR_DATA_RESV,
1276 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001277 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001278}
Chris Masonc8b97812008-10-29 14:49:59 -04001279
Chris Mason771ed682008-11-06 22:02:51 -05001280/*
1281 * work queue call back to started compression on a file and pages
1282 */
1283static noinline void async_cow_start(struct btrfs_work *work)
1284{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001285 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001286 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001287
Nikolay Borisovb5326272019-03-12 17:20:25 +02001288 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001289
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001290 compressed_extents = compress_file_range(async_chunk);
1291 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001292 btrfs_add_delayed_iput(async_chunk->inode);
1293 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001294 }
Chris Mason771ed682008-11-06 22:02:51 -05001295}
1296
1297/*
1298 * work queue call back to submit previously compressed pages
1299 */
1300static noinline void async_cow_submit(struct btrfs_work *work)
1301{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001302 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1303 work);
1304 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001305 unsigned long nr_pages;
1306
Nikolay Borisovb5326272019-03-12 17:20:25 +02001307 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001308 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001309
Nikolay Borisov4546d172019-01-03 10:50:03 +02001310 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001311 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001312 * in which case we don't have anything to submit, yet we need to
1313 * always adjust ->async_delalloc_pages as its paired with the init
1314 * happening in cow_file_range_async
1315 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001316 if (async_chunk->inode)
1317 submit_compressed_extents(async_chunk);
Josef Bacikac981412021-07-14 14:47:17 -04001318
1319 /* atomic_sub_return implies a barrier */
1320 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1321 5 * SZ_1M)
1322 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001323}
Chris Masonc8b97812008-10-29 14:49:59 -04001324
Chris Mason771ed682008-11-06 22:02:51 -05001325static noinline void async_cow_free(struct btrfs_work *work)
1326{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001327 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001328
Nikolay Borisovb5326272019-03-12 17:20:25 +02001329 async_chunk = container_of(work, struct async_chunk, work);
1330 if (async_chunk->inode)
1331 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001332 if (async_chunk->blkcg_css)
1333 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001334 /*
1335 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001336 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001337 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001338 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001339 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001340}
1341
Nikolay Borisov751b6432020-06-03 08:55:22 +03001342static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001343 struct writeback_control *wbc,
1344 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001345 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001346 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001347{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001348 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001349 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001350 struct async_cow *ctx;
1351 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001352 unsigned long nr_pages;
1353 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001354 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1355 int i;
1356 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001357 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001358 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001359
Nikolay Borisov751b6432020-06-03 08:55:22 +03001360 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001361
Nikolay Borisov751b6432020-06-03 08:55:22 +03001362 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001363 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1364 num_chunks = 1;
1365 should_compress = false;
1366 } else {
1367 should_compress = true;
1368 }
1369
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001370 nofs_flag = memalloc_nofs_save();
1371 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1372 memalloc_nofs_restore(nofs_flag);
1373
Nikolay Borisov97db1202019-03-12 17:20:24 +02001374 if (!ctx) {
1375 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1376 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1377 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001378 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1379 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001380
Nikolay Borisov751b6432020-06-03 08:55:22 +03001381 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1382 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001383 return -ENOMEM;
1384 }
1385
1386 async_chunk = ctx->chunks;
1387 atomic_set(&ctx->num_chunks, num_chunks);
1388
1389 for (i = 0; i < num_chunks; i++) {
1390 if (should_compress)
1391 cur_end = min(end, start + SZ_512K - 1);
1392 else
1393 cur_end = end;
1394
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001395 /*
1396 * igrab is called higher up in the call chain, take only the
1397 * lightweight reference for the callback lifetime
1398 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001399 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001400 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001401 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001402 async_chunk[i].start = start;
1403 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001404 async_chunk[i].write_flags = write_flags;
1405 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001406
Chris Mason1d53c9e2019-07-10 12:28:16 -07001407 /*
1408 * The locked_page comes all the way from writepage and its
1409 * the original page we were actually given. As we spread
1410 * this large delalloc region across multiple async_chunk
1411 * structs, only the first struct needs a pointer to locked_page
1412 *
1413 * This way we don't need racey decisions about who is supposed
1414 * to unlock it.
1415 */
1416 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001417 /*
1418 * Depending on the compressibility, the pages might or
1419 * might not go through async. We want all of them to
1420 * be accounted against wbc once. Let's do it here
1421 * before the paths diverge. wbc accounting is used
1422 * only for foreign writeback detection and doesn't
1423 * need full accuracy. Just account the whole thing
1424 * against the first page.
1425 */
1426 wbc_account_cgroup_owner(wbc, locked_page,
1427 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001428 async_chunk[i].locked_page = locked_page;
1429 locked_page = NULL;
1430 } else {
1431 async_chunk[i].locked_page = NULL;
1432 }
1433
Chris Masonec39f762019-07-10 12:28:17 -07001434 if (blkcg_css != blkcg_root_css) {
1435 css_get(blkcg_css);
1436 async_chunk[i].blkcg_css = blkcg_css;
1437 } else {
1438 async_chunk[i].blkcg_css = NULL;
1439 }
1440
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001441 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1442 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001443
Nikolay Borisov97db1202019-03-12 17:20:24 +02001444 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001445 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001446
Nikolay Borisov97db1202019-03-12 17:20:24 +02001447 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001448
Chris Mason771ed682008-11-06 22:02:51 -05001449 *nr_written += nr_pages;
1450 start = cur_end + 1;
1451 }
1452 *page_started = 1;
1453 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001454}
1455
Naohiro Aota42c01102021-02-04 19:22:07 +09001456static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1457 struct page *locked_page, u64 start,
1458 u64 end, int *page_started,
1459 unsigned long *nr_written)
1460{
1461 int ret;
1462
1463 ret = cow_file_range(inode, locked_page, start, end, page_started,
1464 nr_written, 0);
1465 if (ret)
1466 return ret;
1467
1468 if (*page_started)
1469 return 0;
1470
1471 __set_page_dirty_nobuffers(locked_page);
1472 account_page_redirty(locked_page);
1473 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1474 *page_started = 1;
1475
1476 return 0;
1477}
1478
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001479static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001480 u64 bytenr, u64 num_bytes)
1481{
1482 int ret;
1483 struct btrfs_ordered_sum *sums;
1484 LIST_HEAD(list);
1485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001487 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001488 if (ret == 0 && list_empty(&list))
1489 return 0;
1490
1491 while (!list_empty(&list)) {
1492 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1493 list_del(&sums->list);
1494 kfree(sums);
1495 }
Liu Bo58113752018-01-31 17:09:13 -07001496 if (ret < 0)
1497 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001498 return 1;
1499}
1500
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001501static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001502 const u64 start, const u64 end,
1503 int *page_started, unsigned long *nr_written)
1504{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001505 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1506 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001507 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001508 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001509 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001510 u64 range_start = start;
1511 u64 count;
1512
1513 /*
1514 * If EXTENT_NORESERVE is set it means that when the buffered write was
1515 * made we had not enough available data space and therefore we did not
1516 * reserve data space for it, since we though we could do NOCOW for the
1517 * respective file range (either there is prealloc extent or the inode
1518 * has the NOCOW bit set).
1519 *
1520 * However when we need to fallback to COW mode (because for example the
1521 * block group for the corresponding extent was turned to RO mode by a
1522 * scrub or relocation) we need to do the following:
1523 *
1524 * 1) We increment the bytes_may_use counter of the data space info.
1525 * If COW succeeds, it allocates a new data extent and after doing
1526 * that it decrements the space info's bytes_may_use counter and
1527 * increments its bytes_reserved counter by the same amount (we do
1528 * this at btrfs_add_reserved_bytes()). So we need to increment the
1529 * bytes_may_use counter to compensate (when space is reserved at
1530 * buffered write time, the bytes_may_use counter is incremented);
1531 *
1532 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1533 * that if the COW path fails for any reason, it decrements (through
1534 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1535 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001536 *
1537 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001538 * space cache inode or an inode of the data relocation tree, we must
1539 * also increment bytes_may_use of the data space_info for the same
1540 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001541 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001542 * group that contains that extent to RO mode and therefore force COW
1543 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001544 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001545 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001546 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001547 if (count > 0 || is_space_ino || is_reloc_ino) {
1548 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001549 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001550 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1551
Filipe Manana6bd335b2020-06-08 13:33:05 +01001552 if (is_space_ino || is_reloc_ino)
1553 bytes = range_bytes;
1554
Filipe Manana467dc472020-05-27 11:16:07 +01001555 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001556 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001557 spin_unlock(&sinfo->lock);
1558
Filipe Manana2166e5e2020-05-27 11:16:19 +01001559 if (count > 0)
1560 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1561 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001562 }
1563
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001564 return cow_file_range(inode, locked_page, start, end, page_started,
1565 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001566}
1567
Chris Masond352ac62008-09-29 15:18:18 -04001568/*
1569 * when nowcow writeback call back. This checks for snapshots or COW copies
1570 * of the extents that exist in the file, and COWs the file as required.
1571 *
1572 * If no cow copies or snapshots exist, we write directly to the existing
1573 * blocks on disk
1574 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001575static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001576 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001577 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001578 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001579 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001580{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001581 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1582 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001583 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001584 u64 cow_start = (u64)-1;
1585 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001586 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001587 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001588 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1589 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001590 bool nocow = false;
1591 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001592 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001593
1594 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001595 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001596 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001597 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001598 EXTENT_DO_ACCOUNTING |
1599 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001600 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001601 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001602 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001603 }
Li Zefan82d59022011-04-20 10:33:24 +08001604
Yan Zheng80ff3852008-10-30 14:20:02 -04001605 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001606 struct btrfs_key found_key;
1607 struct btrfs_file_extent_item *fi;
1608 struct extent_buffer *leaf;
1609 u64 extent_end;
1610 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001611 u64 num_bytes = 0;
1612 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001613 u64 ram_bytes;
1614 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001615
1616 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001617
Liu Boe4c3b2d2017-01-30 12:25:28 -08001618 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001619 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001620 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001621 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001622
1623 /*
1624 * If there is no extent for our range when doing the initial
1625 * search, then go back to the previous slot as it will be the
1626 * one containing the search offset
1627 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001628 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1629 leaf = path->nodes[0];
1630 btrfs_item_key_to_cpu(leaf, &found_key,
1631 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001632 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001633 found_key.type == BTRFS_EXTENT_DATA_KEY)
1634 path->slots[0]--;
1635 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001636 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001637next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001638 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001639 leaf = path->nodes[0];
1640 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1641 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001642 if (ret < 0) {
1643 if (cow_start != (u64)-1)
1644 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001645 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001646 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001647 if (ret > 0)
1648 break;
1649 leaf = path->nodes[0];
1650 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001651
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001653
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001654 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001655 if (found_key.objectid > ino)
1656 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001657 /*
1658 * Keep searching until we find an EXTENT_ITEM or there are no
1659 * more extents for this inode
1660 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001661 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1662 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1663 path->slots[0]++;
1664 goto next_slot;
1665 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001666
1667 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001668 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 found_key.offset > end)
1670 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001671
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001672 /*
1673 * If the found extent starts after requested offset, then
1674 * adjust extent_end to be right before this extent begins
1675 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 if (found_key.offset > cur_offset) {
1677 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001678 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001679 goto out_check;
1680 }
Chris Masonc31f8832008-01-08 15:46:31 -05001681
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001682 /*
1683 * Found extent which begins before our range and potentially
1684 * intersect it
1685 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 fi = btrfs_item_ptr(leaf, path->slots[0],
1687 struct btrfs_file_extent_item);
1688 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001689
Josef Bacikcc95bef2013-04-04 14:31:27 -04001690 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001691 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1692 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001693 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001695 extent_end = found_key.offset +
1696 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001697 disk_num_bytes =
1698 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001699 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001700 * If the extent we got ends before our current offset,
1701 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001702 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001703 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001704 path->slots[0]++;
1705 goto next_slot;
1706 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001707 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001708 if (disk_bytenr == 0)
1709 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001710 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001711 if (btrfs_file_extent_compression(leaf, fi) ||
1712 btrfs_file_extent_encryption(leaf, fi) ||
1713 btrfs_file_extent_other_encoding(leaf, fi))
1714 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001715 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001716 * If extent is created before the last volume's snapshot
1717 * this implies the extent is shared, hence we can't do
1718 * nocow. This is the same check as in
1719 * btrfs_cross_ref_exist but without calling
1720 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001721 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001722 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001723 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001724 btrfs_root_last_snapshot(&root->root_item))
1725 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001726 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1727 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001728
1729 /*
1730 * The following checks can be expensive, as they need to
1731 * take other locks and do btree or rbtree searches, so
1732 * release the path to avoid blocking other tasks for too
1733 * long.
1734 */
1735 btrfs_release_path(path);
1736
Liu Bo58113752018-01-31 17:09:13 -07001737 ret = btrfs_cross_ref_exist(root, ino,
1738 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001739 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001740 if (ret) {
1741 /*
1742 * ret could be -EIO if the above fails to read
1743 * metadata.
1744 */
1745 if (ret < 0) {
1746 if (cow_start != (u64)-1)
1747 cur_offset = cow_start;
1748 goto error;
1749 }
1750
Nikolay Borisov3e024842019-08-21 10:42:03 +03001751 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001752 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001753 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001754 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001755 disk_bytenr += cur_offset - found_key.offset;
1756 num_bytes = min(end + 1, extent_end) - cur_offset;
1757 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001758 * If there are pending snapshots for this root, we
1759 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001760 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001761 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001762 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001763 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001764 * force cow if csum exists in the range.
1765 * this ensure that csum for a given extent are
1766 * either valid or do not exist.
1767 */
Liu Bo58113752018-01-31 17:09:13 -07001768 ret = csum_exist_in_range(fs_info, disk_bytenr,
1769 num_bytes);
1770 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001771 /*
1772 * ret could be -EIO if the above fails to read
1773 * metadata.
1774 */
1775 if (ret < 0) {
1776 if (cow_start != (u64)-1)
1777 cur_offset = cow_start;
1778 goto error;
1779 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001780 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001781 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001782 }
Filipe Manana20903032021-02-05 12:55:36 +00001783 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001784 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001785 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001786 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001787 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001788 extent_end = found_key.offset + ram_bytes;
1789 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001790 /* Skip extents outside of our requested range */
1791 if (extent_end <= start) {
1792 path->slots[0]++;
1793 goto next_slot;
1794 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001795 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001796 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001797 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001798 }
1799out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001800 /*
1801 * If nocow is false then record the beginning of the range
1802 * that needs to be COWed
1803 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001804 if (!nocow) {
1805 if (cow_start == (u64)-1)
1806 cow_start = cur_offset;
1807 cur_offset = extent_end;
1808 if (cur_offset > end)
1809 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001810 if (!path->nodes[0])
1811 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001812 path->slots[0]++;
1813 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001814 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001815
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001816 /*
1817 * COW range from cow_start to found_key.offset - 1. As the key
1818 * will contain the beginning of the first extent that can be
1819 * NOCOW, following one which needs to be COW'ed
1820 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001821 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001822 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001823 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001824 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001825 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001826 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001827 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001828 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001829
Yan Zhengd899e052008-10-30 14:25:28 -04001830 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001831 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001832 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001833
Nikolay Borisov968322c2020-06-03 08:55:21 +03001834 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001835 orig_start,
1836 disk_bytenr, /* block_start */
1837 num_bytes, /* block_len */
1838 disk_num_bytes, /* orig_block_len */
1839 ram_bytes, BTRFS_COMPRESS_NONE,
1840 BTRFS_ORDERED_PREALLOC);
1841 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001842 ret = PTR_ERR(em);
1843 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001844 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001845 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001846 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001847 disk_bytenr, num_bytes,
1848 num_bytes,
1849 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001850 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001851 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001852 cur_offset + num_bytes - 1,
1853 0);
1854 goto error;
1855 }
Yan Zhengd899e052008-10-30 14:25:28 -04001856 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001857 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001858 disk_bytenr, num_bytes,
1859 num_bytes,
1860 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001861 if (ret)
1862 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001863 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001864
Filipe Mananaf78c4362016-05-09 13:15:41 +01001865 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001866 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001867 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001868
Yan, Zhengefa56462010-05-16 10:49:59 -04001869 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001870 BTRFS_DATA_RELOC_TREE_OBJECTID)
1871 /*
1872 * Error handled later, as we must prevent
1873 * extent_clear_unlock_delalloc() in error handler
1874 * from freeing metadata of created ordered extent.
1875 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001876 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001877 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001878
Nikolay Borisov968322c2020-06-03 08:55:21 +03001879 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001880 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001881 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001882 EXTENT_DELALLOC |
1883 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001884 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001885
Yan Zheng80ff3852008-10-30 14:20:02 -04001886 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001887
1888 /*
1889 * btrfs_reloc_clone_csums() error, now we're OK to call error
1890 * handler, as metadata for created ordered extent will only
1891 * be freed by btrfs_finish_ordered_io().
1892 */
1893 if (ret)
1894 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001895 if (cur_offset > end)
1896 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001897 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001898 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001899
Robbie Ko506481b2018-10-30 18:04:04 +08001900 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001901 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001902
Yan Zheng80ff3852008-10-30 14:20:02 -04001903 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001904 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001905 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1906 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001907 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001909 }
1910
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001912 if (nocow)
1913 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1914
Josef Bacik17ca04a2012-05-31 15:58:55 -04001915 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001916 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001917 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001918 EXTENT_DELALLOC | EXTENT_DEFRAG |
1919 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001920 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001921 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001922 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001924}
1925
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001926static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001927{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001928 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1929 if (inode->defrag_bytes &&
1930 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1931 0, NULL))
1932 return false;
1933 return true;
1934 }
1935 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001936}
1937
Chris Masond352ac62008-09-29 15:18:18 -04001938/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001939 * Function to process delayed allocation (create CoW) for ranges which are
1940 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001941 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001942int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001943 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1944 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001945{
Chris Masonbe20aa92007-12-17 20:14:01 -05001946 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001947 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001948
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001949 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001950 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001951 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001952 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001953 } else if (!inode_can_compress(inode) ||
1954 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001955 if (zoned)
1956 ret = run_delalloc_zoned(inode, locked_page, start, end,
1957 page_started, nr_written);
1958 else
1959 ret = cow_file_range(inode, locked_page, start, end,
1960 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001961 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001962 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1963 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001964 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001965 }
Qu Wenruo7361b4a2021-07-28 14:05:05 +08001966 ASSERT(ret <= 0);
Qu Wenruo524272602017-03-08 10:25:52 +08001967 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001968 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001969 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001970 return ret;
1971}
1972
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001973void btrfs_split_delalloc_extent(struct inode *inode,
1974 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001975{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001976 u64 size;
1977
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001978 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001979 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001980 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001981
Josef Bacikdcab6a32015-02-11 15:08:59 -05001982 size = orig->end - orig->start + 1;
1983 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001984 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001985 u64 new_size;
1986
1987 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001988 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001989 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001990 */
1991 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001992 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001993 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001994 num_extents += count_max_extents(new_size);
1995 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001996 return;
1997 }
1998
Josef Bacik9e0baf62011-07-15 15:16:44 +00001999 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002000 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002001 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002002}
2003
2004/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002005 * Handle merged delayed allocation extents so we can keep track of new extents
2006 * that are just merged onto old extents, such as when we are doing sequential
2007 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04002008 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02002009void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2010 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002011{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002012 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01002013 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002014
Josef Bacik9ed74f22009-09-11 16:12:44 -04002015 /* not delalloc, ignore it */
2016 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002017 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002018
Josef Bacik8461a3d2015-03-13 15:12:08 -04002019 if (new->start > other->start)
2020 new_size = new->end - other->start + 1;
2021 else
2022 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002023
2024 /* we're not bigger than the max, unreserve the space and go */
2025 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2026 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002027 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002028 spin_unlock(&BTRFS_I(inode)->lock);
2029 return;
2030 }
2031
2032 /*
Josef Bacikba117212015-03-13 15:01:24 -04002033 * We have to add up either side to figure out how many extents were
2034 * accounted for before we merged into one big extent. If the number of
2035 * extents we accounted for is <= the amount we need for the new range
2036 * then we can return, otherwise drop. Think of it like this
2037 *
2038 * [ 4k][MAX_SIZE]
2039 *
2040 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2041 * need 2 outstanding extents, on one side we have 1 and the other side
2042 * we have 1 so they are == and we can return. But in this case
2043 *
2044 * [MAX_SIZE+4k][MAX_SIZE+4k]
2045 *
2046 * Each range on their own accounts for 2 extents, but merged together
2047 * they are only 3 extents worth of accounting, so we need to drop in
2048 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002049 */
Josef Bacikba117212015-03-13 15:01:24 -04002050 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002051 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002052 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002053 num_extents += count_max_extents(old_size);
2054 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002055 return;
2056
Josef Bacik9e0baf62011-07-15 15:16:44 +00002057 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002058 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002059 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002060}
2061
Miao Xieeb73c1b2013-05-15 07:48:22 +00002062static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2063 struct inode *inode)
2064{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002065 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2066
Miao Xieeb73c1b2013-05-15 07:48:22 +00002067 spin_lock(&root->delalloc_lock);
2068 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2069 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2070 &root->delalloc_inodes);
2071 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2072 &BTRFS_I(inode)->runtime_flags);
2073 root->nr_delalloc_inodes++;
2074 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002075 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002076 BUG_ON(!list_empty(&root->delalloc_root));
2077 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002078 &fs_info->delalloc_roots);
2079 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002080 }
2081 }
2082 spin_unlock(&root->delalloc_lock);
2083}
2084
Nikolay Borisov2b877332018-04-27 12:21:51 +03002085
2086void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2087 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002088{
David Sterba3ffbd682018-06-29 10:56:42 +02002089 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002090
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002091 if (!list_empty(&inode->delalloc_inodes)) {
2092 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002093 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002094 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002095 root->nr_delalloc_inodes--;
2096 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002097 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002098 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002099 BUG_ON(list_empty(&root->delalloc_root));
2100 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002101 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002102 }
2103 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002104}
2105
2106static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2107 struct btrfs_inode *inode)
2108{
2109 spin_lock(&root->delalloc_lock);
2110 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002111 spin_unlock(&root->delalloc_lock);
2112}
2113
Chris Masond352ac62008-09-29 15:18:18 -04002114/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002115 * Properly track delayed allocation bytes in the inode and to maintain the
2116 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002117 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002118void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2119 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002120{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2122
Wang Shilong47059d92014-07-03 18:22:07 +08002123 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2124 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002125 /*
2126 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002127 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002128 * bit, which is only set or cleared with irqs on
2129 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002130 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002131 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002132 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002133 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002134 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002135
Josef Bacik8b62f872017-10-19 14:15:55 -04002136 spin_lock(&BTRFS_I(inode)->lock);
2137 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2138 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002139
Josef Bacik6a3891c2015-03-16 17:38:52 -04002140 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002141 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002142 return;
2143
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002144 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2145 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002146 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002147 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002148 if (*bits & EXTENT_DEFRAG)
2149 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002150 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002151 &BTRFS_I(inode)->runtime_flags))
2152 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002153 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002154 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002155
2156 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2157 (*bits & EXTENT_DELALLOC_NEW)) {
2158 spin_lock(&BTRFS_I(inode)->lock);
2159 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2160 state->start;
2161 spin_unlock(&BTRFS_I(inode)->lock);
2162 }
Chris Mason291d6732008-01-29 15:55:23 -05002163}
2164
Chris Masond352ac62008-09-29 15:18:18 -04002165/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002166 * Once a range is no longer delalloc this function ensures that proper
2167 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002168 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002169void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2170 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002171{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002172 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2173 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002174 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002175 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002176
Filipe Manana4a4b9642017-07-27 19:52:55 +01002177 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2178 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002179 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002180 spin_unlock(&inode->lock);
2181 }
Wang Shilong47059d92014-07-03 18:22:07 +08002182
Chris Mason75eff682008-12-15 15:54:40 -05002183 /*
2184 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002185 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002186 * bit, which is only set or cleared with irqs on
2187 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002188 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002189 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002190 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002191
Josef Bacik8b62f872017-10-19 14:15:55 -04002192 spin_lock(&inode->lock);
2193 btrfs_mod_outstanding_extents(inode, -num_extents);
2194 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002195
Josef Bacikb6d08f02013-09-27 14:57:43 -04002196 /*
2197 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002198 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002199 * error.
2200 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002201 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002203 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002204
Josef Bacik6a3891c2015-03-16 17:38:52 -04002205 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002206 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002207 return;
2208
Filipe Mananaa315e682017-03-06 23:04:20 +00002209 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2210 do_list && !(state->state & EXTENT_NORESERVE) &&
2211 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002212 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002213
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002214 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2215 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002216 spin_lock(&inode->lock);
2217 inode->delalloc_bytes -= len;
2218 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002219 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002220 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002221 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002222 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002223 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002224
2225 if ((state->state & EXTENT_DELALLOC_NEW) &&
2226 (*bits & EXTENT_DELALLOC_NEW)) {
2227 spin_lock(&inode->lock);
2228 ASSERT(inode->new_delalloc_bytes >= len);
2229 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002230 if (*bits & EXTENT_ADD_INODE_BYTES)
2231 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002232 spin_unlock(&inode->lock);
2233 }
Chris Mason291d6732008-01-29 15:55:23 -05002234}
2235
Chris Masond352ac62008-09-29 15:18:18 -04002236/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002237 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2238 * in a chunk's stripe. This function ensures that bios do not span a
2239 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002240 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002241 * @page - The page we are about to add to the bio
2242 * @size - size we want to add to the bio
2243 * @bio - bio we want to ensure is smaller than a stripe
2244 * @bio_flags - flags of the bio
2245 *
2246 * return 1 if page cannot be added to the bio
2247 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002248 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002249 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002250int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2251 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002253 struct inode *inode = page->mapping->host;
2254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002255 u64 logical = bio->bi_iter.bi_sector << 9;
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002256 u32 bio_len = bio->bi_iter.bi_size;
Michal Rostecki42034312021-01-27 14:57:27 +01002257 struct extent_map *em;
Michal Rostecki42034312021-01-27 14:57:27 +01002258 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002259 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002260
Chris Mason771ed682008-11-06 22:02:51 -05002261 if (bio_flags & EXTENT_BIO_COMPRESSED)
2262 return 0;
2263
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002264 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
Michal Rostecki42034312021-01-27 14:57:27 +01002265 if (IS_ERR(em))
2266 return PTR_ERR(em);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002267 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002268 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002269 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002270
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002271 if (geom.len < bio_len + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002272 ret = 1;
2273out:
2274 free_extent_map(em);
2275 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002276}
2277
Chris Masond352ac62008-09-29 15:18:18 -04002278/*
2279 * in order to insert checksums into the metadata in large chunks,
2280 * we wait until bio submission time. All the pages in the bio are
2281 * checksummed and sums are attached onto the ordered extent record.
2282 *
2283 * At IO completion time the cums attached on the ordered extent record
2284 * are inserted into the btree
2285 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002286static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002287 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002288{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002289 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002290}
Chris Masone0156402008-04-16 11:15:20 -04002291
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002292/*
2293 * Split an extent_map at [start, start + len]
2294 *
2295 * This function is intended to be used only for extract_ordered_extent().
2296 */
2297static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2298 u64 pre, u64 post)
2299{
2300 struct extent_map_tree *em_tree = &inode->extent_tree;
2301 struct extent_map *em;
2302 struct extent_map *split_pre = NULL;
2303 struct extent_map *split_mid = NULL;
2304 struct extent_map *split_post = NULL;
2305 int ret = 0;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002306 unsigned long flags;
2307
2308 /* Sanity check */
2309 if (pre == 0 && post == 0)
2310 return 0;
2311
2312 split_pre = alloc_extent_map();
2313 if (pre)
2314 split_mid = alloc_extent_map();
2315 if (post)
2316 split_post = alloc_extent_map();
2317 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2318 ret = -ENOMEM;
2319 goto out;
2320 }
2321
2322 ASSERT(pre + post < len);
2323
2324 lock_extent(&inode->io_tree, start, start + len - 1);
2325 write_lock(&em_tree->lock);
2326 em = lookup_extent_mapping(em_tree, start, len);
2327 if (!em) {
2328 ret = -EIO;
2329 goto out_unlock;
2330 }
2331
2332 ASSERT(em->len == len);
2333 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2334 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
Naohiro Aota63fb5872021-08-09 09:29:18 +09002335 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2336 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2337 ASSERT(!list_empty(&em->list));
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002338
2339 flags = em->flags;
2340 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002341
2342 /* First, replace the em with a new extent_map starting from * em->start */
2343 split_pre->start = em->start;
2344 split_pre->len = (pre ? pre : em->len - post);
2345 split_pre->orig_start = split_pre->start;
2346 split_pre->block_start = em->block_start;
2347 split_pre->block_len = split_pre->len;
2348 split_pre->orig_block_len = split_pre->block_len;
2349 split_pre->ram_bytes = split_pre->len;
2350 split_pre->flags = flags;
2351 split_pre->compress_type = em->compress_type;
2352 split_pre->generation = em->generation;
2353
Naohiro Aota63fb5872021-08-09 09:29:18 +09002354 replace_extent_mapping(em_tree, em, split_pre, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002355
2356 /*
2357 * Now we only have an extent_map at:
2358 * [em->start, em->start + pre] if pre != 0
2359 * [em->start, em->start + em->len - post] if pre == 0
2360 */
2361
2362 if (pre) {
2363 /* Insert the middle extent_map */
2364 split_mid->start = em->start + pre;
2365 split_mid->len = em->len - pre - post;
2366 split_mid->orig_start = split_mid->start;
2367 split_mid->block_start = em->block_start + pre;
2368 split_mid->block_len = split_mid->len;
2369 split_mid->orig_block_len = split_mid->block_len;
2370 split_mid->ram_bytes = split_mid->len;
2371 split_mid->flags = flags;
2372 split_mid->compress_type = em->compress_type;
2373 split_mid->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002374 add_extent_mapping(em_tree, split_mid, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002375 }
2376
2377 if (post) {
2378 split_post->start = em->start + em->len - post;
2379 split_post->len = post;
2380 split_post->orig_start = split_post->start;
2381 split_post->block_start = em->block_start + em->len - post;
2382 split_post->block_len = split_post->len;
2383 split_post->orig_block_len = split_post->block_len;
2384 split_post->ram_bytes = split_post->len;
2385 split_post->flags = flags;
2386 split_post->compress_type = em->compress_type;
2387 split_post->generation = em->generation;
Naohiro Aota63fb5872021-08-09 09:29:18 +09002388 add_extent_mapping(em_tree, split_post, 1);
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002389 }
2390
2391 /* Once for us */
2392 free_extent_map(em);
2393 /* Once for the tree */
2394 free_extent_map(em);
2395
2396out_unlock:
2397 write_unlock(&em_tree->lock);
2398 unlock_extent(&inode->io_tree, start, start + len - 1);
2399out:
2400 free_extent_map(split_pre);
2401 free_extent_map(split_mid);
2402 free_extent_map(split_post);
2403
2404 return ret;
2405}
2406
Naohiro Aotad22002f2021-02-04 19:22:00 +09002407static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2408 struct bio *bio, loff_t file_offset)
2409{
2410 struct btrfs_ordered_extent *ordered;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002411 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002412 u64 file_len;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002413 u64 len = bio->bi_iter.bi_size;
2414 u64 end = start + len;
2415 u64 ordered_end;
2416 u64 pre, post;
2417 int ret = 0;
2418
2419 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2420 if (WARN_ON_ONCE(!ordered))
2421 return BLK_STS_IOERR;
2422
2423 /* No need to split */
2424 if (ordered->disk_num_bytes == len)
2425 goto out;
2426
2427 /* We cannot split once end_bio'd ordered extent */
2428 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2429 ret = -EINVAL;
2430 goto out;
2431 }
2432
2433 /* We cannot split a compressed ordered extent */
2434 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438
2439 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2440 /* bio must be in one ordered extent */
2441 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2442 ret = -EINVAL;
2443 goto out;
2444 }
2445
2446 /* Checksum list should be empty */
2447 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2448 ret = -EINVAL;
2449 goto out;
2450 }
2451
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002452 file_len = ordered->num_bytes;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002453 pre = start - ordered->disk_bytenr;
2454 post = ordered_end - end;
2455
2456 ret = btrfs_split_ordered_extent(ordered, pre, post);
2457 if (ret)
2458 goto out;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002459 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
Naohiro Aotad22002f2021-02-04 19:22:00 +09002460
2461out:
Naohiro Aotad22002f2021-02-04 19:22:00 +09002462 btrfs_put_ordered_extent(ordered);
2463
2464 return errno_to_blk_status(ret);
2465}
2466
Chris Mason4a69a412008-11-06 22:03:00 -05002467/*
Chris Masoncad321a2008-12-17 14:51:42 -05002468 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002469 * on write, or reading the csums from the tree before a read.
2470 *
2471 * Rules about async/sync submit,
2472 * a) read: sync submit
2473 *
2474 * b) write without checksum: sync submit
2475 *
2476 * c) write with checksum:
2477 * c-1) if bio is issued by fsync: sync submit
2478 * (sync_writers != 0)
2479 *
2480 * c-2) if root is reloc root: sync submit
2481 * (only in case of buffered IO)
2482 *
2483 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002484 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002485blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2486 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002487
Chris Mason44b8bd72008-04-16 11:14:51 -04002488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002490 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302491 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002492 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002493 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002494 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002495
Josef Bacik42437a62020-10-16 11:29:18 -04002496 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2497 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002498
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002499 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302500 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002501
Naohiro Aotad22002f2021-02-04 19:22:00 +09002502 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2503 struct page *page = bio_first_bvec_all(bio)->bv_page;
2504 loff_t file_offset = page_offset(page);
2505
2506 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2507 if (ret)
2508 goto out;
2509 }
2510
Naohiro Aotacfe94442021-02-04 19:21:59 +09002511 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002512 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002513 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002514 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002515
Chris Masond20f7042008-12-08 16:58:54 -05002516 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002517 ret = btrfs_submit_compressed_read(inode, bio,
2518 mirror_num,
2519 bio_flags);
2520 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002521 } else {
2522 /*
2523 * Lookup bio sums does extra checks around whether we
2524 * need to csum or not, which is why we ignore skip_sum
2525 * here.
2526 */
Qu Wenruo62751932020-12-02 14:48:06 +08002527 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002528 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002529 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002530 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002531 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002532 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002533 /* csum items have already been cloned */
2534 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2535 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002536 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002537 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2538 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002539 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002540 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002541 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002542 if (ret)
2543 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002544 }
2545
Chris Mason0b86a832008-03-24 15:01:56 -04002546mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002547 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002548
2549out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002550 if (ret) {
2551 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002552 bio_endio(bio);
2553 }
Stefan Behrens61891922012-11-05 18:51:52 +01002554 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002555}
Chris Mason6885f302008-02-20 16:11:05 -05002556
Chris Masond352ac62008-09-29 15:18:18 -04002557/*
2558 * given a list of ordered sums record them in the inode. This happens
2559 * at IO completion time based on sums calculated at bio submission time.
2560 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002561static int add_pending_csums(struct btrfs_trans_handle *trans,
2562 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002563{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002564 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002565 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002566
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002567 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002568 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002569 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002570 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002571 if (ret)
2572 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002573 }
2574 return 0;
2575}
2576
Filipe Mananac3347302020-11-04 11:07:31 +00002577static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2578 const u64 start,
2579 const u64 len,
2580 struct extent_state **cached_state)
2581{
2582 u64 search_start = start;
2583 const u64 end = start + len - 1;
2584
2585 while (search_start < end) {
2586 const u64 search_len = end - search_start + 1;
2587 struct extent_map *em;
2588 u64 em_len;
2589 int ret = 0;
2590
2591 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2592 if (IS_ERR(em))
2593 return PTR_ERR(em);
2594
2595 if (em->block_start != EXTENT_MAP_HOLE)
2596 goto next;
2597
2598 em_len = em->len;
2599 if (em->start < search_start)
2600 em_len -= search_start - em->start;
2601 if (em_len > search_len)
2602 em_len = search_len;
2603
2604 ret = set_extent_bit(&inode->io_tree, search_start,
2605 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002606 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2607 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002608next:
2609 search_start = extent_map_end(em);
2610 free_extent_map(em);
2611 if (ret)
2612 return ret;
2613 }
2614 return 0;
2615}
2616
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002617int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002618 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002619 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002620{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002621 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002622
2623 if (start >= i_size_read(&inode->vfs_inode) &&
2624 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2625 /*
2626 * There can't be any extents following eof in this case so just
2627 * set the delalloc new bit for the range directly.
2628 */
2629 extra_bits |= EXTENT_DELALLOC_NEW;
2630 } else {
2631 int ret;
2632
2633 ret = btrfs_find_new_delalloc_bytes(inode, start,
2634 end + 1 - start,
2635 cached_state);
2636 if (ret)
2637 return ret;
2638 }
2639
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002640 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2641 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002642}
2643
Chris Masond352ac62008-09-29 15:18:18 -04002644/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002645struct btrfs_writepage_fixup {
2646 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002647 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002648 struct btrfs_work work;
2649};
2650
Christoph Hellwigb2950862008-12-02 09:54:17 -05002651static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002652{
2653 struct btrfs_writepage_fixup *fixup;
2654 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002655 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002656 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002657 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002658 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002659 u64 page_start;
2660 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002661 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002662 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002663
2664 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2665 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002666 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002667 page_start = page_offset(page);
2668 page_end = page_offset(page) + PAGE_SIZE - 1;
2669
2670 /*
2671 * This is similar to page_mkwrite, we need to reserve the space before
2672 * we take the page lock.
2673 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002674 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2675 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002676again:
Chris Mason247e7432008-07-17 12:53:51 -04002677 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002678
Chris Mason25f3c502020-01-21 11:51:42 -05002679 /*
2680 * Before we queued this fixup, we took a reference on the page.
2681 * page->mapping may go NULL, but it shouldn't be moved to a different
2682 * address space.
2683 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002684 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2685 /*
2686 * Unfortunately this is a little tricky, either
2687 *
2688 * 1) We got here and our page had already been dealt with and
2689 * we reserved our space, thus ret == 0, so we need to just
2690 * drop our space reservation and bail. This can happen the
2691 * first time we come into the fixup worker, or could happen
2692 * while waiting for the ordered extent.
2693 * 2) Our page was already dealt with, but we happened to get an
2694 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2695 * this case we obviously don't have anything to release, but
2696 * because the page was already dealt with we don't want to
2697 * mark the page with an error, so make sure we're resetting
2698 * ret to 0. This is why we have this check _before_ the ret
2699 * check, because we do not want to have a surprise ENOSPC
2700 * when the page was already properly dealt with.
2701 */
2702 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002703 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2704 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002705 page_start, PAGE_SIZE,
2706 true);
2707 }
2708 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002709 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002710 }
Chris Mason25f3c502020-01-21 11:51:42 -05002711
2712 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002713 * We can't mess with the page state unless it is locked, so now that
2714 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002715 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002716 if (ret)
2717 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002718
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002719 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002720
2721 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002722 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002723 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002724
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002725 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002726 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002727 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2728 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002729 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002730 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002731 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002732 goto again;
2733 }
Chris Mason247e7432008-07-17 12:53:51 -04002734
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002735 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002736 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002737 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002738 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002739
Chris Mason25f3c502020-01-21 11:51:42 -05002740 /*
2741 * Everything went as planned, we're now the owner of a dirty page with
2742 * delayed allocation bits set and space reserved for our COW
2743 * destination.
2744 *
2745 * The page was dirty when we started, nothing should have cleaned it.
2746 */
2747 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002748 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002749out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002750 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002751 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002752 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2753 PAGE_SIZE, true);
2754 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002755 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002756out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002757 if (ret) {
2758 /*
2759 * We hit ENOSPC or other errors. Update the mapping and page
2760 * to reflect the errors and clean the page.
2761 */
2762 mapping_set_error(page->mapping, ret);
2763 end_extent_writepage(page, ret, page_start, page_end);
2764 clear_page_dirty_for_io(page);
2765 SetPageError(page);
2766 }
2767 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002768 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002769 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002770 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002771 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002772 /*
2773 * As a precaution, do a delayed iput in case it would be the last iput
2774 * that could need flushing space. Recursing back to fixup worker would
2775 * deadlock.
2776 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002777 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002778}
2779
2780/*
2781 * There are a few paths in the higher layers of the kernel that directly
2782 * set the page dirty bit without asking the filesystem if it is a
2783 * good idea. This causes problems because we want to make sure COW
2784 * properly happens and the data=ordered rules are followed.
2785 *
Chris Masonc8b97812008-10-29 14:49:59 -04002786 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002787 * hasn't been properly setup for IO. We kick off an async process
2788 * to fix it up. The async helper will wait for ordered extents, set
2789 * the delalloc bit and make it safe to write the page.
2790 */
Qu Wenruoa129ffb2021-07-27 13:41:32 +08002791int btrfs_writepage_cow_fixup(struct page *page)
Chris Mason247e7432008-07-17 12:53:51 -04002792{
2793 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002795 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002796
Qu Wenruof57ad932021-04-07 19:22:13 +08002797 /* This page has ordered extent covering it already */
2798 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002799 return 0;
2800
Chris Mason25f3c502020-01-21 11:51:42 -05002801 /*
2802 * PageChecked is set below when we create a fixup worker for this page,
2803 * don't try to create another one if we're already PageChecked()
2804 *
2805 * The extent_io writepage code will redirty the page if we send back
2806 * EAGAIN.
2807 */
Chris Mason247e7432008-07-17 12:53:51 -04002808 if (PageChecked(page))
2809 return -EAGAIN;
2810
2811 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2812 if (!fixup)
2813 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002814
Josef Bacikf4b13632020-01-21 14:34:52 -05002815 /*
2816 * We are already holding a reference to this inode from
2817 * write_cache_pages. We need to hold it because the space reservation
2818 * takes place outside of the page lock, and we can't trust
2819 * page->mapping outside of the page lock.
2820 */
2821 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002822 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002823 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002824 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002825 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002826 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002827 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002828
2829 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002830}
2831
Yan Zhengd899e052008-10-30 14:25:28 -04002832static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002833 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002834 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002835 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002836 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002837{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002838 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002839 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002840 struct btrfs_path *path;
2841 struct extent_buffer *leaf;
2842 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002843 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2844 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2845 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2846 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002847 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002848 int ret;
2849
2850 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002851 if (!path)
2852 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002853
Chris Masona1ed8352009-09-11 12:27:37 -04002854 /*
2855 * we may be replacing one extent in the tree with another.
2856 * The new extent is pinned in the extent map, and we don't want
2857 * to drop it from the cache until it is completely in the btree.
2858 *
2859 * So, tell btrfs_drop_extents to leave this extent in the cache.
2860 * the caller is expected to unpin it and allow it to be merged
2861 * with the others.
2862 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002863 drop_args.path = path;
2864 drop_args.start = file_pos;
2865 drop_args.end = file_pos + num_bytes;
2866 drop_args.replace_extent = true;
2867 drop_args.extent_item_size = sizeof(*stack_fi);
2868 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869 if (ret)
2870 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002871
Filipe Manana5893dfb2020-11-04 11:07:32 +00002872 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002873 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002874 ins.offset = file_pos;
2875 ins.type = BTRFS_EXTENT_DATA_KEY;
2876
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002877 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002878 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002879 if (ret)
2880 goto out;
2881 }
Yan Zhengd899e052008-10-30 14:25:28 -04002882 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002883 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2884 write_extent_buffer(leaf, stack_fi,
2885 btrfs_item_ptr_offset(leaf, path->slots[0]),
2886 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002887
Yan Zhengd899e052008-10-30 14:25:28 -04002888 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002889 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002890
Filipe Manana2766ff62020-11-04 11:07:34 +00002891 /*
2892 * If we dropped an inline extent here, we know the range where it is
2893 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
David Sterba1a9fd412021-05-21 17:42:23 +02002894 * number of bytes only for that range containing the inline extent.
Filipe Manana2766ff62020-11-04 11:07:34 +00002895 * The remaining of the range will be processed when clearning the
2896 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2897 */
2898 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2899 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2900
2901 inline_size = drop_args.bytes_found - inline_size;
2902 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2903 drop_args.bytes_found -= inline_size;
2904 num_bytes -= sectorsize;
2905 }
2906
2907 if (update_inode_bytes)
2908 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002909
2910 ins.objectid = disk_bytenr;
2911 ins.offset = disk_num_bytes;
2912 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002913
Nikolay Borisovc553f942020-06-03 08:55:19 +03002914 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002915 if (ret)
2916 goto out;
2917
Nikolay Borisovc553f942020-06-03 08:55:19 +03002918 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002919 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002920out:
Yan Zhengd899e052008-10-30 14:25:28 -04002921 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002922
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002923 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002924}
2925
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002926static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002927 u64 start, u64 len)
2928{
David Sterba32da53862019-10-29 19:20:18 +01002929 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002930
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002931 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002932 ASSERT(cache);
2933
2934 spin_lock(&cache->lock);
2935 cache->delalloc_bytes -= len;
2936 spin_unlock(&cache->lock);
2937
2938 btrfs_put_block_group(cache);
2939}
2940
Qu Wenruo203f44c52020-06-10 09:04:40 +08002941static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002942 struct btrfs_ordered_extent *oe)
2943{
2944 struct btrfs_file_extent_item stack_fi;
2945 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002946 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002947
2948 memset(&stack_fi, 0, sizeof(stack_fi));
2949 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2950 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2951 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2952 oe->disk_num_bytes);
2953 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2954 logical_len = oe->truncated_len;
2955 else
2956 logical_len = oe->num_bytes;
2957 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2958 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2959 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2960 /* Encryption and other encoding is reserved and all 0 */
2961
Filipe Manana2766ff62020-11-04 11:07:34 +00002962 /*
2963 * For delalloc, when completing an ordered extent we update the inode's
2964 * bytes when clearing the range in the inode's io tree, so pass false
2965 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2966 * except if the ordered extent was truncated.
2967 */
2968 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2969 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2970
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002971 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2972 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002973 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002974}
2975
2976/*
2977 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002978 * an ordered extent if the range of bytes in the file it covers are
2979 * fully written.
2980 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002981static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002982{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002983 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2984 struct btrfs_root *root = inode->root;
2985 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002986 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002987 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002988 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002989 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002990 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002991 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002992 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002993 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002994 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002995 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002996 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002997
Omar Sandovalbffe6332019-12-02 17:34:19 -08002998 start = ordered_extent->file_offset;
2999 end = start + ordered_extent->num_bytes - 1;
3000
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003001 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3002 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3003 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00003004 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003005
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003006 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003007
Josef Bacik5fd02042012-05-02 14:00:54 -04003008 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3009 ret = -EIO;
3010 goto out;
3011 }
3012
Christoph Hellwigc7c3a6d2021-07-22 09:53:59 +02003013 if (ordered_extent->bdev)
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003014 btrfs_rewrite_logical_zoned(ordered_extent);
3015
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003016 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08003017
Josef Bacik77cef2e2013-08-29 13:57:21 -04003018 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3019 truncated = true;
3020 logical_len = ordered_extent->truncated_len;
3021 /* Truncated the entire extent, don't bother adding */
3022 if (!logical_len)
3023 goto out;
3024 }
3025
Yan, Zhengc2167752009-11-12 09:34:21 +00003026 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003028
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003029 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003030 if (freespace_inode)
3031 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003032 else
3033 trans = btrfs_join_transaction(root);
3034 if (IS_ERR(trans)) {
3035 ret = PTR_ERR(trans);
3036 trans = NULL;
3037 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003038 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003039 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02003040 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003041 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003042 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003043 goto out;
3044 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003045
Filipe Manana2766ff62020-11-04 11:07:34 +00003046 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003047 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003048
Nikolay Borisov8d510122019-10-08 20:43:06 +03003049 if (freespace_inode)
3050 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003051 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003052 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 if (IS_ERR(trans)) {
3054 ret = PTR_ERR(trans);
3055 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003056 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003057 }
Chris Masona79b7d42014-05-22 16:18:52 -07003058
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003059 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003060
Chris Masonc8b97812008-10-29 14:49:59 -04003061 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003062 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003063 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003064 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003065 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04003066 ordered_extent->file_offset,
3067 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003068 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003069 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003070 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03003071 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04003072 if (!ret) {
3073 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003074 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003075 ordered_extent->disk_bytenr,
3076 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04003077 }
Yan Zhengd899e052008-10-30 14:25:28 -04003078 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003079 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003080 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003081 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003082 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003083 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003084 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003085
Nikolay Borisov510f85e2020-09-18 12:15:52 +03003086 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02003087 if (ret) {
3088 btrfs_abort_transaction(trans, ret);
3089 goto out;
3090 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091
Filipe Manana2766ff62020-11-04 11:07:34 +00003092 /*
3093 * If this is a new delalloc range, clear its new delalloc flag to
3094 * update the inode's number of bytes. This needs to be done first
3095 * before updating the inode item.
3096 */
3097 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3098 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003099 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00003100 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3101 0, 0, &cached_state);
3102
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003103 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003104 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003105 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003106 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003107 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003108 }
3109 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003110out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003111 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003112 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003113 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003114
Miao Xiea698d0752012-09-20 01:51:59 -06003115 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003117
Josef Bacik77cef2e2013-08-29 13:57:21 -04003118 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003119 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003120
Josef Bacikd61bec02021-05-19 09:38:27 -04003121 /*
3122 * If we failed to finish this ordered extent for any reason we
3123 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3124 * extent, and mark the inode with the error if it wasn't
3125 * already set. Any error during writeback would have already
3126 * set the mapping error, so we need to set it if we're the ones
3127 * marking this ordered extent as failed.
3128 */
3129 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3130 &ordered_extent->flags))
3131 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3132
Josef Bacik77cef2e2013-08-29 13:57:21 -04003133 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003134 unwritten_start += logical_len;
3135 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003136
3137 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003138 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003139
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003140 /*
3141 * If the ordered extent had an IOERR or something else went
3142 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003143 * back to the allocator. We only free the extent in the
3144 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003145 *
3146 * If we made it past insert_reserved_file_extent before we
3147 * errored out then we don't need to do this as the accounting
3148 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003149 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003150 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003151 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003152 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003153 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3154 /*
3155 * Discard the range before returning it back to the
3156 * free space pool
3157 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003158 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003159 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003160 ordered_extent->disk_bytenr,
3161 ordered_extent->disk_num_bytes,
3162 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003163 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003164 ordered_extent->disk_bytenr,
3165 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003166 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003167 }
3168
Josef Bacik5fd02042012-05-02 14:00:54 -04003169 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003170 * This needs to be done to make sure anybody waiting knows we are done
3171 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003172 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003173 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003174
Chris Masone6dcd2d2008-07-17 12:53:50 -04003175 /* once for us */
3176 btrfs_put_ordered_extent(ordered_extent);
3177 /* once for the tree */
3178 btrfs_put_ordered_extent(ordered_extent);
3179
Josef Bacik5fd02042012-05-02 14:00:54 -04003180 return ret;
3181}
3182
3183static void finish_ordered_fn(struct btrfs_work *work)
3184{
3185 struct btrfs_ordered_extent *ordered_extent;
3186 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3187 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003188}
3189
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003190void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3191 struct page *page, u64 start,
David Sterba25c12522021-07-26 14:15:08 +02003192 u64 end, bool uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003193{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003194 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003195
Qu Wenruoe65f1522021-04-01 15:15:06 +08003196 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3197 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003198}
3199
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003200/*
3201 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003202 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003203 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003204 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003205 * @page: page where is the data to be verified
3206 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003207 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003208 *
3209 * The length of such check is always one sector size.
3210 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003211static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003212 u32 bio_offset, struct page *page, u32 pgoff,
3213 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003214{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3216 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003217 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003218 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003219 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003220 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003221 u8 *csum_expected;
3222 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003223
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003224 ASSERT(pgoff + len <= PAGE_SIZE);
3225
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003226 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3227 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003228
3229 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003230 shash->tfm = fs_info->csum_shash;
3231
Eric Biggersfd080012020-04-30 23:51:59 -07003232 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003233
3234 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003235 goto zeroit;
3236
3237 kunmap_atomic(kaddr);
3238 return 0;
3239zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003240 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3241 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003242 if (io_bio->device)
3243 btrfs_dev_stat_inc_and_print(io_bio->device,
3244 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003245 memset(kaddr + pgoff, 1, len);
3246 flush_dcache_page(page);
3247 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003248 return -EIO;
3249}
3250
Chris Masond352ac62008-09-29 15:18:18 -04003251/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003252 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003253 * if there's a match, we allow the bio to finish. If not, the code in
3254 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003255 *
3256 * @bio_offset: offset to the beginning of the bio (in bytes)
3257 * @start: file offset of the range start
3258 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003259 *
3260 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3261 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003262 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003263unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3264 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003265{
Chris Mason07157aa2007-08-30 08:50:51 -04003266 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003267 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003268 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003269 const u32 sectorsize = root->fs_info->sectorsize;
3270 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003271 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003272
Chris Masond20f7042008-12-08 16:58:54 -05003273 if (PageChecked(page)) {
3274 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003275 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003276 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003277
Qu Wenruo3670e642021-07-26 14:34:51 +08003278 /*
3279 * For subpage case, above PageChecked is not safe as it's not subpage
3280 * compatible.
3281 * But for now only cow fixup and compressed read utilize PageChecked
3282 * flag, while in this context we can easily use io_bio->csum to
3283 * determine if we really need to do csum verification.
3284 *
3285 * So for now, just exit if io_bio->csum is NULL, as it means it's
3286 * compressed read, and its compressed data csum has already been
3287 * verified.
3288 */
3289 if (io_bio->csum == NULL)
3290 return 0;
3291
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003292 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003293 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003294
Josef Bacik42437a62020-10-16 11:29:18 -04003295 if (!root->fs_info->csum_root)
3296 return 0;
3297
Qu Wenruof44cf412020-12-02 14:47:59 +08003298 ASSERT(page_offset(page) <= start &&
3299 end <= page_offset(page) + PAGE_SIZE - 1);
3300 for (pg_off = offset_in_page(start);
3301 pg_off < offset_in_page(end);
3302 pg_off += sectorsize, bio_offset += sectorsize) {
Qu Wenruoe3c62322021-07-26 14:35:04 +08003303 u64 file_offset = pg_off + page_offset(page);
Qu Wenruof44cf412020-12-02 14:47:59 +08003304 int ret;
3305
Qu Wenruoe3c62322021-07-26 14:35:04 +08003306 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3307 test_range_bit(io_tree, file_offset,
3308 file_offset + sectorsize - 1,
3309 EXTENT_NODATASUM, 1, NULL)) {
3310 /* Skip the range without csum for data reloc inode */
3311 clear_extent_bits(io_tree, file_offset,
3312 file_offset + sectorsize - 1,
3313 EXTENT_NODATASUM);
3314 continue;
3315 }
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003316 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3317 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003318 if (ret < 0) {
3319 const int nr_bit = (pg_off - offset_in_page(start)) >>
3320 root->fs_info->sectorsize_bits;
3321
3322 result |= (1U << nr_bit);
3323 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003324 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003325 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003326}
Chris Masonb888db22007-08-27 16:49:44 -04003327
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003328/*
3329 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3330 *
3331 * @inode: The inode we want to perform iput on
3332 *
3333 * This function uses the generic vfs_inode::i_count to track whether we should
3334 * just decrement it (in case it's > 1) or if this is the last iput then link
3335 * the inode to the delayed iput machinery. Delayed iputs are processed at
3336 * transaction commit time/superblock commit/cleaner kthread.
3337 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003338void btrfs_add_delayed_iput(struct inode *inode)
3339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003341 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003342
3343 if (atomic_add_unless(&inode->i_count, -1, 1))
3344 return;
3345
Josef Bacik034f7842018-12-03 11:06:52 -05003346 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003347 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003348 ASSERT(list_empty(&binode->delayed_iput));
3349 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003350 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003351 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3352 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003353}
3354
Josef Bacik63611e72019-06-18 10:59:18 -04003355static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3356 struct btrfs_inode *inode)
3357{
3358 list_del_init(&inode->delayed_iput);
3359 spin_unlock(&fs_info->delayed_iput_lock);
3360 iput(&inode->vfs_inode);
3361 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3362 wake_up(&fs_info->delayed_iputs_wait);
3363 spin_lock(&fs_info->delayed_iput_lock);
3364}
3365
3366static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3367 struct btrfs_inode *inode)
3368{
3369 if (!list_empty(&inode->delayed_iput)) {
3370 spin_lock(&fs_info->delayed_iput_lock);
3371 if (!list_empty(&inode->delayed_iput))
3372 run_delayed_iput_locked(fs_info, inode);
3373 spin_unlock(&fs_info->delayed_iput_lock);
3374 }
3375}
3376
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003377void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003378{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003379
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003380 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003381 while (!list_empty(&fs_info->delayed_iputs)) {
3382 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003383
David Sterba8089fe62015-11-19 14:15:51 +01003384 inode = list_first_entry(&fs_info->delayed_iputs,
3385 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003386 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003387 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003388 }
David Sterba8089fe62015-11-19 14:15:51 +01003389 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003390}
3391
Josef Bacik034f7842018-12-03 11:06:52 -05003392/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003393 * Wait for flushing all delayed iputs
3394 *
3395 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003396 *
3397 * This will wait on any delayed iputs that are currently running with KILLABLE
3398 * set. Once they are all done running we will return, unless we are killed in
3399 * which case we return EINTR. This helps in user operations like fallocate etc
3400 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003401 *
3402 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003403 */
3404int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3405{
3406 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3407 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3408 if (ret)
3409 return -EINTR;
3410 return 0;
3411}
3412
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003414 * This creates an orphan entry for the given inode in case something goes wrong
3415 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003416 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003417int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003418 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003419{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003421
Omar Sandoval27919062018-05-11 13:13:37 -07003422 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3423 if (ret && ret != -EEXIST) {
3424 btrfs_abort_transaction(trans, ret);
3425 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003426 }
3427
Yan, Zhengd68fc572010-05-16 10:49:58 -04003428 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003429}
3430
3431/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003432 * We have done the delete so we can go ahead and remove the orphan item for
3433 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003434 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003435static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003436 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003437{
Omar Sandoval27919062018-05-11 13:13:37 -07003438 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003439}
3440
3441/*
3442 * this cleans up any orphans that may be left on the list from the last use
3443 * of this root.
3444 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003447 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003448 struct btrfs_path *path;
3449 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003450 struct btrfs_key key, found_key;
3451 struct btrfs_trans_handle *trans;
3452 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003453 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003454 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003455
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003458
3459 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 if (!path) {
3461 ret = -ENOMEM;
3462 goto out;
3463 }
David Sterbae4058b52015-11-27 16:31:35 +01003464 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003465
3466 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003467 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003468 key.offset = (u64)-1;
3469
Josef Bacik7b128762008-07-24 12:17:14 -04003470 while (1) {
3471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (ret < 0)
3473 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003474
3475 /*
3476 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003477 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003478 * find the key and see if we have stuff that matches
3479 */
3480 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003482 if (path->slots[0] == 0)
3483 break;
3484 path->slots[0]--;
3485 }
3486
3487 /* pull out the item */
3488 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3490
3491 /* make sure the item matches what we want */
3492 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3493 break;
David Sterba962a2982014-06-04 18:41:45 +02003494 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003495 break;
3496
3497 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003498 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003499
3500 /*
3501 * this is where we are basically btrfs_lookup, without the
3502 * crossing root thing. we store the inode number in the
3503 * offset of the orphan item.
3504 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003505
3506 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 btrfs_err(fs_info,
3508 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003509 ret = -EINVAL;
3510 goto out;
3511 }
3512
3513 last_objectid = found_key.offset;
3514
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003515 found_key.objectid = found_key.offset;
3516 found_key.type = BTRFS_INODE_ITEM_KEY;
3517 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003518 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303519 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003520 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003522
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003524 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003525 int is_dead_root = 0;
3526
3527 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003528 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003529 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003530 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003531 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003532 * We need to distinguish those two, as the orphan item
3533 * for a root must not get deleted before the deletion
3534 * of the snapshot/subvolume's tree completes.
3535 *
3536 * btrfs_find_orphan_roots() ran before us, which has
3537 * found all deleted roots and loaded them into
3538 * fs_info->fs_roots_radix. So here we can find if an
3539 * orphan item corresponds to a deleted root by looking
3540 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003541 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003542
3543 spin_lock(&fs_info->fs_roots_radix_lock);
3544 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3545 (unsigned long)found_key.objectid);
3546 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3547 is_dead_root = 1;
3548 spin_unlock(&fs_info->fs_roots_radix_lock);
3549
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003550 if (is_dead_root) {
3551 /* prevent this orphan from being found again */
3552 key.offset = found_key.objectid - 1;
3553 continue;
3554 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003555
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003556 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003557
Josef Bacika8c9e572011-09-21 16:55:59 -04003558 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003559 * If we have an inode with links, there are a couple of
Boris Burkov70524252021-06-30 13:01:50 -07003560 * possibilities:
3561 *
3562 * 1. We were halfway through creating fsverity metadata for the
3563 * file. In that case, the orphan item represents incomplete
3564 * fsverity metadata which must be cleaned up with
3565 * btrfs_drop_verity_items and deleting the orphan item.
3566
3567 * 2. Old kernels (before v3.12) used to create an
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003568 * orphan item for truncate indicating that there were possibly
3569 * extent items past i_size that needed to be deleted. In v3.12,
3570 * truncate was changed to update i_size in sync with the extent
3571 * items, but the (useless) orphan item was still created. Since
3572 * v4.18, we don't create the orphan item for truncate at all.
3573 *
3574 * So, this item could mean that we need to do a truncate, but
3575 * only if this filesystem was last used on a pre-v3.12 kernel
3576 * and was not cleanly unmounted. The odds of that are quite
3577 * slim, and it's a pain to do the truncate now, so just delete
3578 * the orphan item.
3579 *
3580 * It's also possible that this orphan item was supposed to be
3581 * deleted but wasn't. The inode number may have been reused,
3582 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003584 if (ret == -ENOENT || inode->i_nlink) {
Boris Burkov70524252021-06-30 13:01:50 -07003585 if (!ret) {
3586 ret = btrfs_drop_verity_items(BTRFS_I(inode));
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003587 iput(inode);
Boris Burkov70524252021-06-30 13:01:50 -07003588 if (ret)
3589 goto out;
3590 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003591 trans = btrfs_start_transaction(root, 1);
3592 if (IS_ERR(trans)) {
3593 ret = PTR_ERR(trans);
3594 goto out;
3595 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003596 btrfs_debug(fs_info, "auto deleting %Lu",
3597 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003598 ret = btrfs_del_orphan_item(trans, root,
3599 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003600 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003601 if (ret)
3602 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003603 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003604 }
Josef Bacik7b128762008-07-24 12:17:14 -04003605
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003606 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003607
3608 /* this will do delete_inode and everything for us */
3609 iput(inode);
3610 }
Miao Xie3254c872011-11-10 20:45:05 -05003611 /* release the path since we're done with it */
3612 btrfs_release_path(path);
3613
Yan, Zhengd68fc572010-05-16 10:49:58 -04003614 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3615
Omar Sandovala575cee2018-05-11 13:13:38 -07003616 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003617 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003618 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003619 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003620 }
Josef Bacik7b128762008-07-24 12:17:14 -04003621
3622 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003624
3625out:
3626 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003627 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628 btrfs_free_path(path);
3629 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003630}
3631
Chris Masond352ac62008-09-29 15:18:18 -04003632/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003633 * very simple check to peek ahead in the leaf looking for xattrs. If we
3634 * don't find any xattrs, we know there can't be any acls.
3635 *
3636 * slot is the slot the inode is in, objectid is the objectid of the inode
3637 */
3638static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003639 int slot, u64 objectid,
3640 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003641{
3642 u32 nritems = btrfs_header_nritems(leaf);
3643 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 static u64 xattr_access = 0;
3645 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 int scanned = 0;
3647
Josef Bacikf23b5a52013-06-19 10:16:26 -04003648 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003649 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3650 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3651 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3652 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003653 }
3654
Chris Mason46a53cc2009-04-27 11:47:50 -04003655 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003656 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003657 while (slot < nritems) {
3658 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3659
3660 /* we found a different objectid, there must not be acls */
3661 if (found_key.objectid != objectid)
3662 return 0;
3663
3664 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003665 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003666 if (*first_xattr_slot == -1)
3667 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003668 if (found_key.offset == xattr_access ||
3669 found_key.offset == xattr_default)
3670 return 1;
3671 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003672
3673 /*
3674 * we found a key greater than an xattr key, there can't
3675 * be any acls later on
3676 */
3677 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3678 return 0;
3679
3680 slot++;
3681 scanned++;
3682
3683 /*
3684 * it goes inode, inode backrefs, xattrs, extents,
3685 * so if there are a ton of hard links to an inode there can
3686 * be a lot of backrefs. Don't waste time searching too hard,
3687 * this is just an optimization
3688 */
3689 if (scanned >= 8)
3690 break;
3691 }
3692 /* we hit the end of the leaf before we found an xattr or
3693 * something larger than an xattr. We have to assume the inode
3694 * has acls
3695 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003696 if (*first_xattr_slot == -1)
3697 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003698 return 1;
3699}
3700
3701/*
Chris Masond352ac62008-09-29 15:18:18 -04003702 * read an inode from the btree into the in-memory inode
3703 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003704static int btrfs_read_locked_inode(struct inode *inode,
3705 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003708 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003710 struct btrfs_inode_item *inode_item;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
3712 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003713 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003714 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003715 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003717 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003718 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003719
3720 ret = btrfs_fill_inode(inode, &rdev);
3721 if (!ret)
3722 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003723
Filipe Manana4222ea72018-10-24 10:13:03 +01003724 if (!path) {
3725 path = btrfs_alloc_path();
3726 if (!path)
3727 return -ENOMEM;
3728 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003729
Chris Mason39279cc2007-06-12 06:35:45 -04003730 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003731
Chris Mason39279cc2007-06-12 06:35:45 -04003732 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003733 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003734 if (path != in_path)
3735 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003736 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003737 }
Chris Mason39279cc2007-06-12 06:35:45 -04003738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740
3741 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003742 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3745 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003746 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003747 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003748 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3749 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003750 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003751 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3752 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003753
David Sterbaa937b972014-12-12 17:39:12 +01003754 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3755 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
David Sterbaa937b972014-12-12 17:39:12 +01003757 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3758 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
David Sterbaa937b972014-12-12 17:39:12 +01003760 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3761 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003762
chandan r9cc97d62012-07-04 12:48:07 +05303763 BTRFS_I(inode)->i_otime.tv_sec =
3764 btrfs_timespec_sec(leaf, &inode_item->otime);
3765 BTRFS_I(inode)->i_otime.tv_nsec =
3766 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003768 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003769 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003770 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3771
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003772 inode_set_iversion_queried(inode,
3773 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003774 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003775 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003776 rdev = btrfs_inode_rdev(leaf, inode_item);
3777
Josef Bacikaec74772008-07-24 12:12:38 -04003778 BTRFS_I(inode)->index_cnt = (u64)-1;
Boris Burkov77eea052021-06-30 13:01:48 -07003779 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3780 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
Miao Xie67de1172013-12-26 13:07:06 +08003781
3782cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003783 /*
3784 * If we were modified in the current generation and evicted from memory
3785 * and then re-read we need to do a full sync since we don't have any
3786 * idea about which extents were modified before we were evicted from
3787 * cache.
3788 *
3789 * This is required for both inode re-read from disk and delayed inode
3790 * in delayed_nodes_tree.
3791 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003792 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003793 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3794 &BTRFS_I(inode)->runtime_flags);
3795
Filipe Mananabde6c242015-07-24 00:00:19 +01003796 /*
3797 * We don't persist the id of the transaction where an unlink operation
3798 * against the inode was last made. So here we assume the inode might
3799 * have been evicted, and therefore the exact value of last_unlink_trans
3800 * lost, and set it to last_trans to avoid metadata inconsistencies
3801 * between the inode and its parent if the inode is fsync'ed and the log
3802 * replayed. For example, in the scenario:
3803 *
3804 * touch mydir/foo
3805 * ln mydir/foo mydir/bar
3806 * sync
3807 * unlink mydir/bar
3808 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3809 * xfs_io -c fsync mydir/foo
3810 * <power failure>
3811 * mount fs, triggers fsync log replay
3812 *
3813 * We must make sure that when we fsync our inode foo we also log its
3814 * parent inode, otherwise after log replay the parent still has the
3815 * dentry with the "bar" name but our inode foo has a link count of 1
3816 * and doesn't have an inode ref with the name "bar" anymore.
3817 *
3818 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003819 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003820 * transaction commits on fsync if our inode is a directory, or if our
3821 * inode is not a directory, logging its parent unnecessarily.
3822 */
3823 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3824
Filipe Manana3ebac172020-07-15 12:30:43 +01003825 /*
3826 * Same logic as for last_unlink_trans. We don't persist the generation
3827 * of the last transaction where this inode was used for a reflink
3828 * operation, so after eviction and reloading the inode we must be
3829 * pessimistic and assume the last transaction that modified the inode.
3830 */
3831 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3832
Miao Xie67de1172013-12-26 13:07:06 +08003833 path->slots[0]++;
3834 if (inode->i_nlink != 1 ||
3835 path->slots[0] >= btrfs_header_nritems(leaf))
3836 goto cache_acl;
3837
3838 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003839 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003840 goto cache_acl;
3841
3842 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3843 if (location.type == BTRFS_INODE_REF_KEY) {
3844 struct btrfs_inode_ref *ref;
3845
3846 ref = (struct btrfs_inode_ref *)ptr;
3847 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3848 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3849 struct btrfs_inode_extref *extref;
3850
3851 extref = (struct btrfs_inode_extref *)ptr;
3852 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3853 extref);
3854 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003855cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003856 /*
3857 * try to precache a NULL acl entry for files that don't have
3858 * any xattrs or acls
3859 */
Li Zefan33345d012011-04-20 10:31:50 +08003860 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003861 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003862 if (first_xattr_slot != -1) {
3863 path->slots[0] = first_xattr_slot;
3864 ret = btrfs_load_inode_props(inode, path);
3865 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003866 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003867 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003868 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003869 root->root_key.objectid, ret);
3870 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003871 if (path != in_path)
3872 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003873
Al Viro72c04902009-06-24 16:58:48 -04003874 if (!maybe_acls)
3875 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003876
Chris Mason39279cc2007-06-12 06:35:45 -04003877 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003878 case S_IFREG:
3879 inode->i_mapping->a_ops = &btrfs_aops;
3880 inode->i_fop = &btrfs_file_operations;
3881 inode->i_op = &btrfs_file_inode_operations;
3882 break;
3883 case S_IFDIR:
3884 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003885 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003886 break;
3887 case S_IFLNK:
3888 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003889 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003890 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003891 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003892 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003893 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003894 init_special_inode(inode, inode->i_mode, rdev);
3895 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003897
David Sterba7b6a2212018-03-26 18:40:21 +02003898 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003899 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003900}
3901
Chris Masond352ac62008-09-29 15:18:18 -04003902/*
3903 * given a leaf and an inode, copy the inode fields into the leaf
3904 */
Chris Masone02119d2008-09-05 16:13:11 -04003905static void fill_inode_item(struct btrfs_trans_handle *trans,
3906 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003907 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003908 struct inode *inode)
3909{
Liu Bo51fab692012-12-27 09:01:21 +00003910 struct btrfs_map_token token;
Boris Burkov77eea052021-06-30 13:01:48 -07003911 u64 flags;
Chris Mason5f39d392007-10-15 16:14:19 -04003912
David Sterbac82f8232019-08-09 17:48:21 +02003913 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003914
David Sterbacc4c13d2020-04-29 02:15:56 +02003915 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3916 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3917 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3918 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3919 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003920
David Sterbacc4c13d2020-04-29 02:15:56 +02003921 btrfs_set_token_timespec_sec(&token, &item->atime,
3922 inode->i_atime.tv_sec);
3923 btrfs_set_token_timespec_nsec(&token, &item->atime,
3924 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003925
David Sterbacc4c13d2020-04-29 02:15:56 +02003926 btrfs_set_token_timespec_sec(&token, &item->mtime,
3927 inode->i_mtime.tv_sec);
3928 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3929 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003930
David Sterbacc4c13d2020-04-29 02:15:56 +02003931 btrfs_set_token_timespec_sec(&token, &item->ctime,
3932 inode->i_ctime.tv_sec);
3933 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3934 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003935
David Sterbacc4c13d2020-04-29 02:15:56 +02003936 btrfs_set_token_timespec_sec(&token, &item->otime,
3937 BTRFS_I(inode)->i_otime.tv_sec);
3938 btrfs_set_token_timespec_nsec(&token, &item->otime,
3939 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303940
David Sterbacc4c13d2020-04-29 02:15:56 +02003941 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3942 btrfs_set_token_inode_generation(&token, item,
3943 BTRFS_I(inode)->generation);
3944 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3945 btrfs_set_token_inode_transid(&token, item, trans->transid);
3946 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
Boris Burkov77eea052021-06-30 13:01:48 -07003947 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3948 BTRFS_I(inode)->ro_flags);
3949 btrfs_set_token_inode_flags(&token, item, flags);
David Sterbacc4c13d2020-04-29 02:15:56 +02003950 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003951}
3952
Chris Masond352ac62008-09-29 15:18:18 -04003953/*
3954 * copy everything in the in-memory inode into the btree.
3955 */
Chris Mason21151332011-11-10 20:39:08 -05003956static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003957 struct btrfs_root *root,
3958 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003959{
3960 struct btrfs_inode_item *inode_item;
3961 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003962 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003963 int ret;
3964
3965 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003966 if (!path)
3967 return -ENOMEM;
3968
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003969 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 if (ret) {
3971 if (ret > 0)
3972 ret = -ENOENT;
3973 goto failed;
3974 }
3975
Chris Mason5f39d392007-10-15 16:14:19 -04003976 leaf = path->nodes[0];
3977 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003978 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003979
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003980 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003981 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003982 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003983 ret = 0;
3984failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003985 btrfs_free_path(path);
3986 return ret;
3987}
3988
Chris Masond352ac62008-09-29 15:18:18 -04003989/*
Chris Mason21151332011-11-10 20:39:08 -05003990 * copy everything in the in-memory inode into the btree.
3991 */
3992noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003993 struct btrfs_root *root,
3994 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003995{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003996 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003997 int ret;
3998
3999 /*
4000 * If the inode is a free space inode, we can deadlock during commit
4001 * if we put it into the delayed code.
4002 *
4003 * The data relocation inode should also be directly updated
4004 * without delay
4005 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004006 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04004007 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004008 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004009 btrfs_update_root_times(trans, root);
4010
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004011 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004012 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004013 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05004014 return ret;
4015 }
4016
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004017 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004018}
4019
Nikolay Borisov729f7962020-11-02 16:49:06 +02004020int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004022{
4023 int ret;
4024
Nikolay Borisov729f7962020-11-02 16:49:06 +02004025 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004026 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02004027 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004028 return ret;
4029}
4030
4031/*
Chris Masond352ac62008-09-29 15:18:18 -04004032 * unlink helper that gets used here in inode.c and in the tree logging
4033 * recovery code. It remove a link in a directory with a given name, and
4034 * also drops the back refs in the inode to the directory
4035 */
Al Viro92986792011-03-04 17:14:37 +00004036static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004038 struct btrfs_inode *dir,
4039 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004040 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004042 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004045 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004046 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004047 u64 ino = btrfs_ino(inode);
4048 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004049
4050 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004051 if (!path) {
4052 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004053 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004054 }
4055
Li Zefan33345d012011-04-20 10:31:50 +08004056 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004057 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004058 if (IS_ERR_OR_NULL(di)) {
4059 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004060 goto err;
4061 }
Chris Mason39279cc2007-06-12 06:35:45 -04004062 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004063 if (ret)
4064 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004065 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004066
Miao Xie67de1172013-12-26 13:07:06 +08004067 /*
4068 * If we don't have dir index, we have to get it by looking up
4069 * the inode ref, since we get the inode ref, remove it directly,
4070 * it is unnecessary to do delayed deletion.
4071 *
4072 * But if we have dir index, needn't search inode ref to get it.
4073 * Since the inode ref is close to the inode item, it is better
4074 * that we delay to delete it, and just do this deletion when
4075 * we update the inode item.
4076 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004077 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004078 ret = btrfs_delayed_delete_inode_ref(inode);
4079 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004080 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004081 goto skip_backref;
4082 }
4083 }
4084
Li Zefan33345d012011-04-20 10:31:50 +08004085 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4086 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004087 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004088 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004089 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004090 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004091 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004092 goto err;
4093 }
Miao Xie67de1172013-12-26 13:07:06 +08004094skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004095 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004097 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004098 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 }
Chris Mason39279cc2007-06-12 06:35:45 -04004100
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004101 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4102 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004104 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 goto err;
4106 }
Chris Masone02119d2008-09-05 16:13:11 -04004107
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004108 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4109 index);
Chris Mason6418c962010-10-30 07:34:24 -04004110 if (ret == -ENOENT)
4111 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004112 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004113 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004114
4115 /*
4116 * If we have a pending delayed iput we could end up with the final iput
4117 * being run in btrfs-cleaner context. If we have enough of these built
4118 * up we can end up burning a lot of time in btrfs-cleaner without any
4119 * way to throttle the unlinks. Since we're currently holding a ref on
4120 * the inode we can run the delayed iput here without any issues as the
4121 * final iput won't be done until after we drop the ref we're currently
4122 * holding.
4123 */
4124 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004125err:
4126 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004127 if (ret)
4128 goto out;
4129
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004130 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 inode_inc_iversion(&inode->vfs_inode);
4132 inode_inc_iversion(&dir->vfs_inode);
4133 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4134 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004135 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004136out:
Chris Mason39279cc2007-06-12 06:35:45 -04004137 return ret;
4138}
4139
Al Viro92986792011-03-04 17:14:37 +00004140int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4141 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004142 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004143 const char *name, int name_len)
4144{
4145 int ret;
4146 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4147 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004148 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004149 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004150 }
4151 return ret;
4152}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004153
4154/*
4155 * helper to start transaction for unlink and rmdir.
4156 *
Josef Bacikd52be812013-05-29 14:54:47 -04004157 * unlink and rmdir are special in btrfs, they do not always free space, so
4158 * if we cannot make our reservations the normal way try and see if there is
4159 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4160 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161 */
Josef Bacikd52be812013-05-29 14:54:47 -04004162static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004163{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165
Josef Bacike70bea52011-10-11 14:18:24 -04004166 /*
4167 * 1 for the possible orphan item
4168 * 1 for the dir item
4169 * 1 for the dir index
4170 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004171 * 1 for the inode
4172 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004173 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174}
4175
Chris Mason39279cc2007-06-12 06:35:45 -04004176static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4177{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004180 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004181 int ret;
4182
Josef Bacikd52be812013-05-29 14:54:47 -04004183 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004184 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004185 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004186
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004187 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4188 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004189
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004190 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4191 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4192 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004193 if (ret)
4194 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004195
Yan, Zhenga22285a2010-05-16 10:48:46 -04004196 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004197 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004198 if (ret)
4199 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200 }
Josef Bacik7b128762008-07-24 12:17:14 -04004201
Tsutomu Itohb5324022011-07-19 07:27:20 +00004202out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004203 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004204 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004205 return ret;
4206}
4207
Misono Tomohirof60a2362018-04-18 11:34:52 +09004208static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004209 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004210{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004211 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004212 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213 struct btrfs_path *path;
4214 struct extent_buffer *leaf;
4215 struct btrfs_dir_item *di;
4216 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004217 const char *name = dentry->d_name.name;
4218 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004219 u64 index;
4220 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004221 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004222 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004223
Josef Bacik045d3962019-12-18 17:20:27 -05004224 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4225 objectid = inode->root->root_key.objectid;
4226 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4227 objectid = inode->location.objectid;
4228 } else {
4229 WARN_ON(1);
4230 return -EINVAL;
4231 }
4232
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233 path = btrfs_alloc_path();
4234 if (!path)
4235 return -ENOMEM;
4236
Li Zefan33345d012011-04-20 10:31:50 +08004237 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004238 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004240 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 goto out;
4242 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243
4244 leaf = path->nodes[0];
4245 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4246 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4247 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004249 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 goto out;
4251 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004252 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253
Josef Bacikd49d3282019-12-18 17:20:28 -05004254 /*
4255 * This is a placeholder inode for a subvolume we didn't have a
4256 * reference to at the time of the snapshot creation. In the meantime
4257 * we could have renamed the real subvol link into our snapshot, so
David Sterba1a9fd412021-05-21 17:42:23 +02004258 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
Josef Bacikd49d3282019-12-18 17:20:28 -05004259 * Instead simply lookup the dir_index_item for this entry so we can
4260 * remove it. Otherwise we know we have a ref to the root and we can
4261 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4262 */
4263 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004264 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 if (IS_ERR_OR_NULL(di)) {
4267 if (!di)
4268 ret = -ENOENT;
4269 else
4270 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004271 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 goto out;
4273 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274
4275 leaf = path->nodes[0];
4276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004278 btrfs_release_path(path);
4279 } else {
4280 ret = btrfs_del_root_ref(trans, objectid,
4281 root->root_key.objectid, dir_ino,
4282 &index, name, name_len);
4283 if (ret) {
4284 btrfs_abort_transaction(trans, ret);
4285 goto out;
4286 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288
Lu Fengqi9add2942018-08-01 11:32:26 +08004289 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004291 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004292 goto out;
4293 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004294
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004295 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004296 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004297 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004298 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004300 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004302 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004304}
4305
Misono Tomohiroec42f162018-04-18 11:34:13 +09004306/*
4307 * Helper to check if the subvolume references other subvolumes or if it's
4308 * default.
4309 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004310static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004311{
4312 struct btrfs_fs_info *fs_info = root->fs_info;
4313 struct btrfs_path *path;
4314 struct btrfs_dir_item *di;
4315 struct btrfs_key key;
4316 u64 dir_id;
4317 int ret;
4318
4319 path = btrfs_alloc_path();
4320 if (!path)
4321 return -ENOMEM;
4322
4323 /* Make sure this root isn't set as the default subvol */
4324 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4325 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4326 dir_id, "default", 7, 0);
4327 if (di && !IS_ERR(di)) {
4328 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4329 if (key.objectid == root->root_key.objectid) {
4330 ret = -EPERM;
4331 btrfs_err(fs_info,
4332 "deleting default subvolume %llu is not allowed",
4333 key.objectid);
4334 goto out;
4335 }
4336 btrfs_release_path(path);
4337 }
4338
4339 key.objectid = root->root_key.objectid;
4340 key.type = BTRFS_ROOT_REF_KEY;
4341 key.offset = (u64)-1;
4342
4343 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4344 if (ret < 0)
4345 goto out;
4346 BUG_ON(ret == 0);
4347
4348 ret = 0;
4349 if (path->slots[0] > 0) {
4350 path->slots[0]--;
4351 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4352 if (key.objectid == root->root_key.objectid &&
4353 key.type == BTRFS_ROOT_REF_KEY)
4354 ret = -ENOTEMPTY;
4355 }
4356out:
4357 btrfs_free_path(path);
4358 return ret;
4359}
4360
Nikolay Borisov20a68002018-04-27 14:36:24 +03004361/* Delete all dentries for inodes belonging to the root */
4362static void btrfs_prune_dentries(struct btrfs_root *root)
4363{
4364 struct btrfs_fs_info *fs_info = root->fs_info;
4365 struct rb_node *node;
4366 struct rb_node *prev;
4367 struct btrfs_inode *entry;
4368 struct inode *inode;
4369 u64 objectid = 0;
4370
4371 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4372 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4373
4374 spin_lock(&root->inode_lock);
4375again:
4376 node = root->inode_tree.rb_node;
4377 prev = NULL;
4378 while (node) {
4379 prev = node;
4380 entry = rb_entry(node, struct btrfs_inode, rb_node);
4381
David Sterba37508512018-06-29 10:56:40 +02004382 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004383 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004384 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004385 node = node->rb_right;
4386 else
4387 break;
4388 }
4389 if (!node) {
4390 while (prev) {
4391 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004392 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004393 node = prev;
4394 break;
4395 }
4396 prev = rb_next(prev);
4397 }
4398 }
4399 while (node) {
4400 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004401 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004402 inode = igrab(&entry->vfs_inode);
4403 if (inode) {
4404 spin_unlock(&root->inode_lock);
4405 if (atomic_read(&inode->i_count) > 1)
4406 d_prune_aliases(inode);
4407 /*
4408 * btrfs_drop_inode will have it removed from the inode
4409 * cache when its usage count hits zero.
4410 */
4411 iput(inode);
4412 cond_resched();
4413 spin_lock(&root->inode_lock);
4414 goto again;
4415 }
4416
4417 if (cond_resched_lock(&root->inode_lock))
4418 goto again;
4419
4420 node = rb_next(node);
4421 }
4422 spin_unlock(&root->inode_lock);
4423}
4424
Misono Tomohirof60a2362018-04-18 11:34:52 +09004425int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4426{
4427 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4428 struct btrfs_root *root = BTRFS_I(dir)->root;
4429 struct inode *inode = d_inode(dentry);
4430 struct btrfs_root *dest = BTRFS_I(inode)->root;
4431 struct btrfs_trans_handle *trans;
4432 struct btrfs_block_rsv block_rsv;
4433 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004434 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004435
4436 /*
4437 * Don't allow to delete a subvolume with send in progress. This is
4438 * inside the inode lock so the error handling that has to drop the bit
4439 * again is not run concurrently.
4440 */
4441 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004442 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004443 spin_unlock(&dest->root_item_lock);
4444 btrfs_warn(fs_info,
4445 "attempt to delete subvolume %llu during send",
4446 dest->root_key.objectid);
4447 return -EPERM;
4448 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004449 root_flags = btrfs_root_flags(&dest->root_item);
4450 btrfs_set_root_flags(&dest->root_item,
4451 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4452 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004453
4454 down_write(&fs_info->subvol_sem);
4455
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004456 ret = may_destroy_subvol(dest);
4457 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004458 goto out_up_write;
4459
4460 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4461 /*
4462 * One for dir inode,
4463 * two for dir entries,
4464 * two for root ref/backref.
4465 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004466 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4467 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004468 goto out_up_write;
4469
4470 trans = btrfs_start_transaction(root, 0);
4471 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004472 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004473 goto out_release;
4474 }
4475 trans->block_rsv = &block_rsv;
4476 trans->bytes_reserved = block_rsv.size;
4477
4478 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4479
Josef Bacik045d3962019-12-18 17:20:27 -05004480 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004481 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004482 btrfs_abort_transaction(trans, ret);
4483 goto out_end_trans;
4484 }
4485
Josef Bacik2731f512021-03-12 15:25:04 -05004486 ret = btrfs_record_root_in_trans(trans, dest);
4487 if (ret) {
4488 btrfs_abort_transaction(trans, ret);
4489 goto out_end_trans;
4490 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004491
4492 memset(&dest->root_item.drop_progress, 0,
4493 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004494 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004495 btrfs_set_root_refs(&dest->root_item, 0);
4496
4497 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4498 ret = btrfs_insert_orphan_item(trans,
4499 fs_info->tree_root,
4500 dest->root_key.objectid);
4501 if (ret) {
4502 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004503 goto out_end_trans;
4504 }
4505 }
4506
Lu Fengqid1957792018-05-29 15:01:54 +08004507 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004508 BTRFS_UUID_KEY_SUBVOL,
4509 dest->root_key.objectid);
4510 if (ret && ret != -ENOENT) {
4511 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004512 goto out_end_trans;
4513 }
4514 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004515 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004516 dest->root_item.received_uuid,
4517 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4518 dest->root_key.objectid);
4519 if (ret && ret != -ENOENT) {
4520 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004521 goto out_end_trans;
4522 }
4523 }
4524
Qu Wenruo082b6c92020-06-16 10:17:37 +08004525 free_anon_bdev(dest->anon_dev);
4526 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004527out_end_trans:
4528 trans->block_rsv = NULL;
4529 trans->bytes_reserved = 0;
4530 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004531 inode->i_flags |= S_DEAD;
4532out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004533 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004534out_up_write:
4535 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004536 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004537 spin_lock(&dest->root_item_lock);
4538 root_flags = btrfs_root_flags(&dest->root_item);
4539 btrfs_set_root_flags(&dest->root_item,
4540 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4541 spin_unlock(&dest->root_item_lock);
4542 } else {
4543 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004544 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004545 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004546 }
4547
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004548 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004549}
4550
Chris Mason39279cc2007-06-12 06:35:45 -04004551static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4552{
David Howells2b0143b2015-03-17 22:25:59 +00004553 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004554 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004555 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004556 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004557 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004558
David Sterbab3ae2442012-09-13 16:04:34 -06004559 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004560 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004561 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004562 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004563
Josef Bacikd52be812013-05-29 14:54:47 -04004564 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004565 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004566 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004567
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004568 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004569 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004570 goto out;
4571 }
4572
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004573 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004574 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004575 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004576
Filipe Manana44f714d2016-06-06 16:11:13 +01004577 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4578
Chris Mason39279cc2007-06-12 06:35:45 -04004579 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004580 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4581 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4582 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004583 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004584 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004585 /*
4586 * Propagate the last_unlink_trans value of the deleted dir to
4587 * its parent directory. This is to prevent an unrecoverable
4588 * log tree in the case we do something like this:
4589 * 1) create dir foo
4590 * 2) create snapshot under dir foo
4591 * 3) delete the snapshot
4592 * 4) rmdir foo
4593 * 5) mkdir foo
4594 * 6) fsync foo or some file inside foo
4595 */
4596 if (last_unlink_trans >= trans->transid)
4597 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4598 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004599out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004600 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004601 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004602
Chris Mason39279cc2007-06-12 06:35:45 -04004603 return err;
4604}
4605
Josef Bacikddfae632017-10-19 14:16:02 -04004606/*
4607 * Return this if we need to call truncate_block for the last bit of the
4608 * truncate.
4609 */
4610#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004611
Chris Mason323ac952008-10-01 19:05:46 -04004612/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004613 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004614 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004615 * @trans: A transaction handle.
4616 * @root: The root from which to remove items.
4617 * @inode: The inode whose items we want to remove.
4618 * @new_size: The new i_size for the inode. This is only applicable when
4619 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4620 * @min_type: The minimum key type to remove. All keys with a type
4621 * greater than this value are removed and all keys with
4622 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004623 * @extents_found: Output parameter that will contain the number of file
4624 * extent items that were removed or adjusted to the new
4625 * inode i_size. The caller is responsible for initializing
4626 * the counter. Also, it can be NULL if the caller does not
4627 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004628 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004629 * Remove all keys associated with the inode from the given root that have a key
4630 * with a type greater than or equals to @min_type. When @min_type has a value of
4631 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4632 * greater than or equals to @new_size. If a file extent item that starts before
4633 * @new_size and ends after it is found, its length is adjusted.
4634 *
4635 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4636 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004637 */
Yan, Zheng80825102009-11-12 09:35:36 +00004638int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4639 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004640 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004641 u64 new_size, u32 min_type,
4642 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004643{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004645 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004646 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004648 struct btrfs_key key;
4649 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004650 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004651 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004652 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004653 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004654 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004655 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004656 int found_extent;
4657 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 int pending_del_nr = 0;
4659 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004660 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004661 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004662 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004663 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004664 bool be_nice = false;
4665 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004666 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4667 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004668
4669 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004670
Chris Mason28ed1342014-12-17 09:41:04 -08004671 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004672 * For non-free space inodes and non-shareable roots, we want to back
4673 * off from time to time. This means all inodes in subvolume roots,
4674 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004675 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004676 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004677 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004678 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004679
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004680 path = btrfs_alloc_path();
4681 if (!path)
4682 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004683 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004684
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004685 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004686 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004687 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004688
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004689 /*
4690 * We want to drop from the next block forward in case this
4691 * new size is not block aligned since we will be keeping the
4692 * last block of the extent just the way it is.
4693 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004694 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004695 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004696 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004697 }
Yan, Zheng80825102009-11-12 09:35:36 +00004698
Miao Xie16cdcec2011-04-22 18:12:22 +08004699 /*
4700 * This function is also used to drop the items in the log tree before
4701 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004702 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004703 * items.
4704 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004705 if (min_type == 0 && root == inode->root)
4706 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004707
Li Zefan33345d012011-04-20 10:31:50 +08004708 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004709 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004710 key.type = (u8)-1;
4711
Chris Mason85e21ba2008-01-29 15:11:36 -05004712search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004713 /*
4714 * with a 16K leaf size and 128MB extents, you can actually queue
4715 * up a huge file in a single leaf. Most of the time that
4716 * bytes_deleted is > 0, it will be huge by the time we get here
4717 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004718 if (be_nice && bytes_deleted > SZ_32M &&
4719 btrfs_should_end_transaction(trans)) {
4720 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004721 goto out;
4722 }
Chris Masond3977122009-01-05 21:25:51 -05004723
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004724 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4725 if (ret < 0)
4726 goto out;
4727
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004729 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004730 /* there are no items in the tree for us to truncate, we're
4731 * done
4732 */
Yan, Zheng80825102009-11-12 09:35:36 +00004733 if (path->slots[0] == 0)
4734 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004735 path->slots[0]--;
4736 }
4737
Chris Masond3977122009-01-05 21:25:51 -05004738 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004739 u64 clear_start = 0, clear_len = 0;
4740
Chris Mason39279cc2007-06-12 06:35:45 -04004741 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004742 leaf = path->nodes[0];
4743 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004744 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004745
Li Zefan33345d012011-04-20 10:31:50 +08004746 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004747 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004748
Chris Mason85e21ba2008-01-29 15:11:36 -05004749 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004750 break;
4751
Chris Mason5f39d392007-10-15 16:14:19 -04004752 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004753 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004754 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004755 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004756 extent_type = btrfs_file_extent_type(leaf, fi);
4757 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004758 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004759 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004760
4761 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004762 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004763 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004764 item_end += btrfs_file_extent_ram_bytes(leaf,
4765 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004766
4767 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004768 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004769 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004770 }
Yan008630c2007-11-07 13:31:09 -05004771 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004772 }
Yan, Zheng80825102009-11-12 09:35:36 +00004773 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004774 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004775 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004776 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004777 break;
4778 if (found_key.offset >= new_size)
4779 del_item = 1;
4780 else
4781 del_item = 0;
4782 }
Chris Mason39279cc2007-06-12 06:35:45 -04004783 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004784 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004785 if (found_type != BTRFS_EXTENT_DATA_KEY)
4786 goto delete;
4787
Filipe Manana0d7d3162021-05-24 11:35:55 +01004788 if (extents_found != NULL)
4789 (*extents_found)++;
4790
Chris Mason179e29e2007-11-01 11:28:41 -04004791 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004792 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004793
4794 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004795 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004796 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004797 u64 orig_num_bytes =
4798 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004799 extent_num_bytes = ALIGN(new_size -
4800 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004801 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004802 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004803 btrfs_set_file_extent_num_bytes(leaf, fi,
4804 extent_num_bytes);
4805 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004806 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004807 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004808 &root->state) &&
4809 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004810 inode_sub_bytes(&inode->vfs_inode,
4811 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004812 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004813 } else {
Chris Masondb945352007-10-15 16:15:53 -04004814 extent_num_bytes =
4815 btrfs_file_extent_disk_num_bytes(leaf,
4816 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004817 extent_offset = found_key.offset -
4818 btrfs_file_extent_offset(leaf, fi);
4819
Chris Mason39279cc2007-06-12 06:35:45 -04004820 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004821 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004822 if (extent_start != 0) {
4823 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004824 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004825 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004826 inode_sub_bytes(&inode->vfs_inode,
4827 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004828 }
4829 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004830 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004831 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004832 /*
4833 * we can't truncate inline items that have had
4834 * special encodings
4835 */
4836 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004837 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004838 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4839 btrfs_file_extent_compression(leaf, fi) == 0) {
4840 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004841
Josef Bacikddfae632017-10-19 14:16:02 -04004842 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4843 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004844 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004845 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004846 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004847 * We have to bail so the last_size is set to
4848 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004849 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004850 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004851 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004852 } else {
4853 /*
4854 * Inline extents are special, we just treat
4855 * them as a full sector worth in the file
4856 * extent tree just for simplicity sake.
4857 */
4858 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004859 }
Josef Bacikddfae632017-10-19 14:16:02 -04004860
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004861 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004862 inode_sub_bytes(&inode->vfs_inode,
4863 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004864 }
Chris Mason179e29e2007-11-01 11:28:41 -04004865delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004866 /*
4867 * We use btrfs_truncate_inode_items() to clean up log trees for
4868 * multiple fsyncs, and in this case we don't want to clear the
4869 * file extent range because it's just the log.
4870 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004871 if (root == inode->root) {
4872 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004873 clear_start, clear_len);
4874 if (ret) {
4875 btrfs_abort_transaction(trans, ret);
4876 break;
4877 }
4878 }
4879
Josef Bacikddfae632017-10-19 14:16:02 -04004880 if (del_item)
4881 last_size = found_key.offset;
4882 else
4883 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004884 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004885 if (!pending_del_nr) {
4886 /* no pending yet, add ourselves */
4887 pending_del_slot = path->slots[0];
4888 pending_del_nr = 1;
4889 } else if (pending_del_nr &&
4890 path->slots[0] + 1 == pending_del_slot) {
4891 /* hop on the pending chunk */
4892 pending_del_nr++;
4893 pending_del_slot = path->slots[0];
4894 } else {
Chris Masond3977122009-01-05 21:25:51 -05004895 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004896 }
Chris Mason39279cc2007-06-12 06:35:45 -04004897 } else {
4898 break;
4899 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004900 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004901
Miao Xie27cdeb72014-04-02 19:51:05 +08004902 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004903 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004904 struct btrfs_ref ref = { 0 };
4905
Chris Mason28ed1342014-12-17 09:41:04 -08004906 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004907
4908 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4909 extent_start, extent_num_bytes, 0);
4910 ref.real_root = root->root_key.objectid;
4911 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4912 ino, extent_offset);
4913 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004914 if (ret) {
4915 btrfs_abort_transaction(trans, ret);
4916 break;
4917 }
Chris Mason28f75a02015-02-04 06:59:29 -08004918 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004919 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004920 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004921 }
Chris Mason39279cc2007-06-12 06:35:45 -04004922 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004923
Yan, Zheng80825102009-11-12 09:35:36 +00004924 if (found_type == BTRFS_INODE_ITEM_KEY)
4925 break;
4926
4927 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004928 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004929 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004930 if (pending_del_nr) {
4931 ret = btrfs_del_items(trans, root, path,
4932 pending_del_slot,
4933 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004934 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004935 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004936 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004937 }
Yan, Zheng80825102009-11-12 09:35:36 +00004938 pending_del_nr = 0;
4939 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004940 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004941
Chris Mason28f75a02015-02-04 06:59:29 -08004942 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004943 * We can generate a lot of delayed refs, so we need to
4944 * throttle every once and a while and make sure we're
4945 * adding enough space to keep up with the work we are
4946 * generating. Since we hold a transaction here we
4947 * can't flush, and we don't want to FLUSH_LIMIT because
4948 * we could have generated too many delayed refs to
4949 * actually allocate, so just bail if we're short and
4950 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004951 */
Josef Bacik28bad212018-12-03 10:20:38 -05004952 if (should_throttle) {
4953 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4954 BTRFS_RESERVE_NO_FLUSH);
4955 if (ret) {
4956 ret = -EAGAIN;
4957 break;
4958 }
Chris Mason28f75a02015-02-04 06:59:29 -08004959 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004960 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004961 } else {
4962 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004963 }
Chris Mason39279cc2007-06-12 06:35:45 -04004964 }
Yan, Zheng80825102009-11-12 09:35:36 +00004965out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004966 if (ret >= 0 && pending_del_nr) {
4967 int err;
4968
4969 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004971 if (err) {
4972 btrfs_abort_transaction(trans, err);
4973 ret = err;
4974 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004975 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004976 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4977 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004978 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004979 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004980 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4981 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4982 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004983 }
Chris Mason28ed1342014-12-17 09:41:04 -08004984
Chris Mason39279cc2007-06-12 06:35:45 -04004985 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004986 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004987}
4988
Chris Masona52d9a82007-08-27 16:49:44 -04004989/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304990 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004991 * @inode - inode that we're zeroing
4992 * @from - the offset to start zeroing
4993 * @len - the length to zero, 0 to zero the entire range respective to the
4994 * offset
4995 * @front - zero up to the offset instead of from the offset on
4996 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304997 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004998 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004999 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005000int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
5001 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04005002{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005003 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5004 struct address_space *mapping = inode->vfs_inode.i_mapping;
5005 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005006 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005007 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08005008 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005009 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005010 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005011 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305012 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04005013 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005014 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005015 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04005016 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305017 u64 block_start;
5018 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04005019
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02005020 if (IS_ALIGNED(offset, blocksize) &&
5021 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04005022 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305023
Josef Bacik8b62f872017-10-19 14:15:55 -04005024 block_start = round_down(from, blocksize);
5025 block_end = block_start + blocksize - 1;
5026
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005027 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5028 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005029 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005030 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005031 /* For nocow case, no need to reserve data space */
5032 only_release_metadata = true;
5033 } else {
5034 goto out;
5035 }
5036 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005037 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005038 if (ret < 0) {
5039 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005040 btrfs_free_reserved_data_space(inode, data_reserved,
5041 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005042 goto out;
5043 }
Chris Mason211c17f2008-05-15 09:13:45 -04005044again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005045 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005046 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005047 btrfs_delalloc_release_space(inode, data_reserved, block_start,
5048 blocksize, true);
5049 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005050 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005051 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005052 }
Qu Wenruo32443de2021-01-26 16:34:00 +08005053 ret = set_page_extent_mapped(page);
5054 if (ret < 0)
5055 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005056
Chris Masona52d9a82007-08-27 16:49:44 -04005057 if (!PageUptodate(page)) {
5058 ret = btrfs_readpage(NULL, page);
5059 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005060 if (page->mapping != mapping) {
5061 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005062 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005063 goto again;
5064 }
Chris Masona52d9a82007-08-27 16:49:44 -04005065 if (!PageUptodate(page)) {
5066 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005067 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005068 }
5069 }
Chris Mason211c17f2008-05-15 09:13:45 -04005070 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005071
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305072 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005073
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005074 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005075 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305076 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005077 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005079 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03005080 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005081 btrfs_put_ordered_extent(ordered);
5082 goto again;
5083 }
5084
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005085 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005086 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5087 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005088
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005089 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005090 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005091 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305092 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005093 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005094 goto out_unlock;
5095 }
5096
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305097 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005098 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305099 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04005100 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07005101 memzero_page(page, (block_start - page_offset(page)),
5102 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005103 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07005104 memzero_page(page, (block_start - page_offset(page)) + offset,
5105 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005106 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005107 }
Chris Mason247e7432008-07-17 12:53:51 -04005108 ClearPageChecked(page);
Qu Wenruo6c9ac8b2021-05-31 16:50:51 +08005109 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
David Sterbae43bbe52017-12-12 21:43:52 +01005110 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005111
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005112 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005113 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02005114 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005115
Chris Mason89642222008-07-24 09:41:53 -04005116out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005117 if (ret) {
5118 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005119 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005120 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005121 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005122 block_start, blocksize, true);
5123 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005124 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005125 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005126 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005127out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005128 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005129 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08005130 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005131 return ret;
5132}
5133
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005134static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04005135 u64 offset, u64 len)
5136{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005137 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04005138 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00005139 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04005140 int ret;
5141
5142 /*
Filipe Mananacceaa892021-07-20 16:03:40 +01005143 * If NO_HOLES is enabled, we don't need to do anything.
5144 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5145 * or btrfs_update_inode() will be called, which guarantee that the next
5146 * fsync will know this inode was changed and needs to be logged.
Josef Bacik16e75492013-10-22 12:18:51 -04005147 */
Filipe Mananacceaa892021-07-20 16:03:40 +01005148 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04005149 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005150
5151 /*
5152 * 1 - for the one we're dropping
5153 * 1 - for the one we're adding
5154 * 1 - for updating the inode.
5155 */
5156 trans = btrfs_start_transaction(root, 3);
5157 if (IS_ERR(trans))
5158 return PTR_ERR(trans);
5159
Filipe Manana5893dfb2020-11-04 11:07:32 +00005160 drop_args.start = offset;
5161 drop_args.end = offset + len;
5162 drop_args.drop_cache = true;
5163
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005164 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005165 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005166 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005167 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005168 return ret;
5169 }
5170
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005171 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005172 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005173 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005174 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005175 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005176 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5177 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005178 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005179 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005180 return ret;
5181}
5182
Josef Bacik695a0d02011-03-04 15:46:53 -05005183/*
5184 * This function puts in dummy file extents for the area we're creating a hole
5185 * for. So if we are truncating this file to a larger size we need to insert
5186 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5187 * the range between oldsize and size
5188 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005189int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005190{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005191 struct btrfs_root *root = inode->root;
5192 struct btrfs_fs_info *fs_info = root->fs_info;
5193 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005194 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005195 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005196 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005197 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5198 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005199 u64 last_byte;
5200 u64 cur_offset;
5201 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005202 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005203
Josef Bacika71754f2013-06-17 17:14:39 -04005204 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305205 * If our size started in the middle of a block we need to zero out the
5206 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005207 * expose stale data.
5208 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005209 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005210 if (err)
5211 return err;
5212
Yan Zheng9036c102008-10-30 14:19:41 -04005213 if (size <= hole_start)
5214 return 0;
5215
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005216 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5217 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005218 cur_offset = hole_start;
5219 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005220 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005221 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005222 if (IS_ERR(em)) {
5223 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005224 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005225 break;
5226 }
Yan Zheng9036c102008-10-30 14:19:41 -04005227 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005228 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005229 hole_size = last_byte - cur_offset;
5230
Yan, Zheng80825102009-11-12 09:35:36 +00005231 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005232 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005233
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005234 err = maybe_insert_hole(root, inode, cur_offset,
5235 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005236 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005237 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005238
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005239 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005240 cur_offset, hole_size);
5241 if (err)
5242 break;
5243
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005244 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005245 cur_offset + hole_size - 1, 0);
5246 hole_em = alloc_extent_map();
5247 if (!hole_em) {
5248 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005249 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005250 goto next;
5251 }
5252 hole_em->start = cur_offset;
5253 hole_em->len = hole_size;
5254 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005255
Josef Bacik5dc562c2012-08-17 13:14:17 -04005256 hole_em->block_start = EXTENT_MAP_HOLE;
5257 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005258 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005259 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005260 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005262
5263 while (1) {
5264 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005265 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005266 write_unlock(&em_tree->lock);
5267 if (err != -EEXIST)
5268 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005269 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005270 cur_offset +
5271 hole_size - 1, 0);
5272 }
5273 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005274 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005275 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005276 cur_offset, hole_size);
5277 if (err)
5278 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005279 }
Josef Bacik16e75492013-10-22 12:18:51 -04005280next:
Yan Zheng9036c102008-10-30 14:19:41 -04005281 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005282 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005283 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005284 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005285 break;
5286 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005287 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005288 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005289 return err;
5290}
5291
Eric Sandeen3972f262013-01-12 02:57:22 +00005292static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005293{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005294 struct btrfs_root *root = BTRFS_I(inode)->root;
5295 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005296 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005297 loff_t newsize = attr->ia_size;
5298 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005299 int ret;
5300
Eric Sandeen3972f262013-01-12 02:57:22 +00005301 /*
5302 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5303 * special case where we need to update the times despite not having
5304 * these flags set. For all other operations the VFS set these flags
5305 * explicitly if it wants a timestamp update.
5306 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005307 if (newsize != oldsize) {
5308 inode_inc_iversion(inode);
5309 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5310 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005311 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005312 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005313
Josef Bacika41ad392011-01-31 15:30:16 -05005314 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005315 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005316 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005317 * This is to ensure the snapshot captures a fully consistent
5318 * state of this file - if the snapshot captures this expanding
5319 * truncation, it must capture all writes that happened before
5320 * this truncation.
5321 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005322 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005323 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005324 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005325 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005326 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005327 }
Yan, Zheng80825102009-11-12 09:35:36 +00005328
Miao Xief4a2f4c2011-12-14 20:12:01 -05005329 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005330 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005331 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005332 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005333 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005334
5335 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005336 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305337 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005338 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005339 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005340 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005341 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5343
5344 if (btrfs_is_zoned(fs_info)) {
5345 ret = btrfs_wait_ordered_range(inode,
5346 ALIGN(newsize, fs_info->sectorsize),
5347 (u64)-1);
5348 if (ret)
5349 return ret;
5350 }
Yan, Zheng80825102009-11-12 09:35:36 +00005351
Josef Bacika41ad392011-01-31 15:30:16 -05005352 /*
5353 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005354 * zero. Make sure any new writes to the file get on disk
5355 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005356 */
5357 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005358 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005359 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005360
Josef Bacika41ad392011-01-31 15:30:16 -05005361 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005362
Miao Xie2e60a512013-02-08 07:01:08 +00005363 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005364
Filipe Manana213e8c52018-02-06 20:40:31 +00005365 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005366 if (ret && inode->i_nlink) {
5367 int err;
5368
5369 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005370 * Truncate failed, so fix up the in-memory size. We
5371 * adjusted disk_i_size down as we removed extents, so
5372 * wait for disk_i_size to be stable and then update the
5373 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005374 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005375 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005376 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005377 return err;
5378 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005379 }
Yan, Zheng80825102009-11-12 09:35:36 +00005380 }
5381
Josef Bacika41ad392011-01-31 15:30:16 -05005382 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005383}
5384
Christian Brauner549c7292021-01-21 14:19:43 +01005385static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5386 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005387{
David Howells2b0143b2015-03-17 22:25:59 +00005388 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005389 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 int err;
5391
Li Zefanb83cc962010-12-20 16:04:08 +08005392 if (btrfs_root_readonly(root))
5393 return -EROFS;
5394
Christian Braunerd4d09462021-07-27 12:48:49 +02005395 err = setattr_prepare(mnt_userns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005396 if (err)
5397 return err;
5398
Chris Mason5a3f23d2009-03-31 13:27:11 -04005399 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005400 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005401 if (err)
5402 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005403 }
Yan Zheng9036c102008-10-30 14:19:41 -04005404
Christoph Hellwig10257742010-06-04 11:30:02 +02005405 if (attr->ia_valid) {
Christian Braunerd4d09462021-07-27 12:48:49 +02005406 setattr_copy(mnt_userns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005407 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005408 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005409
Josef Bacik22c44fe2011-11-30 10:45:38 -05005410 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunerd4d09462021-07-27 12:48:49 +02005411 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005412 }
5413
Chris Mason39279cc2007-06-12 06:35:45 -04005414 return err;
5415}
Chris Mason61295eb2008-01-14 16:24:38 -05005416
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005417/*
5418 * While truncating the inode pages during eviction, we get the VFS calling
5419 * btrfs_invalidatepage() against each page of the inode. This is slow because
5420 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5421 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5422 * extent_state structures over and over, wasting lots of time.
5423 *
5424 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5425 * those expensive operations on a per page basis and do only the ordered io
5426 * finishing, while we release here the extent_map and extent_state structures,
5427 * without the excessive merging and splitting.
5428 */
5429static void evict_inode_truncate_pages(struct inode *inode)
5430{
5431 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5432 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5433 struct rb_node *node;
5434
5435 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005436 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005437
5438 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005439 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005440 struct extent_map *em;
5441
Liu Bo07e1ce02018-08-23 03:51:52 +08005442 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005443 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005444 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5445 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005446 remove_extent_mapping(map_tree, em);
5447 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005448 if (need_resched()) {
5449 write_unlock(&map_tree->lock);
5450 cond_resched();
5451 write_lock(&map_tree->lock);
5452 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005453 }
5454 write_unlock(&map_tree->lock);
5455
Filipe Manana6ca07092015-05-26 00:55:42 +01005456 /*
5457 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005458 * We can have ongoing bios started by readahead that have
5459 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005460 * still in progress (unlocked the pages in the bio but did not yet
5461 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005462 * ranges can still be locked and eviction started because before
5463 * submitting those bios, which are executed by a separate task (work
5464 * queue kthread), inode references (inode->i_count) were not taken
5465 * (which would be dropped in the end io callback of each bio).
5466 * Therefore here we effectively end up waiting for those bios and
5467 * anyone else holding locked ranges without having bumped the inode's
5468 * reference count - if we don't do it, when they access the inode's
5469 * io_tree to unlock a range it may be too late, leading to an
5470 * use-after-free issue.
5471 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005472 spin_lock(&io_tree->lock);
5473 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5474 struct extent_state *state;
5475 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005476 u64 start;
5477 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005478 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005479
5480 node = rb_first(&io_tree->state);
5481 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005482 start = state->start;
5483 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005484 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005485 spin_unlock(&io_tree->lock);
5486
David Sterbaff13db42015-12-03 14:30:40 +01005487 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005488
5489 /*
5490 * If still has DELALLOC flag, the extent didn't reach disk,
5491 * and its reserved space won't be freed by delayed_ref.
5492 * So we need to free its reserved space here.
5493 * (Refer to comment in btrfs_invalidatepage, case 2)
5494 *
5495 * Note, end is the bytenr of last byte, so we need + 1 here.
5496 */
Filipe Manana421f0922018-10-12 13:02:48 +01005497 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005498 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5499 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005500
Filipe Manana6ca07092015-05-26 00:55:42 +01005501 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005502 EXTENT_LOCKED | EXTENT_DELALLOC |
5503 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5504 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005505
Filipe Manana7064dd52014-08-08 02:47:05 +01005506 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005507 spin_lock(&io_tree->lock);
5508 }
5509 spin_unlock(&io_tree->lock);
5510}
5511
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005512static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005513 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005514{
5515 struct btrfs_fs_info *fs_info = root->fs_info;
5516 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005517 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005518 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005519 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005520
Josef Bacikd3984c92019-08-01 18:19:37 -04005521 /*
5522 * Eviction should be taking place at some place safe because of our
5523 * delayed iputs. However the normal flushing code will run delayed
5524 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5525 *
5526 * We reserve the delayed_refs_extra here again because we can't use
5527 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5528 * above. We reserve our extra bit here because we generate a ton of
5529 * delayed refs activity by truncating.
5530 *
5531 * If we cannot make our reservation we'll attempt to steal from the
5532 * global reserve, because we really want to be able to free up space.
5533 */
5534 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5535 BTRFS_RESERVE_FLUSH_EVICT);
5536 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005537 /*
5538 * Try to steal from the global reserve if there is space for
5539 * it.
5540 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005541 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5542 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5543 btrfs_warn(fs_info,
5544 "could not allocate space for delete; will truncate on mount");
5545 return ERR_PTR(-ENOSPC);
5546 }
5547 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005548 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005549
5550 trans = btrfs_join_transaction(root);
5551 if (IS_ERR(trans))
5552 return trans;
5553
5554 if (delayed_refs_extra) {
5555 trans->block_rsv = &fs_info->trans_block_rsv;
5556 trans->bytes_reserved = delayed_refs_extra;
5557 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5558 delayed_refs_extra, 1);
5559 }
5560 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005561}
5562
Al Virobd555972010-06-07 11:35:40 -04005563void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005564{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005566 struct btrfs_trans_handle *trans;
5567 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005568 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005569 int ret;
5570
liubo1abe9b82011-03-24 11:18:59 +00005571 trace_btrfs_inode_evict(inode);
5572
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005573 if (!root) {
Boris Burkov14605402021-06-30 13:01:49 -07005574 fsverity_cleanup_inode(inode);
Liu Boe8f1bc12018-01-25 11:02:53 -07005575 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005576 return;
5577 }
5578
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005579 evict_inode_truncate_pages(inode);
5580
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005581 if (inode->i_nlink &&
5582 ((btrfs_root_refs(&root->root_item) != 0 &&
5583 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005584 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005585 goto no_delete;
5586
Omar Sandoval27919062018-05-11 13:13:37 -07005587 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005588 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005589
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005590 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005591
Omar Sandoval7b40b692018-05-11 13:13:33 -07005592 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005593 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005594
Yan, Zheng76dda932009-09-21 16:00:26 -04005595 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005596 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5597 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 goto no_delete;
5599 }
5600
Nikolay Borisovaa790212017-01-10 20:35:40 +02005601 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005602 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005603 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005604
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005605 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005606 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005607 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005608 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005609 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005610
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005611 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005612
Yan, Zheng80825102009-11-12 09:35:36 +00005613 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005614 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005615 if (IS_ERR(trans))
5616 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005617
5618 trans->block_rsv = rsv;
5619
Nikolay Borisov50743392020-11-02 16:48:55 +02005620 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005621 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005622 trans->block_rsv = &fs_info->trans_block_rsv;
5623 btrfs_end_transaction(trans);
5624 btrfs_btree_balance_dirty(fs_info);
5625 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5626 goto free_rsv;
5627 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005628 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005629 }
5630
Josef Bacik4ef31a42013-08-13 14:10:08 -04005631 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005632 * Errors here aren't a big deal, it just means we leave orphan items in
5633 * the tree. They will be cleaned up on the next mount. If the inode
5634 * number gets reused, cleanup deletes the orphan item without doing
5635 * anything, and unlink reuses the existing orphan item.
5636 *
5637 * If it turns out that we are dropping too many of these, we might want
5638 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005639 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005640 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005641 if (!IS_ERR(trans)) {
5642 trans->block_rsv = rsv;
5643 btrfs_orphan_del(trans, BTRFS_I(inode));
5644 trans->block_rsv = &fs_info->trans_block_rsv;
5645 btrfs_end_transaction(trans);
5646 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005647
Omar Sandoval27919062018-05-11 13:13:37 -07005648free_rsv:
5649 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005650no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005651 /*
5652 * If we didn't successfully delete, the orphan item will still be in
5653 * the tree and we'll retry on the next mount. Again, we might also want
5654 * to retry these periodically in the future.
5655 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005656 btrfs_remove_delayed_node(BTRFS_I(inode));
Boris Burkov14605402021-06-30 13:01:49 -07005657 fsverity_cleanup_inode(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005658 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005659}
5660
5661/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005662 * Return the key found in the dir entry in the location pointer, fill @type
5663 * with BTRFS_FT_*, and return 0.
5664 *
Su Yue005d6712018-03-05 17:13:37 +08005665 * If no dir entries were found, returns -ENOENT.
5666 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005667 */
5668static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005669 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005670{
5671 const char *name = dentry->d_name.name;
5672 int namelen = dentry->d_name.len;
5673 struct btrfs_dir_item *di;
5674 struct btrfs_path *path;
5675 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005676 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005677
5678 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005679 if (!path)
5680 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005681
David Sterbaf85b7372017-01-20 14:54:07 +01005682 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5683 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005684 if (IS_ERR_OR_NULL(di)) {
5685 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005686 goto out;
5687 }
Chris Masond3977122009-01-05 21:25:51 -05005688
Chris Mason5f39d392007-10-15 16:14:19 -04005689 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005690 if (location->type != BTRFS_INODE_ITEM_KEY &&
5691 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005692 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005693 btrfs_warn(root->fs_info,
5694"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5695 __func__, name, btrfs_ino(BTRFS_I(dir)),
5696 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005697 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005698 if (!ret)
5699 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005700out:
Chris Mason39279cc2007-06-12 06:35:45 -04005701 btrfs_free_path(path);
5702 return ret;
5703}
5704
5705/*
5706 * when we hit a tree root in a directory, the btrfs part of the inode
5707 * needs to be changed to reflect the root directory of the tree root. This
5708 * is kind of like crossing a mount point.
5709 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005710static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711 struct inode *dir,
5712 struct dentry *dentry,
5713 struct btrfs_key *location,
5714 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005715{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005716 struct btrfs_path *path;
5717 struct btrfs_root *new_root;
5718 struct btrfs_root_ref *ref;
5719 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005720 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721 int ret;
5722 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005723
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724 path = btrfs_alloc_path();
5725 if (!path) {
5726 err = -ENOMEM;
5727 goto out;
5728 }
Chris Mason39279cc2007-06-12 06:35:45 -04005729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005730 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005731 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5732 key.type = BTRFS_ROOT_REF_KEY;
5733 key.offset = location->objectid;
5734
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005735 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736 if (ret) {
5737 if (ret < 0)
5738 err = ret;
5739 goto out;
5740 }
Chris Mason39279cc2007-06-12 06:35:45 -04005741
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005742 leaf = path->nodes[0];
5743 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005744 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005745 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5746 goto out;
5747
5748 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5749 (unsigned long)(ref + 1),
5750 dentry->d_name.len);
5751 if (ret)
5752 goto out;
5753
David Sterbab3b4aa72011-04-21 01:20:15 +02005754 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755
David Sterba56e93572020-05-15 19:35:55 +02005756 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 if (IS_ERR(new_root)) {
5758 err = PTR_ERR(new_root);
5759 goto out;
5760 }
5761
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 *sub_root = new_root;
5763 location->objectid = btrfs_root_dirid(&new_root->root_item);
5764 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766 err = 0;
5767out:
5768 btrfs_free_path(path);
5769 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005770}
5771
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005772static void inode_tree_add(struct inode *inode)
5773{
5774 struct btrfs_root *root = BTRFS_I(inode)->root;
5775 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005776 struct rb_node **p;
5777 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005778 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005779 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005780
Al Viro1d3382cb2010-10-23 15:19:20 -04005781 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005782 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005783 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005784 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005785 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005786 while (*p) {
5787 parent = *p;
5788 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5789
David Sterba37508512018-06-29 10:56:40 +02005790 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005791 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005792 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005793 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005794 else {
5795 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005796 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005797 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005798 RB_CLEAR_NODE(parent);
5799 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005800 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005801 }
5802 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005803 rb_link_node(new, parent, p);
5804 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005805 spin_unlock(&root->inode_lock);
5806}
5807
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005808static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005809{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005810 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005811 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005812
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005813 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005814 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5815 rb_erase(&inode->rb_node, &root->inode_tree);
5816 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005817 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005818 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005819 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005820
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005821 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005822 spin_lock(&root->inode_lock);
5823 empty = RB_EMPTY_ROOT(&root->inode_tree);
5824 spin_unlock(&root->inode_lock);
5825 if (empty)
5826 btrfs_add_dead_root(root);
5827 }
5828}
5829
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005830
Chris Masone02119d2008-09-05 16:13:11 -04005831static int btrfs_init_locked_inode(struct inode *inode, void *p)
5832{
5833 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005834
5835 inode->i_ino = args->ino;
5836 BTRFS_I(inode)->location.objectid = args->ino;
5837 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5838 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005839 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5840 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005841 return 0;
5842}
5843
5844static int btrfs_find_actor(struct inode *inode, void *opaque)
5845{
5846 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005847
5848 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005849 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005850}
5851
David Sterba0202e832020-05-15 19:35:59 +02005852static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005853 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005854{
5855 struct inode *inode;
5856 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005857 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005858
David Sterba0202e832020-05-15 19:35:59 +02005859 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005860 args.root = root;
5861
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005862 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005863 btrfs_init_locked_inode,
5864 (void *)&args);
5865 return inode;
5866}
5867
David Sterba4c66e0d2019-10-03 19:09:35 +02005868/*
David Sterba0202e832020-05-15 19:35:59 +02005869 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005870 * Path can be preallocated to prevent recursing back to iget through
5871 * allocator. NULL is also valid but may require an additional allocation
5872 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305873 */
David Sterba0202e832020-05-15 19:35:59 +02005874struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005875 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305876{
5877 struct inode *inode;
5878
David Sterba0202e832020-05-15 19:35:59 +02005879 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305880 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005881 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305882
5883 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005884 int ret;
5885
Filipe Manana4222ea72018-10-24 10:13:03 +01005886 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005887 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005888 inode_tree_add(inode);
5889 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005890 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005891 iget_failed(inode);
5892 /*
5893 * ret > 0 can come from btrfs_search_slot called by
5894 * btrfs_read_locked_inode, this means the inode item
5895 * was not found.
5896 */
5897 if (ret > 0)
5898 ret = -ENOENT;
5899 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005900 }
5901 }
5902
Balaji Rao1a54ef82008-07-21 02:01:04 +05305903 return inode;
5904}
5905
David Sterba0202e832020-05-15 19:35:59 +02005906struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005907{
David Sterba0202e832020-05-15 19:35:59 +02005908 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005909}
5910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005911static struct inode *new_simple_dir(struct super_block *s,
5912 struct btrfs_key *key,
5913 struct btrfs_root *root)
5914{
5915 struct inode *inode = new_inode(s);
5916
5917 if (!inode)
5918 return ERR_PTR(-ENOMEM);
5919
Josef Bacik5c8fd992020-02-14 16:11:43 -05005920 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005921 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005922 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005923
5924 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005925 /*
5926 * We only need lookup, the rest is read-only and there's no inode
5927 * associated with the dentry
5928 */
5929 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005930 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005931 inode->i_fop = &simple_dir_operations;
5932 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005933 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305934 inode->i_atime = inode->i_mtime;
5935 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005936 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005937
5938 return inode;
5939}
5940
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005941static inline u8 btrfs_inode_type(struct inode *inode)
5942{
5943 /*
5944 * Compile-time asserts that generic FT_* types still match
5945 * BTRFS_FT_* types
5946 */
5947 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5948 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5949 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5950 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5951 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5952 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5953 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5954 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5955
5956 return fs_umode_to_ftype(inode->i_mode);
5957}
5958
Chris Mason3de45862008-11-17 21:02:50 -05005959struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005960{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005961 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005962 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005963 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005964 struct btrfs_root *sub_root = root;
5965 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005966 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005967 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005968
5969 if (dentry->d_name.len > BTRFS_NAME_LEN)
5970 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005971
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005972 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005973 if (ret < 0)
5974 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005975
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005976 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005977 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005978 if (IS_ERR(inode))
5979 return inode;
5980
5981 /* Do extra check against inode mode with di_type */
5982 if (btrfs_inode_type(inode) != di_type) {
5983 btrfs_crit(fs_info,
5984"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5985 inode->i_mode, btrfs_inode_type(inode),
5986 di_type);
5987 iput(inode);
5988 return ERR_PTR(-EUCLEAN);
5989 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005990 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005991 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005992
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005993 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005994 &location, &sub_root);
5995 if (ret < 0) {
5996 if (ret != -ENOENT)
5997 inode = ERR_PTR(ret);
5998 else
5999 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6000 } else {
David Sterba0202e832020-05-15 19:35:59 +02006001 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04006002 }
Josef Bacik87270022020-01-24 09:32:31 -05006003 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05006004 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04006005
Julia Lawall34d19ba2011-01-24 19:55:19 +00006006 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006007 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006008 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006009 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006010 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006011 if (ret) {
6012 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006013 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006014 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006015 }
6016
Chris Mason3de45862008-11-17 21:02:50 -05006017 return inode;
6018}
6019
Nick Pigginfe15ce42011-01-07 17:49:23 +11006020static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006021{
6022 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006023 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006024
Li Zefan848cce02012-02-21 17:04:28 +08006025 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006026 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006027
Li Zefan848cce02012-02-21 17:04:28 +08006028 if (inode) {
6029 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006030 if (btrfs_root_refs(&root->root_item) == 0)
6031 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006032
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006033 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006034 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006035 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006036 return 0;
6037}
6038
Chris Mason3de45862008-11-17 21:02:50 -05006039static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006040 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006041{
Al Viro3837d202018-10-10 16:38:27 -04006042 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04006043
Al Viro3837d202018-10-10 16:38:27 -04006044 if (inode == ERR_PTR(-ENOENT))
6045 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04006046 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006047}
6048
Josef Bacik23b5ec72017-07-24 15:14:25 -04006049/*
6050 * All this infrastructure exists because dir_emit can fault, and we are holding
6051 * the tree lock when doing readdir. For now just allocate a buffer and copy
6052 * our information into that, and then dir_emit from the buffer. This is
6053 * similar to what NFS does, only we don't keep the buffer around in pagecache
6054 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6055 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6056 * tree lock.
6057 */
6058static int btrfs_opendir(struct inode *inode, struct file *file)
6059{
6060 struct btrfs_file_private *private;
6061
6062 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6063 if (!private)
6064 return -ENOMEM;
6065 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6066 if (!private->filldir_buf) {
6067 kfree(private);
6068 return -ENOMEM;
6069 }
6070 file->private_data = private;
6071 return 0;
6072}
6073
6074struct dir_entry {
6075 u64 ino;
6076 u64 offset;
6077 unsigned type;
6078 int name_len;
6079};
6080
6081static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6082{
6083 while (entries--) {
6084 struct dir_entry *entry = addr;
6085 char *name = (char *)(entry + 1);
6086
David Sterba92d32172018-04-16 21:10:14 +02006087 ctx->pos = get_unaligned(&entry->offset);
6088 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6089 get_unaligned(&entry->ino),
6090 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006091 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006092 addr += sizeof(struct dir_entry) +
6093 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006094 ctx->pos++;
6095 }
6096 return 0;
6097}
6098
Al Viro9cdda8d2013-05-22 16:48:09 -04006099static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006100{
Al Viro9cdda8d2013-05-22 16:48:09 -04006101 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006102 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006103 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006104 struct btrfs_dir_item *di;
6105 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006106 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006107 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006108 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006109 struct list_head ins_list;
6110 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006111 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006112 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006113 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006114 char *name_ptr;
6115 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006116 int entries = 0;
6117 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006118 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006119 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006120
Al Viro9cdda8d2013-05-22 16:48:09 -04006121 if (!dir_emit_dots(file, ctx))
6122 return 0;
6123
David Woodhouse49593bf2008-08-17 17:08:36 +01006124 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006125 if (!path)
6126 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006127
Josef Bacik23b5ec72017-07-24 15:14:25 -04006128 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006129 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006130
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006131 INIT_LIST_HEAD(&ins_list);
6132 INIT_LIST_HEAD(&del_list);
6133 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006134
Josef Bacik23b5ec72017-07-24 15:14:25 -04006135again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006136 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006137 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006138 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006139
Chris Mason39279cc2007-06-12 06:35:45 -04006140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6141 if (ret < 0)
6142 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006143
6144 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006145 struct dir_entry *entry;
6146
Chris Mason5f39d392007-10-15 16:14:19 -04006147 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006148 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006149 if (slot >= btrfs_header_nritems(leaf)) {
6150 ret = btrfs_next_leaf(root, path);
6151 if (ret < 0)
6152 goto err;
6153 else if (ret > 0)
6154 break;
6155 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006156 }
Chris Mason3de45862008-11-17 21:02:50 -05006157
Chris Mason5f39d392007-10-15 16:14:19 -04006158 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6159
6160 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006161 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006162 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006163 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006164 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006165 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006166 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006167 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006168 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006169 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006170 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6171 PAGE_SIZE) {
6172 btrfs_release_path(path);
6173 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6174 if (ret)
6175 goto nopos;
6176 addr = private->filldir_buf;
6177 entries = 0;
6178 total_len = 0;
6179 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006180 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006181
6182 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006183 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006184 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006185 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6186 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006187 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006188 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006189 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006190 put_unaligned(location.objectid, &entry->ino);
6191 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006192 entries++;
6193 addr += sizeof(struct dir_entry) + name_len;
6194 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006195next:
6196 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006197 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006198 btrfs_release_path(path);
6199
6200 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6201 if (ret)
6202 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006203
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006204 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006205 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006206 goto nopos;
6207
Zach Browndb62efb2013-07-11 16:19:42 -07006208 /*
6209 * Stop new entries from being returned after we return the last
6210 * entry.
6211 *
6212 * New directory entries are assigned a strictly increasing
6213 * offset. This means that new entries created during readdir
6214 * are *guaranteed* to be seen in the future by that readdir.
6215 * This has broken buggy programs which operate on names as
6216 * they're returned by readdir. Until we re-use freed offsets
6217 * we have this hack to stop new entries from being returned
6218 * under the assumption that they'll never reach this huge
6219 * offset.
6220 *
6221 * This is being careful not to overflow 32bit loff_t unless the
6222 * last entry requires it because doing so has broken 32bit apps
6223 * in the past.
6224 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006225 if (ctx->pos >= INT_MAX)
6226 ctx->pos = LLONG_MAX;
6227 else
6228 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006229nopos:
6230 ret = 0;
6231err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006232 if (put)
6233 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006234 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006235 return ret;
6236}
6237
Chris Mason39279cc2007-06-12 06:35:45 -04006238/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006239 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006240 * inode changes. But, it is most likely to find the inode in cache.
6241 * FIXME, needs more benchmarking...there are no reasons other than performance
6242 * to keep or drop this code.
6243 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006244static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006245{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006246 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006247 struct btrfs_root *root = BTRFS_I(inode)->root;
6248 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006249 int ret;
6250
Josef Bacik72ac3c02012-05-23 14:13:11 -04006251 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006252 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006253
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006254 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006255 if (IS_ERR(trans))
6256 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006257
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006258 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006259 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006260 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006261 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006262 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006263 if (IS_ERR(trans))
6264 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006265
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006266 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006267 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006268 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006269 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006270 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006271
6272 return ret;
6273}
6274
6275/*
6276 * This is a copy of file_update_time. We need this so we can return error on
6277 * ENOSPC for updating the inode in the case of file write and mmap writes.
6278 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006279static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006280 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006281{
Alexander Block2bc5565282012-06-15 09:49:33 +02006282 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006283 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006284
6285 if (btrfs_root_readonly(root))
6286 return -EROFS;
6287
Josef Bacike41f9412012-03-26 09:46:47 -04006288 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006289 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006290 if (flags & S_CTIME)
6291 inode->i_ctime = *now;
6292 if (flags & S_MTIME)
6293 inode->i_mtime = *now;
6294 if (flags & S_ATIME)
6295 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006296 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006297}
6298
Chris Masond352ac62008-09-29 15:18:18 -04006299/*
6300 * find the highest existing sequence number in a directory
6301 * and then set the in-memory index_cnt variable to reflect
6302 * free sequence numbers
6303 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006304static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006305{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006306 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006307 struct btrfs_key key, found_key;
6308 struct btrfs_path *path;
6309 struct extent_buffer *leaf;
6310 int ret;
6311
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006312 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006313 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006314 key.offset = (u64)-1;
6315
6316 path = btrfs_alloc_path();
6317 if (!path)
6318 return -ENOMEM;
6319
6320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6321 if (ret < 0)
6322 goto out;
6323 /* FIXME: we should be able to handle this */
6324 if (ret == 0)
6325 goto out;
6326 ret = 0;
6327
6328 /*
6329 * MAGIC NUMBER EXPLANATION:
6330 * since we search a directory based on f_pos we have to start at 2
6331 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6332 * else has to start at 2
6333 */
6334 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006335 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006336 goto out;
6337 }
6338
6339 path->slots[0]--;
6340
6341 leaf = path->nodes[0];
6342 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6343
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006344 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006345 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006346 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006347 goto out;
6348 }
6349
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006350 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006351out:
6352 btrfs_free_path(path);
6353 return ret;
6354}
6355
Chris Masond352ac62008-09-29 15:18:18 -04006356/*
6357 * helper to find a free sequence number in a given directory. This current
6358 * code is very simple, later versions will do smarter things in the btree
6359 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006360int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006361{
6362 int ret = 0;
6363
Nikolay Borisov877574e2017-02-20 13:50:33 +02006364 if (dir->index_cnt == (u64)-1) {
6365 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006366 if (ret) {
6367 ret = btrfs_set_inode_index_count(dir);
6368 if (ret)
6369 return ret;
6370 }
Josef Bacikaec74772008-07-24 12:12:38 -04006371 }
6372
Nikolay Borisov877574e2017-02-20 13:50:33 +02006373 *index = dir->index_cnt;
6374 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006375
6376 return ret;
6377}
6378
Chris Masonb0d5d102014-09-08 13:08:51 -07006379static int btrfs_insert_inode_locked(struct inode *inode)
6380{
6381 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006382
6383 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006384 args.root = BTRFS_I(inode)->root;
6385
6386 return insert_inode_locked4(inode,
6387 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6388 btrfs_find_actor, &args);
6389}
6390
Anand Jain19aee8d2017-07-18 17:37:05 +08006391/*
6392 * Inherit flags from the parent inode.
6393 *
6394 * Currently only the compression flags and the cow flags are inherited.
6395 */
6396static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6397{
6398 unsigned int flags;
6399
6400 if (!dir)
6401 return;
6402
6403 flags = BTRFS_I(dir)->flags;
6404
6405 if (flags & BTRFS_INODE_NOCOMPRESS) {
6406 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6407 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6408 } else if (flags & BTRFS_INODE_COMPRESS) {
6409 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6410 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6411 }
6412
6413 if (flags & BTRFS_INODE_NODATACOW) {
6414 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6415 if (S_ISREG(inode->i_mode))
6416 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6417 }
6418
David Sterba7b6a2212018-03-26 18:40:21 +02006419 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006420}
6421
Chris Mason39279cc2007-06-12 06:35:45 -04006422static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6423 struct btrfs_root *root,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006424 struct user_namespace *mnt_userns,
Josef Bacikaec74772008-07-24 12:12:38 -04006425 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006426 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006427 u64 ref_objectid, u64 objectid,
6428 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006430 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006431 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006432 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006433 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006434 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006435 struct btrfs_inode_ref *ref;
6436 struct btrfs_key key[2];
6437 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006438 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006439 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006440 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006441 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006442
Chris Mason5f39d392007-10-15 16:14:19 -04006443 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006444 if (!path)
6445 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006446
Josef Bacik11a19a92019-09-09 10:12:04 -04006447 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006448 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006449 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006450 if (!inode) {
6451 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006452 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006453 }
Chris Mason39279cc2007-06-12 06:35:45 -04006454
Li Zefan581bb052011-04-20 10:06:11 +08006455 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006456 * O_TMPFILE, set link count to 0, so that after this point,
6457 * we fill in an inode item with the correct link count.
6458 */
6459 if (!name)
6460 set_nlink(inode, 0);
6461
6462 /*
Li Zefan581bb052011-04-20 10:06:11 +08006463 * we have to initialize this early, so we can reclaim the inode
6464 * number if we fail afterwards in this function.
6465 */
6466 inode->i_ino = objectid;
6467
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006468 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006469 trace_btrfs_inode_request(dir);
6470
Nikolay Borisov877574e2017-02-20 13:50:33 +02006471 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006472 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006473 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006474 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006475 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006476 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006477 } else if (dir) {
6478 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006479 }
6480 /*
6481 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006482 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006483 * number
6484 */
6485 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006486 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006487 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006488 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006489 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006490
Josef Bacik5dc562c2012-08-17 13:14:17 -04006491 /*
6492 * We could have gotten an inode number from somebody who was fsynced
6493 * and then removed in this same transaction, so let's just set full
6494 * sync since it will be a full sync anyway and this will blow away the
6495 * old info in the log.
6496 */
6497 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6498
Chris Mason9c583092008-01-29 15:15:18 -05006499 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006500 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006501 key[0].offset = 0;
6502
Chris Mason9c583092008-01-29 15:15:18 -05006503 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006504
6505 if (name) {
6506 /*
6507 * Start new inodes with an inode_ref. This is slightly more
6508 * efficient for small numbers of hard links since they will
6509 * be packed into one item. Extended refs will kick in if we
6510 * add more hard links than can fit in the ref item.
6511 */
6512 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006513 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006514 key[1].offset = ref_objectid;
6515
6516 sizes[1] = name_len + sizeof(*ref);
6517 }
Chris Mason9c583092008-01-29 15:15:18 -05006518
Chris Masonb0d5d102014-09-08 13:08:51 -07006519 location = &BTRFS_I(inode)->location;
6520 location->objectid = objectid;
6521 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006522 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006523
6524 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006525 if (ret < 0) {
6526 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006527 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006528 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006529
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006530 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006531 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006532 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006533
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006534 inode_init_owner(mnt_userns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006535 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306536
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006537 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306538 inode->i_atime = inode->i_mtime;
6539 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006540 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306541
Chris Mason5f39d392007-10-15 16:14:19 -04006542 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6543 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006544 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006545 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006546 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006547
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006548 if (name) {
6549 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6550 struct btrfs_inode_ref);
6551 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6552 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6553 ptr = (unsigned long)(ref + 1);
6554 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6555 }
Chris Mason9c583092008-01-29 15:15:18 -05006556
Chris Mason5f39d392007-10-15 16:14:19 -04006557 btrfs_mark_buffer_dirty(path->nodes[0]);
6558 btrfs_free_path(path);
6559
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006560 btrfs_inherit_iflags(inode, dir);
6561
Al Viro569254b2011-07-24 17:08:40 -04006562 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006563 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006564 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006565 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006566 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6567 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006568 }
6569
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006570 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006571
6572 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006573 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006574
Alexander Block8ea05e32012-07-25 17:35:53 +02006575 btrfs_update_root_times(trans, root);
6576
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006577 ret = btrfs_inode_inherit_props(trans, inode, dir);
6578 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006579 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006580 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006581 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006582
Chris Mason39279cc2007-06-12 06:35:45 -04006583 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006584
6585fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006586 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006587fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006588 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006589 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006590 btrfs_free_path(path);
6591 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006592}
6593
Chris Masond352ac62008-09-29 15:18:18 -04006594/*
6595 * utility function to add 'inode' into 'parent_inode' with
6596 * a give name and a given sequence number.
6597 * if 'add_backref' is true, also insert a backref from the
6598 * inode to the parent directory.
6599 */
Chris Masone02119d2008-09-05 16:13:11 -04006600int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006601 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006602 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006603{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006604 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006605 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006606 struct btrfs_root *root = parent_inode->root;
6607 u64 ino = btrfs_ino(inode);
6608 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006609
Li Zefan33345d012011-04-20 10:31:50 +08006610 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006611 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006612 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006613 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006614 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006615 key.offset = 0;
6616 }
Chris Mason39279cc2007-06-12 06:35:45 -04006617
Li Zefan33345d012011-04-20 10:31:50 +08006618 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006619 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006620 root->root_key.objectid, parent_ino,
6621 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006622 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006623 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6624 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006625 }
6626
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006627 /* Nothing to clean up yet */
6628 if (ret)
6629 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006630
Lu Fengqi684572d2018-08-04 21:10:57 +08006631 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006632 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006633 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006634 goto fail_dir_item;
6635 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006636 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006637 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006638 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006639
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006640 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006641 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006642 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006643 /*
6644 * If we are replaying a log tree, we do not want to update the mtime
6645 * and ctime of the parent directory with the current time, since the
6646 * log replay procedure is responsible for setting them to their correct
6647 * values (the ones it had when the fsync was done).
6648 */
6649 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6650 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6651
6652 parent_inode->vfs_inode.i_mtime = now;
6653 parent_inode->vfs_inode.i_ctime = now;
6654 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006655 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006656 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006657 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006658 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006659
6660fail_dir_item:
6661 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6662 u64 local_index;
6663 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006664 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006665 root->root_key.objectid, parent_ino,
6666 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006667 if (err)
6668 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006669 } else if (add_backref) {
6670 u64 local_index;
6671 int err;
6672
6673 err = btrfs_del_inode_ref(trans, root, name, name_len,
6674 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006675 if (err)
6676 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006677 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006678
6679 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006680 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006681}
6682
6683static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006684 struct btrfs_inode *dir, struct dentry *dentry,
6685 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006686{
Josef Bacika1b075d2010-11-19 20:36:11 +00006687 int err = btrfs_add_link(trans, dir, inode,
6688 dentry->d_name.name, dentry->d_name.len,
6689 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 if (err > 0)
6691 err = -EEXIST;
6692 return err;
6693}
6694
Christian Brauner549c7292021-01-21 14:19:43 +01006695static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6696 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006697{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006698 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006699 struct btrfs_trans_handle *trans;
6700 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006701 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006702 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006703 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006704 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006705
Josef Bacik9ed74f22009-09-11 16:12:44 -04006706 /*
6707 * 2 for inode item and ref
6708 * 2 for dir items
6709 * 1 for xattr if selinux is on
6710 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006711 trans = btrfs_start_transaction(root, 5);
6712 if (IS_ERR(trans))
6713 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006714
Nikolay Borisov543068a2020-12-07 17:32:33 +02006715 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006716 if (err)
6717 goto out_unlock;
6718
Christian Brauner72105272021-07-27 12:48:44 +02006719 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006720 dentry->d_name.name, dentry->d_name.len,
6721 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006722 if (IS_ERR(inode)) {
6723 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006724 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006725 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006726 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006727
Casey Schauflerad19db72011-12-15 10:09:07 -05006728 /*
6729 * If the active LSM wants to access the inode during
6730 * d_instantiate it needs these. Smack checks to see
6731 * if the filesystem supports xattrs by looking at the
6732 * ops vector.
6733 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006734 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006735 init_special_inode(inode, inode->i_mode, rdev);
6736
6737 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006738 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006739 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006740
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006741 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6742 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006743 if (err)
6744 goto out_unlock;
6745
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006746 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006747 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006748
Josef Bacik618e21d2007-07-11 10:18:17 -04006749out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006750 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006751 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006752 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006753 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006754 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006755 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006756 return err;
6757}
6758
Christian Brauner549c7292021-01-21 14:19:43 +01006759static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6760 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006761{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006762 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006763 struct btrfs_trans_handle *trans;
6764 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006765 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006766 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006767 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006768 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006769
Josef Bacik9ed74f22009-09-11 16:12:44 -04006770 /*
6771 * 2 for inode item and ref
6772 * 2 for dir items
6773 * 1 for xattr if selinux is on
6774 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006775 trans = btrfs_start_transaction(root, 5);
6776 if (IS_ERR(trans))
6777 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006778
Nikolay Borisov543068a2020-12-07 17:32:33 +02006779 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006780 if (err)
6781 goto out_unlock;
6782
Christian Braunere93ca492021-07-27 12:48:45 +02006783 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006784 dentry->d_name.name, dentry->d_name.len,
6785 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006786 if (IS_ERR(inode)) {
6787 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006788 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006789 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006790 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006791 /*
6792 * If the active LSM wants to access the inode during
6793 * d_instantiate it needs these. Smack checks to see
6794 * if the filesystem supports xattrs by looking at the
6795 * ops vector.
6796 */
6797 inode->i_fop = &btrfs_file_operations;
6798 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006799 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006800
6801 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6802 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006803 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006804
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006805 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006806 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006807 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006808
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006809 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6810 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006811 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006812 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006813
Al Viro1e2e5472018-05-04 08:23:01 -04006814 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006815
Chris Mason39279cc2007-06-12 06:35:45 -04006816out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006817 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006818 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006819 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006820 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006821 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006822 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006823 return err;
6824}
6825
6826static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6827 struct dentry *dentry)
6828{
Filipe Manana271dba452016-01-05 16:24:05 +00006829 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006830 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006831 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006833 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006834 int err;
6835 int drop_inode = 0;
6836
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006837 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006838 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006839 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006840
Mark Fashehf1863732012-08-08 11:32:27 -07006841 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006842 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006843
Nikolay Borisov877574e2017-02-20 13:50:33 +02006844 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006845 if (err)
6846 goto fail;
6847
Yan, Zhenga22285a2010-05-16 10:48:46 -04006848 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006849 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006850 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006851 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006852 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006853 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006854 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006855 if (IS_ERR(trans)) {
6856 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006857 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006858 goto fail;
6859 }
Chris Mason5f39d392007-10-15 16:14:19 -04006860
Miao Xie67de1172013-12-26 13:07:06 +08006861 /* There are several dir indexes for this inode, clear the cache. */
6862 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006863 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006864 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006865 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006866 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006867 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006868
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006869 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6870 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006871
Yan, Zhenga5719522009-09-24 09:17:31 -04006872 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006873 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006874 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006875 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006876
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006877 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006878 if (err)
6879 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006880 if (inode->i_nlink == 1) {
6881 /*
6882 * If new hard link count is 1, it's a file created
6883 * with open(2) O_TMPFILE flag.
6884 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006885 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006886 if (err)
6887 goto fail;
6888 }
Al Viro08c422c2011-12-23 07:58:13 -05006889 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006890 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006891 }
Chris Mason39279cc2007-06-12 06:35:45 -04006892
Chris Mason1832a6d2007-12-21 16:27:21 -05006893fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006894 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006895 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006896 if (drop_inode) {
6897 inode_dec_link_count(inode);
6898 iput(inode);
6899 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006900 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006901 return err;
6902}
6903
Christian Brauner549c7292021-01-21 14:19:43 +01006904static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6905 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006906{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006907 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006908 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006909 struct btrfs_trans_handle *trans;
6910 struct btrfs_root *root = BTRFS_I(dir)->root;
6911 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006912 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006913 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006914
Josef Bacik9ed74f22009-09-11 16:12:44 -04006915 /*
6916 * 2 items for inode and ref
6917 * 2 items for dir items
6918 * 1 for xattr if selinux is on
6919 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006920 trans = btrfs_start_transaction(root, 5);
6921 if (IS_ERR(trans))
6922 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006923
Nikolay Borisov543068a2020-12-07 17:32:33 +02006924 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006925 if (err)
6926 goto out_fail;
6927
Christian Braunerb0b3e442021-07-27 12:48:46 +02006928 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006929 dentry->d_name.name, dentry->d_name.len,
6930 btrfs_ino(BTRFS_I(dir)), objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01006931 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006932 if (IS_ERR(inode)) {
6933 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006934 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006935 goto out_fail;
6936 }
Chris Mason5f39d392007-10-15 16:14:19 -04006937
Chris Masonb0d5d102014-09-08 13:08:51 -07006938 /* these must be set before we unlock the inode */
6939 inode->i_op = &btrfs_dir_inode_operations;
6940 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006941
Eric Paris2a7dba32011-02-01 11:05:39 -05006942 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006943 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006944 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006945
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006946 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006947 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006948 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006949 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006950
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006951 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6952 dentry->d_name.name,
6953 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006954 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006955 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006956
Al Viro1e2e5472018-05-04 08:23:01 -04006957 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006958
6959out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006960 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006961 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006962 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006963 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006964 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006965 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006966 return err;
6967}
6968
Chris Masonc8b97812008-10-29 14:49:59 -04006969static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006970 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006971 size_t pg_offset, u64 extent_offset,
6972 struct btrfs_file_extent_item *item)
6973{
6974 int ret;
6975 struct extent_buffer *leaf = path->nodes[0];
6976 char *tmp;
6977 size_t max_size;
6978 unsigned long inline_size;
6979 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006980 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006981
6982 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006983 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006984 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6985 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006986 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006987 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006988 if (!tmp)
6989 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006990 ptr = btrfs_file_extent_inline_start(item);
6991
6992 read_extent_buffer(leaf, tmp, ptr, inline_size);
6993
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006994 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006995 ret = btrfs_decompress(compress_type, tmp, page,
6996 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006997
6998 /*
6999 * decompression code contains a memset to fill in any space between the end
7000 * of the uncompressed data and the end of max_size in case the decompressed
7001 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7002 * the end of an inline extent and the beginning of the next block, so we
7003 * cover that region here.
7004 */
7005
Ira Weinyd048b9c2021-05-04 18:40:07 -07007006 if (max_size + pg_offset < PAGE_SIZE)
7007 memzero_page(page, pg_offset + max_size,
7008 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04007009 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007010 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007011}
7012
Omar Sandoval39b07b52019-12-02 17:34:23 -08007013/**
7014 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
7015 * @inode: file to search in
7016 * @page: page to read extent data into if the extent is inline
7017 * @pg_offset: offset into @page to copy to
7018 * @start: file offset
7019 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04007020 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08007021 * This returns the first &struct extent_map which overlaps with the given
7022 * range, reading it from the B-tree and caching it if necessary. Note that
7023 * there may be more extents which overlap the given range after the returned
7024 * extent_map.
7025 *
7026 * If @page is not NULL and the extent is inline, this also reads the extent
7027 * data directly into the page and marks the extent up to date in the io_tree.
7028 *
7029 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04007030 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007031struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08007032 struct page *page, size_t pg_offset,
7033 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04007034{
David Sterba3ffbd682018-06-29 10:56:42 +02007035 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007036 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007037 u64 extent_start = 0;
7038 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007039 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07007040 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04007041 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007042 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007043 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007044 struct extent_buffer *leaf;
7045 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007046 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007047 struct extent_map_tree *em_tree = &inode->extent_tree;
7048 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007049
Chris Mason890871b2009-09-02 16:24:52 -04007050 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007051 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007052 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007053
Chris Masona52d9a82007-08-27 16:49:44 -04007054 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007055 if (em->start > start || em->start + em->len <= start)
7056 free_extent_map(em);
7057 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007058 free_extent_map(em);
7059 else
7060 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007061 }
David Sterba172ddd62011-04-21 00:48:27 +02007062 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007063 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007064 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05007065 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
Chris Masond1310b22008-01-24 16:13:08 -05007067 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007068 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007069 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007070 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007071
Liu Bobee6ec82018-08-17 05:05:28 +08007072 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04007073 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007074 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08007075 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04007076 }
7077
Liu Bobee6ec82018-08-17 05:05:28 +08007078 /* Chances are we'll be called again, so go ahead and do readahead */
7079 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04007080
7081 /*
7082 * The same explanation in load_free_space_cache applies here as well,
7083 * we only read when we're loading the free space cache, and at that
7084 * point the commit_root has everything we need.
7085 */
7086 if (btrfs_is_free_space_inode(inode)) {
7087 path->search_commit_root = 1;
7088 path->skip_locking = 1;
7089 }
Josef Bacik51899412020-08-20 11:46:01 -04007090
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007091 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007092 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007093 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007094 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007095 if (path->slots[0] == 0)
7096 goto not_found;
7097 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007098 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007099 }
7100
Chris Mason5f39d392007-10-15 16:14:19 -04007101 leaf = path->nodes[0];
7102 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007103 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007104 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007105 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007106 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007107 /*
7108 * If we backup past the first extent we want to move forward
7109 * and see if there is an extent in front of us, otherwise we'll
7110 * say there is a hole for our whole search range which can
7111 * cause problems.
7112 */
7113 extent_end = start;
7114 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007115 }
7116
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007117 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007118 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00007119 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007120 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7121 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007122 /* Only regular file could have regular/prealloc extent */
7123 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007124 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007125 btrfs_crit(fs_info,
7126 "regular/prealloc extent found for non-regular inode %llu",
7127 btrfs_ino(inode));
7128 goto out;
7129 }
Liu Bo09ed2f12017-03-10 11:09:48 -08007130 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7131 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007132 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08007133 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7134 path->slots[0],
7135 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007136 }
Josef Bacik25a50342013-10-14 12:08:38 -04007137next:
Yan Zheng9036c102008-10-30 14:19:41 -04007138 if (start >= extent_end) {
7139 path->slots[0]++;
7140 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7141 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007142 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007143 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007144 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007145 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007146
Yan Zheng9036c102008-10-30 14:19:41 -04007147 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007148 }
Yan Zheng9036c102008-10-30 14:19:41 -04007149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7150 if (found_key.objectid != objectid ||
7151 found_key.type != BTRFS_EXTENT_DATA_KEY)
7152 goto not_found;
7153 if (start + len <= found_key.offset)
7154 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007155 if (start > found_key.offset)
7156 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007157
7158 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007159 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007160 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007161 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007162 em->block_start = EXTENT_MAP_HOLE;
7163 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007164 }
7165
Omar Sandoval39b07b52019-12-02 17:34:23 -08007166 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007167
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007168 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7169 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007170 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007171 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007172 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007173 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007174 size_t size;
7175 size_t extent_offset;
7176 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007177
Omar Sandoval39b07b52019-12-02 17:34:23 -08007178 if (!page)
Yan689f9342007-10-29 11:41:07 -04007179 goto out;
Yan689f9342007-10-29 11:41:07 -04007180
Qu Wenruoe41ca582018-06-06 15:41:49 +08007181 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007182 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007183 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7184 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007185 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007186 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007187 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007188 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007189 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007190
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007191 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007192 if (btrfs_file_extent_compression(leaf, item) !=
7193 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007194 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007195 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007196 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007197 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007198 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007199 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007200 read_extent_buffer(leaf, map + pg_offset, ptr,
7201 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007202 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007203 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007204 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007205 copy_size);
7206 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007207 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007208 }
Chris Mason179e29e2007-11-01 11:28:41 -04007209 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007210 }
Chris Masond1310b22008-01-24 16:13:08 -05007211 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007212 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007213 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007214 }
7215not_found:
7216 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007217 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007218 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007219 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007220insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007221 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007222 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007223 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007224 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007225 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7226 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007227 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007228 goto out;
7229 }
Chris Masond1310b22008-01-24 16:13:08 -05007230
Chris Mason890871b2009-09-02 16:24:52 -04007231 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007232 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007233 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007234out:
Liu Boc6414282018-08-23 07:36:17 +08007235 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007236
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007237 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007238
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007239 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007240 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007241 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007242 }
7243 return em;
7244}
7245
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007246struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007247 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007248{
7249 struct extent_map *em;
7250 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007251 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007252 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007253 u64 delalloc_len;
7254 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007255 int err = 0;
7256
Omar Sandoval39b07b52019-12-02 17:34:23 -08007257 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007258 if (IS_ERR(em))
7259 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007260 /*
7261 * If our em maps to:
7262 * - a hole or
7263 * - a pre-alloc extent,
7264 * there might actually be delalloc bytes behind it.
7265 */
7266 if (em->block_start != EXTENT_MAP_HOLE &&
7267 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7268 return em;
7269 else
7270 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007271
7272 /* check to see if we've wrapped (len == -1 or similar) */
7273 end = start + len;
7274 if (end < start)
7275 end = (u64)-1;
7276 else
7277 end -= 1;
7278
7279 em = NULL;
7280
7281 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007282 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007283 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007284 delalloc_end = delalloc_start + delalloc_len;
7285 if (delalloc_end < delalloc_start)
7286 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007287
7288 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007289 * We didn't find anything useful, return the original results from
7290 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007291 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007292 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007293 em = hole_em;
7294 hole_em = NULL;
7295 goto out;
7296 }
7297
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007298 /*
7299 * Adjust the delalloc_start to make sure it doesn't go backwards from
7300 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007301 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007302 delalloc_start = max(start, delalloc_start);
7303 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007304
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007305 if (delalloc_len > 0) {
7306 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007307 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007308 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007309
David Sterba172ddd62011-04-21 00:48:27 +02007310 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007311 if (!em) {
7312 err = -ENOMEM;
7313 goto out;
7314 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007315
7316 ASSERT(hole_em);
7317 /*
7318 * When btrfs_get_extent can't find anything it returns one
7319 * huge hole
7320 *
7321 * Make sure what it found really fits our range, and adjust to
7322 * make sure it is based on the start from the caller
7323 */
7324 if (hole_end <= start || hole_em->start > end) {
7325 free_extent_map(hole_em);
7326 hole_em = NULL;
7327 } else {
7328 hole_start = max(hole_em->start, start);
7329 hole_len = hole_end - hole_start;
7330 }
7331
7332 if (hole_em && delalloc_start > hole_start) {
7333 /*
7334 * Our hole starts before our delalloc, so we have to
7335 * return just the parts of the hole that go until the
7336 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007337 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007338 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007339 em->start = hole_start;
7340 em->orig_start = hole_start;
7341 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007342 * Don't adjust block start at all, it is fixed at
7343 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007344 */
7345 em->block_start = hole_em->block_start;
7346 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007347 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7348 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007349 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007350 /*
7351 * Hole is out of passed range or it starts after
7352 * delalloc range
7353 */
7354 em->start = delalloc_start;
7355 em->len = delalloc_len;
7356 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007357 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007358 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007359 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007360 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007361 return hole_em;
7362 }
7363out:
7364
7365 free_extent_map(hole_em);
7366 if (err) {
7367 free_extent_map(em);
7368 return ERR_PTR(err);
7369 }
7370 return em;
7371}
7372
Nikolay Borisov64f54182020-06-03 08:55:31 +03007373static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007374 const u64 start,
7375 const u64 len,
7376 const u64 orig_start,
7377 const u64 block_start,
7378 const u64 block_len,
7379 const u64 orig_block_len,
7380 const u64 ram_bytes,
7381 const int type)
7382{
7383 struct extent_map *em = NULL;
7384 int ret;
7385
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007386 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007387 em = create_io_em(inode, start, len, orig_start, block_start,
7388 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007389 BTRFS_COMPRESS_NONE, /* compress_type */
7390 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007391 if (IS_ERR(em))
7392 goto out;
7393 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007394 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7395 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007396 if (ret) {
7397 if (em) {
7398 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007399 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007400 }
7401 em = ERR_PTR(ret);
7402 }
7403 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007404
7405 return em;
7406}
7407
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007408static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007409 u64 start, u64 len)
7410{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007411 struct btrfs_root *root = inode->root;
7412 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007413 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007414 struct btrfs_key ins;
7415 u64 alloc_hint;
7416 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007418 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007419 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007420 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007421 if (ret)
7422 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007423
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007424 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007425 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007426 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007427 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007428 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007429 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7430 1);
Josef Bacik00361582013-08-14 14:02:47 -04007431
Josef Bacik4b46fce2010-05-23 11:00:55 -04007432 return em;
7433}
7434
Anand Jainf4639632021-02-10 21:25:16 -08007435static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007436{
7437 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007438 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007439
7440 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7441 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007442 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007443 if (block_group)
7444 btrfs_put_block_group(block_group);
7445 return readonly;
7446}
7447
Chris Mason46bfbb52010-05-26 11:04:10 -04007448/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007449 * Check if we can do nocow write into the range [@offset, @offset + @len)
7450 *
7451 * @offset: File offset
7452 * @len: The length to write, will be updated to the nocow writeable
7453 * range
7454 * @orig_start: (optional) Return the original file offset of the file extent
7455 * @orig_len: (optional) Return the original on-disk length of the file extent
7456 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007457 * @strict: if true, omit optimizations that might force us into unnecessary
7458 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007459 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007460 * Return:
7461 * >0 and update @len if we can do nocow write
7462 * 0 if we can't do nocow write
7463 * <0 if error happened
7464 *
7465 * NOTE: This only checks the file extents, caller is responsible to wait for
7466 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007467 */
Josef Bacik00361582013-08-14 14:02:47 -04007468noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007469 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007470 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007471{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007473 struct btrfs_path *path;
7474 int ret;
7475 struct extent_buffer *leaf;
7476 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007477 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007478 struct btrfs_file_extent_item *fi;
7479 struct btrfs_key key;
7480 u64 disk_bytenr;
7481 u64 backref_offset;
7482 u64 extent_end;
7483 u64 num_bytes;
7484 int slot;
7485 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007486 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007487
Chris Mason46bfbb52010-05-26 11:04:10 -04007488 path = btrfs_alloc_path();
7489 if (!path)
7490 return -ENOMEM;
7491
David Sterbaf85b7372017-01-20 14:54:07 +01007492 ret = btrfs_lookup_file_extent(NULL, root, path,
7493 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007494 if (ret < 0)
7495 goto out;
7496
7497 slot = path->slots[0];
7498 if (ret == 1) {
7499 if (slot == 0) {
7500 /* can't find the item, must cow */
7501 ret = 0;
7502 goto out;
7503 }
7504 slot--;
7505 }
7506 ret = 0;
7507 leaf = path->nodes[0];
7508 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007509 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007510 key.type != BTRFS_EXTENT_DATA_KEY) {
7511 /* not our file or wrong item type, must cow */
7512 goto out;
7513 }
7514
7515 if (key.offset > offset) {
7516 /* Wrong offset, must cow */
7517 goto out;
7518 }
7519
7520 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7521 found_type = btrfs_file_extent_type(leaf, fi);
7522 if (found_type != BTRFS_FILE_EXTENT_REG &&
7523 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7524 /* not a regular extent, must cow */
7525 goto out;
7526 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007527
7528 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7529 goto out;
7530
Miao Xiee77751a2013-12-27 21:11:50 +08007531 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7532 if (extent_end <= offset)
7533 goto out;
7534
Chris Mason46bfbb52010-05-26 11:04:10 -04007535 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007536 if (disk_bytenr == 0)
7537 goto out;
7538
7539 if (btrfs_file_extent_compression(leaf, fi) ||
7540 btrfs_file_extent_encryption(leaf, fi) ||
7541 btrfs_file_extent_other_encoding(leaf, fi))
7542 goto out;
7543
Ethan Lien78d42952018-05-17 14:58:29 +08007544 /*
7545 * Do the same check as in btrfs_cross_ref_exist but without the
7546 * unnecessary search.
7547 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007548 if (!strict &&
7549 (btrfs_file_extent_generation(leaf, fi) <=
7550 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007551 goto out;
7552
Chris Mason46bfbb52010-05-26 11:04:10 -04007553 backref_offset = btrfs_file_extent_offset(leaf, fi);
7554
Josef Bacik7ee9e442013-06-21 16:37:03 -04007555 if (orig_start) {
7556 *orig_start = key.offset - backref_offset;
7557 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7558 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7559 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007560
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007561 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007562 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007563
7564 num_bytes = min(offset + *len, extent_end) - offset;
7565 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7566 u64 range_end;
7567
Jeff Mahoneyda170662016-06-15 09:22:56 -04007568 range_end = round_up(offset + num_bytes,
7569 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007570 ret = test_range_bit(io_tree, offset, range_end,
7571 EXTENT_DELALLOC, 0, NULL);
7572 if (ret) {
7573 ret = -EAGAIN;
7574 goto out;
7575 }
7576 }
7577
Josef Bacik1bda19e2013-10-18 12:10:36 -04007578 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007579
7580 /*
7581 * look for other files referencing this extent, if we
7582 * find any we must cow
7583 */
Josef Bacik00361582013-08-14 14:02:47 -04007584
Liu Boe4c3b2d2017-01-30 12:25:28 -08007585 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007586 key.offset - backref_offset, disk_bytenr,
7587 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007588 if (ret) {
7589 ret = 0;
7590 goto out;
7591 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007592
7593 /*
7594 * adjust disk_bytenr and num_bytes to cover just the bytes
7595 * in this extent we are about to write. If there
7596 * are any csums in that range we have to cow in order
7597 * to keep the csums correct
7598 */
7599 disk_bytenr += backref_offset;
7600 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007601 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7602 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007603 /*
7604 * all of the above have passed, it is safe to overwrite this extent
7605 * without cow
7606 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007607 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007608 ret = 1;
7609out:
7610 btrfs_free_path(path);
7611 return ret;
7612}
7613
Josef Bacikeb838e72012-07-31 16:28:48 -04007614static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007615 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007616{
7617 struct btrfs_ordered_extent *ordered;
7618 int ret = 0;
7619
7620 while (1) {
7621 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007622 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 /*
7624 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007625 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007626 * extents in this range.
7627 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007628 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007629 lockend - lockstart + 1);
7630
7631 /*
7632 * We need to make sure there are no buffered pages in this
7633 * range either, we could have raced between the invalidate in
7634 * generic_file_direct_write and locking the extent. The
7635 * invalidate needs to happen so that reads after a write do not
7636 * get stale data.
7637 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007638 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007639 (!writing || !filemap_range_has_page(inode->i_mapping,
7640 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007641 break;
7642
7643 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007644 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007645
7646 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007647 /*
7648 * If we are doing a DIO read and the ordered extent we
7649 * found is for a buffered write, we can not wait for it
7650 * to complete and retry, because if we do so we can
7651 * deadlock with concurrent buffered writes on page
7652 * locks. This happens only if our DIO read covers more
7653 * than one extent map, if at this point has already
7654 * created an ordered extent for a previous extent map
7655 * and locked its range in the inode's io tree, and a
7656 * concurrent write against that previous extent map's
7657 * range and this range started (we unlock the ranges
7658 * in the io tree only when the bios complete and
7659 * buffered writes always lock pages before attempting
7660 * to lock range in the io tree).
7661 */
7662 if (writing ||
7663 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007664 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007665 else
7666 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 btrfs_put_ordered_extent(ordered);
7668 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007670 * We could trigger writeback for this range (and wait
7671 * for it to complete) and then invalidate the pages for
7672 * this range (through invalidate_inode_pages2_range()),
7673 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007674 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007675 * triggered a readahead) on a page lock due to an
7676 * ordered dio extent we created before but did not have
7677 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007678 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007679 * ordered extent to complete while holding a lock on
7680 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007681 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007682 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007683 }
7684
Filipe Mananaade77022016-02-18 14:28:55 +00007685 if (ret)
7686 break;
7687
Josef Bacikeb838e72012-07-31 16:28:48 -04007688 cond_resched();
7689 }
7690
7691 return ret;
7692}
7693
Liu Bo6f9994d2017-01-31 07:50:22 -08007694/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007695static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7696 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007697 u64 block_len, u64 orig_block_len,
7698 u64 ram_bytes, int compress_type,
7699 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007700{
7701 struct extent_map_tree *em_tree;
7702 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007703 int ret;
7704
Liu Bo6f9994d2017-01-31 07:50:22 -08007705 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7706 type == BTRFS_ORDERED_COMPRESSED ||
7707 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007708 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007709
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007710 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007711 em = alloc_extent_map();
7712 if (!em)
7713 return ERR_PTR(-ENOMEM);
7714
7715 em->start = start;
7716 em->orig_start = orig_start;
7717 em->len = len;
7718 em->block_len = block_len;
7719 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007720 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007721 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007722 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007723 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007724 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007725 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007726 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007727 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7728 em->compress_type = compress_type;
7729 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007730
7731 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007732 btrfs_drop_extent_cache(inode, em->start,
7733 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007734 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007735 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007736 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007737 /*
7738 * The caller has taken lock_extent(), who could race with us
7739 * to add em?
7740 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007741 } while (ret == -EEXIST);
7742
7743 if (ret) {
7744 free_extent_map(em);
7745 return ERR_PTR(ret);
7746 }
7747
Liu Bo6f9994d2017-01-31 07:50:22 -08007748 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007749 return em;
7750}
7751
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007752
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007753static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007754 struct inode *inode,
7755 struct btrfs_dio_data *dio_data,
7756 u64 start, u64 len)
7757{
7758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7759 struct extent_map *em = *map;
7760 int ret = 0;
7761
7762 /*
7763 * We don't allocate a new extent in the following cases
7764 *
7765 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7766 * existing extent.
7767 * 2) The extent is marked as PREALLOC. We're good to go here and can
7768 * just use the extent.
7769 *
7770 */
7771 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7772 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7773 em->block_start != EXTENT_MAP_HOLE)) {
7774 int type;
7775 u64 block_start, orig_start, orig_block_len, ram_bytes;
7776
7777 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7778 type = BTRFS_ORDERED_PREALLOC;
7779 else
7780 type = BTRFS_ORDERED_NOCOW;
7781 len = min(len, em->len - (start - em->start));
7782 block_start = em->block_start + (start - em->start);
7783
7784 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007785 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007786 btrfs_inc_nocow_writers(fs_info, block_start)) {
7787 struct extent_map *em2;
7788
Nikolay Borisov64f54182020-06-03 08:55:31 +03007789 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007790 orig_start, block_start,
7791 len, orig_block_len,
7792 ram_bytes, type);
7793 btrfs_dec_nocow_writers(fs_info, block_start);
7794 if (type == BTRFS_ORDERED_PREALLOC) {
7795 free_extent_map(em);
7796 *map = em = em2;
7797 }
7798
7799 if (em2 && IS_ERR(em2)) {
7800 ret = PTR_ERR(em2);
7801 goto out;
7802 }
7803 /*
7804 * For inode marked NODATACOW or extent marked PREALLOC,
7805 * use the existing or preallocated extent, so does not
7806 * need to adjust btrfs_space_info's bytes_may_use.
7807 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007808 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007809 goto skip_cow;
7810 }
7811 }
7812
7813 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007814 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007815 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007816 if (IS_ERR(em)) {
7817 ret = PTR_ERR(em);
7818 goto out;
7819 }
7820
7821 len = min(len, em->len - (start - em->start));
7822
7823skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007824 /*
7825 * Need to update the i_size under the extent lock so buffered
7826 * readers will get the updated i_size when we unlock.
7827 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007828 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007829 i_size_write(inode, start + len);
7830
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007831 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007832out:
7833 return ret;
7834}
7835
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007836static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7837 loff_t length, unsigned int flags, struct iomap *iomap,
7838 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007841 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007842 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307843 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007844 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007845 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007846 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007847 u64 len = length;
7848 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007849
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007850 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007851 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007852
Josef Bacikc3298612012-08-03 16:49:19 -04007853 lockstart = start;
7854 lockend = start + len - 1;
7855
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007856 /*
7857 * The generic stuff only does filemap_write_and_wait_range, which
7858 * isn't enough if we've written compressed pages to this area, so we
7859 * need to flush the dirty pages again to make absolutely sure that any
7860 * outstanding dirty pages are on disk.
7861 */
7862 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7863 &BTRFS_I(inode)->runtime_flags)) {
7864 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7865 start + length - 1);
7866 if (ret)
7867 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007868 }
7869
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007870 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7871 if (!dio_data)
7872 return -ENOMEM;
7873
7874 dio_data->length = length;
7875 if (write) {
7876 dio_data->reserve = round_up(length, fs_info->sectorsize);
7877 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7878 &dio_data->data_reserved,
7879 start, dio_data->reserve);
7880 if (ret) {
7881 extent_changeset_free(dio_data->data_reserved);
7882 kfree(dio_data);
7883 return ret;
7884 }
7885 }
7886 iomap->private = dio_data;
7887
7888
Josef Bacikeb838e72012-07-31 16:28:48 -04007889 /*
7890 * If this errors out it's because we couldn't invalidate pagecache for
7891 * this range and we need to fallback to buffered.
7892 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007893 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007894 ret = -ENOTBLK;
7895 goto err;
7896 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007897
Omar Sandoval39b07b52019-12-02 17:34:23 -08007898 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007899 if (IS_ERR(em)) {
7900 ret = PTR_ERR(em);
7901 goto unlock_err;
7902 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007903
7904 /*
7905 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7906 * io. INLINE is special, and we could probably kludge it in here, but
7907 * it's still buffered so for safety lets just fall back to the generic
7908 * buffered path.
7909 *
7910 * For COMPRESSED we _have_ to read the entire extent in so we can
7911 * decompress it, so there will be buffering required no matter what we
7912 * do, so go ahead and fallback to buffered.
7913 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007914 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007915 * to buffered IO. Don't blame me, this is the price we pay for using
7916 * the generic code.
7917 */
7918 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7919 em->block_start == EXTENT_MAP_INLINE) {
7920 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007921 ret = -ENOTBLK;
7922 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007923 }
7924
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007925 len = min(len, em->len - (start - em->start));
7926 if (write) {
7927 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7928 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007929 if (ret < 0)
7930 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007931 unlock_extents = true;
7932 /* Recalc len in case the new em is smaller than requested */
7933 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007934 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007935 /*
7936 * We need to unlock only the end area that we aren't using.
7937 * The rest is going to be unlocked by the endio routine.
7938 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007939 lockstart = start + len;
7940 if (lockstart < lockend)
7941 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007942 }
7943
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007944 if (unlock_extents)
7945 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7946 lockstart, lockend, &cached_state);
7947 else
7948 free_extent_state(cached_state);
7949
7950 /*
7951 * Translate extent map information to iomap.
7952 * We trim the extents (and move the addr) even though iomap code does
7953 * that, since we have locked only the parts we are performing I/O in.
7954 */
7955 if ((em->block_start == EXTENT_MAP_HOLE) ||
7956 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7957 iomap->addr = IOMAP_NULL_ADDR;
7958 iomap->type = IOMAP_HOLE;
7959 } else {
7960 iomap->addr = em->block_start + (start - em->start);
7961 iomap->type = IOMAP_MAPPED;
7962 }
7963 iomap->offset = start;
7964 iomap->bdev = fs_info->fs_devices->latest_bdev;
7965 iomap->length = len;
7966
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007967 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007968 iomap->flags |= IOMAP_F_ZONE_APPEND;
7969
Josef Bacik4b46fce2010-05-23 11:00:55 -04007970 free_extent_map(em);
7971
7972 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007973
7974unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007975 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7976 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007977err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007978 if (dio_data) {
7979 btrfs_delalloc_release_space(BTRFS_I(inode),
7980 dio_data->data_reserved, start,
7981 dio_data->reserve, true);
7982 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7983 extent_changeset_free(dio_data->data_reserved);
7984 kfree(dio_data);
7985 }
7986 return ret;
7987}
7988
7989static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7990 ssize_t written, unsigned int flags, struct iomap *iomap)
7991{
7992 int ret = 0;
7993 struct btrfs_dio_data *dio_data = iomap->private;
7994 size_t submitted = dio_data->submitted;
7995 const bool write = !!(flags & IOMAP_WRITE);
7996
7997 if (!write && (iomap->type == IOMAP_HOLE)) {
7998 /* If reading from a hole, unlock and return */
7999 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
8000 goto out;
8001 }
8002
8003 if (submitted < length) {
8004 pos += submitted;
8005 length -= submitted;
8006 if (write)
8007 __endio_write_update_ordered(BTRFS_I(inode), pos,
8008 length, false);
8009 else
8010 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
8011 pos + length - 1);
8012 ret = -ENOTBLK;
8013 }
8014
8015 if (write) {
8016 if (dio_data->reserve)
8017 btrfs_delalloc_release_space(BTRFS_I(inode),
8018 dio_data->data_reserved, pos,
8019 dio_data->reserve, true);
8020 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
8021 extent_changeset_free(dio_data->data_reserved);
8022 }
8023out:
8024 kfree(dio_data);
8025 iomap->private = NULL;
8026
Josef Bacikeb838e72012-07-31 16:28:48 -04008027 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008028}
8029
Omar Sandoval769b4f22020-04-16 14:46:22 -07008030static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8031{
8032 /*
8033 * This implies a barrier so that stores to dio_bio->bi_status before
8034 * this and loads of dio_bio->bi_status after this are fully ordered.
8035 */
8036 if (!refcount_dec_and_test(&dip->refs))
8037 return;
8038
Naohiro Aotacfe94442021-02-04 19:21:59 +09008039 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008040 __endio_write_update_ordered(BTRFS_I(dip->inode),
8041 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07008042 dip->bytes,
8043 !dip->dio_bio->bi_status);
8044 } else {
8045 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
8046 dip->logical_offset,
8047 dip->logical_offset + dip->bytes - 1);
8048 }
8049
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008050 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008051 kfree(dip);
8052}
8053
Omar Sandoval77d5d682020-04-16 14:46:25 -07008054static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8055 int mirror_num,
8056 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08008057{
Omar Sandoval77d5d682020-04-16 14:46:25 -07008058 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008060 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008061
Mike Christie37226b22016-06-05 14:31:52 -05008062 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008063
Omar Sandoval5c047a62020-04-16 14:46:24 -07008064 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08008065 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02008066 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008067
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008068 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07008069 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07008070 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008071 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08008072 return ret;
8073}
8074
Omar Sandoval769b4f22020-04-16 14:46:22 -07008075static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
8076 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008077 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08008078{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008079 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8080 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04008081 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8083 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07008084 struct bio_vec bvec;
8085 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008086 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008087 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008088 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008089
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008090 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
8091 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308092
Liu Bo17347ce2017-05-15 15:33:27 -07008093 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8094 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008095 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07008096 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008097 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008098 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07008099 bio_offset, bvec.bv_page,
8100 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008101 clean_io_failure(fs_info, failure_tree, io_tree,
8102 start, bvec.bv_page,
8103 btrfs_ino(BTRFS_I(inode)),
8104 pgoff);
8105 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08008106 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008107
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008108 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08008109 ret = btrfs_repair_one_sector(inode,
8110 &io_bio->bio,
8111 start - io_bio->logical,
8112 bvec.bv_page, pgoff,
8113 start, io_bio->mirror_num,
8114 submit_dio_repair_bio);
8115 if (ret)
8116 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008117 }
8118 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008119 ASSERT(bio_offset + sectorsize > bio_offset);
8120 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008123 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008124 return err;
8125}
8126
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008127static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08008128 const u64 offset, const u64 bytes,
8129 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008130{
Qu Wenruoe65f1522021-04-01 15:15:06 +08008131 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8132 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00008133}
8134
Qu Wenruo8896a082020-10-21 14:24:53 +08008135static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008136 struct bio *bio,
8137 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008138{
Qu Wenruo1941b642020-12-02 14:47:57 +08008139 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008140}
8141
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008142static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008143{
8144 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008145 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008146
Miao Xie8b110e32014-09-12 18:44:03 +08008147 if (err)
8148 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008149 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008150 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008151 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008152 bio->bi_iter.bi_size, err);
8153
Omar Sandoval769b4f22020-04-16 14:46:22 -07008154 if (bio_op(bio) == REQ_OP_READ) {
8155 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008156 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008157 }
8158
Omar Sandoval769b4f22020-04-16 14:46:22 -07008159 if (err)
8160 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008161
Naohiro Aota544d24f2021-02-04 19:22:06 +09008162 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8163
Miao Xiee65e1532010-11-22 03:04:43 +00008164 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008165 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008166}
8167
David Sterbad0ee3932018-03-08 14:35:48 +01008168static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8169 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008170{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008171 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008172 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008173 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008174 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008175
Liu Bo4c274bc2017-11-01 17:19:27 -06008176 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008177 if (async_submit)
8178 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8179
Josef Bacik5fd02042012-05-02 14:00:54 -04008180 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008181 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008182 if (ret)
8183 goto err;
8184 }
Miao Xiee65e1532010-11-22 03:04:43 +00008185
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008186 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008187 goto map;
8188
8189 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008190 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008191 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008192 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008193 } else if (write) {
8194 /*
8195 * If we aren't doing async submit, calculate the csum of the
8196 * bio now.
8197 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008198 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008199 if (ret)
8200 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008201 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008202 u64 csum_offset;
8203
8204 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008205 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008206 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008207 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008208 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008209map:
Chris Mason08635ba2019-07-10 12:28:14 -07008210 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008211err:
Miao Xiee65e1532010-11-22 03:04:43 +00008212 return ret;
8213}
8214
Omar Sandovalc36cac22020-04-16 14:46:13 -07008215/*
8216 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8217 * or ordered extents whether or not we submit any bios.
8218 */
8219static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8220 struct inode *inode,
8221 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008222{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008223 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008224 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8225 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008226 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008227
Omar Sandoval85879572020-04-16 14:46:21 -07008228 dip_size = sizeof(*dip);
8229 if (!write && csum) {
8230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008231 size_t nblocks;
8232
David Sterba265fdfa2020-07-01 21:19:09 +02008233 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008234 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008235 }
8236
8237 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008238 if (!dip)
8239 return NULL;
8240
Omar Sandovalc36cac22020-04-16 14:46:13 -07008241 dip->inode = inode;
8242 dip->logical_offset = file_offset;
8243 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008244 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008245 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008246 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008247 return dip;
8248}
8249
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008250static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8251 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008252{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008253 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008255 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8256 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008257 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008258 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008259 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008260 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008261 u64 submit_len;
Naohiro Aota42b5d732021-07-21 21:43:34 +09008262 u64 clone_offset = 0;
8263 u64 clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008264 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308265 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008266 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008267 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008268 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008269 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008270
Omar Sandovalc36cac22020-04-16 14:46:13 -07008271 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8272 if (!dip) {
8273 if (!write) {
8274 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8275 file_offset + dio_bio->bi_iter.bi_size - 1);
8276 }
8277 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008278 bio_endio(dio_bio);
8279 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008280 }
8281
Josef Bacik334c16d2020-10-16 11:29:14 -04008282 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008283 /*
8284 * Load the csums up front to reduce csum tree searches and
8285 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008286 *
8287 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008288 */
Qu Wenruo62751932020-12-02 14:48:06 +08008289 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008290 if (status != BLK_STS_OK)
8291 goto out_err;
8292 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008293
Omar Sandoval769b4f22020-04-16 14:46:22 -07008294 start_sector = dio_bio->bi_iter.bi_sector;
8295 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008296
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008297 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008298 logical = start_sector << 9;
8299 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8300 if (IS_ERR(em)) {
8301 status = errno_to_blk_status(PTR_ERR(em));
8302 em = NULL;
8303 goto out_err_em;
8304 }
8305 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008306 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008307 if (ret) {
8308 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008309 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008310 }
Omar Sandoval769b4f22020-04-16 14:46:22 -07008311
Naohiro Aota42b5d732021-07-21 21:43:34 +09008312 clone_len = min(submit_len, geom.len);
8313 ASSERT(clone_len <= UINT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008314
Liu Bo725130b2017-05-16 09:51:39 -07008315 /*
8316 * This will never fail as it's passing GPF_NOFS and
8317 * the allocation is backed by btrfs_bioset.
8318 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008319 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008320 bio->bi_private = dip;
8321 bio->bi_end_io = btrfs_end_dio_bio;
8322 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008323
Naohiro Aota544d24f2021-02-04 19:22:06 +09008324 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8325 status = extract_ordered_extent(BTRFS_I(inode), bio,
8326 file_offset);
8327 if (status) {
8328 bio_put(bio);
8329 goto out_err;
8330 }
8331 }
8332
Liu Bo725130b2017-05-16 09:51:39 -07008333 ASSERT(submit_len >= clone_len);
8334 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008335
Liu Bo725130b2017-05-16 09:51:39 -07008336 /*
8337 * Increase the count before we submit the bio so we know
8338 * the end IO handler won't happen before we increase the
8339 * count. Otherwise, the dip might get freed before we're
8340 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008341 *
8342 * We transfer the initial reference to the last bio, so we
8343 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008344 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008345 if (submit_len > 0) {
8346 refcount_inc(&dip->refs);
8347 /*
8348 * If we are submitting more than one bio, submit them
8349 * all asynchronously. The exception is RAID 5 or 6, as
8350 * asynchronous checksums make it difficult to collect
8351 * full stripe writes.
8352 */
8353 if (!raid56)
8354 async_submit = 1;
8355 }
Miao Xiee65e1532010-11-22 03:04:43 +00008356
David Sterbad0ee3932018-03-08 14:35:48 +01008357 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008358 async_submit);
8359 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008360 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008361 if (submit_len > 0)
8362 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008363 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008364 }
Liu Bo725130b2017-05-16 09:51:39 -07008365
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008366 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008367 clone_offset += clone_len;
8368 start_sector += clone_len >> 9;
8369 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008370
8371 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008372 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008373 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008374
Michal Rostecki42034312021-01-27 14:57:27 +01008375out_err_em:
8376 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008377out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008378 dip->dio_bio->bi_status = status;
8379 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008380
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008381 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008382}
8383
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008384const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008385 .iomap_begin = btrfs_dio_iomap_begin,
8386 .iomap_end = btrfs_dio_iomap_end,
8387};
8388
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008389const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008390 .submit_io = btrfs_submit_direct,
8391};
8392
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008393static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008394 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008395{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008396 int ret;
8397
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008398 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008399 if (ret)
8400 return ret;
8401
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008402 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008403}
8404
Chris Mason9ebefb182007-06-15 13:50:00 -04008405int btrfs_readpage(struct file *file, struct page *page)
8406{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008407 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8408 u64 start = page_offset(page);
8409 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008410 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008411 int ret;
8412
Nikolay Borisov0f208812020-09-14 14:39:16 +03008413 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8414
Qu Wenruo390ed292021-04-14 16:42:15 +08008415 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8416 if (bio_ctrl.bio)
8417 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008418 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008419}
Chris Mason1832a6d2007-12-21 16:27:21 -05008420
Chris Mason39279cc2007-06-12 06:35:45 -04008421static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8422{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008423 struct inode *inode = page->mapping->host;
8424 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008425
8426 if (current->flags & PF_MEMALLOC) {
8427 redirty_page_for_writepage(wbc, page);
8428 unlock_page(page);
8429 return 0;
8430 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008431
8432 /*
8433 * If we are under memory pressure we will call this directly from the
8434 * VM, we need to make sure we have the inode referenced for the ordered
8435 * extent. If not just return like we didn't do anything.
8436 */
8437 if (!igrab(inode)) {
8438 redirty_page_for_writepage(wbc, page);
8439 return AOP_WRITEPAGE_ACTIVATE;
8440 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008441 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008442 btrfs_add_delayed_iput(inode);
8443 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008444}
Chris Mason39279cc2007-06-12 06:35:45 -04008445
Eric Sandeen48a3b632013-04-25 20:41:01 +00008446static int btrfs_writepages(struct address_space *mapping,
8447 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008448{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008449 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008450}
8451
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008452static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008453{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008454 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008455}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008456
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008457/*
8458 * For releasepage() and invalidatepage() we have a race window where
8459 * end_page_writeback() is called but the subpage spinlock is not yet released.
8460 * If we continue to release/invalidate the page, we could cause use-after-free
8461 * for subpage spinlock. So this function is to spin and wait for subpage
8462 * spinlock.
8463 */
8464static void wait_subpage_spinlock(struct page *page)
8465{
8466 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8467 struct btrfs_subpage *subpage;
8468
8469 if (fs_info->sectorsize == PAGE_SIZE)
8470 return;
8471
8472 ASSERT(PagePrivate(page) && page->private);
8473 subpage = (struct btrfs_subpage *)page->private;
8474
8475 /*
8476 * This may look insane as we just acquire the spinlock and release it,
8477 * without doing anything. But we just want to make sure no one is
8478 * still holding the subpage spinlock.
8479 * And since the page is not dirty nor writeback, and we have page
8480 * locked, the only possible way to hold a spinlock is from the endio
8481 * function to clear page writeback.
8482 *
8483 * Here we just acquire the spinlock so that all existing callers
8484 * should exit and we're safe to release/invalidate the page.
8485 */
8486 spin_lock_irq(&subpage->lock);
8487 spin_unlock_irq(&subpage->lock);
8488}
8489
Chris Masone6dcd2d2008-07-17 12:53:50 -04008490static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008491{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008492 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008493
8494 if (ret == 1) {
8495 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008496 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008497 }
Chris Masona52d9a82007-08-27 16:49:44 -04008498 return ret;
8499}
Chris Mason39279cc2007-06-12 06:35:45 -04008500
Chris Masone6dcd2d2008-07-17 12:53:50 -04008501static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8502{
Chris Mason98509cf2008-09-11 15:51:43 -04008503 if (PageWriteback(page) || PageDirty(page))
8504 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008505 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008506}
8507
Roman Gushchinf8e66082020-03-04 16:57:35 -08008508#ifdef CONFIG_MIGRATION
8509static int btrfs_migratepage(struct address_space *mapping,
8510 struct page *newpage, struct page *page,
8511 enum migrate_mode mode)
8512{
8513 int ret;
8514
8515 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8516 if (ret != MIGRATEPAGE_SUCCESS)
8517 return ret;
8518
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008519 if (page_has_private(page))
8520 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008521
Qu Wenruof57ad932021-04-07 19:22:13 +08008522 if (PageOrdered(page)) {
8523 ClearPageOrdered(page);
8524 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008525 }
8526
8527 if (mode != MIGRATE_SYNC_NO_COPY)
8528 migrate_page_copy(newpage, page);
8529 else
8530 migrate_page_states(newpage, page);
8531 return MIGRATEPAGE_SUCCESS;
8532}
8533#endif
8534
Lukas Czernerd47992f2013-05-21 23:17:23 -04008535static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8536 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008537{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008538 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008539 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008540 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008541 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008542 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008543 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008544 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008545 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008546
Chris Mason8b62b722009-09-02 16:53:46 -04008547 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008548 * We have page locked so no new ordered extent can be created on this
8549 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008550 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008551 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008552 * Furthermore, endio function won't skip page which has Ordered
8553 * (Private2) already cleared, so it's possible for endio and
8554 * invalidatepage to do the same ordered extent accounting twice
8555 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008556 *
8557 * So here we wait for any submitted bios to finish, so that we won't
8558 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008559 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008560 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008561 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008562
Qu Wenruobcd77452021-05-31 16:50:55 +08008563 /*
8564 * For subpage case, we have call sites like
8565 * btrfs_punch_hole_lock_range() which passes range not aligned to
8566 * sectorsize.
8567 * If the range doesn't cover the full page, we don't need to and
8568 * shouldn't clear page extent mapped, as page->private can still
8569 * record subpage dirty bits for other part of the range.
8570 *
8571 * For cases that can invalidate the full even the range doesn't
8572 * cover the full page, like invalidating the last page, we're
8573 * still safe to wait for ordered extent to finish.
8574 */
8575 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008576 btrfs_releasepage(page, GFP_NOFS);
8577 return;
8578 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008579
8580 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008581 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008582
Qu Wenruo3b835842021-04-06 19:54:53 +08008583 cur = page_start;
8584 while (cur < page_end) {
8585 struct btrfs_ordered_extent *ordered;
8586 bool delete_states;
8587 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008588 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008589
8590 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8591 page_end + 1 - cur);
8592 if (!ordered) {
8593 range_end = page_end;
8594 /*
8595 * No ordered extent covering this range, we are safe
8596 * to delete all extent states in the range.
8597 */
8598 delete_states = true;
8599 goto next;
8600 }
8601 if (ordered->file_offset > cur) {
8602 /*
8603 * There is a range between [cur, oe->file_offset) not
8604 * covered by any ordered extent.
8605 * We are safe to delete all extent states, and handle
8606 * the ordered extent in the next iteration.
8607 */
8608 range_end = ordered->file_offset - 1;
8609 delete_states = true;
8610 goto next;
8611 }
8612
8613 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8614 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008615 ASSERT(range_end + 1 - cur < U32_MAX);
8616 range_len = range_end + 1 - cur;
8617 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008618 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008619 * If Ordered (Private2) is cleared, it means endio has
8620 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008621 * We can't delete the extent states as
8622 * btrfs_finish_ordered_io() may still use some of them.
8623 */
8624 delete_states = false;
8625 goto next;
8626 }
Qu Wenruob945a462021-05-31 16:50:46 +08008627 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008628
Chris Masoneb84ae02008-07-17 13:53:27 -04008629 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008630 * IO on this page will never be started, so we need to account
8631 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8632 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008633 *
8634 * This will also unlock the range for incoming
8635 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008636 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008637 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008638 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008639 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008640 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008641 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008642
8643 spin_lock_irq(&inode->ordered_tree.lock);
8644 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8645 ordered->truncated_len = min(ordered->truncated_len,
8646 cur - ordered->file_offset);
8647 spin_unlock_irq(&inode->ordered_tree.lock);
8648
8649 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008650 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008651 btrfs_finish_ordered_io(ordered);
8652 /*
8653 * The ordered extent has finished, now we're again
8654 * safe to delete all extent states of the range.
8655 */
8656 delete_states = true;
8657 } else {
8658 /*
8659 * btrfs_finish_ordered_io() will get executed by endio
8660 * of other pages, thus we can't delete extent states
8661 * anymore
8662 */
8663 delete_states = false;
8664 }
8665next:
8666 if (ordered)
8667 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008668 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008669 * Qgroup reserved space handler
8670 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008671 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008672 * 1) Already written to disk or bio already finished
8673 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8674 * Qgroup will be handled by its qgroup_record then.
8675 * btrfs_qgroup_free_data() call will do nothing here.
8676 *
8677 * 2) Not written to disk yet
8678 * Then btrfs_qgroup_free_data() call will clear the
8679 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8680 * reserved data space.
8681 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008682 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008683 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008684 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008685 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008686 EXTENT_DELALLOC | EXTENT_UPTODATE |
8687 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008688 delete_states, &cached_state);
8689 }
8690 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008691 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008692 /*
8693 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008694 * should not have Ordered (Private2) anymore, or the above iteration
8695 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008696 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008697 ASSERT(!PageOrdered(page));
Qu Wenruo3b835842021-04-06 19:54:53 +08008698 if (!inode_evicting)
8699 __btrfs_releasepage(page, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04008700 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008701 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008702}
8703
Chris Mason9ebefb182007-06-15 13:50:00 -04008704/*
8705 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8706 * called from a page fault handler when a page is first dirtied. Hence we must
8707 * be careful to check for EOF conditions here. We set the page up correctly
8708 * for a written page which means we get ENOSPC checking when writing into
8709 * holes and correct delalloc and unwritten extent mapping on filesystems that
8710 * support these features.
8711 *
8712 * We are not allowed to take the i_mutex here so we have to play games to
8713 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008714 * truncate_setsize() writes the inode size before removing pages, once we have
8715 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008716 * beyond EOF, then the page is guaranteed safe against truncation until we
8717 * unlock the page.
8718 */
Souptick Joardera528a242018-06-06 19:54:44 +05308719vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008720{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008721 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008722 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008723 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008724 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8725 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008726 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008727 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008728 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008729 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308730 vm_fault_t ret;
8731 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008732 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308733 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008734 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008735 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308736 u64 end;
8737
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008738 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008739
Jan Karab2b5ef52012-06-12 16:20:45 +02008740 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008741 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008742 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308743 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008744
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308745 /*
8746 * Reserving delalloc space after obtaining the page lock can lead to
8747 * deadlock. For example, if a dirty page is locked by this function
8748 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8749 * dirty page write out, then the btrfs_writepage() function could
8750 * end up waiting indefinitely to get a lock on the page currently
8751 * being processed by btrfs_page_mkwrite() function.
8752 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008753 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8754 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308755 if (!ret2) {
8756 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008757 reserved = 1;
8758 }
Souptick Joardera528a242018-06-06 19:54:44 +05308759 if (ret2) {
8760 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008761 if (reserved)
8762 goto out;
8763 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008764 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008765
Nick Piggin56a76f82009-03-31 15:23:23 -07008766 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008767again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008768 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008769 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008770 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008771
Chris Mason9ebefb182007-06-15 13:50:00 -04008772 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008774 /* page got truncated out from underneath us */
8775 goto out_unlock;
8776 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008777 wait_on_page_writeback(page);
8778
David Sterbaff13db42015-12-03 14:30:40 +01008779 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008780 ret2 = set_page_extent_mapped(page);
8781 if (ret2 < 0) {
8782 ret = vmf_error(ret2);
8783 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8784 goto out_unlock;
8785 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008786
Chris Masoneb84ae02008-07-17 13:53:27 -04008787 /*
8788 * we can't set the delalloc bits if there are pending ordered
8789 * extents. Drop our locks and wait for them to finish
8790 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008791 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8792 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008793 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008794 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008795 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008796 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008797 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008798 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008799 btrfs_put_ordered_extent(ordered);
8800 goto again;
8801 }
8802
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008803 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008804 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008805 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008806 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308807 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008808 btrfs_delalloc_release_space(BTRFS_I(inode),
8809 data_reserved, page_start,
8810 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308811 }
8812 }
8813
Josef Bacikfbf19082009-10-01 17:10:23 -04008814 /*
Liu Bo54160342016-12-13 12:15:19 -08008815 * page_mkwrite gets called when the page is firstly dirtied after it's
8816 * faulted in, but write(2) could also dirty a page and set delalloc
8817 * bits, thus in this case for space account reason, we still need to
8818 * clear any delalloc bits within this page range since we have to
8819 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008820 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308821 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008822 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8823 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008824
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008825 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008826 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308827 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008828 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008829 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008830 ret = VM_FAULT_SIGBUS;
8831 goto out_unlock;
8832 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008833
8834 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008835 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008836 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008837 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008838 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008839
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008840 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008841 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008842 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008843 }
Chris Mason247e7432008-07-17 12:53:51 -04008844 ClearPageChecked(page);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008845 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8846 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008847
Filipe Mananabc0939f2021-02-23 12:08:48 +00008848 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008849
David Sterbae43bbe52017-12-12 21:43:52 +01008850 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008851 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008852
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008853 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8854 sb_end_pagefault(inode->i_sb);
8855 extent_changeset_free(data_reserved);
8856 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008857
8858out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008859 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008860 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008861out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008862 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008863 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008864 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008865out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008866 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008867 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008868 return ret;
8869}
8870
Filipe Manana213e8c52018-02-06 20:40:31 +00008871static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008873 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008874 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008875 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008876 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008877 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008878 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008879 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008880 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008881
Filipe Manana213e8c52018-02-06 20:40:31 +00008882 if (!skip_writeback) {
8883 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8884 (u64)-1);
8885 if (ret)
8886 return ret;
8887 }
Chris Mason39279cc2007-06-12 06:35:45 -04008888
Josef Bacikfcb80c22011-05-03 10:40:22 -04008889 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008890 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8891 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008892 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008893 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008894 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008895 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008896 * be free'd up by the truncate operation, but also have some slack
8897 * space reserved in case it uses space during the truncate (thank you
8898 * very much snapshotting).
8899 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008900 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008901 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008902 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008903 * doesn't end up using space reserved for updating the inode. We also
8904 * need to be able to stop the transaction and start a new one, which
8905 * means we need to be able to update the inode several times, and we
8906 * have no idea of knowing how many times that will be, so we can't just
8907 * reserve 1 item for the entirety of the operation, so that has to be
8908 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008909 *
8910 * So that leaves us with
8911 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008912 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008913 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008914 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008915 * updating the inode.
8916 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008917 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008918 if (!rsv)
8919 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008920 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008921 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008922
Josef Bacik907cbce2011-08-08 13:46:15 -04008923 /*
Josef Bacik07127182011-08-19 10:29:59 -04008924 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008925 * 1 for updating the inode.
8926 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008927 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008928 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008929 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008930 goto out;
8931 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008932
Josef Bacik907cbce2011-08-08 13:46:15 -04008933 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008934 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008935 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008936 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008937
Josef Bacikca7e70f2012-08-27 17:48:15 -04008938 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008939
Yan, Zheng80825102009-11-12 09:35:36 +00008940 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008941 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008942 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008943 BTRFS_EXTENT_DATA_KEY,
8944 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008945 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008946 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008947 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008948
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008949 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008950 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008951 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008952
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008953 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008954 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008955
8956 trans = btrfs_start_transaction(root, 2);
8957 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008958 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008959 trans = NULL;
8960 break;
8961 }
8962
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008963 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008964 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008965 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008966 BUG_ON(ret); /* shouldn't happen */
8967 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008968 }
8969
Josef Bacikddfae632017-10-19 14:16:02 -04008970 /*
8971 * We can't call btrfs_truncate_block inside a trans handle as we could
8972 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8973 * we've truncated everything except the last little bit, and can do
8974 * btrfs_truncate_block and then update the disk_i_size.
8975 */
8976 if (ret == NEED_TRUNCATE_BLOCK) {
8977 btrfs_end_transaction(trans);
8978 btrfs_btree_balance_dirty(fs_info);
8979
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008980 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008981 if (ret)
8982 goto out;
8983 trans = btrfs_start_transaction(root, 1);
8984 if (IS_ERR(trans)) {
8985 ret = PTR_ERR(trans);
8986 goto out;
8987 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008988 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008989 }
8990
Chris Mason917c16b2011-11-08 14:49:59 -05008991 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008992 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008993
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008994 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008995 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008996 if (ret2 && !ret)
8997 ret = ret2;
8998
8999 ret2 = btrfs_end_transaction(trans);
9000 if (ret2 && !ret)
9001 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009002 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009003 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009004out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009005 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01009006 /*
9007 * So if we truncate and then write and fsync we normally would just
9008 * write the extents that changed, which is a problem if we need to
9009 * first truncate that entire inode. So set this flag so we write out
9010 * all of the extents in the inode to the sync log so we're completely
9011 * safe.
9012 *
9013 * If no extents were dropped or trimmed we don't need to force the next
9014 * fsync to truncate all the inode's items from the log and re-log them
9015 * all. This means the truncate operation did not change the file size,
9016 * or changed it to a smaller size but there was only an implicit hole
9017 * between the old i_size and the new i_size, and there were no prealloc
9018 * extents beyond i_size to drop.
9019 */
9020 if (extents_found > 0)
9021 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009022
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009023 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009024}
9025
Sven Wegener3b963622008-06-09 21:57:42 -04009026/*
Chris Masond352ac62008-09-29 15:18:18 -04009027 * create a new subvolume directory/inode (helper for the ioctl).
9028 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009029int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009030 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02009031 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04009032{
Chris Mason39279cc2007-06-12 06:35:45 -04009033 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009034 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009035 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02009036 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04009037
Nikolay Borisov23125102020-12-07 17:32:36 +02009038 err = btrfs_get_free_objectid(new_root, &ino);
9039 if (err < 0)
9040 return err;
9041
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02009042 inode = btrfs_new_inode(trans, new_root, &init_user_ns, NULL, "..", 2,
9043 ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009044 S_IFDIR | (~current_umask() & S_IRWXUGO),
9045 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009046 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009047 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009048 inode->i_op = &btrfs_dir_inode_operations;
9049 inode->i_fop = &btrfs_dir_file_operations;
9050
Miklos Szeredibfe86842011-10-28 14:13:29 +02009051 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009052 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009053 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009054
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009055 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9056 if (err)
9057 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009058 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009059 new_root->root_key.objectid, err);
9060
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009061 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009062
Yan, Zheng76dda932009-09-21 16:00:26 -04009063 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009064 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009065}
9066
Chris Mason39279cc2007-06-12 06:35:45 -04009067struct inode *btrfs_alloc_inode(struct super_block *sb)
9068{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009069 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009070 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009071 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009072
David Sterba712e36c2017-10-31 17:08:27 +01009073 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009074 if (!ei)
9075 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009076
9077 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009078 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009079 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009080 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009081 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009082 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009083 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009084 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009085 ei->disk_i_size = 0;
9086 ei->flags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -07009087 ei->ro_flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009088 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009089 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009090 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009091 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01009092 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009093 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009094
Josef Bacik9e0baf62011-07-15 15:16:44 +00009095 spin_lock_init(&ei->lock);
9096 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009097 if (sb->s_magic != BTRFS_TEST_MAGIC)
9098 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9099 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009100 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009101 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009102 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009103
Miao Xie16cdcec2011-04-22 18:12:22 +08009104 ei->delayed_node = NULL;
9105
chandan r9cc97d62012-07-04 12:48:07 +05309106 ei->i_otime.tv_sec = 0;
9107 ei->i_otime.tv_nsec = 0;
9108
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009109 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009110 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009111 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9112 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9113 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05009114 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9115 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01009116 ei->io_tree.track_uptodate = true;
9117 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009118 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009119 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009120 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009121 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009122 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009123 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05009124 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009125
9126 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009127}
9128
Josef Bacikaaedb552013-10-11 14:44:09 -04009129#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9130void btrfs_test_destroy_inode(struct inode *inode)
9131{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009132 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009133 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9134}
9135#endif
9136
Al Viro26602ca2019-04-10 15:14:41 -04009137void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009138{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009139 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9140}
9141
Nikolay Borisov633cc812020-09-18 12:15:49 +03009142void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009143{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009144 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03009145 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9146 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009147
Nikolay Borisov633cc812020-09-18 12:15:49 +03009148 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9149 WARN_ON(vfs_inode->i_data.nrpages);
9150 WARN_ON(inode->block_rsv.reserved);
9151 WARN_ON(inode->block_rsv.size);
9152 WARN_ON(inode->outstanding_extents);
9153 WARN_ON(inode->delalloc_bytes);
9154 WARN_ON(inode->new_delalloc_bytes);
9155 WARN_ON(inode->csum_bytes);
9156 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009157
Chris Mason5a3f23d2009-03-31 13:27:11 -04009158 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009159 * This can happen where we create an inode, but somebody else also
9160 * created the same inode and we need to destroy the one we already
9161 * created.
9162 */
9163 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009164 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009165
Chris Masond3977122009-01-05 21:25:51 -05009166 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009167 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009168 if (!ordered)
9169 break;
9170 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009171 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009172 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08009173 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03009174 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009175 btrfs_put_ordered_extent(ordered);
9176 btrfs_put_ordered_extent(ordered);
9177 }
9178 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009179 btrfs_qgroup_check_reserved_leak(inode);
9180 inode_tree_del(inode);
9181 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9182 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9183 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04009184}
9185
Al Viro45321ac2010-06-07 13:43:19 -04009186int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009187{
9188 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009189
Naohiro Aota6379ef92013-06-06 09:56:34 +00009190 if (root == NULL)
9191 return 1;
9192
Liu Bofa6ac872013-02-20 14:10:23 +00009193 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009194 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009195 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009196 else
Al Viro45321ac2010-06-07 13:43:19 -04009197 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009198}
9199
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009200static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009201{
9202 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9203
9204 inode_init_once(&ei->vfs_inode);
9205}
9206
David Sterbae67c7182018-02-19 17:24:18 +01009207void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009208{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009209 /*
9210 * Make sure all delayed rcu free inodes are flushed before we
9211 * destroy cache.
9212 */
9213 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009214 kmem_cache_destroy(btrfs_inode_cachep);
9215 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009216 kmem_cache_destroy(btrfs_path_cachep);
9217 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009218 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009219}
9220
Liu Bof5c29bd2017-11-02 17:21:50 -06009221int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009222{
David Sterba837e1972012-09-07 03:00:48 -06009223 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009224 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009225 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9226 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009227 if (!btrfs_inode_cachep)
9228 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009229
David Sterba837e1972012-09-07 03:00:48 -06009230 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009231 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009232 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009233 if (!btrfs_trans_handle_cachep)
9234 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009235
David Sterba837e1972012-09-07 03:00:48 -06009236 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009237 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009238 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009239 if (!btrfs_path_cachep)
9240 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009241
David Sterba837e1972012-09-07 03:00:48 -06009242 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009243 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009244 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009245 if (!btrfs_free_space_cachep)
9246 goto fail;
9247
Christophe Leroy3acd4852019-08-21 15:05:55 +00009248 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9249 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009250 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009251 if (!btrfs_free_space_bitmap_cachep)
9252 goto fail;
9253
Chris Mason39279cc2007-06-12 06:35:45 -04009254 return 0;
9255fail:
9256 btrfs_destroy_cachep();
9257 return -ENOMEM;
9258}
9259
Christian Brauner549c7292021-01-21 14:19:43 +01009260static int btrfs_getattr(struct user_namespace *mnt_userns,
9261 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009262 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009263{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009264 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009265 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009266 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009267 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009268 u32 bi_flags = BTRFS_I(inode)->flags;
Boris Burkov14605402021-06-30 13:01:49 -07009269 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
Yonghong Song04a87e32017-05-12 15:07:43 -07009270
9271 stat->result_mask |= STATX_BTIME;
9272 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9273 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9274 if (bi_flags & BTRFS_INODE_APPEND)
9275 stat->attributes |= STATX_ATTR_APPEND;
9276 if (bi_flags & BTRFS_INODE_COMPRESS)
9277 stat->attributes |= STATX_ATTR_COMPRESSED;
9278 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9279 stat->attributes |= STATX_ATTR_IMMUTABLE;
9280 if (bi_flags & BTRFS_INODE_NODUMP)
9281 stat->attributes |= STATX_ATTR_NODUMP;
Boris Burkov14605402021-06-30 13:01:49 -07009282 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9283 stat->attributes |= STATX_ATTR_VERITY;
Yonghong Song04a87e32017-05-12 15:07:43 -07009284
9285 stat->attributes_mask |= (STATX_ATTR_APPEND |
9286 STATX_ATTR_COMPRESSED |
9287 STATX_ATTR_IMMUTABLE |
9288 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009289
Christian Braunerc020d2e2021-07-27 12:48:43 +02009290 generic_fillattr(mnt_userns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009291 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009292
9293 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009294 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009295 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009296 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009297 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009298 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009299 return 0;
9300}
9301
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009302static int btrfs_rename_exchange(struct inode *old_dir,
9303 struct dentry *old_dentry,
9304 struct inode *new_dir,
9305 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009307 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009308 struct btrfs_trans_handle *trans;
9309 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009310 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009311 struct inode *new_inode = new_dentry->d_inode;
9312 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009313 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009314 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9315 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009316 u64 old_idx = 0;
9317 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009318 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009319 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009320 bool root_log_pinned = false;
9321 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009322 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009323
NeilBrown3f79f6f2021-08-06 14:26:24 +10009324 /*
9325 * For non-subvolumes allow exchange only within one subvolume, in the
9326 * same inode namespace. Two subvolumes (represented as directory) can
9327 * be exchanged as they're a logical link and have a fixed inode number.
9328 */
9329 if (root != dest &&
9330 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9331 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009332 return -EXDEV;
9333
9334 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009335 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9336 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009337 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009338
9339 /*
9340 * We want to reserve the absolute worst case amount of items. So if
9341 * both inodes are subvols and we need to unlink them then that would
9342 * require 4 item modifications, but if they are both normal inodes it
9343 * would require 5 item modifications, so we'll assume their normal
9344 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9345 * should cover the worst case number of items we'll modify.
9346 */
9347 trans = btrfs_start_transaction(root, 12);
9348 if (IS_ERR(trans)) {
9349 ret = PTR_ERR(trans);
9350 goto out_notrans;
9351 }
9352
Josef Bacik00aa8e82021-03-12 15:25:02 -05009353 if (dest != root) {
9354 ret = btrfs_record_root_in_trans(trans, dest);
9355 if (ret)
9356 goto out_fail;
9357 }
Josef Bacik3e174092019-11-15 15:43:06 -05009358
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009359 /*
9360 * We need to find a free sequence number both in the source and
9361 * in the destination directory for the exchange.
9362 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009363 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009364 if (ret)
9365 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009366 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009367 if (ret)
9368 goto out_fail;
9369
9370 BTRFS_I(old_inode)->dir_index = 0ULL;
9371 BTRFS_I(new_inode)->dir_index = 0ULL;
9372
9373 /* Reference for the source. */
9374 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9375 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009376 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009377 } else {
9378 ret = btrfs_insert_inode_ref(trans, dest,
9379 new_dentry->d_name.name,
9380 new_dentry->d_name.len,
9381 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009382 btrfs_ino(BTRFS_I(new_dir)),
9383 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009384 if (ret)
9385 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009386 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009387 }
9388
9389 /* And now for the dest. */
9390 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9391 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009392 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009393 } else {
9394 ret = btrfs_insert_inode_ref(trans, root,
9395 old_dentry->d_name.name,
9396 old_dentry->d_name.len,
9397 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009398 btrfs_ino(BTRFS_I(old_dir)),
9399 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009400 if (ret) {
9401 if (need_abort)
9402 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009403 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009404 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009405 }
9406
9407 /* Update inode version and ctime/mtime. */
9408 inode_inc_iversion(old_dir);
9409 inode_inc_iversion(new_dir);
9410 inode_inc_iversion(old_inode);
9411 inode_inc_iversion(new_inode);
9412 old_dir->i_ctime = old_dir->i_mtime = ctime;
9413 new_dir->i_ctime = new_dir->i_mtime = ctime;
9414 old_inode->i_ctime = ctime;
9415 new_inode->i_ctime = ctime;
9416
9417 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009418 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9419 BTRFS_I(old_inode), 1);
9420 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9421 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009422 }
9423
Filipe Mananabd54f382021-07-27 11:24:45 +01009424 /*
9425 * Now pin the logs of the roots. We do it to ensure that no other task
9426 * can sync the logs while we are in progress with the rename, because
9427 * that could result in an inconsistency in case any of the inodes that
9428 * are part of this rename operation were logged before.
9429 *
9430 * We pin the logs even if at this precise moment none of the inodes was
9431 * logged before. This is because right after we checked for that, some
9432 * other task fsyncing some other inode not involved with this rename
9433 * operation could log that one of our inodes exists.
9434 *
9435 * We don't need to pin the logs before the above calls to
9436 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9437 */
9438 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9439 btrfs_pin_log_trans(root);
9440 root_log_pinned = true;
9441 }
9442 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9443 btrfs_pin_log_trans(dest);
9444 dest_log_pinned = true;
9445 }
9446
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 /* src is a subvolume */
9448 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009449 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009451 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9452 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009453 old_dentry->d_name.name,
9454 old_dentry->d_name.len);
9455 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009456 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 }
9458 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009459 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 goto out_fail;
9461 }
9462
9463 /* dest is a subvolume */
9464 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009465 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009467 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9468 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 new_dentry->d_name.name,
9470 new_dentry->d_name.len);
9471 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009472 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473 }
9474 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009475 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009476 goto out_fail;
9477 }
9478
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009479 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 new_dentry->d_name.name,
9481 new_dentry->d_name.len, 0, old_idx);
9482 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009483 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009484 goto out_fail;
9485 }
9486
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009487 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488 old_dentry->d_name.name,
9489 old_dentry->d_name.len, 0, new_idx);
9490 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009491 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492 goto out_fail;
9493 }
9494
9495 if (old_inode->i_nlink == 1)
9496 BTRFS_I(old_inode)->dir_index = old_idx;
9497 if (new_inode->i_nlink == 1)
9498 BTRFS_I(new_inode)->dir_index = new_idx;
9499
Filipe Manana86e8aa02016-05-05 02:02:27 +01009500 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009501 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9502 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009504 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009506 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009507 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9508 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009510 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009511 }
9512out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009513 /*
9514 * If we have pinned a log and an error happened, we unpin tasks
9515 * trying to sync the log and force them to fallback to a transaction
9516 * commit if the log currently contains any of the inodes involved in
9517 * this rename operation (to ensure we do not persist a log with an
9518 * inconsistent state for any of these inodes or leading to any
9519 * inconsistencies when replayed). If the transaction was aborted, the
9520 * abortion reason is propagated to userspace when attempting to commit
9521 * the transaction. If the log does not contain any of these inodes, we
9522 * allow the tasks to sync it.
9523 */
9524 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009525 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9526 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9527 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana1c167b82021-07-29 15:28:34 +01009528 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
David Sterba90787762019-03-20 13:28:05 +01009529 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009530
9531 if (root_log_pinned) {
9532 btrfs_end_log_trans(root);
9533 root_log_pinned = false;
9534 }
9535 if (dest_log_pinned) {
9536 btrfs_end_log_trans(dest);
9537 dest_log_pinned = false;
9538 }
9539 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009540 ret2 = btrfs_end_transaction(trans);
9541 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009543 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9544 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009545 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546
9547 return ret;
9548}
9549
9550static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9551 struct btrfs_root *root,
Christian Braunerca072742021-07-27 12:48:42 +02009552 struct user_namespace *mnt_userns,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 struct inode *dir,
9554 struct dentry *dentry)
9555{
9556 int ret;
9557 struct inode *inode;
9558 u64 objectid;
9559 u64 index;
9560
Nikolay Borisov543068a2020-12-07 17:32:33 +02009561 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 if (ret)
9563 return ret;
9564
Christian Braunerca072742021-07-27 12:48:42 +02009565 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 dentry->d_name.name,
9567 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009568 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 objectid,
9570 S_IFCHR | WHITEOUT_MODE,
9571 &index);
9572
9573 if (IS_ERR(inode)) {
9574 ret = PTR_ERR(inode);
9575 return ret;
9576 }
9577
9578 inode->i_op = &btrfs_special_inode_operations;
9579 init_special_inode(inode, inode->i_mode,
9580 WHITEOUT_DEV);
9581
9582 ret = btrfs_init_inode_security(trans, inode, dir,
9583 &dentry->d_name);
9584 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009585 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009587 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9588 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009590 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009592 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009593out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009595 if (ret)
9596 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 iput(inode);
9598
Filipe Mananac9901612016-05-05 01:41:57 +01009599 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600}
9601
Christian Braunerca072742021-07-27 12:48:42 +02009602static int btrfs_rename(struct user_namespace *mnt_userns,
9603 struct inode *old_dir, struct dentry *old_dentry,
9604 struct inode *new_dir, struct dentry *new_dentry,
9605 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009606{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009607 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009608 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009609 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009610 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9611 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009612 struct inode *new_inode = d_inode(new_dentry);
9613 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009614 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009615 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009616 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009617 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009618 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009619
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009620 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009621 return -EPERM;
9622
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009623 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009624 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009625 return -EXDEV;
9626
Li Zefan33345d012011-04-20 10:31:50 +08009627 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009628 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009629 return -ENOTEMPTY;
9630
Chris Mason39279cc2007-06-12 06:35:45 -04009631 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009632 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009633 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009634
9635
9636 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009637 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009638 new_dentry->d_name.name,
9639 new_dentry->d_name.len);
9640
9641 if (ret) {
9642 if (ret == -EEXIST) {
9643 /* we shouldn't get
9644 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309645 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009646 return ret;
9647 }
9648 } else {
9649 /* maybe -EOVERFLOW */
9650 return ret;
9651 }
9652 }
9653 ret = 0;
9654
Chris Mason5a3f23d2009-03-31 13:27:11 -04009655 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009656 * we're using rename to replace one file with another. Start IO on it
9657 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009658 */
Chris Mason8d875f92014-08-12 10:47:42 -07009659 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009660 filemap_flush(old_inode->i_mapping);
9661
Yan, Zheng76dda932009-09-21 16:00:26 -04009662 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009663 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009664 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009665 /*
9666 * We want to reserve the absolute worst case amount of items. So if
9667 * both inodes are subvols and we need to unlink them then that would
9668 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009670 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9671 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009672 * If our rename has the whiteout flag, we need more 5 units for the
9673 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9674 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009675 */
Filipe Manana5062af32016-05-05 10:26:26 +01009676 trans_num_items = 11;
9677 if (flags & RENAME_WHITEOUT)
9678 trans_num_items += 5;
9679 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009680 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009681 ret = PTR_ERR(trans);
9682 goto out_notrans;
9683 }
Chris Mason5f39d392007-10-15 16:14:19 -04009684
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009685 if (dest != root) {
9686 ret = btrfs_record_root_in_trans(trans, dest);
9687 if (ret)
9688 goto out_fail;
9689 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009690
Nikolay Borisov877574e2017-02-20 13:50:33 +02009691 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009692 if (ret)
9693 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009694
Miao Xie67de1172013-12-26 13:07:06 +08009695 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009696 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009697 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009698 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009699 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009700 ret = btrfs_insert_inode_ref(trans, dest,
9701 new_dentry->d_name.name,
9702 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009703 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009704 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009705 if (ret)
9706 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009707 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009708
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009709 inode_inc_iversion(old_dir);
9710 inode_inc_iversion(new_dir);
9711 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009712 old_dir->i_ctime = old_dir->i_mtime =
9713 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009714 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009715
Chris Mason12fcfd22009-03-24 10:24:20 -04009716 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009717 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9718 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009719
Li Zefan33345d012011-04-20 10:31:50 +08009720 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009721 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009722 } else {
Filipe Mananabd54f382021-07-27 11:24:45 +01009723 /*
9724 * Now pin the log. We do it to ensure that no other task can
9725 * sync the log while we are in progress with the rename, as
9726 * that could result in an inconsistency in case any of the
9727 * inodes that are part of this rename operation were logged
9728 * before.
9729 *
9730 * We pin the log even if at this precise moment none of the
9731 * inodes was logged before. This is because right after we
9732 * checked for that, some other task fsyncing some other inode
9733 * not involved with this rename operation could log that one of
9734 * our inodes exists.
9735 *
9736 * We don't need to pin the logs before the above call to
9737 * btrfs_insert_inode_ref(), since that does not need to change
9738 * a log.
9739 */
9740 btrfs_pin_log_trans(root);
9741 log_pinned = true;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009742 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9743 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009744 old_dentry->d_name.name,
9745 old_dentry->d_name.len);
9746 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009747 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009748 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009749 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009750 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009751 goto out_fail;
9752 }
Chris Mason39279cc2007-06-12 06:35:45 -04009753
9754 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009755 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009756 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009757 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009758 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009759 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009760 BUG_ON(new_inode->i_nlink == 0);
9761 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009762 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9763 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009764 new_dentry->d_name.name,
9765 new_dentry->d_name.len);
9766 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009767 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009768 ret = btrfs_orphan_add(trans,
9769 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009770 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009771 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009772 goto out_fail;
9773 }
Chris Mason39279cc2007-06-12 06:35:45 -04009774 }
Josef Bacikaec74772008-07-24 12:12:38 -04009775
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009776 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009777 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009778 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009779 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009780 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009781 goto out_fail;
9782 }
Chris Mason39279cc2007-06-12 06:35:45 -04009783
Miao Xie67de1172013-12-26 13:07:06 +08009784 if (old_inode->i_nlink == 1)
9785 BTRFS_I(old_inode)->dir_index = index;
9786
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009787 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009788 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9789 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009790 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009791 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009792 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793
9794 if (flags & RENAME_WHITEOUT) {
Christian Braunerca072742021-07-27 12:48:42 +02009795 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9796 old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797
9798 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009799 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800 goto out_fail;
9801 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009802 }
Chris Mason39279cc2007-06-12 06:35:45 -04009803out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009804 /*
9805 * If we have pinned the log and an error happened, we unpin tasks
9806 * trying to sync the log and force them to fallback to a transaction
9807 * commit if the log currently contains any of the inodes involved in
9808 * this rename operation (to ensure we do not persist a log with an
9809 * inconsistent state for any of these inodes or leading to any
9810 * inconsistencies when replayed). If the transaction was aborted, the
9811 * abortion reason is propagated to userspace when attempting to commit
9812 * the transaction. If the log does not contain any of these inodes, we
9813 * allow the tasks to sync it.
9814 */
9815 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009816 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9817 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9818 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009819 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009820 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009821 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009822
9823 btrfs_end_log_trans(root);
9824 log_pinned = false;
9825 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009826 ret2 = btrfs_end_transaction(trans);
9827 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009828out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009829 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009830 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009831
Chris Mason39279cc2007-06-12 06:35:45 -04009832 return ret;
9833}
9834
Christian Brauner549c7292021-01-21 14:19:43 +01009835static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9836 struct dentry *old_dentry, struct inode *new_dir,
9837 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009838{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009840 return -EINVAL;
9841
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842 if (flags & RENAME_EXCHANGE)
9843 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9844 new_dentry);
9845
Christian Braunerca072742021-07-27 12:48:42 +02009846 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9847 new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009848}
9849
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009850struct btrfs_delalloc_work {
9851 struct inode *inode;
9852 struct completion completion;
9853 struct list_head list;
9854 struct btrfs_work work;
9855};
9856
Miao Xie8ccf6f192012-10-25 09:28:04 +00009857static void btrfs_run_delalloc_work(struct btrfs_work *work)
9858{
9859 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009860 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009861
9862 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9863 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009864 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009865 filemap_flush(inode->i_mapping);
9866 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9867 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009868 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009869
Nikolay Borisov076da912018-04-23 10:54:16 +03009870 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009871 complete(&delalloc_work->completion);
9872}
9873
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009874static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009875{
9876 struct btrfs_delalloc_work *work;
9877
David Sterba100d5702015-12-08 14:39:32 +01009878 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009879 if (!work)
9880 return NULL;
9881
9882 init_completion(&work->completion);
9883 INIT_LIST_HEAD(&work->list);
9884 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009885 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009886
9887 return work;
9888}
9889
Chris Masond352ac62008-09-29 15:18:18 -04009890/*
9891 * some fairly slow code that needs optimization. This walks the list
9892 * of all the inodes with pending delalloc and forces them to disk.
9893 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009894static int start_delalloc_inodes(struct btrfs_root *root,
9895 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009896 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009897{
Chris Masonea8c2812008-08-04 23:17:27 -04009898 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009899 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009900 struct btrfs_delalloc_work *work, *next;
9901 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009902 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009903 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009904 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009905
Miao Xie8ccf6f192012-10-25 09:28:04 +00009906 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009907 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009908
Miao Xie573bfb72014-03-06 13:55:03 +08009909 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009910 spin_lock(&root->delalloc_lock);
9911 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009912 while (!list_empty(&splice)) {
9913 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009914 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009915
Miao Xieeb73c1b2013-05-15 07:48:22 +00009916 list_move_tail(&binode->delalloc_inodes,
9917 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009918
9919 if (in_reclaim_context &&
9920 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9921 continue;
9922
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009923 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009924 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009925 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009926 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009927 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009928 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009929
Ethan Lien3cd24c62018-11-01 14:49:03 +08009930 if (snapshot)
9931 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9932 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009933 if (full_flush) {
9934 work = btrfs_alloc_delalloc_work(inode);
9935 if (!work) {
9936 iput(inode);
9937 ret = -ENOMEM;
9938 goto out;
9939 }
9940 list_add_tail(&work->list, &works);
9941 btrfs_queue_work(root->fs_info->flush_workers,
9942 &work->work);
9943 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009944 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009945 btrfs_add_delayed_iput(inode);
9946 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009947 goto out;
9948 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009949 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009950 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009951 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009952 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009953
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009954out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009955 list_for_each_entry_safe(work, next, &works, list) {
9956 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009957 wait_for_completion(&work->completion);
9958 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009959 }
9960
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009961 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009962 spin_lock(&root->delalloc_lock);
9963 list_splice_tail(&splice, &root->delalloc_inodes);
9964 spin_unlock(&root->delalloc_lock);
9965 }
Miao Xie573bfb72014-03-06 13:55:03 +08009966 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009967 return ret;
9968}
9969
Filipe Mananaf9baa502021-04-22 12:08:05 +01009970int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009971{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009972 struct writeback_control wbc = {
9973 .nr_to_write = LONG_MAX,
9974 .sync_mode = WB_SYNC_NONE,
9975 .range_start = 0,
9976 .range_end = LLONG_MAX,
9977 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009978 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009979
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009980 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009981 return -EROFS;
9982
Filipe Mananaf9baa502021-04-22 12:08:05 +01009983 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009984}
9985
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009986int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009987 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009988{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009989 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009990 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009991 .sync_mode = WB_SYNC_NONE,
9992 .range_start = 0,
9993 .range_end = LLONG_MAX,
9994 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009995 struct btrfs_root *root;
9996 struct list_head splice;
9997 int ret;
9998
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009999 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000 return -EROFS;
10001
10002 INIT_LIST_HEAD(&splice);
10003
Miao Xie573bfb72014-03-06 13:55:03 +080010004 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010005 spin_lock(&fs_info->delalloc_root_lock);
10006 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +020010007 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -050010008 /*
10009 * Reset nr_to_write here so we know that we're doing a full
10010 * flush.
10011 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010012 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -050010013 wbc.nr_to_write = LONG_MAX;
10014
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 root = list_first_entry(&splice, struct btrfs_root,
10016 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -050010017 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 BUG_ON(!root);
10019 list_move_tail(&root->delalloc_root,
10020 &fs_info->delalloc_roots);
10021 spin_unlock(&fs_info->delalloc_root_lock);
10022
Josef Bacike076ab2a2021-01-07 17:08:30 -050010023 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -050010024 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -050010025 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010026 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010027 spin_lock(&fs_info->delalloc_root_lock);
10028 }
10029 spin_unlock(&fs_info->delalloc_root_lock);
10030
Miao Xie6c255e62014-03-06 13:55:01 +080010031 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010032out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010033 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010034 spin_lock(&fs_info->delalloc_root_lock);
10035 list_splice_tail(&splice, &fs_info->delalloc_roots);
10036 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010037 }
Miao Xie573bfb72014-03-06 13:55:03 +080010038 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010039 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010040}
10041
Christian Brauner549c7292021-01-21 14:19:43 +010010042static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
10043 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -040010044{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010045 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010046 struct btrfs_trans_handle *trans;
10047 struct btrfs_root *root = BTRFS_I(dir)->root;
10048 struct btrfs_path *path;
10049 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010050 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010051 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010052 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010053 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010054 int name_len;
10055 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010056 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010057 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010058 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010059
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010060 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010061 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010062 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010063
Josef Bacik9ed74f22009-09-11 16:12:44 -040010064 /*
10065 * 2 items for inode item and ref
10066 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010067 * 1 item for updating parent inode item
10068 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010069 * 1 item for xattr if selinux is on
10070 */
Filipe Manana9269d122015-12-31 18:16:29 +000010071 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010072 if (IS_ERR(trans))
10073 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010074
Nikolay Borisov543068a2020-12-07 17:32:33 +020010075 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +080010076 if (err)
10077 goto out_unlock;
10078
Christian Brauner5a052102021-07-27 12:48:47 +020010079 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +020010080 dentry->d_name.name, dentry->d_name.len,
10081 btrfs_ino(BTRFS_I(dir)), objectid,
10082 S_IFLNK | S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010083 if (IS_ERR(inode)) {
10084 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010085 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010086 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010087 }
Chris Mason39279cc2007-06-12 06:35:45 -040010088
Casey Schauflerad19db72011-12-15 10:09:07 -050010089 /*
10090 * If the active LSM wants to access the inode during
10091 * d_instantiate it needs these. Smack checks to see
10092 * if the filesystem supports xattrs by looking at the
10093 * ops vector.
10094 */
10095 inode->i_fop = &btrfs_file_operations;
10096 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010097 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010098
10099 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10100 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010101 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010102
Chris Mason39279cc2007-06-12 06:35:45 -040010103 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010104 if (!path) {
10105 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010106 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010107 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010108 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010109 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010110 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010111 datasize = btrfs_file_extent_calc_inline_size(name_len);
10112 err = btrfs_insert_empty_item(trans, root, path, &key,
10113 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010114 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010115 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010116 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010117 }
Chris Mason5f39d392007-10-15 16:14:19 -040010118 leaf = path->nodes[0];
10119 ei = btrfs_item_ptr(leaf, path->slots[0],
10120 struct btrfs_file_extent_item);
10121 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10122 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010123 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010124 btrfs_set_file_extent_encryption(leaf, ei, 0);
10125 btrfs_set_file_extent_compression(leaf, ei, 0);
10126 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10127 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10128
Chris Mason39279cc2007-06-12 06:35:45 -040010129 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010130 write_extent_buffer(leaf, symname, ptr, name_len);
10131 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010132 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010133
Chris Mason39279cc2007-06-12 06:35:45 -040010134 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010135 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010136 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010137 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010138 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +000010139 /*
10140 * Last step, add directory indexes for our symlink inode. This is the
10141 * last step to avoid extra cleanup of these indexes if an error happens
10142 * elsewhere above.
10143 */
10144 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010145 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10146 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010147 if (err)
10148 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010149
Al Viro1e2e5472018-05-04 08:23:01 -040010150 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010151
10152out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010153 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010154 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010155 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010156 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010157 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010158 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010159 return err;
10160}
Chris Mason16432982008-04-10 10:23:21 -040010161
Filipe Manana8fccebf2020-09-08 11:27:20 +010010162static struct btrfs_trans_handle *insert_prealloc_file_extent(
10163 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010164 struct btrfs_inode *inode,
10165 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +080010166 u64 file_offset)
10167{
10168 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +010010169 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010170 struct btrfs_trans_handle *trans = trans_in;
10171 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010172 u64 start = ins->objectid;
10173 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010174 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +080010175 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010176
10177 memset(&stack_fi, 0, sizeof(stack_fi));
10178
10179 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10180 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10181 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10182 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10183 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10184 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10185 /* Encryption and other encoding is reserved and all 0 */
10186
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010187 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10188 if (qgroup_released < 0)
10189 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010190
10191 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010192 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +000010193 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010194 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010195 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010196 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010197 return trans;
10198 }
10199
10200 extent_info.disk_offset = start;
10201 extent_info.disk_len = len;
10202 extent_info.data_offset = 0;
10203 extent_info.data_len = len;
10204 extent_info.file_offset = file_offset;
10205 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010206 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010207 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010208 extent_info.insertions = 0;
10209
10210 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010211 if (!path) {
10212 ret = -ENOMEM;
10213 goto free_qgroup;
10214 }
Filipe Manana8fccebf2020-09-08 11:27:20 +010010215
Nikolay Borisovbfc78472021-02-17 15:12:47 +020010216 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +010010217 file_offset + len - 1, &extent_info,
10218 &trans);
10219 btrfs_free_path(path);
10220 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010221 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010222 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010223
10224free_qgroup:
10225 /*
10226 * We have released qgroup data range at the beginning of the function,
10227 * and normally qgroup_released bytes will be freed when committing
10228 * transaction.
10229 * But if we error out early, we have to free what we have released
10230 * or we leak qgroup data reservation.
10231 */
10232 btrfs_qgroup_free_refroot(inode->root->fs_info,
10233 inode->root->root_key.objectid, qgroup_released,
10234 BTRFS_QGROUP_RSV_DATA);
10235 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +080010236}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010237
Josef Bacik0af3d002010-06-21 14:48:16 -040010238static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10239 u64 start, u64 num_bytes, u64 min_size,
10240 loff_t actual_len, u64 *alloc_hint,
10241 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010243 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010244 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10245 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010246 struct btrfs_root *root = BTRFS_I(inode)->root;
10247 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010248 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010249 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010250 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010251 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010252 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010253 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010254 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010255 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010256
Josef Bacik0af3d002010-06-21 14:48:16 -040010257 if (trans)
10258 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010259 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010260 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010261 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010262 /*
10263 * If we are severely fragmented we could end up with really
10264 * small allocations, so if the allocator is returning small
10265 * chunks lets make its job easier by only searching for those
10266 * sized chunks.
10267 */
10268 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010269 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10270 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010271 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010272 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010273
10274 /*
10275 * We've reserved this space, and thus converted it from
10276 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10277 * from here on out we will only need to clear our reservation
10278 * for the remaining unreserved area, so advance our
10279 * clear_offset by our extent size.
10280 */
10281 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010282
Josef Bacik0b670dc2015-09-23 17:11:16 -040010283 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010284 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10285 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010286 /*
10287 * Now that we inserted the prealloc extent we can finally
10288 * decrement the number of reservations in the block group.
10289 * If we did it before, we could race with relocation and have
10290 * relocation miss the reserved extent, making it fail later.
10291 */
10292 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010293 if (IS_ERR(trans)) {
10294 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010295 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010296 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010297 break;
10298 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010299
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010300 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010301 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010302
Josef Bacik5dc562c2012-08-17 13:14:17 -040010303 em = alloc_extent_map();
10304 if (!em) {
10305 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10306 &BTRFS_I(inode)->runtime_flags);
10307 goto next;
10308 }
10309
10310 em->start = cur_offset;
10311 em->orig_start = cur_offset;
10312 em->len = ins.offset;
10313 em->block_start = ins.objectid;
10314 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010315 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010316 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010317 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10318 em->generation = trans->transid;
10319
10320 while (1) {
10321 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010322 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010323 write_unlock(&em_tree->lock);
10324 if (ret != -EEXIST)
10325 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010326 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010327 cur_offset + ins.offset - 1,
10328 0);
10329 }
10330 free_extent_map(em);
10331next:
Yan Zhengd899e052008-10-30 14:25:28 -040010332 num_bytes -= ins.offset;
10333 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010334 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010335
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010336 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010337 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010338 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010339 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010340 (actual_len > inode->i_size) &&
10341 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010342 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010343 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010344 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010345 i_size = cur_offset;
10346 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010347 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010348 }
10349
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010350 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010351
10352 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010353 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010354 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010355 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010356 break;
10357 }
Yan Zhengd899e052008-10-30 14:25:28 -040010358
Filipe Manana8fccebf2020-09-08 11:27:20 +010010359 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010360 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010361 trans = NULL;
10362 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010363 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010364 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010365 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010366 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010367 return ret;
10368}
10369
Josef Bacik0af3d002010-06-21 14:48:16 -040010370int btrfs_prealloc_file_range(struct inode *inode, int mode,
10371 u64 start, u64 num_bytes, u64 min_size,
10372 loff_t actual_len, u64 *alloc_hint)
10373{
10374 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10375 min_size, actual_len, alloc_hint,
10376 NULL);
10377}
10378
10379int btrfs_prealloc_file_range_trans(struct inode *inode,
10380 struct btrfs_trans_handle *trans, int mode,
10381 u64 start, u64 num_bytes, u64 min_size,
10382 loff_t actual_len, u64 *alloc_hint)
10383{
10384 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10385 min_size, actual_len, alloc_hint, trans);
10386}
10387
Chris Masone6dcd2d2008-07-17 12:53:50 -040010388static int btrfs_set_page_dirty(struct page *page)
10389{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010390 return __set_page_dirty_nobuffers(page);
10391}
10392
Christian Brauner549c7292021-01-21 14:19:43 +010010393static int btrfs_permission(struct user_namespace *mnt_userns,
10394 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010395{
Li Zefanb83cc962010-12-20 16:04:08 +080010396 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010397 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010398
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010399 if (mask & MAY_WRITE &&
10400 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10401 if (btrfs_root_readonly(root))
10402 return -EROFS;
10403 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10404 return -EACCES;
10405 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010406 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010407}
Chris Mason39279cc2007-06-12 06:35:45 -040010408
Christian Brauner549c7292021-01-21 14:19:43 +010010409static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10410 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010411{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010412 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010413 struct btrfs_trans_handle *trans;
10414 struct btrfs_root *root = BTRFS_I(dir)->root;
10415 struct inode *inode = NULL;
10416 u64 objectid;
10417 u64 index;
10418 int ret = 0;
10419
10420 /*
10421 * 5 units required for adding orphan entry
10422 */
10423 trans = btrfs_start_transaction(root, 5);
10424 if (IS_ERR(trans))
10425 return PTR_ERR(trans);
10426
Nikolay Borisov543068a2020-12-07 17:32:33 +020010427 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010428 if (ret)
10429 goto out;
10430
Christian Brauner98b6ab52021-07-27 12:48:48 +020010431 inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010432 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010433 if (IS_ERR(inode)) {
10434 ret = PTR_ERR(inode);
10435 inode = NULL;
10436 goto out;
10437 }
10438
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010439 inode->i_fop = &btrfs_file_operations;
10440 inode->i_op = &btrfs_file_inode_operations;
10441
10442 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010443
Chris Masonb0d5d102014-09-08 13:08:51 -070010444 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10445 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010446 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010447
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010448 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010449 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010450 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010451 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010452 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010453 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010454
Filipe Manana5762b5c2014-08-01 00:10:32 +010010455 /*
10456 * We set number of links to 0 in btrfs_new_inode(), and here we set
10457 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10458 * through:
10459 *
10460 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10461 */
10462 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010463 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010464 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010465 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010467 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010468 if (ret && inode)
10469 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010470 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010471 return ret;
10472}
10473
Qu Wenruod2a91062021-05-31 16:50:49 +080010474void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010475{
Qu Wenruod2a91062021-05-31 16:50:49 +080010476 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010477 unsigned long index = start >> PAGE_SHIFT;
10478 unsigned long end_index = end >> PAGE_SHIFT;
10479 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010480 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010481
Qu Wenruod2a91062021-05-31 16:50:49 +080010482 ASSERT(end + 1 - start <= U32_MAX);
10483 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010484 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010485 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010486 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010487
10488 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010489 put_page(page);
10490 index++;
10491 }
10492}
10493
Omar Sandovaled46ff32016-11-03 10:28:14 -070010494#ifdef CONFIG_SWAP
10495/*
10496 * Add an entry indicating a block group or device which is pinned by a
10497 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10498 * negative errno on failure.
10499 */
10500static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10501 bool is_block_group)
10502{
10503 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10504 struct btrfs_swapfile_pin *sp, *entry;
10505 struct rb_node **p;
10506 struct rb_node *parent = NULL;
10507
10508 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10509 if (!sp)
10510 return -ENOMEM;
10511 sp->ptr = ptr;
10512 sp->inode = inode;
10513 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010514 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010515
10516 spin_lock(&fs_info->swapfile_pins_lock);
10517 p = &fs_info->swapfile_pins.rb_node;
10518 while (*p) {
10519 parent = *p;
10520 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10521 if (sp->ptr < entry->ptr ||
10522 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10523 p = &(*p)->rb_left;
10524 } else if (sp->ptr > entry->ptr ||
10525 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10526 p = &(*p)->rb_right;
10527 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010528 if (is_block_group)
10529 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010530 spin_unlock(&fs_info->swapfile_pins_lock);
10531 kfree(sp);
10532 return 1;
10533 }
10534 }
10535 rb_link_node(&sp->node, parent, p);
10536 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10537 spin_unlock(&fs_info->swapfile_pins_lock);
10538 return 0;
10539}
10540
10541/* Free all of the entries pinned by this swapfile. */
10542static void btrfs_free_swapfile_pins(struct inode *inode)
10543{
10544 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10545 struct btrfs_swapfile_pin *sp;
10546 struct rb_node *node, *next;
10547
10548 spin_lock(&fs_info->swapfile_pins_lock);
10549 node = rb_first(&fs_info->swapfile_pins);
10550 while (node) {
10551 next = rb_next(node);
10552 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10553 if (sp->inode == inode) {
10554 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010555 if (sp->is_block_group) {
10556 btrfs_dec_block_group_swap_extents(sp->ptr,
10557 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010558 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010559 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010560 kfree(sp);
10561 }
10562 node = next;
10563 }
10564 spin_unlock(&fs_info->swapfile_pins_lock);
10565}
10566
10567struct btrfs_swap_info {
10568 u64 start;
10569 u64 block_start;
10570 u64 block_len;
10571 u64 lowest_ppage;
10572 u64 highest_ppage;
10573 unsigned long nr_pages;
10574 int nr_extents;
10575};
10576
10577static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10578 struct btrfs_swap_info *bsi)
10579{
10580 unsigned long nr_pages;
10581 u64 first_ppage, first_ppage_reported, next_ppage;
10582 int ret;
10583
10584 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10585 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10586 PAGE_SIZE) >> PAGE_SHIFT;
10587
10588 if (first_ppage >= next_ppage)
10589 return 0;
10590 nr_pages = next_ppage - first_ppage;
10591
10592 first_ppage_reported = first_ppage;
10593 if (bsi->start == 0)
10594 first_ppage_reported++;
10595 if (bsi->lowest_ppage > first_ppage_reported)
10596 bsi->lowest_ppage = first_ppage_reported;
10597 if (bsi->highest_ppage < (next_ppage - 1))
10598 bsi->highest_ppage = next_ppage - 1;
10599
10600 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10601 if (ret < 0)
10602 return ret;
10603 bsi->nr_extents += ret;
10604 bsi->nr_pages += nr_pages;
10605 return 0;
10606}
10607
10608static void btrfs_swap_deactivate(struct file *file)
10609{
10610 struct inode *inode = file_inode(file);
10611
10612 btrfs_free_swapfile_pins(inode);
10613 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10614}
10615
10616static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10617 sector_t *span)
10618{
10619 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010620 struct btrfs_root *root = BTRFS_I(inode)->root;
10621 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010622 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10623 struct extent_state *cached_state = NULL;
10624 struct extent_map *em = NULL;
10625 struct btrfs_device *device = NULL;
10626 struct btrfs_swap_info bsi = {
10627 .lowest_ppage = (sector_t)-1ULL,
10628 };
10629 int ret = 0;
10630 u64 isize;
10631 u64 start;
10632
10633 /*
10634 * If the swap file was just created, make sure delalloc is done. If the
10635 * file changes again after this, the user is doing something stupid and
10636 * we don't really care.
10637 */
10638 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10639 if (ret)
10640 return ret;
10641
10642 /*
10643 * The inode is locked, so these flags won't change after we check them.
10644 */
10645 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10646 btrfs_warn(fs_info, "swapfile must not be compressed");
10647 return -EINVAL;
10648 }
10649 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10650 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10651 return -EINVAL;
10652 }
10653 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10654 btrfs_warn(fs_info, "swapfile must not be checksummed");
10655 return -EINVAL;
10656 }
10657
10658 /*
10659 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010660 * under us. The exclop protection makes sure they aren't running/won't
10661 * run concurrently while we are mapping the swap extents, and
10662 * fs_info->swapfile_pins prevents them from running while the swap
10663 * file is active and moving the extents. Note that this also prevents
10664 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010665 * really worth the trouble to allow it.
10666 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010667 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010668 btrfs_warn(fs_info,
10669 "cannot activate swapfile while exclusive operation is running");
10670 return -EBUSY;
10671 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010672
10673 /*
10674 * Prevent snapshot creation while we are activating the swap file.
10675 * We do not want to race with snapshot creation. If snapshot creation
10676 * already started before we bumped nr_swapfiles from 0 to 1 and
10677 * completes before the first write into the swap file after it is
10678 * activated, than that write would fallback to COW.
10679 */
10680 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10681 btrfs_exclop_finish(fs_info);
10682 btrfs_warn(fs_info,
10683 "cannot activate swapfile because snapshot creation is in progress");
10684 return -EINVAL;
10685 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010686 /*
10687 * Snapshots can create extents which require COW even if NODATACOW is
10688 * set. We use this counter to prevent snapshots. We must increment it
10689 * before walking the extents because we don't want a concurrent
10690 * snapshot to run after we've already checked the extents.
10691 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010692 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010693
10694 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10695
10696 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10697 start = 0;
10698 while (start < isize) {
10699 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010700 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010701 u64 len = isize - start;
10702
Omar Sandoval39b07b52019-12-02 17:34:23 -080010703 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010704 if (IS_ERR(em)) {
10705 ret = PTR_ERR(em);
10706 goto out;
10707 }
10708
10709 if (em->block_start == EXTENT_MAP_HOLE) {
10710 btrfs_warn(fs_info, "swapfile must not have holes");
10711 ret = -EINVAL;
10712 goto out;
10713 }
10714 if (em->block_start == EXTENT_MAP_INLINE) {
10715 /*
10716 * It's unlikely we'll ever actually find ourselves
10717 * here, as a file small enough to fit inline won't be
10718 * big enough to store more than the swap header, but in
10719 * case something changes in the future, let's catch it
10720 * here rather than later.
10721 */
10722 btrfs_warn(fs_info, "swapfile must not be inline");
10723 ret = -EINVAL;
10724 goto out;
10725 }
10726 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10727 btrfs_warn(fs_info, "swapfile must not be compressed");
10728 ret = -EINVAL;
10729 goto out;
10730 }
10731
10732 logical_block_start = em->block_start + (start - em->start);
10733 len = min(len, em->len - (start - em->start));
10734 free_extent_map(em);
10735 em = NULL;
10736
Boris Burkova84d5d42020-08-18 11:00:05 -070010737 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010738 if (ret < 0) {
10739 goto out;
10740 } else if (ret) {
10741 ret = 0;
10742 } else {
10743 btrfs_warn(fs_info,
10744 "swapfile must not be copy-on-write");
10745 ret = -EINVAL;
10746 goto out;
10747 }
10748
10749 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10750 if (IS_ERR(em)) {
10751 ret = PTR_ERR(em);
10752 goto out;
10753 }
10754
10755 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10756 btrfs_warn(fs_info,
10757 "swapfile must have single data profile");
10758 ret = -EINVAL;
10759 goto out;
10760 }
10761
10762 if (device == NULL) {
10763 device = em->map_lookup->stripes[0].dev;
10764 ret = btrfs_add_swapfile_pin(inode, device, false);
10765 if (ret == 1)
10766 ret = 0;
10767 else if (ret)
10768 goto out;
10769 } else if (device != em->map_lookup->stripes[0].dev) {
10770 btrfs_warn(fs_info, "swapfile must be on one device");
10771 ret = -EINVAL;
10772 goto out;
10773 }
10774
10775 physical_block_start = (em->map_lookup->stripes[0].physical +
10776 (logical_block_start - em->start));
10777 len = min(len, em->len - (logical_block_start - em->start));
10778 free_extent_map(em);
10779 em = NULL;
10780
10781 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10782 if (!bg) {
10783 btrfs_warn(fs_info,
10784 "could not find block group containing swapfile");
10785 ret = -EINVAL;
10786 goto out;
10787 }
10788
Filipe Manana195a49e2021-02-05 12:55:37 +000010789 if (!btrfs_inc_block_group_swap_extents(bg)) {
10790 btrfs_warn(fs_info,
10791 "block group for swapfile at %llu is read-only%s",
10792 bg->start,
10793 atomic_read(&fs_info->scrubs_running) ?
10794 " (scrub running)" : "");
10795 btrfs_put_block_group(bg);
10796 ret = -EINVAL;
10797 goto out;
10798 }
10799
Omar Sandovaled46ff32016-11-03 10:28:14 -070010800 ret = btrfs_add_swapfile_pin(inode, bg, true);
10801 if (ret) {
10802 btrfs_put_block_group(bg);
10803 if (ret == 1)
10804 ret = 0;
10805 else
10806 goto out;
10807 }
10808
10809 if (bsi.block_len &&
10810 bsi.block_start + bsi.block_len == physical_block_start) {
10811 bsi.block_len += len;
10812 } else {
10813 if (bsi.block_len) {
10814 ret = btrfs_add_swap_extent(sis, &bsi);
10815 if (ret)
10816 goto out;
10817 }
10818 bsi.start = start;
10819 bsi.block_start = physical_block_start;
10820 bsi.block_len = len;
10821 }
10822
10823 start += len;
10824 }
10825
10826 if (bsi.block_len)
10827 ret = btrfs_add_swap_extent(sis, &bsi);
10828
10829out:
10830 if (!IS_ERR_OR_NULL(em))
10831 free_extent_map(em);
10832
10833 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10834
10835 if (ret)
10836 btrfs_swap_deactivate(file);
10837
Filipe Mananadd0734f2021-02-05 12:55:38 +000010838 btrfs_drew_write_unlock(&root->snapshot_lock);
10839
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010840 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010841
10842 if (ret)
10843 return ret;
10844
10845 if (device)
10846 sis->bdev = device->bdev;
10847 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10848 sis->max = bsi.nr_pages;
10849 sis->pages = bsi.nr_pages - 1;
10850 sis->highest_bit = bsi.nr_pages - 1;
10851 return bsi.nr_extents;
10852}
10853#else
10854static void btrfs_swap_deactivate(struct file *file)
10855{
10856}
10857
10858static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10859 sector_t *span)
10860{
10861 return -EOPNOTSUPP;
10862}
10863#endif
10864
Filipe Manana2766ff62020-11-04 11:07:34 +000010865/*
10866 * Update the number of bytes used in the VFS' inode. When we replace extents in
10867 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10868 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10869 * always get a correct value.
10870 */
10871void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10872 const u64 add_bytes,
10873 const u64 del_bytes)
10874{
10875 if (add_bytes == del_bytes)
10876 return;
10877
10878 spin_lock(&inode->lock);
10879 if (del_bytes > 0)
10880 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10881 if (add_bytes > 0)
10882 inode_add_bytes(&inode->vfs_inode, add_bytes);
10883 spin_unlock(&inode->lock);
10884}
10885
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010886static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010887 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010888 .lookup = btrfs_lookup,
10889 .create = btrfs_create,
10890 .unlink = btrfs_unlink,
10891 .link = btrfs_link,
10892 .mkdir = btrfs_mkdir,
10893 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010894 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010895 .symlink = btrfs_symlink,
10896 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010897 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010898 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010899 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010900 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010901 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010902 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010903 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010904 .fileattr_get = btrfs_fileattr_get,
10905 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010906};
Yan, Zheng76dda932009-09-21 16:00:26 -040010907
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010908static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010909 .llseek = generic_file_llseek,
10910 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010911 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010912 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010913 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010914#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010915 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010916#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010917 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010918 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010919};
10920
Chris Mason35054392009-01-21 13:11:13 -050010921/*
10922 * btrfs doesn't support the bmap operation because swapfiles
10923 * use bmap to make a mapping of extents in the file. They assume
10924 * these extents won't change over the life of the file and they
10925 * use the bmap result to do IO directly to the drive.
10926 *
10927 * the btrfs bmap call would return logical addresses that aren't
10928 * suitable for IO and they also will change frequently as COW
10929 * operations happen. So, swapfile + btrfs == corruption.
10930 *
10931 * For now we're avoiding this by dropping bmap.
10932 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010933static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010934 .readpage = btrfs_readpage,
10935 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010936 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010937 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010938 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010939 .invalidatepage = btrfs_invalidatepage,
10940 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010941#ifdef CONFIG_MIGRATION
10942 .migratepage = btrfs_migratepage,
10943#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010944 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010945 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010946 .swap_activate = btrfs_swap_activate,
10947 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010948};
10949
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010950static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010951 .getattr = btrfs_getattr,
10952 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010953 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010954 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010955 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010956 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010957 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010958 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010959 .fileattr_get = btrfs_fileattr_get,
10960 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010961};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010962static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010963 .getattr = btrfs_getattr,
10964 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010965 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010966 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010967 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010968 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010969 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010970};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010971static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010972 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010973 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010974 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010975 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010976 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010977 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010978};
Yan, Zheng76dda932009-09-21 16:00:26 -040010979
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010980const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010981 .d_delete = btrfs_dentry_delete,
10982};