blob: 0ffd48f9b685476c80f98c7a4e6a0d909c86ba59 [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 Brauner2f221d62021-01-21 14:19:26 +01005395 err = setattr_prepare(&init_user_ns, 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 Brauner2f221d62021-01-21 14:19:26 +01005406 setattr_copy(&init_user_ns, 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 Braunere65ce2a2021-01-21 14:19:27 +01005411 err = posix_acl_chmod(&init_user_ns, inode,
5412 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005413 }
5414
Chris Mason39279cc2007-06-12 06:35:45 -04005415 return err;
5416}
Chris Mason61295eb2008-01-14 16:24:38 -05005417
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005418/*
5419 * While truncating the inode pages during eviction, we get the VFS calling
5420 * btrfs_invalidatepage() against each page of the inode. This is slow because
5421 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5422 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5423 * extent_state structures over and over, wasting lots of time.
5424 *
5425 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5426 * those expensive operations on a per page basis and do only the ordered io
5427 * finishing, while we release here the extent_map and extent_state structures,
5428 * without the excessive merging and splitting.
5429 */
5430static void evict_inode_truncate_pages(struct inode *inode)
5431{
5432 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5433 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5434 struct rb_node *node;
5435
5436 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005437 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005438
5439 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005440 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005441 struct extent_map *em;
5442
Liu Bo07e1ce02018-08-23 03:51:52 +08005443 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005444 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005445 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5446 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005447 remove_extent_mapping(map_tree, em);
5448 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005449 if (need_resched()) {
5450 write_unlock(&map_tree->lock);
5451 cond_resched();
5452 write_lock(&map_tree->lock);
5453 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005454 }
5455 write_unlock(&map_tree->lock);
5456
Filipe Manana6ca07092015-05-26 00:55:42 +01005457 /*
5458 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005459 * We can have ongoing bios started by readahead that have
5460 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005461 * still in progress (unlocked the pages in the bio but did not yet
5462 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005463 * ranges can still be locked and eviction started because before
5464 * submitting those bios, which are executed by a separate task (work
5465 * queue kthread), inode references (inode->i_count) were not taken
5466 * (which would be dropped in the end io callback of each bio).
5467 * Therefore here we effectively end up waiting for those bios and
5468 * anyone else holding locked ranges without having bumped the inode's
5469 * reference count - if we don't do it, when they access the inode's
5470 * io_tree to unlock a range it may be too late, leading to an
5471 * use-after-free issue.
5472 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005473 spin_lock(&io_tree->lock);
5474 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5475 struct extent_state *state;
5476 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005477 u64 start;
5478 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005479 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005480
5481 node = rb_first(&io_tree->state);
5482 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005483 start = state->start;
5484 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005485 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005486 spin_unlock(&io_tree->lock);
5487
David Sterbaff13db42015-12-03 14:30:40 +01005488 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005489
5490 /*
5491 * If still has DELALLOC flag, the extent didn't reach disk,
5492 * and its reserved space won't be freed by delayed_ref.
5493 * So we need to free its reserved space here.
5494 * (Refer to comment in btrfs_invalidatepage, case 2)
5495 *
5496 * Note, end is the bytenr of last byte, so we need + 1 here.
5497 */
Filipe Manana421f0922018-10-12 13:02:48 +01005498 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005499 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5500 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005501
Filipe Manana6ca07092015-05-26 00:55:42 +01005502 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005503 EXTENT_LOCKED | EXTENT_DELALLOC |
5504 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5505 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005506
Filipe Manana7064dd52014-08-08 02:47:05 +01005507 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005508 spin_lock(&io_tree->lock);
5509 }
5510 spin_unlock(&io_tree->lock);
5511}
5512
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005513static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005514 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005515{
5516 struct btrfs_fs_info *fs_info = root->fs_info;
5517 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005518 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005519 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005520 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005521
Josef Bacikd3984c92019-08-01 18:19:37 -04005522 /*
5523 * Eviction should be taking place at some place safe because of our
5524 * delayed iputs. However the normal flushing code will run delayed
5525 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5526 *
5527 * We reserve the delayed_refs_extra here again because we can't use
5528 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5529 * above. We reserve our extra bit here because we generate a ton of
5530 * delayed refs activity by truncating.
5531 *
5532 * If we cannot make our reservation we'll attempt to steal from the
5533 * global reserve, because we really want to be able to free up space.
5534 */
5535 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5536 BTRFS_RESERVE_FLUSH_EVICT);
5537 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005538 /*
5539 * Try to steal from the global reserve if there is space for
5540 * it.
5541 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005542 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5543 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5544 btrfs_warn(fs_info,
5545 "could not allocate space for delete; will truncate on mount");
5546 return ERR_PTR(-ENOSPC);
5547 }
5548 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005549 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005550
5551 trans = btrfs_join_transaction(root);
5552 if (IS_ERR(trans))
5553 return trans;
5554
5555 if (delayed_refs_extra) {
5556 trans->block_rsv = &fs_info->trans_block_rsv;
5557 trans->bytes_reserved = delayed_refs_extra;
5558 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5559 delayed_refs_extra, 1);
5560 }
5561 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005562}
5563
Al Virobd555972010-06-07 11:35:40 -04005564void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005565{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005566 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005567 struct btrfs_trans_handle *trans;
5568 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005569 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 int ret;
5571
liubo1abe9b82011-03-24 11:18:59 +00005572 trace_btrfs_inode_evict(inode);
5573
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005574 if (!root) {
Boris Burkov14605402021-06-30 13:01:49 -07005575 fsverity_cleanup_inode(inode);
Liu Boe8f1bc12018-01-25 11:02:53 -07005576 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005577 return;
5578 }
5579
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005580 evict_inode_truncate_pages(inode);
5581
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005582 if (inode->i_nlink &&
5583 ((btrfs_root_refs(&root->root_item) != 0 &&
5584 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005585 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005586 goto no_delete;
5587
Omar Sandoval27919062018-05-11 13:13:37 -07005588 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005589 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005590
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005591 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005592
Omar Sandoval7b40b692018-05-11 13:13:33 -07005593 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005594 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005595
Yan, Zheng76dda932009-09-21 16:00:26 -04005596 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005597 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5598 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 goto no_delete;
5600 }
5601
Nikolay Borisovaa790212017-01-10 20:35:40 +02005602 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005603 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005604 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005606 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005607 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005608 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005609 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005610 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005611
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005612 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005613
Yan, Zheng80825102009-11-12 09:35:36 +00005614 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005615 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005616 if (IS_ERR(trans))
5617 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005618
5619 trans->block_rsv = rsv;
5620
Nikolay Borisov50743392020-11-02 16:48:55 +02005621 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005622 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005623 trans->block_rsv = &fs_info->trans_block_rsv;
5624 btrfs_end_transaction(trans);
5625 btrfs_btree_balance_dirty(fs_info);
5626 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5627 goto free_rsv;
5628 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005629 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005630 }
5631
Josef Bacik4ef31a42013-08-13 14:10:08 -04005632 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005633 * Errors here aren't a big deal, it just means we leave orphan items in
5634 * the tree. They will be cleaned up on the next mount. If the inode
5635 * number gets reused, cleanup deletes the orphan item without doing
5636 * anything, and unlink reuses the existing orphan item.
5637 *
5638 * If it turns out that we are dropping too many of these, we might want
5639 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005640 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005641 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005642 if (!IS_ERR(trans)) {
5643 trans->block_rsv = rsv;
5644 btrfs_orphan_del(trans, BTRFS_I(inode));
5645 trans->block_rsv = &fs_info->trans_block_rsv;
5646 btrfs_end_transaction(trans);
5647 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005648
Omar Sandoval27919062018-05-11 13:13:37 -07005649free_rsv:
5650 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005651no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005652 /*
5653 * If we didn't successfully delete, the orphan item will still be in
5654 * the tree and we'll retry on the next mount. Again, we might also want
5655 * to retry these periodically in the future.
5656 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005657 btrfs_remove_delayed_node(BTRFS_I(inode));
Boris Burkov14605402021-06-30 13:01:49 -07005658 fsverity_cleanup_inode(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005659 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005660}
5661
5662/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005663 * Return the key found in the dir entry in the location pointer, fill @type
5664 * with BTRFS_FT_*, and return 0.
5665 *
Su Yue005d6712018-03-05 17:13:37 +08005666 * If no dir entries were found, returns -ENOENT.
5667 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005668 */
5669static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005670 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005671{
5672 const char *name = dentry->d_name.name;
5673 int namelen = dentry->d_name.len;
5674 struct btrfs_dir_item *di;
5675 struct btrfs_path *path;
5676 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005677 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005678
5679 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005680 if (!path)
5681 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005682
David Sterbaf85b7372017-01-20 14:54:07 +01005683 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5684 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005685 if (IS_ERR_OR_NULL(di)) {
5686 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005687 goto out;
5688 }
Chris Masond3977122009-01-05 21:25:51 -05005689
Chris Mason5f39d392007-10-15 16:14:19 -04005690 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005691 if (location->type != BTRFS_INODE_ITEM_KEY &&
5692 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005693 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005694 btrfs_warn(root->fs_info,
5695"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5696 __func__, name, btrfs_ino(BTRFS_I(dir)),
5697 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005698 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005699 if (!ret)
5700 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005701out:
Chris Mason39279cc2007-06-12 06:35:45 -04005702 btrfs_free_path(path);
5703 return ret;
5704}
5705
5706/*
5707 * when we hit a tree root in a directory, the btrfs part of the inode
5708 * needs to be changed to reflect the root directory of the tree root. This
5709 * is kind of like crossing a mount point.
5710 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005711static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712 struct inode *dir,
5713 struct dentry *dentry,
5714 struct btrfs_key *location,
5715 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005716{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717 struct btrfs_path *path;
5718 struct btrfs_root *new_root;
5719 struct btrfs_root_ref *ref;
5720 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005721 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 int ret;
5723 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005724
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725 path = btrfs_alloc_path();
5726 if (!path) {
5727 err = -ENOMEM;
5728 goto out;
5729 }
Chris Mason39279cc2007-06-12 06:35:45 -04005730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005731 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005732 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5733 key.type = BTRFS_ROOT_REF_KEY;
5734 key.offset = location->objectid;
5735
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005736 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737 if (ret) {
5738 if (ret < 0)
5739 err = ret;
5740 goto out;
5741 }
Chris Mason39279cc2007-06-12 06:35:45 -04005742
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 leaf = path->nodes[0];
5744 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005745 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5747 goto out;
5748
5749 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5750 (unsigned long)(ref + 1),
5751 dentry->d_name.len);
5752 if (ret)
5753 goto out;
5754
David Sterbab3b4aa72011-04-21 01:20:15 +02005755 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
David Sterba56e93572020-05-15 19:35:55 +02005757 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 if (IS_ERR(new_root)) {
5759 err = PTR_ERR(new_root);
5760 goto out;
5761 }
5762
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763 *sub_root = new_root;
5764 location->objectid = btrfs_root_dirid(&new_root->root_item);
5765 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005766 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 err = 0;
5768out:
5769 btrfs_free_path(path);
5770 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005771}
5772
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005773static void inode_tree_add(struct inode *inode)
5774{
5775 struct btrfs_root *root = BTRFS_I(inode)->root;
5776 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005777 struct rb_node **p;
5778 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005779 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005780 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005781
Al Viro1d3382cb2010-10-23 15:19:20 -04005782 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005783 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005784 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005785 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005786 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005787 while (*p) {
5788 parent = *p;
5789 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5790
David Sterba37508512018-06-29 10:56:40 +02005791 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005792 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005793 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005794 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005795 else {
5796 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005797 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005798 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005799 RB_CLEAR_NODE(parent);
5800 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005801 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005802 }
5803 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005804 rb_link_node(new, parent, p);
5805 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005806 spin_unlock(&root->inode_lock);
5807}
5808
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005809static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005810{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005811 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005812 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005813
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005814 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005815 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5816 rb_erase(&inode->rb_node, &root->inode_tree);
5817 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005818 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005819 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005820 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005821
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005822 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005823 spin_lock(&root->inode_lock);
5824 empty = RB_EMPTY_ROOT(&root->inode_tree);
5825 spin_unlock(&root->inode_lock);
5826 if (empty)
5827 btrfs_add_dead_root(root);
5828 }
5829}
5830
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005831
Chris Masone02119d2008-09-05 16:13:11 -04005832static int btrfs_init_locked_inode(struct inode *inode, void *p)
5833{
5834 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005835
5836 inode->i_ino = args->ino;
5837 BTRFS_I(inode)->location.objectid = args->ino;
5838 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5839 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005840 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5841 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 return 0;
5843}
5844
5845static int btrfs_find_actor(struct inode *inode, void *opaque)
5846{
5847 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005848
5849 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005850 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005851}
5852
David Sterba0202e832020-05-15 19:35:59 +02005853static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005854 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005855{
5856 struct inode *inode;
5857 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005858 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005859
David Sterba0202e832020-05-15 19:35:59 +02005860 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005861 args.root = root;
5862
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005863 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005864 btrfs_init_locked_inode,
5865 (void *)&args);
5866 return inode;
5867}
5868
David Sterba4c66e0d2019-10-03 19:09:35 +02005869/*
David Sterba0202e832020-05-15 19:35:59 +02005870 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005871 * Path can be preallocated to prevent recursing back to iget through
5872 * allocator. NULL is also valid but may require an additional allocation
5873 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305874 */
David Sterba0202e832020-05-15 19:35:59 +02005875struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005876 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305877{
5878 struct inode *inode;
5879
David Sterba0202e832020-05-15 19:35:59 +02005880 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305881 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005882 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305883
5884 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005885 int ret;
5886
Filipe Manana4222ea72018-10-24 10:13:03 +01005887 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005888 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005889 inode_tree_add(inode);
5890 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005891 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005892 iget_failed(inode);
5893 /*
5894 * ret > 0 can come from btrfs_search_slot called by
5895 * btrfs_read_locked_inode, this means the inode item
5896 * was not found.
5897 */
5898 if (ret > 0)
5899 ret = -ENOENT;
5900 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005901 }
5902 }
5903
Balaji Rao1a54ef82008-07-21 02:01:04 +05305904 return inode;
5905}
5906
David Sterba0202e832020-05-15 19:35:59 +02005907struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005908{
David Sterba0202e832020-05-15 19:35:59 +02005909 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005910}
5911
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005912static struct inode *new_simple_dir(struct super_block *s,
5913 struct btrfs_key *key,
5914 struct btrfs_root *root)
5915{
5916 struct inode *inode = new_inode(s);
5917
5918 if (!inode)
5919 return ERR_PTR(-ENOMEM);
5920
Josef Bacik5c8fd992020-02-14 16:11:43 -05005921 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005922 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005923 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005924
5925 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005926 /*
5927 * We only need lookup, the rest is read-only and there's no inode
5928 * associated with the dentry
5929 */
5930 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005931 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005932 inode->i_fop = &simple_dir_operations;
5933 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005934 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305935 inode->i_atime = inode->i_mtime;
5936 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005937 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005938
5939 return inode;
5940}
5941
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005942static inline u8 btrfs_inode_type(struct inode *inode)
5943{
5944 /*
5945 * Compile-time asserts that generic FT_* types still match
5946 * BTRFS_FT_* types
5947 */
5948 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5949 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5950 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5951 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5952 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5953 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5954 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5955 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5956
5957 return fs_umode_to_ftype(inode->i_mode);
5958}
5959
Chris Mason3de45862008-11-17 21:02:50 -05005960struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005962 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005963 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005964 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005965 struct btrfs_root *sub_root = root;
5966 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005967 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005968 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005969
5970 if (dentry->d_name.len > BTRFS_NAME_LEN)
5971 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005972
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005973 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005974 if (ret < 0)
5975 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005976
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005977 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005978 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005979 if (IS_ERR(inode))
5980 return inode;
5981
5982 /* Do extra check against inode mode with di_type */
5983 if (btrfs_inode_type(inode) != di_type) {
5984 btrfs_crit(fs_info,
5985"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5986 inode->i_mode, btrfs_inode_type(inode),
5987 di_type);
5988 iput(inode);
5989 return ERR_PTR(-EUCLEAN);
5990 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005991 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005993
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005994 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 &location, &sub_root);
5996 if (ret < 0) {
5997 if (ret != -ENOENT)
5998 inode = ERR_PTR(ret);
5999 else
6000 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6001 } else {
David Sterba0202e832020-05-15 19:35:59 +02006002 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04006003 }
Josef Bacik87270022020-01-24 09:32:31 -05006004 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05006005 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04006006
Julia Lawall34d19ba2011-01-24 19:55:19 +00006007 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006008 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01006009 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006010 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04006012 if (ret) {
6013 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05006014 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04006015 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00006016 }
6017
Chris Mason3de45862008-11-17 21:02:50 -05006018 return inode;
6019}
6020
Nick Pigginfe15ce42011-01-07 17:49:23 +11006021static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006022{
6023 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006024 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006025
Li Zefan848cce02012-02-21 17:04:28 +08006026 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006027 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006028
Li Zefan848cce02012-02-21 17:04:28 +08006029 if (inode) {
6030 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006031 if (btrfs_root_refs(&root->root_item) == 0)
6032 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006033
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006034 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006035 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006036 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006037 return 0;
6038}
6039
Chris Mason3de45862008-11-17 21:02:50 -05006040static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006041 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006042{
Al Viro3837d202018-10-10 16:38:27 -04006043 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04006044
Al Viro3837d202018-10-10 16:38:27 -04006045 if (inode == ERR_PTR(-ENOENT))
6046 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04006047 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006048}
6049
Josef Bacik23b5ec72017-07-24 15:14:25 -04006050/*
6051 * All this infrastructure exists because dir_emit can fault, and we are holding
6052 * the tree lock when doing readdir. For now just allocate a buffer and copy
6053 * our information into that, and then dir_emit from the buffer. This is
6054 * similar to what NFS does, only we don't keep the buffer around in pagecache
6055 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6056 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6057 * tree lock.
6058 */
6059static int btrfs_opendir(struct inode *inode, struct file *file)
6060{
6061 struct btrfs_file_private *private;
6062
6063 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6064 if (!private)
6065 return -ENOMEM;
6066 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6067 if (!private->filldir_buf) {
6068 kfree(private);
6069 return -ENOMEM;
6070 }
6071 file->private_data = private;
6072 return 0;
6073}
6074
6075struct dir_entry {
6076 u64 ino;
6077 u64 offset;
6078 unsigned type;
6079 int name_len;
6080};
6081
6082static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6083{
6084 while (entries--) {
6085 struct dir_entry *entry = addr;
6086 char *name = (char *)(entry + 1);
6087
David Sterba92d32172018-04-16 21:10:14 +02006088 ctx->pos = get_unaligned(&entry->offset);
6089 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6090 get_unaligned(&entry->ino),
6091 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006092 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006093 addr += sizeof(struct dir_entry) +
6094 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006095 ctx->pos++;
6096 }
6097 return 0;
6098}
6099
Al Viro9cdda8d2013-05-22 16:48:09 -04006100static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006101{
Al Viro9cdda8d2013-05-22 16:48:09 -04006102 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006103 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006104 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006105 struct btrfs_dir_item *di;
6106 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006107 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006108 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006109 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006110 struct list_head ins_list;
6111 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006112 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006113 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006114 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006115 char *name_ptr;
6116 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006117 int entries = 0;
6118 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006119 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006120 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006121
Al Viro9cdda8d2013-05-22 16:48:09 -04006122 if (!dir_emit_dots(file, ctx))
6123 return 0;
6124
David Woodhouse49593bf2008-08-17 17:08:36 +01006125 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006126 if (!path)
6127 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006128
Josef Bacik23b5ec72017-07-24 15:14:25 -04006129 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006130 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006131
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006132 INIT_LIST_HEAD(&ins_list);
6133 INIT_LIST_HEAD(&del_list);
6134 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006135
Josef Bacik23b5ec72017-07-24 15:14:25 -04006136again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006137 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006138 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006139 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006140
Chris Mason39279cc2007-06-12 06:35:45 -04006141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6142 if (ret < 0)
6143 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006144
6145 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006146 struct dir_entry *entry;
6147
Chris Mason5f39d392007-10-15 16:14:19 -04006148 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006149 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006150 if (slot >= btrfs_header_nritems(leaf)) {
6151 ret = btrfs_next_leaf(root, path);
6152 if (ret < 0)
6153 goto err;
6154 else if (ret > 0)
6155 break;
6156 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006157 }
Chris Mason3de45862008-11-17 21:02:50 -05006158
Chris Mason5f39d392007-10-15 16:14:19 -04006159 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6160
6161 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006162 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006163 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006164 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006165 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006166 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006167 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006168 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006169 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006170 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006171 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6172 PAGE_SIZE) {
6173 btrfs_release_path(path);
6174 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6175 if (ret)
6176 goto nopos;
6177 addr = private->filldir_buf;
6178 entries = 0;
6179 total_len = 0;
6180 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006181 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006182
6183 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006184 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006185 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006186 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6187 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006188 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006189 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006190 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006191 put_unaligned(location.objectid, &entry->ino);
6192 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006193 entries++;
6194 addr += sizeof(struct dir_entry) + name_len;
6195 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006196next:
6197 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006198 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006199 btrfs_release_path(path);
6200
6201 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6202 if (ret)
6203 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006204
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006205 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006206 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006207 goto nopos;
6208
Zach Browndb62efb2013-07-11 16:19:42 -07006209 /*
6210 * Stop new entries from being returned after we return the last
6211 * entry.
6212 *
6213 * New directory entries are assigned a strictly increasing
6214 * offset. This means that new entries created during readdir
6215 * are *guaranteed* to be seen in the future by that readdir.
6216 * This has broken buggy programs which operate on names as
6217 * they're returned by readdir. Until we re-use freed offsets
6218 * we have this hack to stop new entries from being returned
6219 * under the assumption that they'll never reach this huge
6220 * offset.
6221 *
6222 * This is being careful not to overflow 32bit loff_t unless the
6223 * last entry requires it because doing so has broken 32bit apps
6224 * in the past.
6225 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006226 if (ctx->pos >= INT_MAX)
6227 ctx->pos = LLONG_MAX;
6228 else
6229 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006230nopos:
6231 ret = 0;
6232err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006233 if (put)
6234 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006235 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006236 return ret;
6237}
6238
Chris Mason39279cc2007-06-12 06:35:45 -04006239/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006240 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006241 * inode changes. But, it is most likely to find the inode in cache.
6242 * FIXME, needs more benchmarking...there are no reasons other than performance
6243 * to keep or drop this code.
6244 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006245static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006246{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006248 struct btrfs_root *root = BTRFS_I(inode)->root;
6249 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006250 int ret;
6251
Josef Bacik72ac3c02012-05-23 14:13:11 -04006252 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006253 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006254
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006255 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006256 if (IS_ERR(trans))
6257 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006258
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006259 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006260 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006261 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006262 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006263 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006264 if (IS_ERR(trans))
6265 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006266
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006267 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006268 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006269 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006270 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006271 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006272
6273 return ret;
6274}
6275
6276/*
6277 * This is a copy of file_update_time. We need this so we can return error on
6278 * ENOSPC for updating the inode in the case of file write and mmap writes.
6279 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006280static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006281 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006282{
Alexander Block2bc5565282012-06-15 09:49:33 +02006283 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006284 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006285
6286 if (btrfs_root_readonly(root))
6287 return -EROFS;
6288
Josef Bacike41f9412012-03-26 09:46:47 -04006289 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006290 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006291 if (flags & S_CTIME)
6292 inode->i_ctime = *now;
6293 if (flags & S_MTIME)
6294 inode->i_mtime = *now;
6295 if (flags & S_ATIME)
6296 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006297 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006298}
6299
Chris Masond352ac62008-09-29 15:18:18 -04006300/*
6301 * find the highest existing sequence number in a directory
6302 * and then set the in-memory index_cnt variable to reflect
6303 * free sequence numbers
6304 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006305static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006306{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006307 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006308 struct btrfs_key key, found_key;
6309 struct btrfs_path *path;
6310 struct extent_buffer *leaf;
6311 int ret;
6312
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006313 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006314 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006315 key.offset = (u64)-1;
6316
6317 path = btrfs_alloc_path();
6318 if (!path)
6319 return -ENOMEM;
6320
6321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6322 if (ret < 0)
6323 goto out;
6324 /* FIXME: we should be able to handle this */
6325 if (ret == 0)
6326 goto out;
6327 ret = 0;
6328
6329 /*
6330 * MAGIC NUMBER EXPLANATION:
6331 * since we search a directory based on f_pos we have to start at 2
6332 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6333 * else has to start at 2
6334 */
6335 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006336 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006337 goto out;
6338 }
6339
6340 path->slots[0]--;
6341
6342 leaf = path->nodes[0];
6343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6344
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006345 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006346 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006347 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006348 goto out;
6349 }
6350
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006351 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006352out:
6353 btrfs_free_path(path);
6354 return ret;
6355}
6356
Chris Masond352ac62008-09-29 15:18:18 -04006357/*
6358 * helper to find a free sequence number in a given directory. This current
6359 * code is very simple, later versions will do smarter things in the btree
6360 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006361int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006362{
6363 int ret = 0;
6364
Nikolay Borisov877574e2017-02-20 13:50:33 +02006365 if (dir->index_cnt == (u64)-1) {
6366 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006367 if (ret) {
6368 ret = btrfs_set_inode_index_count(dir);
6369 if (ret)
6370 return ret;
6371 }
Josef Bacikaec74772008-07-24 12:12:38 -04006372 }
6373
Nikolay Borisov877574e2017-02-20 13:50:33 +02006374 *index = dir->index_cnt;
6375 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006376
6377 return ret;
6378}
6379
Chris Masonb0d5d102014-09-08 13:08:51 -07006380static int btrfs_insert_inode_locked(struct inode *inode)
6381{
6382 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006383
6384 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 args.root = BTRFS_I(inode)->root;
6386
6387 return insert_inode_locked4(inode,
6388 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6389 btrfs_find_actor, &args);
6390}
6391
Anand Jain19aee8d2017-07-18 17:37:05 +08006392/*
6393 * Inherit flags from the parent inode.
6394 *
6395 * Currently only the compression flags and the cow flags are inherited.
6396 */
6397static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6398{
6399 unsigned int flags;
6400
6401 if (!dir)
6402 return;
6403
6404 flags = BTRFS_I(dir)->flags;
6405
6406 if (flags & BTRFS_INODE_NOCOMPRESS) {
6407 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6408 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6409 } else if (flags & BTRFS_INODE_COMPRESS) {
6410 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6411 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6412 }
6413
6414 if (flags & BTRFS_INODE_NODATACOW) {
6415 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6416 if (S_ISREG(inode->i_mode))
6417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6418 }
6419
David Sterba7b6a2212018-03-26 18:40:21 +02006420 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006421}
6422
Chris Mason39279cc2007-06-12 06:35:45 -04006423static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6424 struct btrfs_root *root,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006425 struct user_namespace *mnt_userns,
Josef Bacikaec74772008-07-24 12:12:38 -04006426 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006427 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006428 u64 ref_objectid, u64 objectid,
6429 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006431 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006432 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006433 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006435 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006436 struct btrfs_inode_ref *ref;
6437 struct btrfs_key key[2];
6438 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006439 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006440 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006441 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006442 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006443
Chris Mason5f39d392007-10-15 16:14:19 -04006444 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006445 if (!path)
6446 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006447
Josef Bacik11a19a92019-09-09 10:12:04 -04006448 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006449 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006450 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006451 if (!inode) {
6452 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006454 }
Chris Mason39279cc2007-06-12 06:35:45 -04006455
Li Zefan581bb052011-04-20 10:06:11 +08006456 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006457 * O_TMPFILE, set link count to 0, so that after this point,
6458 * we fill in an inode item with the correct link count.
6459 */
6460 if (!name)
6461 set_nlink(inode, 0);
6462
6463 /*
Li Zefan581bb052011-04-20 10:06:11 +08006464 * we have to initialize this early, so we can reclaim the inode
6465 * number if we fail afterwards in this function.
6466 */
6467 inode->i_ino = objectid;
6468
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006469 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006470 trace_btrfs_inode_request(dir);
6471
Nikolay Borisov877574e2017-02-20 13:50:33 +02006472 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006473 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006474 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006475 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006476 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006477 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006478 } else if (dir) {
6479 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006480 }
6481 /*
6482 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006483 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006484 * number
6485 */
6486 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006487 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006488 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006489 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006490 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006491
Josef Bacik5dc562c2012-08-17 13:14:17 -04006492 /*
6493 * We could have gotten an inode number from somebody who was fsynced
6494 * and then removed in this same transaction, so let's just set full
6495 * sync since it will be a full sync anyway and this will blow away the
6496 * old info in the log.
6497 */
6498 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6499
Chris Mason9c583092008-01-29 15:15:18 -05006500 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006501 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006502 key[0].offset = 0;
6503
Chris Mason9c583092008-01-29 15:15:18 -05006504 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006505
6506 if (name) {
6507 /*
6508 * Start new inodes with an inode_ref. This is slightly more
6509 * efficient for small numbers of hard links since they will
6510 * be packed into one item. Extended refs will kick in if we
6511 * add more hard links than can fit in the ref item.
6512 */
6513 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006514 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006515 key[1].offset = ref_objectid;
6516
6517 sizes[1] = name_len + sizeof(*ref);
6518 }
Chris Mason9c583092008-01-29 15:15:18 -05006519
Chris Masonb0d5d102014-09-08 13:08:51 -07006520 location = &BTRFS_I(inode)->location;
6521 location->objectid = objectid;
6522 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006523 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006524
6525 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006526 if (ret < 0) {
6527 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006528 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006529 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006530
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006531 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006532 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006533 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006534
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006535 inode_init_owner(mnt_userns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006536 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306537
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006538 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306539 inode->i_atime = inode->i_mtime;
6540 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006541 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306542
Chris Mason5f39d392007-10-15 16:14:19 -04006543 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6544 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006545 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006546 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006547 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006548
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006549 if (name) {
6550 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6551 struct btrfs_inode_ref);
6552 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6553 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6554 ptr = (unsigned long)(ref + 1);
6555 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6556 }
Chris Mason9c583092008-01-29 15:15:18 -05006557
Chris Mason5f39d392007-10-15 16:14:19 -04006558 btrfs_mark_buffer_dirty(path->nodes[0]);
6559 btrfs_free_path(path);
6560
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006561 btrfs_inherit_iflags(inode, dir);
6562
Al Viro569254b2011-07-24 17:08:40 -04006563 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006564 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006565 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006566 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006567 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6568 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006569 }
6570
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006571 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006572
6573 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006574 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006575
Alexander Block8ea05e32012-07-25 17:35:53 +02006576 btrfs_update_root_times(trans, root);
6577
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006578 ret = btrfs_inode_inherit_props(trans, inode, dir);
6579 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006580 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006581 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006582 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006583
Chris Mason39279cc2007-06-12 06:35:45 -04006584 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006585
6586fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006587 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006588fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006589 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006590 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006591 btrfs_free_path(path);
6592 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006593}
6594
Chris Masond352ac62008-09-29 15:18:18 -04006595/*
6596 * utility function to add 'inode' into 'parent_inode' with
6597 * a give name and a given sequence number.
6598 * if 'add_backref' is true, also insert a backref from the
6599 * inode to the parent directory.
6600 */
Chris Masone02119d2008-09-05 16:13:11 -04006601int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006602 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006603 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006604{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006605 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006606 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006607 struct btrfs_root *root = parent_inode->root;
6608 u64 ino = btrfs_ino(inode);
6609 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006610
Li Zefan33345d012011-04-20 10:31:50 +08006611 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006612 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006613 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006614 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006615 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006616 key.offset = 0;
6617 }
Chris Mason39279cc2007-06-12 06:35:45 -04006618
Li Zefan33345d012011-04-20 10:31:50 +08006619 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006620 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006621 root->root_key.objectid, parent_ino,
6622 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006623 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006624 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6625 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006626 }
6627
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006628 /* Nothing to clean up yet */
6629 if (ret)
6630 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006631
Lu Fengqi684572d2018-08-04 21:10:57 +08006632 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006633 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006634 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006635 goto fail_dir_item;
6636 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006637 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006638 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006639 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006640
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006641 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006642 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006643 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006644 /*
6645 * If we are replaying a log tree, we do not want to update the mtime
6646 * and ctime of the parent directory with the current time, since the
6647 * log replay procedure is responsible for setting them to their correct
6648 * values (the ones it had when the fsync was done).
6649 */
6650 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6651 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6652
6653 parent_inode->vfs_inode.i_mtime = now;
6654 parent_inode->vfs_inode.i_ctime = now;
6655 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006656 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006657 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006658 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006659 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006660
6661fail_dir_item:
6662 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6663 u64 local_index;
6664 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006665 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006666 root->root_key.objectid, parent_ino,
6667 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006668 if (err)
6669 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006670 } else if (add_backref) {
6671 u64 local_index;
6672 int err;
6673
6674 err = btrfs_del_inode_ref(trans, root, name, name_len,
6675 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006676 if (err)
6677 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006678 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006679
6680 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006681 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006682}
6683
6684static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006685 struct btrfs_inode *dir, struct dentry *dentry,
6686 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006687{
Josef Bacika1b075d2010-11-19 20:36:11 +00006688 int err = btrfs_add_link(trans, dir, inode,
6689 dentry->d_name.name, dentry->d_name.len,
6690 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006691 if (err > 0)
6692 err = -EEXIST;
6693 return err;
6694}
6695
Christian Brauner549c7292021-01-21 14:19:43 +01006696static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6697 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006698{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006699 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006700 struct btrfs_trans_handle *trans;
6701 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006702 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006703 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006704 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006705 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006706
Josef Bacik9ed74f22009-09-11 16:12:44 -04006707 /*
6708 * 2 for inode item and ref
6709 * 2 for dir items
6710 * 1 for xattr if selinux is on
6711 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 trans = btrfs_start_transaction(root, 5);
6713 if (IS_ERR(trans))
6714 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006715
Nikolay Borisov543068a2020-12-07 17:32:33 +02006716 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006717 if (err)
6718 goto out_unlock;
6719
Christian Brauner72105272021-07-27 12:48:44 +02006720 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006721 dentry->d_name.name, dentry->d_name.len,
6722 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006723 if (IS_ERR(inode)) {
6724 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006725 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006726 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006727 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006728
Casey Schauflerad19db72011-12-15 10:09:07 -05006729 /*
6730 * If the active LSM wants to access the inode during
6731 * d_instantiate it needs these. Smack checks to see
6732 * if the filesystem supports xattrs by looking at the
6733 * ops vector.
6734 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006735 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006736 init_special_inode(inode, inode->i_mode, rdev);
6737
6738 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006739 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006740 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006741
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006742 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6743 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006744 if (err)
6745 goto out_unlock;
6746
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006747 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006748 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006749
Josef Bacik618e21d2007-07-11 10:18:17 -04006750out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006751 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006752 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006753 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006754 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006755 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006756 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006757 return err;
6758}
6759
Christian Brauner549c7292021-01-21 14:19:43 +01006760static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6761 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006762{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006763 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006764 struct btrfs_trans_handle *trans;
6765 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006766 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006767 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006768 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006769 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006770
Josef Bacik9ed74f22009-09-11 16:12:44 -04006771 /*
6772 * 2 for inode item and ref
6773 * 2 for dir items
6774 * 1 for xattr if selinux is on
6775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006776 trans = btrfs_start_transaction(root, 5);
6777 if (IS_ERR(trans))
6778 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006779
Nikolay Borisov543068a2020-12-07 17:32:33 +02006780 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006781 if (err)
6782 goto out_unlock;
6783
Christian Braunere93ca492021-07-27 12:48:45 +02006784 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006785 dentry->d_name.name, dentry->d_name.len,
6786 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006787 if (IS_ERR(inode)) {
6788 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006789 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006790 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006791 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006792 /*
6793 * If the active LSM wants to access the inode during
6794 * d_instantiate it needs these. Smack checks to see
6795 * if the filesystem supports xattrs by looking at the
6796 * ops vector.
6797 */
6798 inode->i_fop = &btrfs_file_operations;
6799 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006800 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006801
6802 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6803 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006804 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006805
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006806 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006807 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006808 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006809
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006810 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6811 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006812 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006813 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006814
Al Viro1e2e5472018-05-04 08:23:01 -04006815 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006816
Chris Mason39279cc2007-06-12 06:35:45 -04006817out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006818 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006819 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006820 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006821 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006822 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006823 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006824 return err;
6825}
6826
6827static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6828 struct dentry *dentry)
6829{
Filipe Manana271dba452016-01-05 16:24:05 +00006830 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006831 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006832 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006834 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006835 int err;
6836 int drop_inode = 0;
6837
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006838 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006839 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006840 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006841
Mark Fashehf1863732012-08-08 11:32:27 -07006842 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006843 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006844
Nikolay Borisov877574e2017-02-20 13:50:33 +02006845 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006846 if (err)
6847 goto fail;
6848
Yan, Zhenga22285a2010-05-16 10:48:46 -04006849 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006850 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006851 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006852 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006853 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006854 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006855 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006856 if (IS_ERR(trans)) {
6857 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006858 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006859 goto fail;
6860 }
Chris Mason5f39d392007-10-15 16:14:19 -04006861
Miao Xie67de1172013-12-26 13:07:06 +08006862 /* There are several dir indexes for this inode, clear the cache. */
6863 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006864 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006865 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006866 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006867 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006868 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006869
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006870 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6871 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006872
Yan, Zhenga5719522009-09-24 09:17:31 -04006873 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006874 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006875 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006876 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006877
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006878 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006879 if (err)
6880 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006881 if (inode->i_nlink == 1) {
6882 /*
6883 * If new hard link count is 1, it's a file created
6884 * with open(2) O_TMPFILE flag.
6885 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006886 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006887 if (err)
6888 goto fail;
6889 }
Al Viro08c422c2011-12-23 07:58:13 -05006890 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006891 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006892 }
Chris Mason39279cc2007-06-12 06:35:45 -04006893
Chris Mason1832a6d2007-12-21 16:27:21 -05006894fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006895 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006896 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006897 if (drop_inode) {
6898 inode_dec_link_count(inode);
6899 iput(inode);
6900 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006901 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006902 return err;
6903}
6904
Christian Brauner549c7292021-01-21 14:19:43 +01006905static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6906 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006907{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006908 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006909 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006910 struct btrfs_trans_handle *trans;
6911 struct btrfs_root *root = BTRFS_I(dir)->root;
6912 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006913 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006914 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006915
Josef Bacik9ed74f22009-09-11 16:12:44 -04006916 /*
6917 * 2 items for inode and ref
6918 * 2 items for dir items
6919 * 1 for xattr if selinux is on
6920 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006921 trans = btrfs_start_transaction(root, 5);
6922 if (IS_ERR(trans))
6923 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006924
Nikolay Borisov543068a2020-12-07 17:32:33 +02006925 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006926 if (err)
6927 goto out_fail;
6928
Christian Braunerb0b3e442021-07-27 12:48:46 +02006929 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02006930 dentry->d_name.name, dentry->d_name.len,
6931 btrfs_ino(BTRFS_I(dir)), objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01006932 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006933 if (IS_ERR(inode)) {
6934 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006935 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006936 goto out_fail;
6937 }
Chris Mason5f39d392007-10-15 16:14:19 -04006938
Chris Masonb0d5d102014-09-08 13:08:51 -07006939 /* these must be set before we unlock the inode */
6940 inode->i_op = &btrfs_dir_inode_operations;
6941 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006942
Eric Paris2a7dba32011-02-01 11:05:39 -05006943 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006944 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006945 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006946
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006947 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006948 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006949 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006950 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006951
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006952 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6953 dentry->d_name.name,
6954 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006955 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006956 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006957
Al Viro1e2e5472018-05-04 08:23:01 -04006958 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006959
6960out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006961 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006962 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006963 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006964 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006965 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006966 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006967 return err;
6968}
6969
Chris Masonc8b97812008-10-29 14:49:59 -04006970static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006971 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006972 size_t pg_offset, u64 extent_offset,
6973 struct btrfs_file_extent_item *item)
6974{
6975 int ret;
6976 struct extent_buffer *leaf = path->nodes[0];
6977 char *tmp;
6978 size_t max_size;
6979 unsigned long inline_size;
6980 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006981 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006982
6983 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006984 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006985 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6986 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006987 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006988 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006989 if (!tmp)
6990 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006991 ptr = btrfs_file_extent_inline_start(item);
6992
6993 read_extent_buffer(leaf, tmp, ptr, inline_size);
6994
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006995 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006996 ret = btrfs_decompress(compress_type, tmp, page,
6997 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006998
6999 /*
7000 * decompression code contains a memset to fill in any space between the end
7001 * of the uncompressed data and the end of max_size in case the decompressed
7002 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7003 * the end of an inline extent and the beginning of the next block, so we
7004 * cover that region here.
7005 */
7006
Ira Weinyd048b9c2021-05-04 18:40:07 -07007007 if (max_size + pg_offset < PAGE_SIZE)
7008 memzero_page(page, pg_offset + max_size,
7009 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04007010 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04007011 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04007012}
7013
Omar Sandoval39b07b52019-12-02 17:34:23 -08007014/**
7015 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
7016 * @inode: file to search in
7017 * @page: page to read extent data into if the extent is inline
7018 * @pg_offset: offset into @page to copy to
7019 * @start: file offset
7020 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04007021 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08007022 * This returns the first &struct extent_map which overlaps with the given
7023 * range, reading it from the B-tree and caching it if necessary. Note that
7024 * there may be more extents which overlap the given range after the returned
7025 * extent_map.
7026 *
7027 * If @page is not NULL and the extent is inline, this also reads the extent
7028 * data directly into the page and marks the extent up to date in the io_tree.
7029 *
7030 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04007031 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007032struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08007033 struct page *page, size_t pg_offset,
7034 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04007035{
David Sterba3ffbd682018-06-29 10:56:42 +02007036 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007037 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007038 u64 extent_start = 0;
7039 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007040 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07007041 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04007042 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007043 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007044 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007045 struct extent_buffer *leaf;
7046 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007047 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007048 struct extent_map_tree *em_tree = &inode->extent_tree;
7049 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007050
Chris Mason890871b2009-09-02 16:24:52 -04007051 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007052 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007053 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007054
Chris Masona52d9a82007-08-27 16:49:44 -04007055 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007056 if (em->start > start || em->start + em->len <= start)
7057 free_extent_map(em);
7058 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007059 free_extent_map(em);
7060 else
7061 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007062 }
David Sterba172ddd62011-04-21 00:48:27 +02007063 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007064 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007065 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05007066 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Masond1310b22008-01-24 16:13:08 -05007068 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007069 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007070 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007071 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007072
Liu Bobee6ec82018-08-17 05:05:28 +08007073 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04007074 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007075 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08007076 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04007077 }
7078
Liu Bobee6ec82018-08-17 05:05:28 +08007079 /* Chances are we'll be called again, so go ahead and do readahead */
7080 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04007081
7082 /*
7083 * The same explanation in load_free_space_cache applies here as well,
7084 * we only read when we're loading the free space cache, and at that
7085 * point the commit_root has everything we need.
7086 */
7087 if (btrfs_is_free_space_inode(inode)) {
7088 path->search_commit_root = 1;
7089 path->skip_locking = 1;
7090 }
Josef Bacik51899412020-08-20 11:46:01 -04007091
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007092 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007093 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007094 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007095 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007096 if (path->slots[0] == 0)
7097 goto not_found;
7098 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007099 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007100 }
7101
Chris Mason5f39d392007-10-15 16:14:19 -04007102 leaf = path->nodes[0];
7103 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007104 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007106 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007107 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007108 /*
7109 * If we backup past the first extent we want to move forward
7110 * and see if there is an extent in front of us, otherwise we'll
7111 * say there is a hole for our whole search range which can
7112 * cause problems.
7113 */
7114 extent_end = start;
7115 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007116 }
7117
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007118 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007119 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00007120 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007121 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7122 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007123 /* Only regular file could have regular/prealloc extent */
7124 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007125 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007126 btrfs_crit(fs_info,
7127 "regular/prealloc extent found for non-regular inode %llu",
7128 btrfs_ino(inode));
7129 goto out;
7130 }
Liu Bo09ed2f12017-03-10 11:09:48 -08007131 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7132 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007133 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08007134 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7135 path->slots[0],
7136 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007137 }
Josef Bacik25a50342013-10-14 12:08:38 -04007138next:
Yan Zheng9036c102008-10-30 14:19:41 -04007139 if (start >= extent_end) {
7140 path->slots[0]++;
7141 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7142 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007143 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007144 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007145 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007146 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007147
Yan Zheng9036c102008-10-30 14:19:41 -04007148 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007149 }
Yan Zheng9036c102008-10-30 14:19:41 -04007150 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7151 if (found_key.objectid != objectid ||
7152 found_key.type != BTRFS_EXTENT_DATA_KEY)
7153 goto not_found;
7154 if (start + len <= found_key.offset)
7155 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007156 if (start > found_key.offset)
7157 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007158
7159 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007160 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007161 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007162 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007163 em->block_start = EXTENT_MAP_HOLE;
7164 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007165 }
7166
Omar Sandoval39b07b52019-12-02 17:34:23 -08007167 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007168
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007169 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7170 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007171 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007172 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007173 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007174 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007175 size_t size;
7176 size_t extent_offset;
7177 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007178
Omar Sandoval39b07b52019-12-02 17:34:23 -08007179 if (!page)
Yan689f9342007-10-29 11:41:07 -04007180 goto out;
Yan689f9342007-10-29 11:41:07 -04007181
Qu Wenruoe41ca582018-06-06 15:41:49 +08007182 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007183 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007184 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7185 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007186 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007188 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007189 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007190 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007191
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007192 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007193 if (btrfs_file_extent_compression(leaf, item) !=
7194 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007195 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007196 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007197 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007198 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007199 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007200 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007201 read_extent_buffer(leaf, map + pg_offset, ptr,
7202 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007203 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007204 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007205 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007206 copy_size);
7207 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007208 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007209 }
Chris Mason179e29e2007-11-01 11:28:41 -04007210 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007211 }
Chris Masond1310b22008-01-24 16:13:08 -05007212 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007213 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007214 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007215 }
7216not_found:
7217 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007218 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007219 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007220 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007221insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007222 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007223 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007224 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007225 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007226 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7227 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007228 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007229 goto out;
7230 }
Chris Masond1310b22008-01-24 16:13:08 -05007231
Chris Mason890871b2009-09-02 16:24:52 -04007232 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007233 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007234 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007235out:
Liu Boc6414282018-08-23 07:36:17 +08007236 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007237
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007238 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007239
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007240 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007241 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007242 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007243 }
7244 return em;
7245}
7246
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007247struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007248 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007249{
7250 struct extent_map *em;
7251 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007252 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007253 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007254 u64 delalloc_len;
7255 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007256 int err = 0;
7257
Omar Sandoval39b07b52019-12-02 17:34:23 -08007258 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007259 if (IS_ERR(em))
7260 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007261 /*
7262 * If our em maps to:
7263 * - a hole or
7264 * - a pre-alloc extent,
7265 * there might actually be delalloc bytes behind it.
7266 */
7267 if (em->block_start != EXTENT_MAP_HOLE &&
7268 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7269 return em;
7270 else
7271 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007272
7273 /* check to see if we've wrapped (len == -1 or similar) */
7274 end = start + len;
7275 if (end < start)
7276 end = (u64)-1;
7277 else
7278 end -= 1;
7279
7280 em = NULL;
7281
7282 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007283 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007284 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007285 delalloc_end = delalloc_start + delalloc_len;
7286 if (delalloc_end < delalloc_start)
7287 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007288
7289 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007290 * We didn't find anything useful, return the original results from
7291 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007292 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007293 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007294 em = hole_em;
7295 hole_em = NULL;
7296 goto out;
7297 }
7298
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007299 /*
7300 * Adjust the delalloc_start to make sure it doesn't go backwards from
7301 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007302 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007303 delalloc_start = max(start, delalloc_start);
7304 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007305
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007306 if (delalloc_len > 0) {
7307 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007308 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007309 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007310
David Sterba172ddd62011-04-21 00:48:27 +02007311 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007312 if (!em) {
7313 err = -ENOMEM;
7314 goto out;
7315 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007316
7317 ASSERT(hole_em);
7318 /*
7319 * When btrfs_get_extent can't find anything it returns one
7320 * huge hole
7321 *
7322 * Make sure what it found really fits our range, and adjust to
7323 * make sure it is based on the start from the caller
7324 */
7325 if (hole_end <= start || hole_em->start > end) {
7326 free_extent_map(hole_em);
7327 hole_em = NULL;
7328 } else {
7329 hole_start = max(hole_em->start, start);
7330 hole_len = hole_end - hole_start;
7331 }
7332
7333 if (hole_em && delalloc_start > hole_start) {
7334 /*
7335 * Our hole starts before our delalloc, so we have to
7336 * return just the parts of the hole that go until the
7337 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007338 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007339 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007340 em->start = hole_start;
7341 em->orig_start = hole_start;
7342 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007343 * Don't adjust block start at all, it is fixed at
7344 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007345 */
7346 em->block_start = hole_em->block_start;
7347 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007348 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7349 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007350 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007351 /*
7352 * Hole is out of passed range or it starts after
7353 * delalloc range
7354 */
7355 em->start = delalloc_start;
7356 em->len = delalloc_len;
7357 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007358 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007359 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007360 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007361 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007362 return hole_em;
7363 }
7364out:
7365
7366 free_extent_map(hole_em);
7367 if (err) {
7368 free_extent_map(em);
7369 return ERR_PTR(err);
7370 }
7371 return em;
7372}
7373
Nikolay Borisov64f54182020-06-03 08:55:31 +03007374static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007375 const u64 start,
7376 const u64 len,
7377 const u64 orig_start,
7378 const u64 block_start,
7379 const u64 block_len,
7380 const u64 orig_block_len,
7381 const u64 ram_bytes,
7382 const int type)
7383{
7384 struct extent_map *em = NULL;
7385 int ret;
7386
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007387 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007388 em = create_io_em(inode, start, len, orig_start, block_start,
7389 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007390 BTRFS_COMPRESS_NONE, /* compress_type */
7391 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007392 if (IS_ERR(em))
7393 goto out;
7394 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007395 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7396 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007397 if (ret) {
7398 if (em) {
7399 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007400 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007401 }
7402 em = ERR_PTR(ret);
7403 }
7404 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007405
7406 return em;
7407}
7408
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007409static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007410 u64 start, u64 len)
7411{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007412 struct btrfs_root *root = inode->root;
7413 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007414 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007415 struct btrfs_key ins;
7416 u64 alloc_hint;
7417 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007418
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007419 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007420 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007421 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007422 if (ret)
7423 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007424
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007425 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007426 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007427 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007428 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007429 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007430 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7431 1);
Josef Bacik00361582013-08-14 14:02:47 -04007432
Josef Bacik4b46fce2010-05-23 11:00:55 -04007433 return em;
7434}
7435
Anand Jainf4639632021-02-10 21:25:16 -08007436static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007437{
7438 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007439 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007440
7441 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7442 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007443 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007444 if (block_group)
7445 btrfs_put_block_group(block_group);
7446 return readonly;
7447}
7448
Chris Mason46bfbb52010-05-26 11:04:10 -04007449/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007450 * Check if we can do nocow write into the range [@offset, @offset + @len)
7451 *
7452 * @offset: File offset
7453 * @len: The length to write, will be updated to the nocow writeable
7454 * range
7455 * @orig_start: (optional) Return the original file offset of the file extent
7456 * @orig_len: (optional) Return the original on-disk length of the file extent
7457 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007458 * @strict: if true, omit optimizations that might force us into unnecessary
7459 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007460 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007461 * Return:
7462 * >0 and update @len if we can do nocow write
7463 * 0 if we can't do nocow write
7464 * <0 if error happened
7465 *
7466 * NOTE: This only checks the file extents, caller is responsible to wait for
7467 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 */
Josef Bacik00361582013-08-14 14:02:47 -04007469noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007470 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007471 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007472{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 struct btrfs_path *path;
7475 int ret;
7476 struct extent_buffer *leaf;
7477 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007478 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 struct btrfs_file_extent_item *fi;
7480 struct btrfs_key key;
7481 u64 disk_bytenr;
7482 u64 backref_offset;
7483 u64 extent_end;
7484 u64 num_bytes;
7485 int slot;
7486 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007487 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007488
Chris Mason46bfbb52010-05-26 11:04:10 -04007489 path = btrfs_alloc_path();
7490 if (!path)
7491 return -ENOMEM;
7492
David Sterbaf85b7372017-01-20 14:54:07 +01007493 ret = btrfs_lookup_file_extent(NULL, root, path,
7494 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007495 if (ret < 0)
7496 goto out;
7497
7498 slot = path->slots[0];
7499 if (ret == 1) {
7500 if (slot == 0) {
7501 /* can't find the item, must cow */
7502 ret = 0;
7503 goto out;
7504 }
7505 slot--;
7506 }
7507 ret = 0;
7508 leaf = path->nodes[0];
7509 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007510 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007511 key.type != BTRFS_EXTENT_DATA_KEY) {
7512 /* not our file or wrong item type, must cow */
7513 goto out;
7514 }
7515
7516 if (key.offset > offset) {
7517 /* Wrong offset, must cow */
7518 goto out;
7519 }
7520
7521 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7522 found_type = btrfs_file_extent_type(leaf, fi);
7523 if (found_type != BTRFS_FILE_EXTENT_REG &&
7524 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7525 /* not a regular extent, must cow */
7526 goto out;
7527 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007528
7529 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7530 goto out;
7531
Miao Xiee77751a2013-12-27 21:11:50 +08007532 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7533 if (extent_end <= offset)
7534 goto out;
7535
Chris Mason46bfbb52010-05-26 11:04:10 -04007536 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007537 if (disk_bytenr == 0)
7538 goto out;
7539
7540 if (btrfs_file_extent_compression(leaf, fi) ||
7541 btrfs_file_extent_encryption(leaf, fi) ||
7542 btrfs_file_extent_other_encoding(leaf, fi))
7543 goto out;
7544
Ethan Lien78d42952018-05-17 14:58:29 +08007545 /*
7546 * Do the same check as in btrfs_cross_ref_exist but without the
7547 * unnecessary search.
7548 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007549 if (!strict &&
7550 (btrfs_file_extent_generation(leaf, fi) <=
7551 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007552 goto out;
7553
Chris Mason46bfbb52010-05-26 11:04:10 -04007554 backref_offset = btrfs_file_extent_offset(leaf, fi);
7555
Josef Bacik7ee9e442013-06-21 16:37:03 -04007556 if (orig_start) {
7557 *orig_start = key.offset - backref_offset;
7558 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7559 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7560 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007561
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007562 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007563 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007564
7565 num_bytes = min(offset + *len, extent_end) - offset;
7566 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7567 u64 range_end;
7568
Jeff Mahoneyda170662016-06-15 09:22:56 -04007569 range_end = round_up(offset + num_bytes,
7570 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007571 ret = test_range_bit(io_tree, offset, range_end,
7572 EXTENT_DELALLOC, 0, NULL);
7573 if (ret) {
7574 ret = -EAGAIN;
7575 goto out;
7576 }
7577 }
7578
Josef Bacik1bda19e2013-10-18 12:10:36 -04007579 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007580
7581 /*
7582 * look for other files referencing this extent, if we
7583 * find any we must cow
7584 */
Josef Bacik00361582013-08-14 14:02:47 -04007585
Liu Boe4c3b2d2017-01-30 12:25:28 -08007586 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007587 key.offset - backref_offset, disk_bytenr,
7588 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007589 if (ret) {
7590 ret = 0;
7591 goto out;
7592 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007593
7594 /*
7595 * adjust disk_bytenr and num_bytes to cover just the bytes
7596 * in this extent we are about to write. If there
7597 * are any csums in that range we have to cow in order
7598 * to keep the csums correct
7599 */
7600 disk_bytenr += backref_offset;
7601 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007602 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7603 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007604 /*
7605 * all of the above have passed, it is safe to overwrite this extent
7606 * without cow
7607 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007608 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007609 ret = 1;
7610out:
7611 btrfs_free_path(path);
7612 return ret;
7613}
7614
Josef Bacikeb838e72012-07-31 16:28:48 -04007615static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007616 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007617{
7618 struct btrfs_ordered_extent *ordered;
7619 int ret = 0;
7620
7621 while (1) {
7622 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007623 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 /*
7625 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007626 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 * extents in this range.
7628 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007629 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 lockend - lockstart + 1);
7631
7632 /*
7633 * We need to make sure there are no buffered pages in this
7634 * range either, we could have raced between the invalidate in
7635 * generic_file_direct_write and locking the extent. The
7636 * invalidate needs to happen so that reads after a write do not
7637 * get stale data.
7638 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007639 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007640 (!writing || !filemap_range_has_page(inode->i_mapping,
7641 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007642 break;
7643
7644 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007645 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007646
7647 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007648 /*
7649 * If we are doing a DIO read and the ordered extent we
7650 * found is for a buffered write, we can not wait for it
7651 * to complete and retry, because if we do so we can
7652 * deadlock with concurrent buffered writes on page
7653 * locks. This happens only if our DIO read covers more
7654 * than one extent map, if at this point has already
7655 * created an ordered extent for a previous extent map
7656 * and locked its range in the inode's io tree, and a
7657 * concurrent write against that previous extent map's
7658 * range and this range started (we unlock the ranges
7659 * in the io tree only when the bios complete and
7660 * buffered writes always lock pages before attempting
7661 * to lock range in the io tree).
7662 */
7663 if (writing ||
7664 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007665 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007666 else
7667 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007668 btrfs_put_ordered_extent(ordered);
7669 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007671 * We could trigger writeback for this range (and wait
7672 * for it to complete) and then invalidate the pages for
7673 * this range (through invalidate_inode_pages2_range()),
7674 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007675 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007676 * triggered a readahead) on a page lock due to an
7677 * ordered dio extent we created before but did not have
7678 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007679 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007680 * ordered extent to complete while holding a lock on
7681 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007683 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007684 }
7685
Filipe Mananaade77022016-02-18 14:28:55 +00007686 if (ret)
7687 break;
7688
Josef Bacikeb838e72012-07-31 16:28:48 -04007689 cond_resched();
7690 }
7691
7692 return ret;
7693}
7694
Liu Bo6f9994d2017-01-31 07:50:22 -08007695/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007696static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7697 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007698 u64 block_len, u64 orig_block_len,
7699 u64 ram_bytes, int compress_type,
7700 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007701{
7702 struct extent_map_tree *em_tree;
7703 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007704 int ret;
7705
Liu Bo6f9994d2017-01-31 07:50:22 -08007706 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7707 type == BTRFS_ORDERED_COMPRESSED ||
7708 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007709 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007710
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007711 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007712 em = alloc_extent_map();
7713 if (!em)
7714 return ERR_PTR(-ENOMEM);
7715
7716 em->start = start;
7717 em->orig_start = orig_start;
7718 em->len = len;
7719 em->block_len = block_len;
7720 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007721 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007722 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007723 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007724 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007725 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007726 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007727 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007728 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7729 em->compress_type = compress_type;
7730 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007731
7732 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007733 btrfs_drop_extent_cache(inode, em->start,
7734 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007736 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007737 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007738 /*
7739 * The caller has taken lock_extent(), who could race with us
7740 * to add em?
7741 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007742 } while (ret == -EEXIST);
7743
7744 if (ret) {
7745 free_extent_map(em);
7746 return ERR_PTR(ret);
7747 }
7748
Liu Bo6f9994d2017-01-31 07:50:22 -08007749 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007750 return em;
7751}
7752
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007753
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007754static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007755 struct inode *inode,
7756 struct btrfs_dio_data *dio_data,
7757 u64 start, u64 len)
7758{
7759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7760 struct extent_map *em = *map;
7761 int ret = 0;
7762
7763 /*
7764 * We don't allocate a new extent in the following cases
7765 *
7766 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7767 * existing extent.
7768 * 2) The extent is marked as PREALLOC. We're good to go here and can
7769 * just use the extent.
7770 *
7771 */
7772 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7773 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7774 em->block_start != EXTENT_MAP_HOLE)) {
7775 int type;
7776 u64 block_start, orig_start, orig_block_len, ram_bytes;
7777
7778 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7779 type = BTRFS_ORDERED_PREALLOC;
7780 else
7781 type = BTRFS_ORDERED_NOCOW;
7782 len = min(len, em->len - (start - em->start));
7783 block_start = em->block_start + (start - em->start);
7784
7785 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007786 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007787 btrfs_inc_nocow_writers(fs_info, block_start)) {
7788 struct extent_map *em2;
7789
Nikolay Borisov64f54182020-06-03 08:55:31 +03007790 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007791 orig_start, block_start,
7792 len, orig_block_len,
7793 ram_bytes, type);
7794 btrfs_dec_nocow_writers(fs_info, block_start);
7795 if (type == BTRFS_ORDERED_PREALLOC) {
7796 free_extent_map(em);
7797 *map = em = em2;
7798 }
7799
7800 if (em2 && IS_ERR(em2)) {
7801 ret = PTR_ERR(em2);
7802 goto out;
7803 }
7804 /*
7805 * For inode marked NODATACOW or extent marked PREALLOC,
7806 * use the existing or preallocated extent, so does not
7807 * need to adjust btrfs_space_info's bytes_may_use.
7808 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007809 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007810 goto skip_cow;
7811 }
7812 }
7813
7814 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007815 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007816 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007817 if (IS_ERR(em)) {
7818 ret = PTR_ERR(em);
7819 goto out;
7820 }
7821
7822 len = min(len, em->len - (start - em->start));
7823
7824skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007825 /*
7826 * Need to update the i_size under the extent lock so buffered
7827 * readers will get the updated i_size when we unlock.
7828 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007829 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007830 i_size_write(inode, start + len);
7831
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007832 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007833out:
7834 return ret;
7835}
7836
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007837static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7838 loff_t length, unsigned int flags, struct iomap *iomap,
7839 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007843 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307844 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007845 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007846 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007847 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007848 u64 len = length;
7849 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007850
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007851 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007852 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007853
Josef Bacikc3298612012-08-03 16:49:19 -04007854 lockstart = start;
7855 lockend = start + len - 1;
7856
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007857 /*
7858 * The generic stuff only does filemap_write_and_wait_range, which
7859 * isn't enough if we've written compressed pages to this area, so we
7860 * need to flush the dirty pages again to make absolutely sure that any
7861 * outstanding dirty pages are on disk.
7862 */
7863 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7864 &BTRFS_I(inode)->runtime_flags)) {
7865 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7866 start + length - 1);
7867 if (ret)
7868 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007869 }
7870
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007871 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7872 if (!dio_data)
7873 return -ENOMEM;
7874
7875 dio_data->length = length;
7876 if (write) {
7877 dio_data->reserve = round_up(length, fs_info->sectorsize);
7878 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7879 &dio_data->data_reserved,
7880 start, dio_data->reserve);
7881 if (ret) {
7882 extent_changeset_free(dio_data->data_reserved);
7883 kfree(dio_data);
7884 return ret;
7885 }
7886 }
7887 iomap->private = dio_data;
7888
7889
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 /*
7891 * If this errors out it's because we couldn't invalidate pagecache for
7892 * this range and we need to fallback to buffered.
7893 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007894 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007895 ret = -ENOTBLK;
7896 goto err;
7897 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007898
Omar Sandoval39b07b52019-12-02 17:34:23 -08007899 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007900 if (IS_ERR(em)) {
7901 ret = PTR_ERR(em);
7902 goto unlock_err;
7903 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904
7905 /*
7906 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7907 * io. INLINE is special, and we could probably kludge it in here, but
7908 * it's still buffered so for safety lets just fall back to the generic
7909 * buffered path.
7910 *
7911 * For COMPRESSED we _have_ to read the entire extent in so we can
7912 * decompress it, so there will be buffering required no matter what we
7913 * do, so go ahead and fallback to buffered.
7914 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007915 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916 * to buffered IO. Don't blame me, this is the price we pay for using
7917 * the generic code.
7918 */
7919 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7920 em->block_start == EXTENT_MAP_INLINE) {
7921 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007922 ret = -ENOTBLK;
7923 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 }
7925
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007926 len = min(len, em->len - (start - em->start));
7927 if (write) {
7928 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7929 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007930 if (ret < 0)
7931 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007932 unlock_extents = true;
7933 /* Recalc len in case the new em is smaller than requested */
7934 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007935 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007936 /*
7937 * We need to unlock only the end area that we aren't using.
7938 * The rest is going to be unlocked by the endio routine.
7939 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007940 lockstart = start + len;
7941 if (lockstart < lockend)
7942 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007943 }
7944
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007945 if (unlock_extents)
7946 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7947 lockstart, lockend, &cached_state);
7948 else
7949 free_extent_state(cached_state);
7950
7951 /*
7952 * Translate extent map information to iomap.
7953 * We trim the extents (and move the addr) even though iomap code does
7954 * that, since we have locked only the parts we are performing I/O in.
7955 */
7956 if ((em->block_start == EXTENT_MAP_HOLE) ||
7957 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7958 iomap->addr = IOMAP_NULL_ADDR;
7959 iomap->type = IOMAP_HOLE;
7960 } else {
7961 iomap->addr = em->block_start + (start - em->start);
7962 iomap->type = IOMAP_MAPPED;
7963 }
7964 iomap->offset = start;
7965 iomap->bdev = fs_info->fs_devices->latest_bdev;
7966 iomap->length = len;
7967
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007968 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007969 iomap->flags |= IOMAP_F_ZONE_APPEND;
7970
Josef Bacik4b46fce2010-05-23 11:00:55 -04007971 free_extent_map(em);
7972
7973 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007974
7975unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007976 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7977 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007978err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007979 if (dio_data) {
7980 btrfs_delalloc_release_space(BTRFS_I(inode),
7981 dio_data->data_reserved, start,
7982 dio_data->reserve, true);
7983 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7984 extent_changeset_free(dio_data->data_reserved);
7985 kfree(dio_data);
7986 }
7987 return ret;
7988}
7989
7990static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7991 ssize_t written, unsigned int flags, struct iomap *iomap)
7992{
7993 int ret = 0;
7994 struct btrfs_dio_data *dio_data = iomap->private;
7995 size_t submitted = dio_data->submitted;
7996 const bool write = !!(flags & IOMAP_WRITE);
7997
7998 if (!write && (iomap->type == IOMAP_HOLE)) {
7999 /* If reading from a hole, unlock and return */
8000 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
8001 goto out;
8002 }
8003
8004 if (submitted < length) {
8005 pos += submitted;
8006 length -= submitted;
8007 if (write)
8008 __endio_write_update_ordered(BTRFS_I(inode), pos,
8009 length, false);
8010 else
8011 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
8012 pos + length - 1);
8013 ret = -ENOTBLK;
8014 }
8015
8016 if (write) {
8017 if (dio_data->reserve)
8018 btrfs_delalloc_release_space(BTRFS_I(inode),
8019 dio_data->data_reserved, pos,
8020 dio_data->reserve, true);
8021 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
8022 extent_changeset_free(dio_data->data_reserved);
8023 }
8024out:
8025 kfree(dio_data);
8026 iomap->private = NULL;
8027
Josef Bacikeb838e72012-07-31 16:28:48 -04008028 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008029}
8030
Omar Sandoval769b4f22020-04-16 14:46:22 -07008031static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8032{
8033 /*
8034 * This implies a barrier so that stores to dio_bio->bi_status before
8035 * this and loads of dio_bio->bi_status after this are fully ordered.
8036 */
8037 if (!refcount_dec_and_test(&dip->refs))
8038 return;
8039
Naohiro Aotacfe94442021-02-04 19:21:59 +09008040 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008041 __endio_write_update_ordered(BTRFS_I(dip->inode),
8042 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07008043 dip->bytes,
8044 !dip->dio_bio->bi_status);
8045 } else {
8046 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
8047 dip->logical_offset,
8048 dip->logical_offset + dip->bytes - 1);
8049 }
8050
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008051 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008052 kfree(dip);
8053}
8054
Omar Sandoval77d5d682020-04-16 14:46:25 -07008055static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8056 int mirror_num,
8057 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08008058{
Omar Sandoval77d5d682020-04-16 14:46:25 -07008059 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008060 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008061 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008062
Mike Christie37226b22016-06-05 14:31:52 -05008063 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008064
Omar Sandoval5c047a62020-04-16 14:46:24 -07008065 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08008066 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02008067 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008068
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008069 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07008070 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07008071 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008072 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08008073 return ret;
8074}
8075
Omar Sandoval769b4f22020-04-16 14:46:22 -07008076static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
8077 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008078 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08008079{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008080 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8081 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04008082 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008083 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8084 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07008085 struct bio_vec bvec;
8086 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008087 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008088 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008089 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008090
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008091 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
8092 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
Liu Bo17347ce2017-05-15 15:33:27 -07008094 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8095 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008096 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07008097 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008098 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008099 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07008100 bio_offset, bvec.bv_page,
8101 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008102 clean_io_failure(fs_info, failure_tree, io_tree,
8103 start, bvec.bv_page,
8104 btrfs_ino(BTRFS_I(inode)),
8105 pgoff);
8106 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08008107 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008108
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008109 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08008110 ret = btrfs_repair_one_sector(inode,
8111 &io_bio->bio,
8112 start - io_bio->logical,
8113 bvec.bv_page, pgoff,
8114 start, io_bio->mirror_num,
8115 submit_dio_repair_bio);
8116 if (ret)
8117 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008118 }
8119 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008120 ASSERT(bio_offset + sectorsize > bio_offset);
8121 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008124 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008125 return err;
8126}
8127
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008128static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08008129 const u64 offset, const u64 bytes,
8130 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008131{
Qu Wenruoe65f1522021-04-01 15:15:06 +08008132 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8133 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00008134}
8135
Qu Wenruo8896a082020-10-21 14:24:53 +08008136static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008137 struct bio *bio,
8138 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008139{
Qu Wenruo1941b642020-12-02 14:47:57 +08008140 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008141}
8142
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008143static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008144{
8145 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008146 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008147
Miao Xie8b110e32014-09-12 18:44:03 +08008148 if (err)
8149 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008150 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008151 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008152 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008153 bio->bi_iter.bi_size, err);
8154
Omar Sandoval769b4f22020-04-16 14:46:22 -07008155 if (bio_op(bio) == REQ_OP_READ) {
8156 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008157 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008158 }
8159
Omar Sandoval769b4f22020-04-16 14:46:22 -07008160 if (err)
8161 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008162
Naohiro Aota544d24f2021-02-04 19:22:06 +09008163 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8164
Miao Xiee65e1532010-11-22 03:04:43 +00008165 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008166 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008167}
8168
David Sterbad0ee3932018-03-08 14:35:48 +01008169static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8170 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008173 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008174 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008175 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008176
Liu Bo4c274bc2017-11-01 17:19:27 -06008177 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008178 if (async_submit)
8179 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8180
Josef Bacik5fd02042012-05-02 14:00:54 -04008181 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008182 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008183 if (ret)
8184 goto err;
8185 }
Miao Xiee65e1532010-11-22 03:04:43 +00008186
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008187 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008188 goto map;
8189
8190 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008191 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008192 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008193 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008194 } else if (write) {
8195 /*
8196 * If we aren't doing async submit, calculate the csum of the
8197 * bio now.
8198 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008199 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008200 if (ret)
8201 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008202 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008203 u64 csum_offset;
8204
8205 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008206 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008207 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008208 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008209 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008210map:
Chris Mason08635ba2019-07-10 12:28:14 -07008211 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008212err:
Miao Xiee65e1532010-11-22 03:04:43 +00008213 return ret;
8214}
8215
Omar Sandovalc36cac22020-04-16 14:46:13 -07008216/*
8217 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8218 * or ordered extents whether or not we submit any bios.
8219 */
8220static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8221 struct inode *inode,
8222 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008223{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008224 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008225 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8226 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008227 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008228
Omar Sandoval85879572020-04-16 14:46:21 -07008229 dip_size = sizeof(*dip);
8230 if (!write && csum) {
8231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008232 size_t nblocks;
8233
David Sterba265fdfa2020-07-01 21:19:09 +02008234 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008235 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008236 }
8237
8238 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008239 if (!dip)
8240 return NULL;
8241
Omar Sandovalc36cac22020-04-16 14:46:13 -07008242 dip->inode = inode;
8243 dip->logical_offset = file_offset;
8244 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008245 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008246 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008247 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008248 return dip;
8249}
8250
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008251static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8252 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008253{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008254 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008256 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8257 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008258 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008259 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008260 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008261 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008262 u64 submit_len;
Naohiro Aota42b5d732021-07-21 21:43:34 +09008263 u64 clone_offset = 0;
8264 u64 clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008265 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308266 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008267 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008268 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008269 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008270 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008271
Omar Sandovalc36cac22020-04-16 14:46:13 -07008272 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8273 if (!dip) {
8274 if (!write) {
8275 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8276 file_offset + dio_bio->bi_iter.bi_size - 1);
8277 }
8278 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008279 bio_endio(dio_bio);
8280 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008281 }
8282
Josef Bacik334c16d2020-10-16 11:29:14 -04008283 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008284 /*
8285 * Load the csums up front to reduce csum tree searches and
8286 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008287 *
8288 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008289 */
Qu Wenruo62751932020-12-02 14:48:06 +08008290 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008291 if (status != BLK_STS_OK)
8292 goto out_err;
8293 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008294
Omar Sandoval769b4f22020-04-16 14:46:22 -07008295 start_sector = dio_bio->bi_iter.bi_sector;
8296 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008297
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008298 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008299 logical = start_sector << 9;
8300 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8301 if (IS_ERR(em)) {
8302 status = errno_to_blk_status(PTR_ERR(em));
8303 em = NULL;
8304 goto out_err_em;
8305 }
8306 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008307 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008308 if (ret) {
8309 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008310 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008311 }
Omar Sandoval769b4f22020-04-16 14:46:22 -07008312
Naohiro Aota42b5d732021-07-21 21:43:34 +09008313 clone_len = min(submit_len, geom.len);
8314 ASSERT(clone_len <= UINT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008315
Liu Bo725130b2017-05-16 09:51:39 -07008316 /*
8317 * This will never fail as it's passing GPF_NOFS and
8318 * the allocation is backed by btrfs_bioset.
8319 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008320 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008321 bio->bi_private = dip;
8322 bio->bi_end_io = btrfs_end_dio_bio;
8323 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008324
Naohiro Aota544d24f2021-02-04 19:22:06 +09008325 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8326 status = extract_ordered_extent(BTRFS_I(inode), bio,
8327 file_offset);
8328 if (status) {
8329 bio_put(bio);
8330 goto out_err;
8331 }
8332 }
8333
Liu Bo725130b2017-05-16 09:51:39 -07008334 ASSERT(submit_len >= clone_len);
8335 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008336
Liu Bo725130b2017-05-16 09:51:39 -07008337 /*
8338 * Increase the count before we submit the bio so we know
8339 * the end IO handler won't happen before we increase the
8340 * count. Otherwise, the dip might get freed before we're
8341 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008342 *
8343 * We transfer the initial reference to the last bio, so we
8344 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008345 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008346 if (submit_len > 0) {
8347 refcount_inc(&dip->refs);
8348 /*
8349 * If we are submitting more than one bio, submit them
8350 * all asynchronously. The exception is RAID 5 or 6, as
8351 * asynchronous checksums make it difficult to collect
8352 * full stripe writes.
8353 */
8354 if (!raid56)
8355 async_submit = 1;
8356 }
Miao Xiee65e1532010-11-22 03:04:43 +00008357
David Sterbad0ee3932018-03-08 14:35:48 +01008358 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008359 async_submit);
8360 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008361 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008362 if (submit_len > 0)
8363 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008364 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008365 }
Liu Bo725130b2017-05-16 09:51:39 -07008366
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008367 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008368 clone_offset += clone_len;
8369 start_sector += clone_len >> 9;
8370 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008371
8372 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008373 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008374 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008375
Michal Rostecki42034312021-01-27 14:57:27 +01008376out_err_em:
8377 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008378out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008379 dip->dio_bio->bi_status = status;
8380 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008381
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008382 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008383}
8384
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008385const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008386 .iomap_begin = btrfs_dio_iomap_begin,
8387 .iomap_end = btrfs_dio_iomap_end,
8388};
8389
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008390const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008391 .submit_io = btrfs_submit_direct,
8392};
8393
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008394static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008395 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008396{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008397 int ret;
8398
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008399 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008400 if (ret)
8401 return ret;
8402
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008403 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008404}
8405
Chris Mason9ebefb182007-06-15 13:50:00 -04008406int btrfs_readpage(struct file *file, struct page *page)
8407{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008408 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8409 u64 start = page_offset(page);
8410 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008411 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008412 int ret;
8413
Nikolay Borisov0f208812020-09-14 14:39:16 +03008414 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8415
Qu Wenruo390ed292021-04-14 16:42:15 +08008416 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8417 if (bio_ctrl.bio)
8418 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008419 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008420}
Chris Mason1832a6d2007-12-21 16:27:21 -05008421
Chris Mason39279cc2007-06-12 06:35:45 -04008422static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8423{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008424 struct inode *inode = page->mapping->host;
8425 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008426
8427 if (current->flags & PF_MEMALLOC) {
8428 redirty_page_for_writepage(wbc, page);
8429 unlock_page(page);
8430 return 0;
8431 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008432
8433 /*
8434 * If we are under memory pressure we will call this directly from the
8435 * VM, we need to make sure we have the inode referenced for the ordered
8436 * extent. If not just return like we didn't do anything.
8437 */
8438 if (!igrab(inode)) {
8439 redirty_page_for_writepage(wbc, page);
8440 return AOP_WRITEPAGE_ACTIVATE;
8441 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008442 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008443 btrfs_add_delayed_iput(inode);
8444 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008445}
Chris Mason39279cc2007-06-12 06:35:45 -04008446
Eric Sandeen48a3b632013-04-25 20:41:01 +00008447static int btrfs_writepages(struct address_space *mapping,
8448 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008449{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008450 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008451}
8452
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008453static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008454{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008455 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008456}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008457
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008458/*
8459 * For releasepage() and invalidatepage() we have a race window where
8460 * end_page_writeback() is called but the subpage spinlock is not yet released.
8461 * If we continue to release/invalidate the page, we could cause use-after-free
8462 * for subpage spinlock. So this function is to spin and wait for subpage
8463 * spinlock.
8464 */
8465static void wait_subpage_spinlock(struct page *page)
8466{
8467 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8468 struct btrfs_subpage *subpage;
8469
8470 if (fs_info->sectorsize == PAGE_SIZE)
8471 return;
8472
8473 ASSERT(PagePrivate(page) && page->private);
8474 subpage = (struct btrfs_subpage *)page->private;
8475
8476 /*
8477 * This may look insane as we just acquire the spinlock and release it,
8478 * without doing anything. But we just want to make sure no one is
8479 * still holding the subpage spinlock.
8480 * And since the page is not dirty nor writeback, and we have page
8481 * locked, the only possible way to hold a spinlock is from the endio
8482 * function to clear page writeback.
8483 *
8484 * Here we just acquire the spinlock so that all existing callers
8485 * should exit and we're safe to release/invalidate the page.
8486 */
8487 spin_lock_irq(&subpage->lock);
8488 spin_unlock_irq(&subpage->lock);
8489}
8490
Chris Masone6dcd2d2008-07-17 12:53:50 -04008491static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008492{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008493 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008494
8495 if (ret == 1) {
8496 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008497 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008498 }
Chris Masona52d9a82007-08-27 16:49:44 -04008499 return ret;
8500}
Chris Mason39279cc2007-06-12 06:35:45 -04008501
Chris Masone6dcd2d2008-07-17 12:53:50 -04008502static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8503{
Chris Mason98509cf2008-09-11 15:51:43 -04008504 if (PageWriteback(page) || PageDirty(page))
8505 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008506 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008507}
8508
Roman Gushchinf8e66082020-03-04 16:57:35 -08008509#ifdef CONFIG_MIGRATION
8510static int btrfs_migratepage(struct address_space *mapping,
8511 struct page *newpage, struct page *page,
8512 enum migrate_mode mode)
8513{
8514 int ret;
8515
8516 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8517 if (ret != MIGRATEPAGE_SUCCESS)
8518 return ret;
8519
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008520 if (page_has_private(page))
8521 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008522
Qu Wenruof57ad932021-04-07 19:22:13 +08008523 if (PageOrdered(page)) {
8524 ClearPageOrdered(page);
8525 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008526 }
8527
8528 if (mode != MIGRATE_SYNC_NO_COPY)
8529 migrate_page_copy(newpage, page);
8530 else
8531 migrate_page_states(newpage, page);
8532 return MIGRATEPAGE_SUCCESS;
8533}
8534#endif
8535
Lukas Czernerd47992f2013-05-21 23:17:23 -04008536static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8537 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008538{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008539 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008540 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008541 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008542 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008543 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008544 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008545 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008546 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008547
Chris Mason8b62b722009-09-02 16:53:46 -04008548 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008549 * We have page locked so no new ordered extent can be created on this
8550 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008551 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008552 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008553 * Furthermore, endio function won't skip page which has Ordered
8554 * (Private2) already cleared, so it's possible for endio and
8555 * invalidatepage to do the same ordered extent accounting twice
8556 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008557 *
8558 * So here we wait for any submitted bios to finish, so that we won't
8559 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008560 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008561 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008562 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008563
Qu Wenruobcd77452021-05-31 16:50:55 +08008564 /*
8565 * For subpage case, we have call sites like
8566 * btrfs_punch_hole_lock_range() which passes range not aligned to
8567 * sectorsize.
8568 * If the range doesn't cover the full page, we don't need to and
8569 * shouldn't clear page extent mapped, as page->private can still
8570 * record subpage dirty bits for other part of the range.
8571 *
8572 * For cases that can invalidate the full even the range doesn't
8573 * cover the full page, like invalidating the last page, we're
8574 * still safe to wait for ordered extent to finish.
8575 */
8576 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008577 btrfs_releasepage(page, GFP_NOFS);
8578 return;
8579 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008580
8581 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008582 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008583
Qu Wenruo3b835842021-04-06 19:54:53 +08008584 cur = page_start;
8585 while (cur < page_end) {
8586 struct btrfs_ordered_extent *ordered;
8587 bool delete_states;
8588 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008589 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008590
8591 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8592 page_end + 1 - cur);
8593 if (!ordered) {
8594 range_end = page_end;
8595 /*
8596 * No ordered extent covering this range, we are safe
8597 * to delete all extent states in the range.
8598 */
8599 delete_states = true;
8600 goto next;
8601 }
8602 if (ordered->file_offset > cur) {
8603 /*
8604 * There is a range between [cur, oe->file_offset) not
8605 * covered by any ordered extent.
8606 * We are safe to delete all extent states, and handle
8607 * the ordered extent in the next iteration.
8608 */
8609 range_end = ordered->file_offset - 1;
8610 delete_states = true;
8611 goto next;
8612 }
8613
8614 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8615 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008616 ASSERT(range_end + 1 - cur < U32_MAX);
8617 range_len = range_end + 1 - cur;
8618 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008619 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008620 * If Ordered (Private2) is cleared, it means endio has
8621 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008622 * We can't delete the extent states as
8623 * btrfs_finish_ordered_io() may still use some of them.
8624 */
8625 delete_states = false;
8626 goto next;
8627 }
Qu Wenruob945a462021-05-31 16:50:46 +08008628 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008629
Chris Masoneb84ae02008-07-17 13:53:27 -04008630 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008631 * IO on this page will never be started, so we need to account
8632 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8633 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008634 *
8635 * This will also unlock the range for incoming
8636 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008637 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008638 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008639 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008640 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008641 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008642 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008643
8644 spin_lock_irq(&inode->ordered_tree.lock);
8645 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8646 ordered->truncated_len = min(ordered->truncated_len,
8647 cur - ordered->file_offset);
8648 spin_unlock_irq(&inode->ordered_tree.lock);
8649
8650 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008651 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008652 btrfs_finish_ordered_io(ordered);
8653 /*
8654 * The ordered extent has finished, now we're again
8655 * safe to delete all extent states of the range.
8656 */
8657 delete_states = true;
8658 } else {
8659 /*
8660 * btrfs_finish_ordered_io() will get executed by endio
8661 * of other pages, thus we can't delete extent states
8662 * anymore
8663 */
8664 delete_states = false;
8665 }
8666next:
8667 if (ordered)
8668 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008669 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008670 * Qgroup reserved space handler
8671 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008672 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008673 * 1) Already written to disk or bio already finished
8674 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8675 * Qgroup will be handled by its qgroup_record then.
8676 * btrfs_qgroup_free_data() call will do nothing here.
8677 *
8678 * 2) Not written to disk yet
8679 * Then btrfs_qgroup_free_data() call will clear the
8680 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8681 * reserved data space.
8682 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008683 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008684 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008685 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008686 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008687 EXTENT_DELALLOC | EXTENT_UPTODATE |
8688 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008689 delete_states, &cached_state);
8690 }
8691 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008692 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008693 /*
8694 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008695 * should not have Ordered (Private2) anymore, or the above iteration
8696 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008697 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008698 ASSERT(!PageOrdered(page));
Qu Wenruo3b835842021-04-06 19:54:53 +08008699 if (!inode_evicting)
8700 __btrfs_releasepage(page, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04008701 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008702 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008703}
8704
Chris Mason9ebefb182007-06-15 13:50:00 -04008705/*
8706 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8707 * called from a page fault handler when a page is first dirtied. Hence we must
8708 * be careful to check for EOF conditions here. We set the page up correctly
8709 * for a written page which means we get ENOSPC checking when writing into
8710 * holes and correct delalloc and unwritten extent mapping on filesystems that
8711 * support these features.
8712 *
8713 * We are not allowed to take the i_mutex here so we have to play games to
8714 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008715 * truncate_setsize() writes the inode size before removing pages, once we have
8716 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008717 * beyond EOF, then the page is guaranteed safe against truncation until we
8718 * unlock the page.
8719 */
Souptick Joardera528a242018-06-06 19:54:44 +05308720vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008721{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008722 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008723 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8726 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008727 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008728 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008729 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008730 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308731 vm_fault_t ret;
8732 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008733 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308734 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008735 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008736 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308737 u64 end;
8738
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008739 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008740
Jan Karab2b5ef52012-06-12 16:20:45 +02008741 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008742 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008743 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308744 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008745
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308746 /*
8747 * Reserving delalloc space after obtaining the page lock can lead to
8748 * deadlock. For example, if a dirty page is locked by this function
8749 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8750 * dirty page write out, then the btrfs_writepage() function could
8751 * end up waiting indefinitely to get a lock on the page currently
8752 * being processed by btrfs_page_mkwrite() function.
8753 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008754 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8755 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308756 if (!ret2) {
8757 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008758 reserved = 1;
8759 }
Souptick Joardera528a242018-06-06 19:54:44 +05308760 if (ret2) {
8761 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008762 if (reserved)
8763 goto out;
8764 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008765 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008766
Nick Piggin56a76f82009-03-31 15:23:23 -07008767 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008768again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008769 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008770 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008771 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008772
Chris Mason9ebefb182007-06-15 13:50:00 -04008773 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008774 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008775 /* page got truncated out from underneath us */
8776 goto out_unlock;
8777 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 wait_on_page_writeback(page);
8779
David Sterbaff13db42015-12-03 14:30:40 +01008780 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008781 ret2 = set_page_extent_mapped(page);
8782 if (ret2 < 0) {
8783 ret = vmf_error(ret2);
8784 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8785 goto out_unlock;
8786 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787
Chris Masoneb84ae02008-07-17 13:53:27 -04008788 /*
8789 * we can't set the delalloc bits if there are pending ordered
8790 * extents. Drop our locks and wait for them to finish
8791 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008792 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8793 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008794 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008795 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008796 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008797 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008798 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008799 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800 btrfs_put_ordered_extent(ordered);
8801 goto again;
8802 }
8803
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008804 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008805 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008806 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008807 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308808 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008809 btrfs_delalloc_release_space(BTRFS_I(inode),
8810 data_reserved, page_start,
8811 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308812 }
8813 }
8814
Josef Bacikfbf19082009-10-01 17:10:23 -04008815 /*
Liu Bo54160342016-12-13 12:15:19 -08008816 * page_mkwrite gets called when the page is firstly dirtied after it's
8817 * faulted in, but write(2) could also dirty a page and set delalloc
8818 * bits, thus in this case for space account reason, we still need to
8819 * clear any delalloc bits within this page range since we have to
8820 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008821 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308822 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008823 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8824 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008825
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008826 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008827 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308828 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008829 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008830 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008831 ret = VM_FAULT_SIGBUS;
8832 goto out_unlock;
8833 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008834
8835 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008836 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008837 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008838 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008839 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008840
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008841 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008842 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008843 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 }
Chris Mason247e7432008-07-17 12:53:51 -04008845 ClearPageChecked(page);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008846 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8847 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008848
Filipe Mananabc0939f2021-02-23 12:08:48 +00008849 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008850
David Sterbae43bbe52017-12-12 21:43:52 +01008851 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008852 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008853
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008854 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8855 sb_end_pagefault(inode->i_sb);
8856 extent_changeset_free(data_reserved);
8857 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008858
8859out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008860 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008861 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008862out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008863 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008864 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008865 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008866out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008867 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008868 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008869 return ret;
8870}
8871
Filipe Manana213e8c52018-02-06 20:40:31 +00008872static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008873{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008875 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008876 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008877 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008878 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008879 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008880 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008881 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008882
Filipe Manana213e8c52018-02-06 20:40:31 +00008883 if (!skip_writeback) {
8884 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8885 (u64)-1);
8886 if (ret)
8887 return ret;
8888 }
Chris Mason39279cc2007-06-12 06:35:45 -04008889
Josef Bacikfcb80c22011-05-03 10:40:22 -04008890 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008891 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8892 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008893 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008894 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008895 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008896 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008897 * be free'd up by the truncate operation, but also have some slack
8898 * space reserved in case it uses space during the truncate (thank you
8899 * very much snapshotting).
8900 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008901 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008902 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008903 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008904 * doesn't end up using space reserved for updating the inode. We also
8905 * need to be able to stop the transaction and start a new one, which
8906 * means we need to be able to update the inode several times, and we
8907 * have no idea of knowing how many times that will be, so we can't just
8908 * reserve 1 item for the entirety of the operation, so that has to be
8909 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008910 *
8911 * So that leaves us with
8912 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008913 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008914 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008915 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008916 * updating the inode.
8917 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008918 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008919 if (!rsv)
8920 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008921 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008922 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008923
Josef Bacik907cbce2011-08-08 13:46:15 -04008924 /*
Josef Bacik07127182011-08-19 10:29:59 -04008925 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008926 * 1 for updating the inode.
8927 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008928 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008929 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008930 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008931 goto out;
8932 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008933
Josef Bacik907cbce2011-08-08 13:46:15 -04008934 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008935 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008936 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008937 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008938
Josef Bacikca7e70f2012-08-27 17:48:15 -04008939 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008940
Yan, Zheng80825102009-11-12 09:35:36 +00008941 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008942 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008943 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008944 BTRFS_EXTENT_DATA_KEY,
8945 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008946 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008947 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008948 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008949
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008950 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008951 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008952 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008953
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008954 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008955 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008956
8957 trans = btrfs_start_transaction(root, 2);
8958 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008959 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008960 trans = NULL;
8961 break;
8962 }
8963
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008964 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008965 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008966 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008967 BUG_ON(ret); /* shouldn't happen */
8968 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008969 }
8970
Josef Bacikddfae632017-10-19 14:16:02 -04008971 /*
8972 * We can't call btrfs_truncate_block inside a trans handle as we could
8973 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8974 * we've truncated everything except the last little bit, and can do
8975 * btrfs_truncate_block and then update the disk_i_size.
8976 */
8977 if (ret == NEED_TRUNCATE_BLOCK) {
8978 btrfs_end_transaction(trans);
8979 btrfs_btree_balance_dirty(fs_info);
8980
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008981 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008982 if (ret)
8983 goto out;
8984 trans = btrfs_start_transaction(root, 1);
8985 if (IS_ERR(trans)) {
8986 ret = PTR_ERR(trans);
8987 goto out;
8988 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008989 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008990 }
8991
Chris Mason917c16b2011-11-08 14:49:59 -05008992 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008993 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008994
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008995 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008996 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008997 if (ret2 && !ret)
8998 ret = ret2;
8999
9000 ret2 = btrfs_end_transaction(trans);
9001 if (ret2 && !ret)
9002 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009003 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009004 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009005out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009006 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01009007 /*
9008 * So if we truncate and then write and fsync we normally would just
9009 * write the extents that changed, which is a problem if we need to
9010 * first truncate that entire inode. So set this flag so we write out
9011 * all of the extents in the inode to the sync log so we're completely
9012 * safe.
9013 *
9014 * If no extents were dropped or trimmed we don't need to force the next
9015 * fsync to truncate all the inode's items from the log and re-log them
9016 * all. This means the truncate operation did not change the file size,
9017 * or changed it to a smaller size but there was only an implicit hole
9018 * between the old i_size and the new i_size, and there were no prealloc
9019 * extents beyond i_size to drop.
9020 */
9021 if (extents_found > 0)
9022 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009023
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009024 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009025}
9026
Sven Wegener3b963622008-06-09 21:57:42 -04009027/*
Chris Masond352ac62008-09-29 15:18:18 -04009028 * create a new subvolume directory/inode (helper for the ioctl).
9029 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009030int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009031 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02009032 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04009033{
Chris Mason39279cc2007-06-12 06:35:45 -04009034 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009035 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009036 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02009037 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04009038
Nikolay Borisov23125102020-12-07 17:32:36 +02009039 err = btrfs_get_free_objectid(new_root, &ino);
9040 if (err < 0)
9041 return err;
9042
Christian Braunerb3b6f5b2021-07-27 12:48:41 +02009043 inode = btrfs_new_inode(trans, new_root, &init_user_ns, NULL, "..", 2,
9044 ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009045 S_IFDIR | (~current_umask() & S_IRWXUGO),
9046 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009047 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009048 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009049 inode->i_op = &btrfs_dir_inode_operations;
9050 inode->i_fop = &btrfs_dir_file_operations;
9051
Miklos Szeredibfe86842011-10-28 14:13:29 +02009052 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009053 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009054 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009055
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009056 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9057 if (err)
9058 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009059 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009060 new_root->root_key.objectid, err);
9061
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009062 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009063
Yan, Zheng76dda932009-09-21 16:00:26 -04009064 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009065 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009066}
9067
Chris Mason39279cc2007-06-12 06:35:45 -04009068struct inode *btrfs_alloc_inode(struct super_block *sb)
9069{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009070 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009071 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009072 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009073
David Sterba712e36c2017-10-31 17:08:27 +01009074 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009075 if (!ei)
9076 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009077
9078 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009079 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009080 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009081 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009082 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009083 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009084 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009085 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009086 ei->disk_i_size = 0;
9087 ei->flags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -07009088 ei->ro_flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009089 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009090 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009091 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009092 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01009093 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009094 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009095
Josef Bacik9e0baf62011-07-15 15:16:44 +00009096 spin_lock_init(&ei->lock);
9097 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009098 if (sb->s_magic != BTRFS_TEST_MAGIC)
9099 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9100 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009101 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009102 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009103 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009104
Miao Xie16cdcec2011-04-22 18:12:22 +08009105 ei->delayed_node = NULL;
9106
chandan r9cc97d62012-07-04 12:48:07 +05309107 ei->i_otime.tv_sec = 0;
9108 ei->i_otime.tv_nsec = 0;
9109
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009110 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009111 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009112 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9113 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9114 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05009115 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9116 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01009117 ei->io_tree.track_uptodate = true;
9118 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009119 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009120 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009121 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009122 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009123 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009124 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05009125 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009126
9127 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009128}
9129
Josef Bacikaaedb552013-10-11 14:44:09 -04009130#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9131void btrfs_test_destroy_inode(struct inode *inode)
9132{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009133 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009134 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9135}
9136#endif
9137
Al Viro26602ca2019-04-10 15:14:41 -04009138void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009139{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009140 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9141}
9142
Nikolay Borisov633cc812020-09-18 12:15:49 +03009143void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009144{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03009146 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9147 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009148
Nikolay Borisov633cc812020-09-18 12:15:49 +03009149 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9150 WARN_ON(vfs_inode->i_data.nrpages);
9151 WARN_ON(inode->block_rsv.reserved);
9152 WARN_ON(inode->block_rsv.size);
9153 WARN_ON(inode->outstanding_extents);
9154 WARN_ON(inode->delalloc_bytes);
9155 WARN_ON(inode->new_delalloc_bytes);
9156 WARN_ON(inode->csum_bytes);
9157 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009158
Chris Mason5a3f23d2009-03-31 13:27:11 -04009159 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009160 * This can happen where we create an inode, but somebody else also
9161 * created the same inode and we need to destroy the one we already
9162 * created.
9163 */
9164 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009165 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009166
Chris Masond3977122009-01-05 21:25:51 -05009167 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009168 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009169 if (!ordered)
9170 break;
9171 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009172 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009173 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08009174 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03009175 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009176 btrfs_put_ordered_extent(ordered);
9177 btrfs_put_ordered_extent(ordered);
9178 }
9179 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009180 btrfs_qgroup_check_reserved_leak(inode);
9181 inode_tree_del(inode);
9182 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9183 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9184 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04009185}
9186
Al Viro45321ac2010-06-07 13:43:19 -04009187int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009188{
9189 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009190
Naohiro Aota6379ef92013-06-06 09:56:34 +00009191 if (root == NULL)
9192 return 1;
9193
Liu Bofa6ac872013-02-20 14:10:23 +00009194 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009195 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009196 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009197 else
Al Viro45321ac2010-06-07 13:43:19 -04009198 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009199}
9200
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009201static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009202{
9203 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9204
9205 inode_init_once(&ei->vfs_inode);
9206}
9207
David Sterbae67c7182018-02-19 17:24:18 +01009208void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009209{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009210 /*
9211 * Make sure all delayed rcu free inodes are flushed before we
9212 * destroy cache.
9213 */
9214 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009215 kmem_cache_destroy(btrfs_inode_cachep);
9216 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009217 kmem_cache_destroy(btrfs_path_cachep);
9218 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009219 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009220}
9221
Liu Bof5c29bd2017-11-02 17:21:50 -06009222int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009223{
David Sterba837e1972012-09-07 03:00:48 -06009224 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009225 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009226 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9227 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009228 if (!btrfs_inode_cachep)
9229 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009230
David Sterba837e1972012-09-07 03:00:48 -06009231 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009232 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009233 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009234 if (!btrfs_trans_handle_cachep)
9235 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009236
David Sterba837e1972012-09-07 03:00:48 -06009237 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009238 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009239 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009240 if (!btrfs_path_cachep)
9241 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009242
David Sterba837e1972012-09-07 03:00:48 -06009243 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009244 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009245 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009246 if (!btrfs_free_space_cachep)
9247 goto fail;
9248
Christophe Leroy3acd4852019-08-21 15:05:55 +00009249 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9250 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009251 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009252 if (!btrfs_free_space_bitmap_cachep)
9253 goto fail;
9254
Chris Mason39279cc2007-06-12 06:35:45 -04009255 return 0;
9256fail:
9257 btrfs_destroy_cachep();
9258 return -ENOMEM;
9259}
9260
Christian Brauner549c7292021-01-21 14:19:43 +01009261static int btrfs_getattr(struct user_namespace *mnt_userns,
9262 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009263 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009264{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009265 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009266 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009267 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009268 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009269 u32 bi_flags = BTRFS_I(inode)->flags;
Boris Burkov14605402021-06-30 13:01:49 -07009270 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
Yonghong Song04a87e32017-05-12 15:07:43 -07009271
9272 stat->result_mask |= STATX_BTIME;
9273 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9274 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9275 if (bi_flags & BTRFS_INODE_APPEND)
9276 stat->attributes |= STATX_ATTR_APPEND;
9277 if (bi_flags & BTRFS_INODE_COMPRESS)
9278 stat->attributes |= STATX_ATTR_COMPRESSED;
9279 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9280 stat->attributes |= STATX_ATTR_IMMUTABLE;
9281 if (bi_flags & BTRFS_INODE_NODUMP)
9282 stat->attributes |= STATX_ATTR_NODUMP;
Boris Burkov14605402021-06-30 13:01:49 -07009283 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9284 stat->attributes |= STATX_ATTR_VERITY;
Yonghong Song04a87e32017-05-12 15:07:43 -07009285
9286 stat->attributes_mask |= (STATX_ATTR_APPEND |
9287 STATX_ATTR_COMPRESSED |
9288 STATX_ATTR_IMMUTABLE |
9289 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009290
Christian Braunerc020d2e2021-07-27 12:48:43 +02009291 generic_fillattr(mnt_userns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009292 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009293
9294 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009295 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009296 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009297 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009298 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009299 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009300 return 0;
9301}
9302
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009303static int btrfs_rename_exchange(struct inode *old_dir,
9304 struct dentry *old_dentry,
9305 struct inode *new_dir,
9306 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009308 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009309 struct btrfs_trans_handle *trans;
9310 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009311 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009312 struct inode *new_inode = new_dentry->d_inode;
9313 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009314 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009315 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9316 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009317 u64 old_idx = 0;
9318 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009319 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009320 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009321 bool root_log_pinned = false;
9322 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009323 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009324
NeilBrown3f79f6f2021-08-06 14:26:24 +10009325 /*
9326 * For non-subvolumes allow exchange only within one subvolume, in the
9327 * same inode namespace. Two subvolumes (represented as directory) can
9328 * be exchanged as they're a logical link and have a fixed inode number.
9329 */
9330 if (root != dest &&
9331 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9332 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009333 return -EXDEV;
9334
9335 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009336 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9337 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009338 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009339
9340 /*
9341 * We want to reserve the absolute worst case amount of items. So if
9342 * both inodes are subvols and we need to unlink them then that would
9343 * require 4 item modifications, but if they are both normal inodes it
9344 * would require 5 item modifications, so we'll assume their normal
9345 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9346 * should cover the worst case number of items we'll modify.
9347 */
9348 trans = btrfs_start_transaction(root, 12);
9349 if (IS_ERR(trans)) {
9350 ret = PTR_ERR(trans);
9351 goto out_notrans;
9352 }
9353
Josef Bacik00aa8e82021-03-12 15:25:02 -05009354 if (dest != root) {
9355 ret = btrfs_record_root_in_trans(trans, dest);
9356 if (ret)
9357 goto out_fail;
9358 }
Josef Bacik3e174092019-11-15 15:43:06 -05009359
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009360 /*
9361 * We need to find a free sequence number both in the source and
9362 * in the destination directory for the exchange.
9363 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009364 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009365 if (ret)
9366 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009367 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009368 if (ret)
9369 goto out_fail;
9370
9371 BTRFS_I(old_inode)->dir_index = 0ULL;
9372 BTRFS_I(new_inode)->dir_index = 0ULL;
9373
9374 /* Reference for the source. */
9375 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9376 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009377 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009378 } else {
9379 ret = btrfs_insert_inode_ref(trans, dest,
9380 new_dentry->d_name.name,
9381 new_dentry->d_name.len,
9382 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009383 btrfs_ino(BTRFS_I(new_dir)),
9384 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009385 if (ret)
9386 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009387 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009388 }
9389
9390 /* And now for the dest. */
9391 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9392 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009393 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009394 } else {
9395 ret = btrfs_insert_inode_ref(trans, root,
9396 old_dentry->d_name.name,
9397 old_dentry->d_name.len,
9398 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009399 btrfs_ino(BTRFS_I(old_dir)),
9400 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009401 if (ret) {
9402 if (need_abort)
9403 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009404 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009405 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009406 }
9407
9408 /* Update inode version and ctime/mtime. */
9409 inode_inc_iversion(old_dir);
9410 inode_inc_iversion(new_dir);
9411 inode_inc_iversion(old_inode);
9412 inode_inc_iversion(new_inode);
9413 old_dir->i_ctime = old_dir->i_mtime = ctime;
9414 new_dir->i_ctime = new_dir->i_mtime = ctime;
9415 old_inode->i_ctime = ctime;
9416 new_inode->i_ctime = ctime;
9417
9418 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009419 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9420 BTRFS_I(old_inode), 1);
9421 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9422 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009423 }
9424
Filipe Mananabd54f382021-07-27 11:24:45 +01009425 /*
9426 * Now pin the logs of the roots. We do it to ensure that no other task
9427 * can sync the logs while we are in progress with the rename, because
9428 * that could result in an inconsistency in case any of the inodes that
9429 * are part of this rename operation were logged before.
9430 *
9431 * We pin the logs even if at this precise moment none of the inodes was
9432 * logged before. This is because right after we checked for that, some
9433 * other task fsyncing some other inode not involved with this rename
9434 * operation could log that one of our inodes exists.
9435 *
9436 * We don't need to pin the logs before the above calls to
9437 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9438 */
9439 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9440 btrfs_pin_log_trans(root);
9441 root_log_pinned = true;
9442 }
9443 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9444 btrfs_pin_log_trans(dest);
9445 dest_log_pinned = true;
9446 }
9447
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009448 /* src is a subvolume */
9449 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009450 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009451 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009452 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9453 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 old_dentry->d_name.name,
9455 old_dentry->d_name.len);
9456 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009457 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009458 }
9459 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009460 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 goto out_fail;
9462 }
9463
9464 /* dest is a subvolume */
9465 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009466 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009467 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009468 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9469 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009470 new_dentry->d_name.name,
9471 new_dentry->d_name.len);
9472 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009473 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009474 }
9475 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009476 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 goto out_fail;
9478 }
9479
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009480 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009481 new_dentry->d_name.name,
9482 new_dentry->d_name.len, 0, old_idx);
9483 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009484 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 goto out_fail;
9486 }
9487
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009488 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 old_dentry->d_name.name,
9490 old_dentry->d_name.len, 0, new_idx);
9491 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009492 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009493 goto out_fail;
9494 }
9495
9496 if (old_inode->i_nlink == 1)
9497 BTRFS_I(old_inode)->dir_index = old_idx;
9498 if (new_inode->i_nlink == 1)
9499 BTRFS_I(new_inode)->dir_index = new_idx;
9500
Filipe Manana86e8aa02016-05-05 02:02:27 +01009501 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009502 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9503 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009505 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009507 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009508 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9509 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009511 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 }
9513out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009514 /*
9515 * If we have pinned a log and an error happened, we unpin tasks
9516 * trying to sync the log and force them to fallback to a transaction
9517 * commit if the log currently contains any of the inodes involved in
9518 * this rename operation (to ensure we do not persist a log with an
9519 * inconsistent state for any of these inodes or leading to any
9520 * inconsistencies when replayed). If the transaction was aborted, the
9521 * abortion reason is propagated to userspace when attempting to commit
9522 * the transaction. If the log does not contain any of these inodes, we
9523 * allow the tasks to sync it.
9524 */
9525 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009526 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9527 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9528 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana1c167b82021-07-29 15:28:34 +01009529 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
David Sterba90787762019-03-20 13:28:05 +01009530 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009531
9532 if (root_log_pinned) {
9533 btrfs_end_log_trans(root);
9534 root_log_pinned = false;
9535 }
9536 if (dest_log_pinned) {
9537 btrfs_end_log_trans(dest);
9538 dest_log_pinned = false;
9539 }
9540 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009541 ret2 = btrfs_end_transaction(trans);
9542 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009544 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9545 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009546 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547
9548 return ret;
9549}
9550
9551static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9552 struct btrfs_root *root,
Christian Braunerca072742021-07-27 12:48:42 +02009553 struct user_namespace *mnt_userns,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 struct inode *dir,
9555 struct dentry *dentry)
9556{
9557 int ret;
9558 struct inode *inode;
9559 u64 objectid;
9560 u64 index;
9561
Nikolay Borisov543068a2020-12-07 17:32:33 +02009562 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 if (ret)
9564 return ret;
9565
Christian Braunerca072742021-07-27 12:48:42 +02009566 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 dentry->d_name.name,
9568 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009569 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 objectid,
9571 S_IFCHR | WHITEOUT_MODE,
9572 &index);
9573
9574 if (IS_ERR(inode)) {
9575 ret = PTR_ERR(inode);
9576 return ret;
9577 }
9578
9579 inode->i_op = &btrfs_special_inode_operations;
9580 init_special_inode(inode, inode->i_mode,
9581 WHITEOUT_DEV);
9582
9583 ret = btrfs_init_inode_security(trans, inode, dir,
9584 &dentry->d_name);
9585 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009586 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009588 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9589 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009591 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009593 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009594out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009596 if (ret)
9597 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 iput(inode);
9599
Filipe Mananac9901612016-05-05 01:41:57 +01009600 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601}
9602
Christian Braunerca072742021-07-27 12:48:42 +02009603static int btrfs_rename(struct user_namespace *mnt_userns,
9604 struct inode *old_dir, struct dentry *old_dentry,
9605 struct inode *new_dir, struct dentry *new_dentry,
9606 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009607{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009608 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009609 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009610 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009611 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9612 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009613 struct inode *new_inode = d_inode(new_dentry);
9614 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009615 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009616 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009617 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009618 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009619 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009620
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009621 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009622 return -EPERM;
9623
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009624 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009625 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009626 return -EXDEV;
9627
Li Zefan33345d012011-04-20 10:31:50 +08009628 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009629 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009630 return -ENOTEMPTY;
9631
Chris Mason39279cc2007-06-12 06:35:45 -04009632 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009633 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009634 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009635
9636
9637 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009638 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009639 new_dentry->d_name.name,
9640 new_dentry->d_name.len);
9641
9642 if (ret) {
9643 if (ret == -EEXIST) {
9644 /* we shouldn't get
9645 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309646 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009647 return ret;
9648 }
9649 } else {
9650 /* maybe -EOVERFLOW */
9651 return ret;
9652 }
9653 }
9654 ret = 0;
9655
Chris Mason5a3f23d2009-03-31 13:27:11 -04009656 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009657 * we're using rename to replace one file with another. Start IO on it
9658 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009659 */
Chris Mason8d875f92014-08-12 10:47:42 -07009660 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009661 filemap_flush(old_inode->i_mapping);
9662
Yan, Zheng76dda932009-09-21 16:00:26 -04009663 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009664 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009665 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009666 /*
9667 * We want to reserve the absolute worst case amount of items. So if
9668 * both inodes are subvols and we need to unlink them then that would
9669 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009671 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9672 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009673 * If our rename has the whiteout flag, we need more 5 units for the
9674 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9675 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009676 */
Filipe Manana5062af32016-05-05 10:26:26 +01009677 trans_num_items = 11;
9678 if (flags & RENAME_WHITEOUT)
9679 trans_num_items += 5;
9680 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009681 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 ret = PTR_ERR(trans);
9683 goto out_notrans;
9684 }
Chris Mason5f39d392007-10-15 16:14:19 -04009685
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009686 if (dest != root) {
9687 ret = btrfs_record_root_in_trans(trans, dest);
9688 if (ret)
9689 goto out_fail;
9690 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009691
Nikolay Borisov877574e2017-02-20 13:50:33 +02009692 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009693 if (ret)
9694 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009695
Miao Xie67de1172013-12-26 13:07:06 +08009696 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009697 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009698 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009699 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009700 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009701 ret = btrfs_insert_inode_ref(trans, dest,
9702 new_dentry->d_name.name,
9703 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009704 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009705 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009706 if (ret)
9707 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009708 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009709
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009710 inode_inc_iversion(old_dir);
9711 inode_inc_iversion(new_dir);
9712 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009713 old_dir->i_ctime = old_dir->i_mtime =
9714 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009715 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009716
Chris Mason12fcfd22009-03-24 10:24:20 -04009717 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009718 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9719 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009720
Li Zefan33345d012011-04-20 10:31:50 +08009721 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009722 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009723 } else {
Filipe Mananabd54f382021-07-27 11:24:45 +01009724 /*
9725 * Now pin the log. We do it to ensure that no other task can
9726 * sync the log while we are in progress with the rename, as
9727 * that could result in an inconsistency in case any of the
9728 * inodes that are part of this rename operation were logged
9729 * before.
9730 *
9731 * We pin the log even if at this precise moment none of the
9732 * inodes was logged before. This is because right after we
9733 * checked for that, some other task fsyncing some other inode
9734 * not involved with this rename operation could log that one of
9735 * our inodes exists.
9736 *
9737 * We don't need to pin the logs before the above call to
9738 * btrfs_insert_inode_ref(), since that does not need to change
9739 * a log.
9740 */
9741 btrfs_pin_log_trans(root);
9742 log_pinned = true;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009743 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9744 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009745 old_dentry->d_name.name,
9746 old_dentry->d_name.len);
9747 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009748 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009749 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009750 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009751 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009752 goto out_fail;
9753 }
Chris Mason39279cc2007-06-12 06:35:45 -04009754
9755 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009756 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009757 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009758 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009759 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009760 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009761 BUG_ON(new_inode->i_nlink == 0);
9762 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009763 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9764 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009765 new_dentry->d_name.name,
9766 new_dentry->d_name.len);
9767 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009768 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009769 ret = btrfs_orphan_add(trans,
9770 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009771 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009772 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009773 goto out_fail;
9774 }
Chris Mason39279cc2007-06-12 06:35:45 -04009775 }
Josef Bacikaec74772008-07-24 12:12:38 -04009776
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009777 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009778 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009779 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009780 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009781 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009782 goto out_fail;
9783 }
Chris Mason39279cc2007-06-12 06:35:45 -04009784
Miao Xie67de1172013-12-26 13:07:06 +08009785 if (old_inode->i_nlink == 1)
9786 BTRFS_I(old_inode)->dir_index = index;
9787
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009788 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009789 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9790 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009791 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009792 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794
9795 if (flags & RENAME_WHITEOUT) {
Christian Braunerca072742021-07-27 12:48:42 +02009796 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9797 old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798
9799 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009800 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 goto out_fail;
9802 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009803 }
Chris Mason39279cc2007-06-12 06:35:45 -04009804out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009805 /*
9806 * If we have pinned the log and an error happened, we unpin tasks
9807 * trying to sync the log and force them to fallback to a transaction
9808 * commit if the log currently contains any of the inodes involved in
9809 * this rename operation (to ensure we do not persist a log with an
9810 * inconsistent state for any of these inodes or leading to any
9811 * inconsistencies when replayed). If the transaction was aborted, the
9812 * abortion reason is propagated to userspace when attempting to commit
9813 * the transaction. If the log does not contain any of these inodes, we
9814 * allow the tasks to sync it.
9815 */
9816 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009817 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9818 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9819 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009820 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009821 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009822 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009823
9824 btrfs_end_log_trans(root);
9825 log_pinned = false;
9826 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009827 ret2 = btrfs_end_transaction(trans);
9828 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009829out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009830 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009831 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009832
Chris Mason39279cc2007-06-12 06:35:45 -04009833 return ret;
9834}
9835
Christian Brauner549c7292021-01-21 14:19:43 +01009836static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9837 struct dentry *old_dentry, struct inode *new_dir,
9838 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009839{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009840 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009841 return -EINVAL;
9842
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009843 if (flags & RENAME_EXCHANGE)
9844 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9845 new_dentry);
9846
Christian Braunerca072742021-07-27 12:48:42 +02009847 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9848 new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009849}
9850
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009851struct btrfs_delalloc_work {
9852 struct inode *inode;
9853 struct completion completion;
9854 struct list_head list;
9855 struct btrfs_work work;
9856};
9857
Miao Xie8ccf6f192012-10-25 09:28:04 +00009858static void btrfs_run_delalloc_work(struct btrfs_work *work)
9859{
9860 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009861 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009862
9863 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9864 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009865 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009866 filemap_flush(inode->i_mapping);
9867 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9868 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009869 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009870
Nikolay Borisov076da912018-04-23 10:54:16 +03009871 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009872 complete(&delalloc_work->completion);
9873}
9874
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009875static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009876{
9877 struct btrfs_delalloc_work *work;
9878
David Sterba100d5702015-12-08 14:39:32 +01009879 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009880 if (!work)
9881 return NULL;
9882
9883 init_completion(&work->completion);
9884 INIT_LIST_HEAD(&work->list);
9885 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009886 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009887
9888 return work;
9889}
9890
Chris Masond352ac62008-09-29 15:18:18 -04009891/*
9892 * some fairly slow code that needs optimization. This walks the list
9893 * of all the inodes with pending delalloc and forces them to disk.
9894 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009895static int start_delalloc_inodes(struct btrfs_root *root,
9896 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009897 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009898{
Chris Masonea8c2812008-08-04 23:17:27 -04009899 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009900 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009901 struct btrfs_delalloc_work *work, *next;
9902 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009903 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009904 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009905 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009906
Miao Xie8ccf6f192012-10-25 09:28:04 +00009907 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009908 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009909
Miao Xie573bfb72014-03-06 13:55:03 +08009910 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009911 spin_lock(&root->delalloc_lock);
9912 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009913 while (!list_empty(&splice)) {
9914 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009915 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009916
Miao Xieeb73c1b2013-05-15 07:48:22 +00009917 list_move_tail(&binode->delalloc_inodes,
9918 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009919
9920 if (in_reclaim_context &&
9921 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9922 continue;
9923
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009924 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009925 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009926 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009927 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009928 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009929 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009930
Ethan Lien3cd24c62018-11-01 14:49:03 +08009931 if (snapshot)
9932 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9933 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009934 if (full_flush) {
9935 work = btrfs_alloc_delalloc_work(inode);
9936 if (!work) {
9937 iput(inode);
9938 ret = -ENOMEM;
9939 goto out;
9940 }
9941 list_add_tail(&work->list, &works);
9942 btrfs_queue_work(root->fs_info->flush_workers,
9943 &work->work);
9944 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009945 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009946 btrfs_add_delayed_iput(inode);
9947 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009948 goto out;
9949 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009950 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009951 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009952 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009953 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009954
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009955out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009956 list_for_each_entry_safe(work, next, &works, list) {
9957 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009958 wait_for_completion(&work->completion);
9959 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009960 }
9961
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009962 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009963 spin_lock(&root->delalloc_lock);
9964 list_splice_tail(&splice, &root->delalloc_inodes);
9965 spin_unlock(&root->delalloc_lock);
9966 }
Miao Xie573bfb72014-03-06 13:55:03 +08009967 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009968 return ret;
9969}
9970
Filipe Mananaf9baa502021-04-22 12:08:05 +01009971int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009972{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009973 struct writeback_control wbc = {
9974 .nr_to_write = LONG_MAX,
9975 .sync_mode = WB_SYNC_NONE,
9976 .range_start = 0,
9977 .range_end = LLONG_MAX,
9978 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009979 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009980
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009981 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009982 return -EROFS;
9983
Filipe Mananaf9baa502021-04-22 12:08:05 +01009984 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009985}
9986
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009987int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009988 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009989{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009990 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009991 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009992 .sync_mode = WB_SYNC_NONE,
9993 .range_start = 0,
9994 .range_end = LLONG_MAX,
9995 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009996 struct btrfs_root *root;
9997 struct list_head splice;
9998 int ret;
9999
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010000 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010001 return -EROFS;
10002
10003 INIT_LIST_HEAD(&splice);
10004
Miao Xie573bfb72014-03-06 13:55:03 +080010005 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010006 spin_lock(&fs_info->delalloc_root_lock);
10007 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +020010008 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -050010009 /*
10010 * Reset nr_to_write here so we know that we're doing a full
10011 * flush.
10012 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +020010013 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -050010014 wbc.nr_to_write = LONG_MAX;
10015
Miao Xieeb73c1b2013-05-15 07:48:22 +000010016 root = list_first_entry(&splice, struct btrfs_root,
10017 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -050010018 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 BUG_ON(!root);
10020 list_move_tail(&root->delalloc_root,
10021 &fs_info->delalloc_roots);
10022 spin_unlock(&fs_info->delalloc_root_lock);
10023
Josef Bacike076ab2a2021-01-07 17:08:30 -050010024 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -050010025 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -050010026 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010027 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010028 spin_lock(&fs_info->delalloc_root_lock);
10029 }
10030 spin_unlock(&fs_info->delalloc_root_lock);
10031
Miao Xie6c255e62014-03-06 13:55:01 +080010032 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010034 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010035 spin_lock(&fs_info->delalloc_root_lock);
10036 list_splice_tail(&splice, &fs_info->delalloc_roots);
10037 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010038 }
Miao Xie573bfb72014-03-06 13:55:03 +080010039 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010041}
10042
Christian Brauner549c7292021-01-21 14:19:43 +010010043static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
10044 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -040010045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010046 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010047 struct btrfs_trans_handle *trans;
10048 struct btrfs_root *root = BTRFS_I(dir)->root;
10049 struct btrfs_path *path;
10050 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010051 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010052 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010053 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010054 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010055 int name_len;
10056 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010057 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010058 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010059 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010060
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010061 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010062 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010063 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010064
Josef Bacik9ed74f22009-09-11 16:12:44 -040010065 /*
10066 * 2 items for inode item and ref
10067 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010068 * 1 item for updating parent inode item
10069 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010070 * 1 item for xattr if selinux is on
10071 */
Filipe Manana9269d122015-12-31 18:16:29 +000010072 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010073 if (IS_ERR(trans))
10074 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010075
Nikolay Borisov543068a2020-12-07 17:32:33 +020010076 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +080010077 if (err)
10078 goto out_unlock;
10079
Christian Brauner5a052102021-07-27 12:48:47 +020010080 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
Christian Braunerb3b6f5b2021-07-27 12:48:41 +020010081 dentry->d_name.name, dentry->d_name.len,
10082 btrfs_ino(BTRFS_I(dir)), objectid,
10083 S_IFLNK | S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010084 if (IS_ERR(inode)) {
10085 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010086 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010087 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010088 }
Chris Mason39279cc2007-06-12 06:35:45 -040010089
Casey Schauflerad19db72011-12-15 10:09:07 -050010090 /*
10091 * If the active LSM wants to access the inode during
10092 * d_instantiate it needs these. Smack checks to see
10093 * if the filesystem supports xattrs by looking at the
10094 * ops vector.
10095 */
10096 inode->i_fop = &btrfs_file_operations;
10097 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010098 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010099
10100 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10101 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010102 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010103
Chris Mason39279cc2007-06-12 06:35:45 -040010104 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010105 if (!path) {
10106 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010107 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010108 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010109 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010110 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010111 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010112 datasize = btrfs_file_extent_calc_inline_size(name_len);
10113 err = btrfs_insert_empty_item(trans, root, path, &key,
10114 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010115 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010116 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010117 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010118 }
Chris Mason5f39d392007-10-15 16:14:19 -040010119 leaf = path->nodes[0];
10120 ei = btrfs_item_ptr(leaf, path->slots[0],
10121 struct btrfs_file_extent_item);
10122 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10123 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010124 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010125 btrfs_set_file_extent_encryption(leaf, ei, 0);
10126 btrfs_set_file_extent_compression(leaf, ei, 0);
10127 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10128 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10129
Chris Mason39279cc2007-06-12 06:35:45 -040010130 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010131 write_extent_buffer(leaf, symname, ptr, name_len);
10132 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010133 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010134
Chris Mason39279cc2007-06-12 06:35:45 -040010135 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010136 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010137 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010138 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010139 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +000010140 /*
10141 * Last step, add directory indexes for our symlink inode. This is the
10142 * last step to avoid extra cleanup of these indexes if an error happens
10143 * elsewhere above.
10144 */
10145 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010146 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10147 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010148 if (err)
10149 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010150
Al Viro1e2e5472018-05-04 08:23:01 -040010151 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010152
10153out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010154 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010155 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010156 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010157 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010158 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010159 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010160 return err;
10161}
Chris Mason16432982008-04-10 10:23:21 -040010162
Filipe Manana8fccebf2020-09-08 11:27:20 +010010163static struct btrfs_trans_handle *insert_prealloc_file_extent(
10164 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010165 struct btrfs_inode *inode,
10166 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +080010167 u64 file_offset)
10168{
10169 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +010010170 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010171 struct btrfs_trans_handle *trans = trans_in;
10172 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010173 u64 start = ins->objectid;
10174 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010175 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +080010176 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010177
10178 memset(&stack_fi, 0, sizeof(stack_fi));
10179
10180 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10181 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10182 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10183 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10184 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10185 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10186 /* Encryption and other encoding is reserved and all 0 */
10187
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010188 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10189 if (qgroup_released < 0)
10190 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010191
10192 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010193 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +000010194 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010195 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010196 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010197 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010198 return trans;
10199 }
10200
10201 extent_info.disk_offset = start;
10202 extent_info.disk_len = len;
10203 extent_info.data_offset = 0;
10204 extent_info.data_len = len;
10205 extent_info.file_offset = file_offset;
10206 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010207 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010208 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010209 extent_info.insertions = 0;
10210
10211 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010212 if (!path) {
10213 ret = -ENOMEM;
10214 goto free_qgroup;
10215 }
Filipe Manana8fccebf2020-09-08 11:27:20 +010010216
Nikolay Borisovbfc78472021-02-17 15:12:47 +020010217 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +010010218 file_offset + len - 1, &extent_info,
10219 &trans);
10220 btrfs_free_path(path);
10221 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010222 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010223 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010224
10225free_qgroup:
10226 /*
10227 * We have released qgroup data range at the beginning of the function,
10228 * and normally qgroup_released bytes will be freed when committing
10229 * transaction.
10230 * But if we error out early, we have to free what we have released
10231 * or we leak qgroup data reservation.
10232 */
10233 btrfs_qgroup_free_refroot(inode->root->fs_info,
10234 inode->root->root_key.objectid, qgroup_released,
10235 BTRFS_QGROUP_RSV_DATA);
10236 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +080010237}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010238
Josef Bacik0af3d002010-06-21 14:48:16 -040010239static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10240 u64 start, u64 num_bytes, u64 min_size,
10241 loff_t actual_len, u64 *alloc_hint,
10242 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010243{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010245 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10246 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010247 struct btrfs_root *root = BTRFS_I(inode)->root;
10248 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010249 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010250 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010251 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010252 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010253 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010254 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010255 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010256 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010257
Josef Bacik0af3d002010-06-21 14:48:16 -040010258 if (trans)
10259 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010260 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010261 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010262 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010263 /*
10264 * If we are severely fragmented we could end up with really
10265 * small allocations, so if the allocator is returning small
10266 * chunks lets make its job easier by only searching for those
10267 * sized chunks.
10268 */
10269 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010270 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10271 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010272 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010273 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010274
10275 /*
10276 * We've reserved this space, and thus converted it from
10277 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10278 * from here on out we will only need to clear our reservation
10279 * for the remaining unreserved area, so advance our
10280 * clear_offset by our extent size.
10281 */
10282 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010283
Josef Bacik0b670dc2015-09-23 17:11:16 -040010284 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010285 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10286 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010287 /*
10288 * Now that we inserted the prealloc extent we can finally
10289 * decrement the number of reservations in the block group.
10290 * If we did it before, we could race with relocation and have
10291 * relocation miss the reserved extent, making it fail later.
10292 */
10293 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010294 if (IS_ERR(trans)) {
10295 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010296 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010297 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010298 break;
10299 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010300
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010301 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010302 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010303
Josef Bacik5dc562c2012-08-17 13:14:17 -040010304 em = alloc_extent_map();
10305 if (!em) {
10306 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10307 &BTRFS_I(inode)->runtime_flags);
10308 goto next;
10309 }
10310
10311 em->start = cur_offset;
10312 em->orig_start = cur_offset;
10313 em->len = ins.offset;
10314 em->block_start = ins.objectid;
10315 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010316 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010317 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010318 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10319 em->generation = trans->transid;
10320
10321 while (1) {
10322 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010323 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010324 write_unlock(&em_tree->lock);
10325 if (ret != -EEXIST)
10326 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010327 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010328 cur_offset + ins.offset - 1,
10329 0);
10330 }
10331 free_extent_map(em);
10332next:
Yan Zhengd899e052008-10-30 14:25:28 -040010333 num_bytes -= ins.offset;
10334 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010335 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010336
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010337 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010338 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010339 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010340 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010341 (actual_len > inode->i_size) &&
10342 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010343 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010344 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010345 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010346 i_size = cur_offset;
10347 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010348 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010349 }
10350
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010351 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010352
10353 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010354 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010355 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010356 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010357 break;
10358 }
Yan Zhengd899e052008-10-30 14:25:28 -040010359
Filipe Manana8fccebf2020-09-08 11:27:20 +010010360 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010361 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010362 trans = NULL;
10363 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010364 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010365 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010366 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010367 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010368 return ret;
10369}
10370
Josef Bacik0af3d002010-06-21 14:48:16 -040010371int btrfs_prealloc_file_range(struct inode *inode, int mode,
10372 u64 start, u64 num_bytes, u64 min_size,
10373 loff_t actual_len, u64 *alloc_hint)
10374{
10375 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10376 min_size, actual_len, alloc_hint,
10377 NULL);
10378}
10379
10380int btrfs_prealloc_file_range_trans(struct inode *inode,
10381 struct btrfs_trans_handle *trans, int mode,
10382 u64 start, u64 num_bytes, u64 min_size,
10383 loff_t actual_len, u64 *alloc_hint)
10384{
10385 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10386 min_size, actual_len, alloc_hint, trans);
10387}
10388
Chris Masone6dcd2d2008-07-17 12:53:50 -040010389static int btrfs_set_page_dirty(struct page *page)
10390{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010391 return __set_page_dirty_nobuffers(page);
10392}
10393
Christian Brauner549c7292021-01-21 14:19:43 +010010394static int btrfs_permission(struct user_namespace *mnt_userns,
10395 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010396{
Li Zefanb83cc962010-12-20 16:04:08 +080010397 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010398 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010399
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010400 if (mask & MAY_WRITE &&
10401 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10402 if (btrfs_root_readonly(root))
10403 return -EROFS;
10404 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10405 return -EACCES;
10406 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010407 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010408}
Chris Mason39279cc2007-06-12 06:35:45 -040010409
Christian Brauner549c7292021-01-21 14:19:43 +010010410static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10411 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010412{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010413 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010414 struct btrfs_trans_handle *trans;
10415 struct btrfs_root *root = BTRFS_I(dir)->root;
10416 struct inode *inode = NULL;
10417 u64 objectid;
10418 u64 index;
10419 int ret = 0;
10420
10421 /*
10422 * 5 units required for adding orphan entry
10423 */
10424 trans = btrfs_start_transaction(root, 5);
10425 if (IS_ERR(trans))
10426 return PTR_ERR(trans);
10427
Nikolay Borisov543068a2020-12-07 17:32:33 +020010428 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010429 if (ret)
10430 goto out;
10431
Christian Braunerb3b6f5b2021-07-27 12:48:41 +020010432 inode = btrfs_new_inode(trans, root, &init_user_ns, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010433 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010434 if (IS_ERR(inode)) {
10435 ret = PTR_ERR(inode);
10436 inode = NULL;
10437 goto out;
10438 }
10439
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010440 inode->i_fop = &btrfs_file_operations;
10441 inode->i_op = &btrfs_file_inode_operations;
10442
10443 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010444
Chris Masonb0d5d102014-09-08 13:08:51 -070010445 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10446 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010447 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010448
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010449 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010450 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010451 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010452 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010453 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010454 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010455
Filipe Manana5762b5c2014-08-01 00:10:32 +010010456 /*
10457 * We set number of links to 0 in btrfs_new_inode(), and here we set
10458 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10459 * through:
10460 *
10461 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10462 */
10463 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010465 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010467out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010468 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010469 if (ret && inode)
10470 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010471 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010472 return ret;
10473}
10474
Qu Wenruod2a91062021-05-31 16:50:49 +080010475void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010476{
Qu Wenruod2a91062021-05-31 16:50:49 +080010477 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010478 unsigned long index = start >> PAGE_SHIFT;
10479 unsigned long end_index = end >> PAGE_SHIFT;
10480 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010481 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010482
Qu Wenruod2a91062021-05-31 16:50:49 +080010483 ASSERT(end + 1 - start <= U32_MAX);
10484 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010485 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010486 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010487 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010488
10489 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010490 put_page(page);
10491 index++;
10492 }
10493}
10494
Omar Sandovaled46ff32016-11-03 10:28:14 -070010495#ifdef CONFIG_SWAP
10496/*
10497 * Add an entry indicating a block group or device which is pinned by a
10498 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10499 * negative errno on failure.
10500 */
10501static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10502 bool is_block_group)
10503{
10504 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10505 struct btrfs_swapfile_pin *sp, *entry;
10506 struct rb_node **p;
10507 struct rb_node *parent = NULL;
10508
10509 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10510 if (!sp)
10511 return -ENOMEM;
10512 sp->ptr = ptr;
10513 sp->inode = inode;
10514 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010515 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010516
10517 spin_lock(&fs_info->swapfile_pins_lock);
10518 p = &fs_info->swapfile_pins.rb_node;
10519 while (*p) {
10520 parent = *p;
10521 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10522 if (sp->ptr < entry->ptr ||
10523 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10524 p = &(*p)->rb_left;
10525 } else if (sp->ptr > entry->ptr ||
10526 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10527 p = &(*p)->rb_right;
10528 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010529 if (is_block_group)
10530 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010531 spin_unlock(&fs_info->swapfile_pins_lock);
10532 kfree(sp);
10533 return 1;
10534 }
10535 }
10536 rb_link_node(&sp->node, parent, p);
10537 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10538 spin_unlock(&fs_info->swapfile_pins_lock);
10539 return 0;
10540}
10541
10542/* Free all of the entries pinned by this swapfile. */
10543static void btrfs_free_swapfile_pins(struct inode *inode)
10544{
10545 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10546 struct btrfs_swapfile_pin *sp;
10547 struct rb_node *node, *next;
10548
10549 spin_lock(&fs_info->swapfile_pins_lock);
10550 node = rb_first(&fs_info->swapfile_pins);
10551 while (node) {
10552 next = rb_next(node);
10553 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10554 if (sp->inode == inode) {
10555 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010556 if (sp->is_block_group) {
10557 btrfs_dec_block_group_swap_extents(sp->ptr,
10558 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010559 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010560 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010561 kfree(sp);
10562 }
10563 node = next;
10564 }
10565 spin_unlock(&fs_info->swapfile_pins_lock);
10566}
10567
10568struct btrfs_swap_info {
10569 u64 start;
10570 u64 block_start;
10571 u64 block_len;
10572 u64 lowest_ppage;
10573 u64 highest_ppage;
10574 unsigned long nr_pages;
10575 int nr_extents;
10576};
10577
10578static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10579 struct btrfs_swap_info *bsi)
10580{
10581 unsigned long nr_pages;
10582 u64 first_ppage, first_ppage_reported, next_ppage;
10583 int ret;
10584
10585 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10586 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10587 PAGE_SIZE) >> PAGE_SHIFT;
10588
10589 if (first_ppage >= next_ppage)
10590 return 0;
10591 nr_pages = next_ppage - first_ppage;
10592
10593 first_ppage_reported = first_ppage;
10594 if (bsi->start == 0)
10595 first_ppage_reported++;
10596 if (bsi->lowest_ppage > first_ppage_reported)
10597 bsi->lowest_ppage = first_ppage_reported;
10598 if (bsi->highest_ppage < (next_ppage - 1))
10599 bsi->highest_ppage = next_ppage - 1;
10600
10601 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10602 if (ret < 0)
10603 return ret;
10604 bsi->nr_extents += ret;
10605 bsi->nr_pages += nr_pages;
10606 return 0;
10607}
10608
10609static void btrfs_swap_deactivate(struct file *file)
10610{
10611 struct inode *inode = file_inode(file);
10612
10613 btrfs_free_swapfile_pins(inode);
10614 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10615}
10616
10617static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10618 sector_t *span)
10619{
10620 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010621 struct btrfs_root *root = BTRFS_I(inode)->root;
10622 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10624 struct extent_state *cached_state = NULL;
10625 struct extent_map *em = NULL;
10626 struct btrfs_device *device = NULL;
10627 struct btrfs_swap_info bsi = {
10628 .lowest_ppage = (sector_t)-1ULL,
10629 };
10630 int ret = 0;
10631 u64 isize;
10632 u64 start;
10633
10634 /*
10635 * If the swap file was just created, make sure delalloc is done. If the
10636 * file changes again after this, the user is doing something stupid and
10637 * we don't really care.
10638 */
10639 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10640 if (ret)
10641 return ret;
10642
10643 /*
10644 * The inode is locked, so these flags won't change after we check them.
10645 */
10646 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10647 btrfs_warn(fs_info, "swapfile must not be compressed");
10648 return -EINVAL;
10649 }
10650 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10651 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10652 return -EINVAL;
10653 }
10654 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10655 btrfs_warn(fs_info, "swapfile must not be checksummed");
10656 return -EINVAL;
10657 }
10658
10659 /*
10660 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010661 * under us. The exclop protection makes sure they aren't running/won't
10662 * run concurrently while we are mapping the swap extents, and
10663 * fs_info->swapfile_pins prevents them from running while the swap
10664 * file is active and moving the extents. Note that this also prevents
10665 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010666 * really worth the trouble to allow it.
10667 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010668 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010669 btrfs_warn(fs_info,
10670 "cannot activate swapfile while exclusive operation is running");
10671 return -EBUSY;
10672 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010673
10674 /*
10675 * Prevent snapshot creation while we are activating the swap file.
10676 * We do not want to race with snapshot creation. If snapshot creation
10677 * already started before we bumped nr_swapfiles from 0 to 1 and
10678 * completes before the first write into the swap file after it is
10679 * activated, than that write would fallback to COW.
10680 */
10681 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10682 btrfs_exclop_finish(fs_info);
10683 btrfs_warn(fs_info,
10684 "cannot activate swapfile because snapshot creation is in progress");
10685 return -EINVAL;
10686 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010687 /*
10688 * Snapshots can create extents which require COW even if NODATACOW is
10689 * set. We use this counter to prevent snapshots. We must increment it
10690 * before walking the extents because we don't want a concurrent
10691 * snapshot to run after we've already checked the extents.
10692 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010693 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010694
10695 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10696
10697 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10698 start = 0;
10699 while (start < isize) {
10700 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010701 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010702 u64 len = isize - start;
10703
Omar Sandoval39b07b52019-12-02 17:34:23 -080010704 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010705 if (IS_ERR(em)) {
10706 ret = PTR_ERR(em);
10707 goto out;
10708 }
10709
10710 if (em->block_start == EXTENT_MAP_HOLE) {
10711 btrfs_warn(fs_info, "swapfile must not have holes");
10712 ret = -EINVAL;
10713 goto out;
10714 }
10715 if (em->block_start == EXTENT_MAP_INLINE) {
10716 /*
10717 * It's unlikely we'll ever actually find ourselves
10718 * here, as a file small enough to fit inline won't be
10719 * big enough to store more than the swap header, but in
10720 * case something changes in the future, let's catch it
10721 * here rather than later.
10722 */
10723 btrfs_warn(fs_info, "swapfile must not be inline");
10724 ret = -EINVAL;
10725 goto out;
10726 }
10727 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10728 btrfs_warn(fs_info, "swapfile must not be compressed");
10729 ret = -EINVAL;
10730 goto out;
10731 }
10732
10733 logical_block_start = em->block_start + (start - em->start);
10734 len = min(len, em->len - (start - em->start));
10735 free_extent_map(em);
10736 em = NULL;
10737
Boris Burkova84d5d42020-08-18 11:00:05 -070010738 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010739 if (ret < 0) {
10740 goto out;
10741 } else if (ret) {
10742 ret = 0;
10743 } else {
10744 btrfs_warn(fs_info,
10745 "swapfile must not be copy-on-write");
10746 ret = -EINVAL;
10747 goto out;
10748 }
10749
10750 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10751 if (IS_ERR(em)) {
10752 ret = PTR_ERR(em);
10753 goto out;
10754 }
10755
10756 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10757 btrfs_warn(fs_info,
10758 "swapfile must have single data profile");
10759 ret = -EINVAL;
10760 goto out;
10761 }
10762
10763 if (device == NULL) {
10764 device = em->map_lookup->stripes[0].dev;
10765 ret = btrfs_add_swapfile_pin(inode, device, false);
10766 if (ret == 1)
10767 ret = 0;
10768 else if (ret)
10769 goto out;
10770 } else if (device != em->map_lookup->stripes[0].dev) {
10771 btrfs_warn(fs_info, "swapfile must be on one device");
10772 ret = -EINVAL;
10773 goto out;
10774 }
10775
10776 physical_block_start = (em->map_lookup->stripes[0].physical +
10777 (logical_block_start - em->start));
10778 len = min(len, em->len - (logical_block_start - em->start));
10779 free_extent_map(em);
10780 em = NULL;
10781
10782 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10783 if (!bg) {
10784 btrfs_warn(fs_info,
10785 "could not find block group containing swapfile");
10786 ret = -EINVAL;
10787 goto out;
10788 }
10789
Filipe Manana195a49e2021-02-05 12:55:37 +000010790 if (!btrfs_inc_block_group_swap_extents(bg)) {
10791 btrfs_warn(fs_info,
10792 "block group for swapfile at %llu is read-only%s",
10793 bg->start,
10794 atomic_read(&fs_info->scrubs_running) ?
10795 " (scrub running)" : "");
10796 btrfs_put_block_group(bg);
10797 ret = -EINVAL;
10798 goto out;
10799 }
10800
Omar Sandovaled46ff32016-11-03 10:28:14 -070010801 ret = btrfs_add_swapfile_pin(inode, bg, true);
10802 if (ret) {
10803 btrfs_put_block_group(bg);
10804 if (ret == 1)
10805 ret = 0;
10806 else
10807 goto out;
10808 }
10809
10810 if (bsi.block_len &&
10811 bsi.block_start + bsi.block_len == physical_block_start) {
10812 bsi.block_len += len;
10813 } else {
10814 if (bsi.block_len) {
10815 ret = btrfs_add_swap_extent(sis, &bsi);
10816 if (ret)
10817 goto out;
10818 }
10819 bsi.start = start;
10820 bsi.block_start = physical_block_start;
10821 bsi.block_len = len;
10822 }
10823
10824 start += len;
10825 }
10826
10827 if (bsi.block_len)
10828 ret = btrfs_add_swap_extent(sis, &bsi);
10829
10830out:
10831 if (!IS_ERR_OR_NULL(em))
10832 free_extent_map(em);
10833
10834 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10835
10836 if (ret)
10837 btrfs_swap_deactivate(file);
10838
Filipe Mananadd0734f2021-02-05 12:55:38 +000010839 btrfs_drew_write_unlock(&root->snapshot_lock);
10840
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010841 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010842
10843 if (ret)
10844 return ret;
10845
10846 if (device)
10847 sis->bdev = device->bdev;
10848 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10849 sis->max = bsi.nr_pages;
10850 sis->pages = bsi.nr_pages - 1;
10851 sis->highest_bit = bsi.nr_pages - 1;
10852 return bsi.nr_extents;
10853}
10854#else
10855static void btrfs_swap_deactivate(struct file *file)
10856{
10857}
10858
10859static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10860 sector_t *span)
10861{
10862 return -EOPNOTSUPP;
10863}
10864#endif
10865
Filipe Manana2766ff62020-11-04 11:07:34 +000010866/*
10867 * Update the number of bytes used in the VFS' inode. When we replace extents in
10868 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10869 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10870 * always get a correct value.
10871 */
10872void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10873 const u64 add_bytes,
10874 const u64 del_bytes)
10875{
10876 if (add_bytes == del_bytes)
10877 return;
10878
10879 spin_lock(&inode->lock);
10880 if (del_bytes > 0)
10881 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10882 if (add_bytes > 0)
10883 inode_add_bytes(&inode->vfs_inode, add_bytes);
10884 spin_unlock(&inode->lock);
10885}
10886
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010887static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010888 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010889 .lookup = btrfs_lookup,
10890 .create = btrfs_create,
10891 .unlink = btrfs_unlink,
10892 .link = btrfs_link,
10893 .mkdir = btrfs_mkdir,
10894 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010895 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010896 .symlink = btrfs_symlink,
10897 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010898 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010899 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010900 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010901 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010902 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010903 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010904 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010905 .fileattr_get = btrfs_fileattr_get,
10906 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010907};
Yan, Zheng76dda932009-09-21 16:00:26 -040010908
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010909static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010910 .llseek = generic_file_llseek,
10911 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010912 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010913 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010914 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010915#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010916 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010917#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010918 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010919 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010920};
10921
Chris Mason35054392009-01-21 13:11:13 -050010922/*
10923 * btrfs doesn't support the bmap operation because swapfiles
10924 * use bmap to make a mapping of extents in the file. They assume
10925 * these extents won't change over the life of the file and they
10926 * use the bmap result to do IO directly to the drive.
10927 *
10928 * the btrfs bmap call would return logical addresses that aren't
10929 * suitable for IO and they also will change frequently as COW
10930 * operations happen. So, swapfile + btrfs == corruption.
10931 *
10932 * For now we're avoiding this by dropping bmap.
10933 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010934static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010935 .readpage = btrfs_readpage,
10936 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010937 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010938 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010939 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010940 .invalidatepage = btrfs_invalidatepage,
10941 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010942#ifdef CONFIG_MIGRATION
10943 .migratepage = btrfs_migratepage,
10944#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010945 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010946 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010947 .swap_activate = btrfs_swap_activate,
10948 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010949};
10950
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010951static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010952 .getattr = btrfs_getattr,
10953 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010954 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010955 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010956 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010957 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010958 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010959 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010960 .fileattr_get = btrfs_fileattr_get,
10961 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010962};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010963static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010964 .getattr = btrfs_getattr,
10965 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010966 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010967 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010968 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010969 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010970 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010971};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010972static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010973 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010974 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010975 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010976 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010977 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010978 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010979};
Yan, Zheng76dda932009-09-21 16:00:26 -040010980
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010981const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010982 .d_delete = btrfs_dentry_delete,
10983};