blob: 9f7b7110f629eb664692e4f61ba231aaba20b9aa [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050033#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010052#include "space-info.h"
Naohiro Aotad8e3fb12021-02-04 19:22:05 +090053#include "zoned.h"
Qu Wenruob945a462021-05-31 16:50:46 +080054#include "subpage.h"
Chris Mason39279cc2007-06-12 06:35:45 -040055
56struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020057 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040058 struct btrfs_root *root;
59};
60
Filipe Mananaf28a4922015-12-08 19:23:20 +000061struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000062 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050063 loff_t length;
64 ssize_t submitted;
65 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000066};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070070static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
76struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000079struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
Eric Sandeen3972f262013-01-12 02:57:22 +000081static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000082static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040083static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030084static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050085 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030086 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030087 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030088static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
89 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080090 u64 block_len, u64 orig_block_len,
91 u64 ram_bytes, int compress_type,
92 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030094static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080095 const u64 offset, const u64 bytes,
96 const bool uptodate);
97
98/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050099 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
100 *
101 * ilock_flags can have the following bit set:
102 *
103 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
104 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
105 * return -EAGAIN
Josef Bacik8318ba72021-02-10 17:14:33 -0500106 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500107 */
108int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
109{
110 if (ilock_flags & BTRFS_ILOCK_SHARED) {
111 if (ilock_flags & BTRFS_ILOCK_TRY) {
112 if (!inode_trylock_shared(inode))
113 return -EAGAIN;
114 else
115 return 0;
116 }
117 inode_lock_shared(inode);
118 } else {
119 if (ilock_flags & BTRFS_ILOCK_TRY) {
120 if (!inode_trylock(inode))
121 return -EAGAIN;
122 else
123 return 0;
124 }
125 inode_lock(inode);
126 }
Josef Bacik8318ba72021-02-10 17:14:33 -0500127 if (ilock_flags & BTRFS_ILOCK_MMAP)
128 down_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500129 return 0;
130}
131
132/*
133 * btrfs_inode_unlock - unock inode i_rwsem
134 *
135 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
136 * to decide whether the lock acquired is shared or exclusive.
137 */
138void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
139{
Josef Bacik8318ba72021-02-10 17:14:33 -0500140 if (ilock_flags & BTRFS_ILOCK_MMAP)
141 up_write(&BTRFS_I(inode)->i_mmap_lock);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -0500142 if (ilock_flags & BTRFS_ILOCK_SHARED)
143 inode_unlock_shared(inode);
144 else
145 inode_unlock(inode);
146}
147
148/*
Qu Wenruo524272602017-03-08 10:25:52 +0800149 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100150 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800151 *
152 * NOTE: caller must ensure that when an error happens, it can not call
153 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
154 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
155 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200156 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800157 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300158static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200159 struct page *locked_page,
160 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800161{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900162 unsigned long index = offset >> PAGE_SHIFT;
163 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200164 u64 page_start = page_offset(locked_page);
165 u64 page_end = page_start + PAGE_SIZE - 1;
166
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900167 struct page *page;
168
169 while (index <= end_index) {
Qu Wenruo968f2562021-05-18 15:09:41 +0800170 /*
171 * For locked page, we will call end_extent_writepage() on it
172 * in run_delalloc_range() for the error handling. That
173 * end_extent_writepage() function will call
174 * btrfs_mark_ordered_io_finished() to clear page Ordered and
175 * run the ordered extent accounting.
176 *
177 * Here we can't just clear the Ordered bit, or
178 * btrfs_mark_ordered_io_finished() would skip the accounting
179 * for the page range, and the ordered extent will never finish.
180 */
181 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
182 index++;
183 continue;
184 }
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300185 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900186 index++;
187 if (!page)
188 continue;
Qu Wenruo968f2562021-05-18 15:09:41 +0800189
190 /*
191 * Here we just clear all Ordered bits for every page in the
192 * range, then __endio_write_update_ordered() will handle
193 * the ordered extent accounting for the range.
194 */
Qu Wenruob945a462021-05-31 16:50:46 +0800195 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
196 offset, bytes);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900197 put_page(page);
198 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200199
Qu Wenruob945a462021-05-31 16:50:46 +0800200 /* The locked page covers the full range, nothing needs to be done */
201 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
202 return;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200203 /*
204 * In case this page belongs to the delalloc range being instantiated
205 * then skip it, since the first page of a range is going to be
206 * properly cleaned up by the caller of run_delalloc_range
207 */
208 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
Qu Wenruob945a462021-05-31 16:50:46 +0800209 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
210 offset = page_offset(locked_page) + PAGE_SIZE;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200211 }
212
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300213 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800214}
215
Eric Sandeen48a3b632013-04-25 20:41:01 +0000216static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400217
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000218static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500219 struct inode *inode, struct inode *dir,
220 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500221{
222 int err;
223
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000224 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500225 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500226 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500227 return err;
228}
229
Chris Masond352ac62008-09-29 15:18:18 -0400230/*
Chris Masonc8b97812008-10-29 14:49:59 -0400231 * this does all the hard work for inserting an inline extent into
232 * the btree. The caller should have done a btrfs_drop_extents so that
233 * no overlapping inline items exist in the btree
234 */
Chris Mason40f76582014-05-21 13:35:51 -0700235static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000236 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400237 struct btrfs_root *root, struct inode *inode,
238 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000239 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400240 struct page **compressed_pages)
241{
Chris Masonc8b97812008-10-29 14:49:59 -0400242 struct extent_buffer *leaf;
243 struct page *page = NULL;
244 char *kaddr;
245 unsigned long ptr;
246 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400247 int ret;
248 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400249 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400250
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800251 ASSERT((compressed_size > 0 && compressed_pages) ||
252 (compressed_size == 0 && !compressed_pages));
253
Li Zefanfe3f5662011-03-28 08:30:38 +0000254 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400255 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000257 if (!extent_inserted) {
258 struct btrfs_key key;
259 size_t datasize;
260
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200261 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000262 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200263 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000264
265 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 ret = btrfs_insert_empty_item(trans, root, path, &key,
267 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200268 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400270 }
271 leaf = path->nodes[0];
272 ei = btrfs_item_ptr(leaf, path->slots[0],
273 struct btrfs_file_extent_item);
274 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
275 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
276 btrfs_set_file_extent_encryption(leaf, ei, 0);
277 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
278 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
279 ptr = btrfs_file_extent_inline_start(ei);
280
Li Zefan261507a02010-12-17 14:21:50 +0800281 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400282 struct page *cpage;
283 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500284 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400285 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500286 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400288
Cong Wang7ac687d2011-11-25 23:14:28 +0800289 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400290 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800291 kunmap_atomic(kaddr);
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{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300493 if (inode->flags & BTRFS_INODE_NODATACOW ||
494 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000495 return false;
496 return true;
497}
498
499/*
500 * Check if the inode needs to be submitted to compression, based on mount
501 * options, defragmentation, properties or heuristics.
502 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300503static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
504 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800505{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300506 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800507
Nikolay Borisov808a1292020-06-03 08:55:27 +0300508 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000509 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
510 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300511 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000512 return 0;
513 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800514 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400515 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800516 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200517 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300518 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200519 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800520 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300521 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800522 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400523 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300524 inode->flags & BTRFS_INODE_COMPRESS ||
525 inode->prop_compress)
526 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800527 return 0;
528}
529
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200530static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800531 u64 start, u64 end, u64 num_bytes, u64 small_write)
532{
533 /* If this is a small write inside eof, kick off a defrag */
534 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200535 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800536 btrfs_add_inode_defrag(NULL, inode);
537}
538
Chris Masonc8b97812008-10-29 14:49:59 -0400539/*
Chris Mason771ed682008-11-06 22:02:51 -0500540 * we create compressed extents in two phases. The first
541 * phase compresses a range of pages that have already been
542 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400543 *
Chris Mason771ed682008-11-06 22:02:51 -0500544 * This is done inside an ordered work queue, and the compression
545 * is spread across many cpus. The actual IO submission is step
546 * two, and the ordered work queue takes care of making sure that
547 * happens in the same order things were put onto the queue by
548 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400549 *
Chris Mason771ed682008-11-06 22:02:51 -0500550 * If this code finds it can't get good compression, it puts an
551 * entry onto the work queue to write the uncompressed bytes. This
552 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300553 * are written in the same order that the flusher thread sent them
554 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400555 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300556static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400557{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200558 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400559 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400560 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200561 u64 start = async_chunk->start;
562 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400563 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400564 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400565 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400566 struct page **pages = NULL;
567 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400568 unsigned long total_compressed = 0;
569 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400570 int i;
571 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400572 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300573 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400574 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400575
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200576 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
577 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400578
Josef Bacikd98da492019-10-11 09:03:54 -0400579 /*
580 * We need to save i_size before now because it could change in between
581 * us evaluating the size and assigning it. This is because we lock and
582 * unlock the page in truncate and fallocate, and then modify the i_size
583 * later on.
584 *
585 * The barriers are to emulate READ_ONCE, remove that once i_size_read
586 * does that for us.
587 */
588 barrier();
589 i_size = i_size_read(inode);
590 barrier();
591 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400592again:
593 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300594 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100595 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
596 nr_pages = min_t(unsigned long, nr_pages,
597 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400598
Chris Masonf03d9301f2009-02-04 09:31:06 -0500599 /*
600 * we don't want to send crud past the end of i_size through
601 * compression, that's just a waste of CPU time. So, if the
602 * end of the file is before the start of our current
603 * requested range of bytes, we bail out to the uncompressed
604 * cleanup code that can deal with all of this.
605 *
606 * It isn't really the fastest way to fix things, but this is a
607 * very uncommon corner.
608 */
609 if (actual_end <= start)
610 goto cleanup_and_bail_uncompressed;
611
Chris Masonc8b97812008-10-29 14:49:59 -0400612 total_compressed = actual_end - start;
613
Shilong Wang4bcbb332014-10-07 18:44:35 -0400614 /*
615 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400616 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400617 */
618 if (total_compressed <= blocksize &&
619 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
620 goto cleanup_and_bail_uncompressed;
621
David Sterba069eac72017-02-14 19:36:54 +0100622 total_compressed = min_t(unsigned long, total_compressed,
623 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400624 total_in = 0;
625 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400626
Chris Mason771ed682008-11-06 22:02:51 -0500627 /*
628 * we do compression for mount -o compress and when the
629 * inode has not been flagged as nocompress. This flag can
630 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300632 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400633 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100634 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800635 if (!pages) {
636 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100637 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800638 goto cont;
639 }
Chris Mason179e29e2007-11-01 11:28:41 -0400640
David Sterbaeec63c62017-07-17 19:41:31 +0200641 if (BTRFS_I(inode)->defrag_compress)
642 compress_type = BTRFS_I(inode)->defrag_compress;
643 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200644 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800645
Chris Mason4adaa612013-03-26 13:07:00 -0400646 /*
647 * we need to call clear_page_dirty_for_io on each
648 * page in the range. Otherwise applications with the file
649 * mmap'd can wander in and change the page contents while
650 * we are compressing them.
651 *
652 * If the compression fails for any reason, we set the pages
653 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300654 *
655 * Note that the remaining part is redirtied, the start pointer
656 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400657 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300658 if (!redirty) {
659 extent_range_clear_dirty_for_io(inode, start, end);
660 redirty = 1;
661 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200662
663 /* Compression level is applied here and only here */
664 ret = btrfs_compress_pages(
665 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800666 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100667 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100668 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800669 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100670 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400671
672 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100673 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100674 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400675
676 /* zero the tail end of the last page, we might be
677 * sending it down to disk
678 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700679 if (offset)
680 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400681 will_compress = 1;
682 }
683 }
Li Zefan560f7d72011-09-08 10:22:01 +0800684cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400685 if (start == 0) {
686 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300687 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400688 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500689 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400690 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300691 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
692 0, BTRFS_COMPRESS_NONE,
693 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400694 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500695 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300696 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000697 total_compressed,
698 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400699 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100700 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400701 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400702 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
703 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100704 unsigned long page_error_op;
705
Filipe Mananae6eb4312014-10-10 10:45:12 +0100706 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400707
Chris Mason771ed682008-11-06 22:02:51 -0500708 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100709 * inline extent creation worked or returned error,
710 * we don't need to create any more async work items.
711 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400712 *
713 * We use DO_ACCOUNTING here because we need the
714 * delalloc_release_metadata to be done _after_ we drop
715 * our outstanding extent for clearing delalloc for this
716 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500717 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300718 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
719 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300720 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800721 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800722 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100723 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400724 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300725
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800726 /*
727 * Ensure we only free the compressed pages if we have
728 * them allocated, as we can still reach here with
729 * inode_need_compress() == false.
730 */
731 if (pages) {
732 for (i = 0; i < nr_pages; i++) {
733 WARN_ON(pages[i]->mapping);
734 put_page(pages[i]);
735 }
736 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300737 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300738 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400739 }
740 }
741
742 if (will_compress) {
743 /*
744 * we aren't doing an inline extent round the compressed size
745 * up to a block size boundary so the allocator does sane
746 * things
747 */
Qu Wenruofda28322013-02-26 08:10:22 +0000748 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400749
750 /*
751 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300752 * win, compare the page count read with the blocks on disk,
753 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400754 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300755 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300756 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300757 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700758
759 /*
760 * The async work queues will take care of doing actual
761 * allocation on disk for these compressed pages, and
762 * will submit them to the elevator.
763 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100765 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700766 compress_type);
767
Timofey Titovets11708622017-10-03 18:06:01 +0300768 if (start + total_in < end) {
769 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700770 pages = NULL;
771 cond_resched();
772 goto again;
773 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300774 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400775 }
776 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700777 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400778 /*
779 * the compression code ran but failed to make things smaller,
780 * free any pages it allocated and our page pointer array
781 */
David Sterba4d3a8002017-02-14 19:04:07 +0100782 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400783 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300784 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400785 }
786 kfree(pages);
787 pages = NULL;
788 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100789 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400790
791 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400792 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200793 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500794 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500795 }
Chris Masonc8b97812008-10-29 14:49:59 -0400796 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500797cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700798 /*
799 * No compression, but we still need to write the pages in the file
800 * we've been given so far. redirty the locked page if it corresponds
801 * to our extent and set things up for the async work queue to run
802 * cow_file_range to do the normal delalloc dance.
803 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700804 if (async_chunk->locked_page &&
805 (page_offset(async_chunk->locked_page) >= start &&
806 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200807 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700808 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700809 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700810
811 if (redirty)
812 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200813 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700814 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300815 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500816
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300817 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500818}
Chris Mason771ed682008-11-06 22:02:51 -0500819
Filipe Manana40ae8372014-10-06 22:14:24 +0100820static void free_async_extent_pages(struct async_extent *async_extent)
821{
822 int i;
823
824 if (!async_extent->pages)
825 return;
826
827 for (i = 0; i < async_extent->nr_pages; i++) {
828 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300829 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100830 }
831 kfree(async_extent->pages);
832 async_extent->nr_pages = 0;
833 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500834}
835
836/*
837 * phase two of compressed writeback. This is the ordered portion
838 * of the code, which only gets called in the order the work was
839 * queued. We walk all the async extents created by compress_file_range
840 * and send them down to the disk.
841 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200842static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500843{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300844 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
845 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500846 struct async_extent *async_extent;
847 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500848 struct btrfs_key ins;
849 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300850 struct btrfs_root *root = inode->root;
851 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500852 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500853
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200855 while (!list_empty(&async_chunk->extents)) {
856 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500857 struct async_extent, list);
858 list_del(&async_extent->list);
859
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500860retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200861 lock_extent(io_tree, async_extent->start,
862 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500863 /* did the compression code fall back to uncompressed IO? */
864 if (!async_extent->pages) {
865 int page_started = 0;
866 unsigned long nr_written = 0;
867
Chris Mason771ed682008-11-06 22:02:51 -0500868 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300869 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500870 async_extent->start,
871 async_extent->start +
872 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300873 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500874
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875 /* JDM XXX */
876
Chris Mason771ed682008-11-06 22:02:51 -0500877 /*
878 * if page_started, cow_file_range inserted an
879 * inline extent and took care of all the unlocking
880 * and IO for us. Otherwise, we need to submit
881 * all those pages down to the drive.
882 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500883 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300884 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200885 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500886 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500887 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500888 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700889 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200890 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500891 kfree(async_extent);
892 cond_resched();
893 continue;
894 }
895
Wang Xiaoguang18513092016-07-25 15:51:40 +0800896 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500897 async_extent->compressed_size,
898 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800899 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500900 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100901 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500902
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400903 if (ret == -ENOSPC) {
904 unlock_extent(io_tree, async_extent->start,
905 async_extent->start +
906 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800907
908 /*
909 * we need to redirty the pages if we decide to
910 * fallback to uncompressed IO, otherwise we
911 * will not submit these pages down to lower
912 * layers.
913 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300914 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800915 async_extent->start,
916 async_extent->start +
917 async_extent->ram_size - 1);
918
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400920 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500921 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500922 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000923 /*
924 * here we're doing allocation and writeback of the
925 * compressed pages
926 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300927 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800928 async_extent->ram_size, /* len */
929 async_extent->start, /* orig_start */
930 ins.objectid, /* block_start */
931 ins.offset, /* block_len */
932 ins.offset, /* orig_block_len */
933 async_extent->ram_size, /* ram_bytes */
934 async_extent->compress_type,
935 BTRFS_ORDERED_COMPRESSED);
936 if (IS_ERR(em))
937 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500938 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800939 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500940
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300941 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800942 async_extent->start,
943 ins.objectid,
944 async_extent->ram_size,
945 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800946 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100947 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300948 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100949 async_extent->start +
950 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500951 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100952 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400953 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500954
Chris Mason771ed682008-11-06 22:02:51 -0500955 /*
956 * clear dirty, set writeback and unlock the pages.
957 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300958 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400959 async_extent->start +
960 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400961 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800962 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300963 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500964 async_extent->ram_size,
965 ins.objectid,
966 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600967 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700968 async_chunk->write_flags,
969 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100970 struct page *p = async_extent->pages[0];
971 const u64 start = async_extent->start;
972 const u64 end = start + async_extent->ram_size - 1;
973
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300974 p->mapping = inode->vfs_inode.i_mapping;
Qu Wenruo38a39ac72021-04-08 20:32:27 +0800975 btrfs_writepage_endio_finish_ordered(inode, p, start,
976 end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200977
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100978 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300979 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100980 PAGE_END_WRITEBACK |
981 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100982 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100983 }
Chris Mason771ed682008-11-06 22:02:51 -0500984 alloc_hint = ins.objectid + ins.offset;
985 kfree(async_extent);
986 cond_resched();
987 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100988 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500989out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400991 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100992out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300993 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500994 async_extent->start +
995 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400996 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100997 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400998 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800999 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1000 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +01001001 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001003 goto again;
Chris Mason771ed682008-11-06 22:02:51 -05001004}
1005
Nikolay Borisov43c69842020-06-03 08:55:02 +03001006static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04001007 u64 num_bytes)
1008{
Nikolay Borisov43c69842020-06-03 08:55:02 +03001009 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -04001010 struct extent_map *em;
1011 u64 alloc_hint = 0;
1012
1013 read_lock(&em_tree->lock);
1014 em = search_extent_mapping(em_tree, start, num_bytes);
1015 if (em) {
1016 /*
1017 * if block start isn't an actual block number then find the
1018 * first block in this inode and use that as a hint. If that
1019 * block is also bogus then just don't worry about it.
1020 */
1021 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1022 free_extent_map(em);
1023 em = search_extent_mapping(em_tree, 0, 0);
1024 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1025 alloc_hint = em->block_start;
1026 if (em)
1027 free_extent_map(em);
1028 } else {
1029 alloc_hint = em->block_start;
1030 free_extent_map(em);
1031 }
1032 }
1033 read_unlock(&em_tree->lock);
1034
1035 return alloc_hint;
1036}
1037
Chris Mason771ed682008-11-06 22:02:51 -05001038/*
1039 * when extent_io.c finds a delayed allocation range in the file,
1040 * the call backs end up in this code. The basic idea is to
1041 * allocate extents on disk for the range, and create ordered data structs
1042 * in ram to track those extents.
1043 *
1044 * locked_page is the page that writepage had locked already. We use
1045 * it to make sure we don't do extra locks or unlocks.
1046 *
1047 * *page_started is set to one if we unlock locked_page and do everything
1048 * required to start IO on it. It may be clean and already done with
1049 * IO when we return.
1050 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001051static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001052 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001053 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001054 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001055{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001056 struct btrfs_root *root = inode->root;
1057 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001058 u64 alloc_hint = 0;
1059 u64 num_bytes;
1060 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001061 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001062 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001064 struct btrfs_key ins;
1065 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001066 unsigned clear_bits;
1067 unsigned long page_ops;
1068 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001069 int ret = 0;
1070
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001071 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001072 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001073 ret = -EINVAL;
1074 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001075 }
Chris Mason771ed682008-11-06 22:02:51 -05001076
Qu Wenruofda28322013-02-26 08:10:22 +00001077 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001078 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001079 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001080
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001081 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001082
Chris Mason771ed682008-11-06 22:02:51 -05001083 if (start == 0) {
1084 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001085 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001086 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001087 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001088 /*
1089 * We use DO_ACCOUNTING here because we need the
1090 * delalloc_release_metadata to be run _after_ we drop
1091 * our outstanding extent for clearing delalloc for this
1092 * range.
1093 */
Qu Wenruo4750af32021-05-31 16:50:48 +08001094 extent_clear_unlock_delalloc(inode, start, end,
1095 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001096 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001097 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1098 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001099 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001100 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001101 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001102 *page_started = 1;
Qu Wenruo4750af32021-05-31 16:50:48 +08001103 /*
1104 * locked_page is locked by the caller of
1105 * writepage_delalloc(), not locked by
1106 * __process_pages_contig().
1107 *
1108 * We can't let __process_pages_contig() to unlock it,
1109 * as it doesn't have any subpage::writers recorded.
1110 *
1111 * Here we manually unlock the page, since the caller
1112 * can't use page_started to determine if it's an
1113 * inline extent or a compressed extent.
1114 */
1115 unlock_page(locked_page);
Chris Mason771ed682008-11-06 22:02:51 -05001116 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001117 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001119 }
1120 }
Chris Masonc8b97812008-10-29 14:49:59 -04001121
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001122 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1123 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001124
Filipe Manana432cd2a2020-06-08 13:32:55 +01001125 /*
1126 * Relocation relies on the relocated extents to have exactly the same
1127 * size as the original extents. Normally writeback for relocation data
1128 * extents follows a NOCOW path because relocation preallocates the
1129 * extents. However, due to an operation such as scrub turning a block
1130 * group to RO mode, it may fallback to COW mode, so we must make sure
1131 * an extent allocated during COW has exactly the requested size and can
1132 * not be split into smaller extents, otherwise relocation breaks and
1133 * fails during the stage where it updates the bytenr of file extent
1134 * items.
1135 */
1136 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1137 min_alloc_size = num_bytes;
1138 else
1139 min_alloc_size = fs_info->sectorsize;
1140
Anand Jain3752d222018-02-15 12:29:38 +08001141 while (num_bytes > 0) {
1142 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001143 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001144 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001145 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001146 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001147 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001148 cur_alloc_size = ins.offset;
1149 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001150
Chris Mason771ed682008-11-06 22:02:51 -05001151 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001152 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001153 start, /* orig_start */
1154 ins.objectid, /* block_start */
1155 ins.offset, /* block_len */
1156 ins.offset, /* orig_block_len */
1157 ram_size, /* ram_bytes */
1158 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001159 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001160 if (IS_ERR(em)) {
1161 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001162 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001163 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001164 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001165
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001166 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001167 ram_size, cur_alloc_size,
1168 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001169 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001170 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001171
Yan Zheng17d217f2008-12-12 10:03:38 -05001172 if (root->root_key.objectid ==
1173 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001174 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001175 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001176 /*
1177 * Only drop cache here, and process as normal.
1178 *
1179 * We must not allow extent_clear_unlock_delalloc()
1180 * at out_unlock label to free meta of this ordered
1181 * extent, as its meta should be freed by
1182 * btrfs_finish_ordered_io().
1183 *
1184 * So we must continue until @start is increased to
1185 * skip current ordered extent.
1186 */
Josef Bacik00361582013-08-14 14:02:47 -04001187 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001188 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001189 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001190 }
1191
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001192 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001193
Qu Wenruof57ad932021-04-07 19:22:13 +08001194 /*
1195 * We're not doing compressed IO, don't unlock the first page
1196 * (which the caller expects to stay locked), don't clear any
1197 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001198 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001199 * Do set the Ordered (Private2) bit so we know this page was
1200 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001201 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001202 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001203 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001204
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001205 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001206 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001207 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001208 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001209 if (num_bytes < cur_alloc_size)
1210 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001211 else
Anand Jain3752d222018-02-15 12:29:38 +08001212 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001213 alloc_hint = ins.objectid + ins.offset;
1214 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001215 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001216
1217 /*
1218 * btrfs_reloc_clone_csums() error, since start is increased
1219 * extent_clear_unlock_delalloc() at out_unlock label won't
1220 * free metadata of current ordered extent, we're OK to exit.
1221 */
1222 if (ret)
1223 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001224 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001226 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001227
Filipe Mananad9f85962014-08-25 10:43:00 +01001228out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001229 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001230out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001232 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001234 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1235 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001236 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001237 /*
1238 * If we reserved an extent for our delalloc range (or a subrange) and
1239 * failed to create the respective ordered extent, then it means that
1240 * when we reserved the extent we decremented the extent's size from
1241 * the data space_info's bytes_may_use counter and incremented the
1242 * space_info's bytes_reserved counter by the same amount. We must make
1243 * sure extent_clear_unlock_delalloc() does not try to decrement again
1244 * the data space_info's bytes_may_use counter, therefore we do not pass
1245 * it the flag EXTENT_CLEAR_DATA_RESV.
1246 */
1247 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001248 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001249 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001250 locked_page,
1251 clear_bits,
1252 page_ops);
1253 start += cur_alloc_size;
1254 if (start >= end)
1255 goto out;
1256 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001257 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001258 clear_bits | EXTENT_CLEAR_DATA_RESV,
1259 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001261}
Chris Masonc8b97812008-10-29 14:49:59 -04001262
Chris Mason771ed682008-11-06 22:02:51 -05001263/*
1264 * work queue call back to started compression on a file and pages
1265 */
1266static noinline void async_cow_start(struct btrfs_work *work)
1267{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001268 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001269 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001270
Nikolay Borisovb5326272019-03-12 17:20:25 +02001271 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001272
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001273 compressed_extents = compress_file_range(async_chunk);
1274 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001275 btrfs_add_delayed_iput(async_chunk->inode);
1276 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001277 }
Chris Mason771ed682008-11-06 22:02:51 -05001278}
1279
1280/*
1281 * work queue call back to submit previously compressed pages
1282 */
1283static noinline void async_cow_submit(struct btrfs_work *work)
1284{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001285 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1286 work);
1287 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001288 unsigned long nr_pages;
1289
Nikolay Borisovb5326272019-03-12 17:20:25 +02001290 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001292
David Sterba093258e2018-02-26 16:15:17 +01001293 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001294 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001295 5 * SZ_1M)
1296 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001297
Nikolay Borisov4546d172019-01-03 10:50:03 +02001298 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001299 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001300 * in which case we don't have anything to submit, yet we need to
1301 * always adjust ->async_delalloc_pages as its paired with the init
1302 * happening in cow_file_range_async
1303 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001304 if (async_chunk->inode)
1305 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001306}
Chris Masonc8b97812008-10-29 14:49:59 -04001307
Chris Mason771ed682008-11-06 22:02:51 -05001308static noinline void async_cow_free(struct btrfs_work *work)
1309{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001310 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001311
Nikolay Borisovb5326272019-03-12 17:20:25 +02001312 async_chunk = container_of(work, struct async_chunk, work);
1313 if (async_chunk->inode)
1314 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001315 if (async_chunk->blkcg_css)
1316 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001317 /*
1318 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001319 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001320 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001321 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001322 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001323}
1324
Nikolay Borisov751b6432020-06-03 08:55:22 +03001325static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001326 struct writeback_control *wbc,
1327 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001328 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001329 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001330{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001331 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001332 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001333 struct async_cow *ctx;
1334 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001335 unsigned long nr_pages;
1336 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001337 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1338 int i;
1339 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001340 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001341 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001342
Nikolay Borisov751b6432020-06-03 08:55:22 +03001343 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001344
Nikolay Borisov751b6432020-06-03 08:55:22 +03001345 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001346 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1347 num_chunks = 1;
1348 should_compress = false;
1349 } else {
1350 should_compress = true;
1351 }
1352
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001353 nofs_flag = memalloc_nofs_save();
1354 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1355 memalloc_nofs_restore(nofs_flag);
1356
Nikolay Borisov97db1202019-03-12 17:20:24 +02001357 if (!ctx) {
1358 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1359 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1360 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001361 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1362 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001363
Nikolay Borisov751b6432020-06-03 08:55:22 +03001364 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1365 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001366 return -ENOMEM;
1367 }
1368
1369 async_chunk = ctx->chunks;
1370 atomic_set(&ctx->num_chunks, num_chunks);
1371
1372 for (i = 0; i < num_chunks; i++) {
1373 if (should_compress)
1374 cur_end = min(end, start + SZ_512K - 1);
1375 else
1376 cur_end = end;
1377
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001378 /*
1379 * igrab is called higher up in the call chain, take only the
1380 * lightweight reference for the callback lifetime
1381 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001382 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001383 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001384 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001385 async_chunk[i].start = start;
1386 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001387 async_chunk[i].write_flags = write_flags;
1388 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001389
Chris Mason1d53c9e2019-07-10 12:28:16 -07001390 /*
1391 * The locked_page comes all the way from writepage and its
1392 * the original page we were actually given. As we spread
1393 * this large delalloc region across multiple async_chunk
1394 * structs, only the first struct needs a pointer to locked_page
1395 *
1396 * This way we don't need racey decisions about who is supposed
1397 * to unlock it.
1398 */
1399 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001400 /*
1401 * Depending on the compressibility, the pages might or
1402 * might not go through async. We want all of them to
1403 * be accounted against wbc once. Let's do it here
1404 * before the paths diverge. wbc accounting is used
1405 * only for foreign writeback detection and doesn't
1406 * need full accuracy. Just account the whole thing
1407 * against the first page.
1408 */
1409 wbc_account_cgroup_owner(wbc, locked_page,
1410 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001411 async_chunk[i].locked_page = locked_page;
1412 locked_page = NULL;
1413 } else {
1414 async_chunk[i].locked_page = NULL;
1415 }
1416
Chris Masonec39f762019-07-10 12:28:17 -07001417 if (blkcg_css != blkcg_root_css) {
1418 css_get(blkcg_css);
1419 async_chunk[i].blkcg_css = blkcg_css;
1420 } else {
1421 async_chunk[i].blkcg_css = NULL;
1422 }
1423
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001424 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1425 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001426
Nikolay Borisov97db1202019-03-12 17:20:24 +02001427 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001429
Nikolay Borisov97db1202019-03-12 17:20:24 +02001430 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001431
Chris Mason771ed682008-11-06 22:02:51 -05001432 *nr_written += nr_pages;
1433 start = cur_end + 1;
1434 }
1435 *page_started = 1;
1436 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001437}
1438
Naohiro Aota42c01102021-02-04 19:22:07 +09001439static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1440 struct page *locked_page, u64 start,
1441 u64 end, int *page_started,
1442 unsigned long *nr_written)
1443{
1444 int ret;
1445
1446 ret = cow_file_range(inode, locked_page, start, end, page_started,
1447 nr_written, 0);
1448 if (ret)
1449 return ret;
1450
1451 if (*page_started)
1452 return 0;
1453
1454 __set_page_dirty_nobuffers(locked_page);
1455 account_page_redirty(locked_page);
1456 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1457 *page_started = 1;
1458
1459 return 0;
1460}
1461
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001462static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001463 u64 bytenr, u64 num_bytes)
1464{
1465 int ret;
1466 struct btrfs_ordered_sum *sums;
1467 LIST_HEAD(list);
1468
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001469 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001470 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001471 if (ret == 0 && list_empty(&list))
1472 return 0;
1473
1474 while (!list_empty(&list)) {
1475 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1476 list_del(&sums->list);
1477 kfree(sums);
1478 }
Liu Bo58113752018-01-31 17:09:13 -07001479 if (ret < 0)
1480 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001481 return 1;
1482}
1483
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001484static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001485 const u64 start, const u64 end,
1486 int *page_started, unsigned long *nr_written)
1487{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001488 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1489 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001490 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001491 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001492 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001493 u64 range_start = start;
1494 u64 count;
1495
1496 /*
1497 * If EXTENT_NORESERVE is set it means that when the buffered write was
1498 * made we had not enough available data space and therefore we did not
1499 * reserve data space for it, since we though we could do NOCOW for the
1500 * respective file range (either there is prealloc extent or the inode
1501 * has the NOCOW bit set).
1502 *
1503 * However when we need to fallback to COW mode (because for example the
1504 * block group for the corresponding extent was turned to RO mode by a
1505 * scrub or relocation) we need to do the following:
1506 *
1507 * 1) We increment the bytes_may_use counter of the data space info.
1508 * If COW succeeds, it allocates a new data extent and after doing
1509 * that it decrements the space info's bytes_may_use counter and
1510 * increments its bytes_reserved counter by the same amount (we do
1511 * this at btrfs_add_reserved_bytes()). So we need to increment the
1512 * bytes_may_use counter to compensate (when space is reserved at
1513 * buffered write time, the bytes_may_use counter is incremented);
1514 *
1515 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1516 * that if the COW path fails for any reason, it decrements (through
1517 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1518 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001519 *
1520 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001521 * space cache inode or an inode of the data relocation tree, we must
1522 * also increment bytes_may_use of the data space_info for the same
1523 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001524 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001525 * group that contains that extent to RO mode and therefore force COW
1526 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001527 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001528 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001529 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001530 if (count > 0 || is_space_ino || is_reloc_ino) {
1531 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001532 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001533 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1534
Filipe Manana6bd335b2020-06-08 13:33:05 +01001535 if (is_space_ino || is_reloc_ino)
1536 bytes = range_bytes;
1537
Filipe Manana467dc472020-05-27 11:16:07 +01001538 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001539 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001540 spin_unlock(&sinfo->lock);
1541
Filipe Manana2166e5e2020-05-27 11:16:19 +01001542 if (count > 0)
1543 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1544 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001545 }
1546
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001547 return cow_file_range(inode, locked_page, start, end, page_started,
1548 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001549}
1550
Chris Masond352ac62008-09-29 15:18:18 -04001551/*
1552 * when nowcow writeback call back. This checks for snapshots or COW copies
1553 * of the extents that exist in the file, and COWs the file as required.
1554 *
1555 * If no cow copies or snapshots exist, we write directly to the existing
1556 * blocks on disk
1557 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001558static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001559 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001560 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001561 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001562 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001563{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001564 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1565 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001566 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001567 u64 cow_start = (u64)-1;
1568 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001569 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001570 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001571 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1572 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001573 bool nocow = false;
1574 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001575 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001576
1577 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001578 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001579 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001580 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001581 EXTENT_DO_ACCOUNTING |
1582 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001583 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001584 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001585 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001586 }
Li Zefan82d59022011-04-20 10:33:24 +08001587
Yan Zheng80ff3852008-10-30 14:20:02 -04001588 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001589 struct btrfs_key found_key;
1590 struct btrfs_file_extent_item *fi;
1591 struct extent_buffer *leaf;
1592 u64 extent_end;
1593 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001594 u64 num_bytes = 0;
1595 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001596 u64 ram_bytes;
1597 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001598
1599 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001600
Liu Boe4c3b2d2017-01-30 12:25:28 -08001601 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001602 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001603 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001604 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001605
1606 /*
1607 * If there is no extent for our range when doing the initial
1608 * search, then go back to the previous slot as it will be the
1609 * one containing the search offset
1610 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1612 leaf = path->nodes[0];
1613 btrfs_item_key_to_cpu(leaf, &found_key,
1614 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001615 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001616 found_key.type == BTRFS_EXTENT_DATA_KEY)
1617 path->slots[0]--;
1618 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001619 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001620next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001621 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001622 leaf = path->nodes[0];
1623 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1624 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001625 if (ret < 0) {
1626 if (cow_start != (u64)-1)
1627 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001628 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001629 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001630 if (ret > 0)
1631 break;
1632 leaf = path->nodes[0];
1633 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001634
Yan Zheng80ff3852008-10-30 14:20:02 -04001635 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001636
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001637 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001638 if (found_key.objectid > ino)
1639 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001640 /*
1641 * Keep searching until we find an EXTENT_ITEM or there are no
1642 * more extents for this inode
1643 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001644 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1645 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1646 path->slots[0]++;
1647 goto next_slot;
1648 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001649
1650 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001651 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 found_key.offset > end)
1653 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001654
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001655 /*
1656 * If the found extent starts after requested offset, then
1657 * adjust extent_end to be right before this extent begins
1658 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001659 if (found_key.offset > cur_offset) {
1660 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001661 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001662 goto out_check;
1663 }
Chris Masonc31f8832008-01-08 15:46:31 -05001664
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001665 /*
1666 * Found extent which begins before our range and potentially
1667 * intersect it
1668 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 fi = btrfs_item_ptr(leaf, path->slots[0],
1670 struct btrfs_file_extent_item);
1671 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001672
Josef Bacikcc95bef2013-04-04 14:31:27 -04001673 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001674 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1675 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 extent_end = found_key.offset +
1679 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001680 disk_num_bytes =
1681 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001682 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001683 * If the extent we got ends before our current offset,
1684 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001685 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001686 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001687 path->slots[0]++;
1688 goto next_slot;
1689 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001690 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001691 if (disk_bytenr == 0)
1692 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001693 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001694 if (btrfs_file_extent_compression(leaf, fi) ||
1695 btrfs_file_extent_encryption(leaf, fi) ||
1696 btrfs_file_extent_other_encoding(leaf, fi))
1697 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001698 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001699 * If extent is created before the last volume's snapshot
1700 * this implies the extent is shared, hence we can't do
1701 * nocow. This is the same check as in
1702 * btrfs_cross_ref_exist but without calling
1703 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001704 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001705 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001706 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001707 btrfs_root_last_snapshot(&root->root_item))
1708 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001709 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1710 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001711
1712 /*
1713 * The following checks can be expensive, as they need to
1714 * take other locks and do btree or rbtree searches, so
1715 * release the path to avoid blocking other tasks for too
1716 * long.
1717 */
1718 btrfs_release_path(path);
1719
Liu Bo58113752018-01-31 17:09:13 -07001720 ret = btrfs_cross_ref_exist(root, ino,
1721 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001722 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001723 if (ret) {
1724 /*
1725 * ret could be -EIO if the above fails to read
1726 * metadata.
1727 */
1728 if (ret < 0) {
1729 if (cow_start != (u64)-1)
1730 cur_offset = cow_start;
1731 goto error;
1732 }
1733
Nikolay Borisov3e024842019-08-21 10:42:03 +03001734 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001735 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001736 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001738 disk_bytenr += cur_offset - found_key.offset;
1739 num_bytes = min(end + 1, extent_end) - cur_offset;
1740 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001741 * If there are pending snapshots for this root, we
1742 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001743 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001744 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001745 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001746 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001747 * force cow if csum exists in the range.
1748 * this ensure that csum for a given extent are
1749 * either valid or do not exist.
1750 */
Liu Bo58113752018-01-31 17:09:13 -07001751 ret = csum_exist_in_range(fs_info, disk_bytenr,
1752 num_bytes);
1753 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001754 /*
1755 * ret could be -EIO if the above fails to read
1756 * metadata.
1757 */
1758 if (ret < 0) {
1759 if (cow_start != (u64)-1)
1760 cur_offset = cow_start;
1761 goto error;
1762 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001763 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001764 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001765 }
Filipe Manana20903032021-02-05 12:55:36 +00001766 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001767 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001768 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001769 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001770 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001771 extent_end = found_key.offset + ram_bytes;
1772 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001773 /* Skip extents outside of our requested range */
1774 if (extent_end <= start) {
1775 path->slots[0]++;
1776 goto next_slot;
1777 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001778 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001779 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001780 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001781 }
1782out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001783 /*
1784 * If nocow is false then record the beginning of the range
1785 * that needs to be COWed
1786 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001787 if (!nocow) {
1788 if (cow_start == (u64)-1)
1789 cow_start = cur_offset;
1790 cur_offset = extent_end;
1791 if (cur_offset > end)
1792 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001793 if (!path->nodes[0])
1794 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001795 path->slots[0]++;
1796 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001797 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001798
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001799 /*
1800 * COW range from cow_start to found_key.offset - 1. As the key
1801 * will contain the beginning of the first extent that can be
1802 * NOCOW, following one which needs to be COW'ed
1803 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001804 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001805 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001806 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001807 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001808 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001809 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001810 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001811 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001812
Yan Zhengd899e052008-10-30 14:25:28 -04001813 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001814 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001815 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001816
Nikolay Borisov968322c2020-06-03 08:55:21 +03001817 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001818 orig_start,
1819 disk_bytenr, /* block_start */
1820 num_bytes, /* block_len */
1821 disk_num_bytes, /* orig_block_len */
1822 ram_bytes, BTRFS_COMPRESS_NONE,
1823 BTRFS_ORDERED_PREALLOC);
1824 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001825 ret = PTR_ERR(em);
1826 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001827 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001828 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001829 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001830 disk_bytenr, num_bytes,
1831 num_bytes,
1832 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001833 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001834 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001835 cur_offset + num_bytes - 1,
1836 0);
1837 goto error;
1838 }
Yan Zhengd899e052008-10-30 14:25:28 -04001839 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001840 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001841 disk_bytenr, num_bytes,
1842 num_bytes,
1843 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001844 if (ret)
1845 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001846 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001847
Filipe Mananaf78c4362016-05-09 13:15:41 +01001848 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001850 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001851
Yan, Zhengefa56462010-05-16 10:49:59 -04001852 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001853 BTRFS_DATA_RELOC_TREE_OBJECTID)
1854 /*
1855 * Error handled later, as we must prevent
1856 * extent_clear_unlock_delalloc() in error handler
1857 * from freeing metadata of created ordered extent.
1858 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001859 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001860 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001861
Nikolay Borisov968322c2020-06-03 08:55:21 +03001862 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001863 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001864 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001865 EXTENT_DELALLOC |
1866 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001867 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001868
Yan Zheng80ff3852008-10-30 14:20:02 -04001869 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001870
1871 /*
1872 * btrfs_reloc_clone_csums() error, now we're OK to call error
1873 * handler, as metadata for created ordered extent will only
1874 * be freed by btrfs_finish_ordered_io().
1875 */
1876 if (ret)
1877 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001878 if (cur_offset > end)
1879 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001880 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001881 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001882
Robbie Ko506481b2018-10-30 18:04:04 +08001883 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001884 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001885
Yan Zheng80ff3852008-10-30 14:20:02 -04001886 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001887 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001888 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1889 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001890 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001891 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001892 }
1893
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001895 if (nocow)
1896 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1897
Josef Bacik17ca04a2012-05-31 15:58:55 -04001898 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001899 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001900 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001901 EXTENT_DELALLOC | EXTENT_DEFRAG |
1902 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001903 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001904 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001905 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001906 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001907}
1908
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001909static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001910{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001911 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1912 if (inode->defrag_bytes &&
1913 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1914 0, NULL))
1915 return false;
1916 return true;
1917 }
1918 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001919}
1920
Chris Masond352ac62008-09-29 15:18:18 -04001921/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001922 * Function to process delayed allocation (create CoW) for ranges which are
1923 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001924 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001925int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001926 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1927 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001928{
Chris Masonbe20aa92007-12-17 20:14:01 -05001929 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001930 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001931
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001932 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001933 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001934 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001935 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001936 } else if (!inode_can_compress(inode) ||
1937 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001938 if (zoned)
1939 ret = run_delalloc_zoned(inode, locked_page, start, end,
1940 page_started, nr_written);
1941 else
1942 ret = cow_file_range(inode, locked_page, start, end,
1943 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001944 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001945 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1946 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001947 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001948 }
Qu Wenruo524272602017-03-08 10:25:52 +08001949 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001950 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001951 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001952 return ret;
1953}
1954
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001955void btrfs_split_delalloc_extent(struct inode *inode,
1956 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001957{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001958 u64 size;
1959
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001960 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001961 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001962 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001963
Josef Bacikdcab6a32015-02-11 15:08:59 -05001964 size = orig->end - orig->start + 1;
1965 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001966 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001967 u64 new_size;
1968
1969 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001970 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001971 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001972 */
1973 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001974 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001975 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001976 num_extents += count_max_extents(new_size);
1977 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001978 return;
1979 }
1980
Josef Bacik9e0baf62011-07-15 15:16:44 +00001981 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001982 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001983 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001984}
1985
1986/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001987 * Handle merged delayed allocation extents so we can keep track of new extents
1988 * that are just merged onto old extents, such as when we are doing sequential
1989 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001990 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001991void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1992 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001993{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001994 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001995 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001996
Josef Bacik9ed74f22009-09-11 16:12:44 -04001997 /* not delalloc, ignore it */
1998 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001999 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002000
Josef Bacik8461a3d2015-03-13 15:12:08 -04002001 if (new->start > other->start)
2002 new_size = new->end - other->start + 1;
2003 else
2004 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002005
2006 /* we're not bigger than the max, unreserve the space and go */
2007 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2008 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002009 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002010 spin_unlock(&BTRFS_I(inode)->lock);
2011 return;
2012 }
2013
2014 /*
Josef Bacikba117212015-03-13 15:01:24 -04002015 * We have to add up either side to figure out how many extents were
2016 * accounted for before we merged into one big extent. If the number of
2017 * extents we accounted for is <= the amount we need for the new range
2018 * then we can return, otherwise drop. Think of it like this
2019 *
2020 * [ 4k][MAX_SIZE]
2021 *
2022 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2023 * need 2 outstanding extents, on one side we have 1 and the other side
2024 * we have 1 so they are == and we can return. But in this case
2025 *
2026 * [MAX_SIZE+4k][MAX_SIZE+4k]
2027 *
2028 * Each range on their own accounts for 2 extents, but merged together
2029 * they are only 3 extents worth of accounting, so we need to drop in
2030 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002031 */
Josef Bacikba117212015-03-13 15:01:24 -04002032 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002033 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002034 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002035 num_extents += count_max_extents(old_size);
2036 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002037 return;
2038
Josef Bacik9e0baf62011-07-15 15:16:44 +00002039 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002040 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002041 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002042}
2043
Miao Xieeb73c1b2013-05-15 07:48:22 +00002044static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2045 struct inode *inode)
2046{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002047 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2048
Miao Xieeb73c1b2013-05-15 07:48:22 +00002049 spin_lock(&root->delalloc_lock);
2050 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2051 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2052 &root->delalloc_inodes);
2053 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2054 &BTRFS_I(inode)->runtime_flags);
2055 root->nr_delalloc_inodes++;
2056 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002057 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002058 BUG_ON(!list_empty(&root->delalloc_root));
2059 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002060 &fs_info->delalloc_roots);
2061 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002062 }
2063 }
2064 spin_unlock(&root->delalloc_lock);
2065}
2066
Nikolay Borisov2b877332018-04-27 12:21:51 +03002067
2068void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2069 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002070{
David Sterba3ffbd682018-06-29 10:56:42 +02002071 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002073 if (!list_empty(&inode->delalloc_inodes)) {
2074 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002075 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002076 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002077 root->nr_delalloc_inodes--;
2078 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002079 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002080 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002081 BUG_ON(list_empty(&root->delalloc_root));
2082 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002083 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002084 }
2085 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002086}
2087
2088static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2089 struct btrfs_inode *inode)
2090{
2091 spin_lock(&root->delalloc_lock);
2092 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002093 spin_unlock(&root->delalloc_lock);
2094}
2095
Chris Masond352ac62008-09-29 15:18:18 -04002096/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002097 * Properly track delayed allocation bytes in the inode and to maintain the
2098 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002099 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002100void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2101 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002102{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2104
Wang Shilong47059d92014-07-03 18:22:07 +08002105 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2106 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002107 /*
2108 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002109 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002110 * bit, which is only set or cleared with irqs on
2111 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002112 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002113 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002114 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002115 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002116 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002117
Josef Bacik8b62f872017-10-19 14:15:55 -04002118 spin_lock(&BTRFS_I(inode)->lock);
2119 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2120 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002121
Josef Bacik6a3891c2015-03-16 17:38:52 -04002122 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002123 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002124 return;
2125
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002126 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2127 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002128 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002129 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002130 if (*bits & EXTENT_DEFRAG)
2131 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002132 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002133 &BTRFS_I(inode)->runtime_flags))
2134 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002135 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002136 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002137
2138 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2139 (*bits & EXTENT_DELALLOC_NEW)) {
2140 spin_lock(&BTRFS_I(inode)->lock);
2141 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2142 state->start;
2143 spin_unlock(&BTRFS_I(inode)->lock);
2144 }
Chris Mason291d6732008-01-29 15:55:23 -05002145}
2146
Chris Masond352ac62008-09-29 15:18:18 -04002147/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002148 * Once a range is no longer delalloc this function ensures that proper
2149 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002150 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002151void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2152 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002153{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002154 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2155 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002156 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002157 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002158
Filipe Manana4a4b9642017-07-27 19:52:55 +01002159 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2160 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002161 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002162 spin_unlock(&inode->lock);
2163 }
Wang Shilong47059d92014-07-03 18:22:07 +08002164
Chris Mason75eff682008-12-15 15:54:40 -05002165 /*
2166 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002167 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002168 * bit, which is only set or cleared with irqs on
2169 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002170 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002171 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002172 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002173
Josef Bacik8b62f872017-10-19 14:15:55 -04002174 spin_lock(&inode->lock);
2175 btrfs_mod_outstanding_extents(inode, -num_extents);
2176 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002177
Josef Bacikb6d08f02013-09-27 14:57:43 -04002178 /*
2179 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002180 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002181 * error.
2182 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002183 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002184 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002185 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002186
Josef Bacik6a3891c2015-03-16 17:38:52 -04002187 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002188 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002189 return;
2190
Filipe Mananaa315e682017-03-06 23:04:20 +00002191 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2192 do_list && !(state->state & EXTENT_NORESERVE) &&
2193 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002194 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002195
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002196 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2197 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002198 spin_lock(&inode->lock);
2199 inode->delalloc_bytes -= len;
2200 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002201 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002202 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002203 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002204 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002205 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002206
2207 if ((state->state & EXTENT_DELALLOC_NEW) &&
2208 (*bits & EXTENT_DELALLOC_NEW)) {
2209 spin_lock(&inode->lock);
2210 ASSERT(inode->new_delalloc_bytes >= len);
2211 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002212 if (*bits & EXTENT_ADD_INODE_BYTES)
2213 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002214 spin_unlock(&inode->lock);
2215 }
Chris Mason291d6732008-01-29 15:55:23 -05002216}
2217
Chris Masond352ac62008-09-29 15:18:18 -04002218/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002219 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2220 * in a chunk's stripe. This function ensures that bios do not span a
2221 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002222 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002223 * @page - The page we are about to add to the bio
2224 * @size - size we want to add to the bio
2225 * @bio - bio we want to ensure is smaller than a stripe
2226 * @bio_flags - flags of the bio
2227 *
2228 * return 1 if page cannot be added to the bio
2229 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002230 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002231 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002232int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2233 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002235 struct inode *inode = page->mapping->host;
2236 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002237 u64 logical = bio->bi_iter.bi_sector << 9;
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002238 u32 bio_len = bio->bi_iter.bi_size;
Michal Rostecki42034312021-01-27 14:57:27 +01002239 struct extent_map *em;
Michal Rostecki42034312021-01-27 14:57:27 +01002240 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002241 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002242
Chris Mason771ed682008-11-06 22:02:51 -05002243 if (bio_flags & EXTENT_BIO_COMPRESSED)
2244 return 0;
2245
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002246 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
Michal Rostecki42034312021-01-27 14:57:27 +01002247 if (IS_ERR(em))
2248 return PTR_ERR(em);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002249 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002250 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002251 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002252
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002253 if (geom.len < bio_len + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002254 ret = 1;
2255out:
2256 free_extent_map(em);
2257 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002258}
2259
Chris Masond352ac62008-09-29 15:18:18 -04002260/*
2261 * in order to insert checksums into the metadata in large chunks,
2262 * we wait until bio submission time. All the pages in the bio are
2263 * checksummed and sums are attached onto the ordered extent record.
2264 *
2265 * At IO completion time the cums attached on the ordered extent record
2266 * are inserted into the btree
2267 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002268static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002269 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002270{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002271 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002272}
Chris Masone0156402008-04-16 11:15:20 -04002273
Naohiro Aotad22002f2021-02-04 19:22:00 +09002274static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2275 struct bio *bio, loff_t file_offset)
2276{
2277 struct btrfs_ordered_extent *ordered;
2278 struct extent_map *em = NULL, *em_new = NULL;
2279 struct extent_map_tree *em_tree = &inode->extent_tree;
2280 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2281 u64 len = bio->bi_iter.bi_size;
2282 u64 end = start + len;
2283 u64 ordered_end;
2284 u64 pre, post;
2285 int ret = 0;
2286
2287 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2288 if (WARN_ON_ONCE(!ordered))
2289 return BLK_STS_IOERR;
2290
2291 /* No need to split */
2292 if (ordered->disk_num_bytes == len)
2293 goto out;
2294
2295 /* We cannot split once end_bio'd ordered extent */
2296 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2297 ret = -EINVAL;
2298 goto out;
2299 }
2300
2301 /* We cannot split a compressed ordered extent */
2302 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2303 ret = -EINVAL;
2304 goto out;
2305 }
2306
2307 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2308 /* bio must be in one ordered extent */
2309 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2310 ret = -EINVAL;
2311 goto out;
2312 }
2313
2314 /* Checksum list should be empty */
2315 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2316 ret = -EINVAL;
2317 goto out;
2318 }
2319
2320 pre = start - ordered->disk_bytenr;
2321 post = ordered_end - end;
2322
2323 ret = btrfs_split_ordered_extent(ordered, pre, post);
2324 if (ret)
2325 goto out;
2326
2327 read_lock(&em_tree->lock);
2328 em = lookup_extent_mapping(em_tree, ordered->file_offset, len);
2329 if (!em) {
2330 read_unlock(&em_tree->lock);
2331 ret = -EIO;
2332 goto out;
2333 }
2334 read_unlock(&em_tree->lock);
2335
2336 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2337 /*
2338 * We cannot reuse em_new here but have to create a new one, as
2339 * unpin_extent_cache() expects the start of the extent map to be the
2340 * logical offset of the file, which does not hold true anymore after
2341 * splitting.
2342 */
2343 em_new = create_io_em(inode, em->start + pre, len,
2344 em->start + pre, em->block_start + pre, len,
2345 len, len, BTRFS_COMPRESS_NONE,
2346 BTRFS_ORDERED_REGULAR);
2347 if (IS_ERR(em_new)) {
2348 ret = PTR_ERR(em_new);
2349 goto out;
2350 }
2351 free_extent_map(em_new);
2352
2353out:
2354 free_extent_map(em);
2355 btrfs_put_ordered_extent(ordered);
2356
2357 return errno_to_blk_status(ret);
2358}
2359
Chris Mason4a69a412008-11-06 22:03:00 -05002360/*
Chris Masoncad321a2008-12-17 14:51:42 -05002361 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002362 * on write, or reading the csums from the tree before a read.
2363 *
2364 * Rules about async/sync submit,
2365 * a) read: sync submit
2366 *
2367 * b) write without checksum: sync submit
2368 *
2369 * c) write with checksum:
2370 * c-1) if bio is issued by fsync: sync submit
2371 * (sync_writers != 0)
2372 *
2373 * c-2) if root is reloc root: sync submit
2374 * (only in case of buffered IO)
2375 *
2376 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002377 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002378blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2379 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002380
Chris Mason44b8bd72008-04-16 11:14:51 -04002381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002383 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302384 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002385 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002386 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002387 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002388
Josef Bacik42437a62020-10-16 11:29:18 -04002389 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2390 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002391
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002392 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302393 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002394
Naohiro Aotad22002f2021-02-04 19:22:00 +09002395 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2396 struct page *page = bio_first_bvec_all(bio)->bv_page;
2397 loff_t file_offset = page_offset(page);
2398
2399 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2400 if (ret)
2401 goto out;
2402 }
2403
Naohiro Aotacfe94442021-02-04 19:21:59 +09002404 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002405 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002406 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002407 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002408
Chris Masond20f7042008-12-08 16:58:54 -05002409 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002410 ret = btrfs_submit_compressed_read(inode, bio,
2411 mirror_num,
2412 bio_flags);
2413 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002414 } else {
2415 /*
2416 * Lookup bio sums does extra checks around whether we
2417 * need to csum or not, which is why we ignore skip_sum
2418 * here.
2419 */
Qu Wenruo62751932020-12-02 14:48:06 +08002420 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002421 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002422 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002423 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002424 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002425 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002426 /* csum items have already been cloned */
2427 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2428 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002429 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002430 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2431 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002432 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002433 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002434 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002435 if (ret)
2436 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002437 }
2438
Chris Mason0b86a832008-03-24 15:01:56 -04002439mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002440 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002441
2442out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002443 if (ret) {
2444 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002445 bio_endio(bio);
2446 }
Stefan Behrens61891922012-11-05 18:51:52 +01002447 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002448}
Chris Mason6885f302008-02-20 16:11:05 -05002449
Chris Masond352ac62008-09-29 15:18:18 -04002450/*
2451 * given a list of ordered sums record them in the inode. This happens
2452 * at IO completion time based on sums calculated at bio submission time.
2453 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002454static int add_pending_csums(struct btrfs_trans_handle *trans,
2455 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002456{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002457 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002458 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002459
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002460 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002461 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002462 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002463 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002464 if (ret)
2465 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002466 }
2467 return 0;
2468}
2469
Filipe Mananac3347302020-11-04 11:07:31 +00002470static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2471 const u64 start,
2472 const u64 len,
2473 struct extent_state **cached_state)
2474{
2475 u64 search_start = start;
2476 const u64 end = start + len - 1;
2477
2478 while (search_start < end) {
2479 const u64 search_len = end - search_start + 1;
2480 struct extent_map *em;
2481 u64 em_len;
2482 int ret = 0;
2483
2484 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2485 if (IS_ERR(em))
2486 return PTR_ERR(em);
2487
2488 if (em->block_start != EXTENT_MAP_HOLE)
2489 goto next;
2490
2491 em_len = em->len;
2492 if (em->start < search_start)
2493 em_len -= search_start - em->start;
2494 if (em_len > search_len)
2495 em_len = search_len;
2496
2497 ret = set_extent_bit(&inode->io_tree, search_start,
2498 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002499 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2500 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002501next:
2502 search_start = extent_map_end(em);
2503 free_extent_map(em);
2504 if (ret)
2505 return ret;
2506 }
2507 return 0;
2508}
2509
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002510int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002511 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002512 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002513{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002514 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002515
2516 if (start >= i_size_read(&inode->vfs_inode) &&
2517 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2518 /*
2519 * There can't be any extents following eof in this case so just
2520 * set the delalloc new bit for the range directly.
2521 */
2522 extra_bits |= EXTENT_DELALLOC_NEW;
2523 } else {
2524 int ret;
2525
2526 ret = btrfs_find_new_delalloc_bytes(inode, start,
2527 end + 1 - start,
2528 cached_state);
2529 if (ret)
2530 return ret;
2531 }
2532
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002533 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2534 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002535}
2536
Chris Masond352ac62008-09-29 15:18:18 -04002537/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002538struct btrfs_writepage_fixup {
2539 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002540 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002541 struct btrfs_work work;
2542};
2543
Christoph Hellwigb2950862008-12-02 09:54:17 -05002544static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002545{
2546 struct btrfs_writepage_fixup *fixup;
2547 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002548 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002549 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002550 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002551 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002552 u64 page_start;
2553 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002554 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002555 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002556
2557 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2558 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002559 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002560 page_start = page_offset(page);
2561 page_end = page_offset(page) + PAGE_SIZE - 1;
2562
2563 /*
2564 * This is similar to page_mkwrite, we need to reserve the space before
2565 * we take the page lock.
2566 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002567 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2568 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002569again:
Chris Mason247e7432008-07-17 12:53:51 -04002570 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002571
Chris Mason25f3c502020-01-21 11:51:42 -05002572 /*
2573 * Before we queued this fixup, we took a reference on the page.
2574 * page->mapping may go NULL, but it shouldn't be moved to a different
2575 * address space.
2576 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002577 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2578 /*
2579 * Unfortunately this is a little tricky, either
2580 *
2581 * 1) We got here and our page had already been dealt with and
2582 * we reserved our space, thus ret == 0, so we need to just
2583 * drop our space reservation and bail. This can happen the
2584 * first time we come into the fixup worker, or could happen
2585 * while waiting for the ordered extent.
2586 * 2) Our page was already dealt with, but we happened to get an
2587 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2588 * this case we obviously don't have anything to release, but
2589 * because the page was already dealt with we don't want to
2590 * mark the page with an error, so make sure we're resetting
2591 * ret to 0. This is why we have this check _before_ the ret
2592 * check, because we do not want to have a surprise ENOSPC
2593 * when the page was already properly dealt with.
2594 */
2595 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002596 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2597 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002598 page_start, PAGE_SIZE,
2599 true);
2600 }
2601 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002602 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002603 }
Chris Mason25f3c502020-01-21 11:51:42 -05002604
2605 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002606 * We can't mess with the page state unless it is locked, so now that
2607 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002608 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002609 if (ret)
2610 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002611
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002612 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002613
2614 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002615 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002616 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002617
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002618 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002619 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002620 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2621 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002622 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002623 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002624 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002625 goto again;
2626 }
Chris Mason247e7432008-07-17 12:53:51 -04002627
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002628 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002629 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002630 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002631 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002632
Chris Mason25f3c502020-01-21 11:51:42 -05002633 /*
2634 * Everything went as planned, we're now the owner of a dirty page with
2635 * delayed allocation bits set and space reserved for our COW
2636 * destination.
2637 *
2638 * The page was dirty when we started, nothing should have cleaned it.
2639 */
2640 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002641 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002642out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002643 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002644 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002645 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2646 PAGE_SIZE, true);
2647 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002648 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002649out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002650 if (ret) {
2651 /*
2652 * We hit ENOSPC or other errors. Update the mapping and page
2653 * to reflect the errors and clean the page.
2654 */
2655 mapping_set_error(page->mapping, ret);
2656 end_extent_writepage(page, ret, page_start, page_end);
2657 clear_page_dirty_for_io(page);
2658 SetPageError(page);
2659 }
2660 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002661 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002662 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002663 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002664 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002665 /*
2666 * As a precaution, do a delayed iput in case it would be the last iput
2667 * that could need flushing space. Recursing back to fixup worker would
2668 * deadlock.
2669 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002670 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002671}
2672
2673/*
2674 * There are a few paths in the higher layers of the kernel that directly
2675 * set the page dirty bit without asking the filesystem if it is a
2676 * good idea. This causes problems because we want to make sure COW
2677 * properly happens and the data=ordered rules are followed.
2678 *
Chris Masonc8b97812008-10-29 14:49:59 -04002679 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002680 * hasn't been properly setup for IO. We kick off an async process
2681 * to fix it up. The async helper will wait for ordered extents, set
2682 * the delalloc bit and make it safe to write the page.
2683 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002684int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002685{
2686 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002688 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002689
Qu Wenruof57ad932021-04-07 19:22:13 +08002690 /* This page has ordered extent covering it already */
2691 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002692 return 0;
2693
Chris Mason25f3c502020-01-21 11:51:42 -05002694 /*
2695 * PageChecked is set below when we create a fixup worker for this page,
2696 * don't try to create another one if we're already PageChecked()
2697 *
2698 * The extent_io writepage code will redirty the page if we send back
2699 * EAGAIN.
2700 */
Chris Mason247e7432008-07-17 12:53:51 -04002701 if (PageChecked(page))
2702 return -EAGAIN;
2703
2704 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2705 if (!fixup)
2706 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002707
Josef Bacikf4b13632020-01-21 14:34:52 -05002708 /*
2709 * We are already holding a reference to this inode from
2710 * write_cache_pages. We need to hold it because the space reservation
2711 * takes place outside of the page lock, and we can't trust
2712 * page->mapping outside of the page lock.
2713 */
2714 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002715 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002716 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002717 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002718 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002719 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002721
2722 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002723}
2724
Yan Zhengd899e052008-10-30 14:25:28 -04002725static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002726 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002727 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002728 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002729 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002730{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002731 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002732 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002733 struct btrfs_path *path;
2734 struct extent_buffer *leaf;
2735 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002736 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2737 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2738 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2739 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002740 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002741 int ret;
2742
2743 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002744 if (!path)
2745 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002746
Chris Masona1ed8352009-09-11 12:27:37 -04002747 /*
2748 * we may be replacing one extent in the tree with another.
2749 * The new extent is pinned in the extent map, and we don't want
2750 * to drop it from the cache until it is completely in the btree.
2751 *
2752 * So, tell btrfs_drop_extents to leave this extent in the cache.
2753 * the caller is expected to unpin it and allow it to be merged
2754 * with the others.
2755 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002756 drop_args.path = path;
2757 drop_args.start = file_pos;
2758 drop_args.end = file_pos + num_bytes;
2759 drop_args.replace_extent = true;
2760 drop_args.extent_item_size = sizeof(*stack_fi);
2761 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002762 if (ret)
2763 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002764
Filipe Manana5893dfb2020-11-04 11:07:32 +00002765 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002766 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002767 ins.offset = file_pos;
2768 ins.type = BTRFS_EXTENT_DATA_KEY;
2769
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002770 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002771 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002772 if (ret)
2773 goto out;
2774 }
Yan Zhengd899e052008-10-30 14:25:28 -04002775 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002776 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2777 write_extent_buffer(leaf, stack_fi,
2778 btrfs_item_ptr_offset(leaf, path->slots[0]),
2779 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002780
Yan Zhengd899e052008-10-30 14:25:28 -04002781 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002782 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002783
Filipe Manana2766ff62020-11-04 11:07:34 +00002784 /*
2785 * If we dropped an inline extent here, we know the range where it is
2786 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2787 * number of bytes only for that range contaning the inline extent.
2788 * The remaining of the range will be processed when clearning the
2789 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2790 */
2791 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2792 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2793
2794 inline_size = drop_args.bytes_found - inline_size;
2795 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2796 drop_args.bytes_found -= inline_size;
2797 num_bytes -= sectorsize;
2798 }
2799
2800 if (update_inode_bytes)
2801 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002802
2803 ins.objectid = disk_bytenr;
2804 ins.offset = disk_num_bytes;
2805 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002806
Nikolay Borisovc553f942020-06-03 08:55:19 +03002807 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002808 if (ret)
2809 goto out;
2810
Nikolay Borisovc553f942020-06-03 08:55:19 +03002811 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002812 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002813out:
Yan Zhengd899e052008-10-30 14:25:28 -04002814 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002815
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002817}
2818
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002819static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002820 u64 start, u64 len)
2821{
David Sterba32da53862019-10-29 19:20:18 +01002822 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002823
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002825 ASSERT(cache);
2826
2827 spin_lock(&cache->lock);
2828 cache->delalloc_bytes -= len;
2829 spin_unlock(&cache->lock);
2830
2831 btrfs_put_block_group(cache);
2832}
2833
Qu Wenruo203f44c52020-06-10 09:04:40 +08002834static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002835 struct btrfs_ordered_extent *oe)
2836{
2837 struct btrfs_file_extent_item stack_fi;
2838 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002839 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002840
2841 memset(&stack_fi, 0, sizeof(stack_fi));
2842 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2843 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2844 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2845 oe->disk_num_bytes);
2846 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2847 logical_len = oe->truncated_len;
2848 else
2849 logical_len = oe->num_bytes;
2850 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2851 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2852 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2853 /* Encryption and other encoding is reserved and all 0 */
2854
Filipe Manana2766ff62020-11-04 11:07:34 +00002855 /*
2856 * For delalloc, when completing an ordered extent we update the inode's
2857 * bytes when clearing the range in the inode's io tree, so pass false
2858 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2859 * except if the ordered extent was truncated.
2860 */
2861 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2862 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2863
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002864 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2865 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002866 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002867}
2868
2869/*
2870 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002871 * an ordered extent if the range of bytes in the file it covers are
2872 * fully written.
2873 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002874static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002875{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002876 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2877 struct btrfs_root *root = inode->root;
2878 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002879 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002880 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002881 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002882 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002883 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002884 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002885 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002886 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002887 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002888 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002889 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002890
Omar Sandovalbffe6332019-12-02 17:34:19 -08002891 start = ordered_extent->file_offset;
2892 end = start + ordered_extent->num_bytes - 1;
2893
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002894 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2895 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2896 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002897 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002899 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002900
Josef Bacik5fd02042012-05-02 14:00:54 -04002901 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2902 ret = -EIO;
2903 goto out;
2904 }
2905
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002906 if (ordered_extent->disk)
2907 btrfs_rewrite_logical_zoned(ordered_extent);
2908
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002909 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002910
Josef Bacik77cef2e2013-08-29 13:57:21 -04002911 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2912 truncated = true;
2913 logical_len = ordered_extent->truncated_len;
2914 /* Truncated the entire extent, don't bother adding */
2915 if (!logical_len)
2916 goto out;
2917 }
2918
Yan, Zhengc2167752009-11-12 09:34:21 +00002919 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002920 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002921
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002922 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002923 if (freespace_inode)
2924 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002925 else
2926 trans = btrfs_join_transaction(root);
2927 if (IS_ERR(trans)) {
2928 ret = PTR_ERR(trans);
2929 trans = NULL;
2930 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002931 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002932 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002933 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002934 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002935 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002936 goto out;
2937 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002938
Filipe Manana2766ff62020-11-04 11:07:34 +00002939 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002940 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941
Nikolay Borisov8d510122019-10-08 20:43:06 +03002942 if (freespace_inode)
2943 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002945 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002946 if (IS_ERR(trans)) {
2947 ret = PTR_ERR(trans);
2948 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002949 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002950 }
Chris Masona79b7d42014-05-22 16:18:52 -07002951
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002952 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002953
Chris Masonc8b97812008-10-29 14:49:59 -04002954 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002955 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002956 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002957 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002958 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002962 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002963 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002964 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002965 if (!ret) {
2966 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002967 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002968 ordered_extent->disk_bytenr,
2969 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002970 }
Yan Zhengd899e052008-10-30 14:25:28 -04002971 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002972 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002973 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002975 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002976 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002978
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002979 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002980 if (ret) {
2981 btrfs_abort_transaction(trans, ret);
2982 goto out;
2983 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984
Filipe Manana2766ff62020-11-04 11:07:34 +00002985 /*
2986 * If this is a new delalloc range, clear its new delalloc flag to
2987 * update the inode's number of bytes. This needs to be done first
2988 * before updating the inode item.
2989 */
2990 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2991 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002992 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002993 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2994 0, 0, &cached_state);
2995
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002996 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002997 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002998 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002999 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003000 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003001 }
3002 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003003out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003004 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003005 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003006 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003007
Miao Xiea698d0752012-09-20 01:51:59 -06003008 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003009 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003010
Josef Bacik77cef2e2013-08-29 13:57:21 -04003011 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003012 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003013
Josef Bacikd61bec02021-05-19 09:38:27 -04003014 /*
3015 * If we failed to finish this ordered extent for any reason we
3016 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3017 * extent, and mark the inode with the error if it wasn't
3018 * already set. Any error during writeback would have already
3019 * set the mapping error, so we need to set it if we're the ones
3020 * marking this ordered extent as failed.
3021 */
3022 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3023 &ordered_extent->flags))
3024 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3025
Josef Bacik77cef2e2013-08-29 13:57:21 -04003026 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003027 unwritten_start += logical_len;
3028 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003029
3030 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003031 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003032
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003033 /*
3034 * If the ordered extent had an IOERR or something else went
3035 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003036 * back to the allocator. We only free the extent in the
3037 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003038 *
3039 * If we made it past insert_reserved_file_extent before we
3040 * errored out then we don't need to do this as the accounting
3041 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003042 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003043 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003044 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003045 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003046 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3047 /*
3048 * Discard the range before returning it back to the
3049 * free space pool
3050 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003051 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003052 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003053 ordered_extent->disk_bytenr,
3054 ordered_extent->disk_num_bytes,
3055 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003056 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003057 ordered_extent->disk_bytenr,
3058 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003059 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003060 }
3061
Josef Bacik5fd02042012-05-02 14:00:54 -04003062 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003063 * This needs to be done to make sure anybody waiting knows we are done
3064 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003066 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003067
Chris Masone6dcd2d2008-07-17 12:53:50 -04003068 /* once for us */
3069 btrfs_put_ordered_extent(ordered_extent);
3070 /* once for the tree */
3071 btrfs_put_ordered_extent(ordered_extent);
3072
Josef Bacik5fd02042012-05-02 14:00:54 -04003073 return ret;
3074}
3075
3076static void finish_ordered_fn(struct btrfs_work *work)
3077{
3078 struct btrfs_ordered_extent *ordered_extent;
3079 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3080 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003081}
3082
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003083void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3084 struct page *page, u64 start,
Nikolay Borisovc6297322018-11-08 10:18:08 +02003085 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003086{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003087 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003088
Qu Wenruoe65f1522021-04-01 15:15:06 +08003089 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3090 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003091}
3092
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003093/*
3094 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003095 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003096 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003097 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003098 * @page: page where is the data to be verified
3099 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003100 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003101 *
3102 * The length of such check is always one sector size.
3103 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003104static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003105 u32 bio_offset, struct page *page, u32 pgoff,
3106 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003107{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003108 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3109 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003110 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003111 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003112 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003113 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003114 u8 *csum_expected;
3115 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003116
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003117 ASSERT(pgoff + len <= PAGE_SIZE);
3118
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003119 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3120 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003121
3122 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003123 shash->tfm = fs_info->csum_shash;
3124
Eric Biggersfd080012020-04-30 23:51:59 -07003125 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003126
3127 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003128 goto zeroit;
3129
3130 kunmap_atomic(kaddr);
3131 return 0;
3132zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003133 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3134 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003135 if (io_bio->device)
3136 btrfs_dev_stat_inc_and_print(io_bio->device,
3137 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003138 memset(kaddr + pgoff, 1, len);
3139 flush_dcache_page(page);
3140 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003141 return -EIO;
3142}
3143
Chris Masond352ac62008-09-29 15:18:18 -04003144/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003145 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003146 * if there's a match, we allow the bio to finish. If not, the code in
3147 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003148 *
3149 * @bio_offset: offset to the beginning of the bio (in bytes)
3150 * @start: file offset of the range start
3151 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003152 *
3153 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3154 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003155 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003156unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3157 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003158{
Chris Mason07157aa2007-08-30 08:50:51 -04003159 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003160 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003161 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003162 const u32 sectorsize = root->fs_info->sectorsize;
3163 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003164 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003165
Chris Masond20f7042008-12-08 16:58:54 -05003166 if (PageChecked(page)) {
3167 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003168 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003169 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003170
3171 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003172 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003173
Josef Bacik42437a62020-10-16 11:29:18 -04003174 if (!root->fs_info->csum_root)
3175 return 0;
3176
Yan Zheng17d217f2008-12-12 10:03:38 -05003177 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003178 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003179 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003180 return 0;
3181 }
3182
Qu Wenruof44cf412020-12-02 14:47:59 +08003183 ASSERT(page_offset(page) <= start &&
3184 end <= page_offset(page) + PAGE_SIZE - 1);
3185 for (pg_off = offset_in_page(start);
3186 pg_off < offset_in_page(end);
3187 pg_off += sectorsize, bio_offset += sectorsize) {
3188 int ret;
3189
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003190 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3191 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003192 if (ret < 0) {
3193 const int nr_bit = (pg_off - offset_in_page(start)) >>
3194 root->fs_info->sectorsize_bits;
3195
3196 result |= (1U << nr_bit);
3197 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003198 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003199 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003200}
Chris Masonb888db22007-08-27 16:49:44 -04003201
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003202/*
3203 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3204 *
3205 * @inode: The inode we want to perform iput on
3206 *
3207 * This function uses the generic vfs_inode::i_count to track whether we should
3208 * just decrement it (in case it's > 1) or if this is the last iput then link
3209 * the inode to the delayed iput machinery. Delayed iputs are processed at
3210 * transaction commit time/superblock commit/cleaner kthread.
3211 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003212void btrfs_add_delayed_iput(struct inode *inode)
3213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003214 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003215 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216
3217 if (atomic_add_unless(&inode->i_count, -1, 1))
3218 return;
3219
Josef Bacik034f7842018-12-03 11:06:52 -05003220 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003221 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003222 ASSERT(list_empty(&binode->delayed_iput));
3223 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003225 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3226 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003227}
3228
Josef Bacik63611e72019-06-18 10:59:18 -04003229static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3230 struct btrfs_inode *inode)
3231{
3232 list_del_init(&inode->delayed_iput);
3233 spin_unlock(&fs_info->delayed_iput_lock);
3234 iput(&inode->vfs_inode);
3235 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3236 wake_up(&fs_info->delayed_iputs_wait);
3237 spin_lock(&fs_info->delayed_iput_lock);
3238}
3239
3240static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3241 struct btrfs_inode *inode)
3242{
3243 if (!list_empty(&inode->delayed_iput)) {
3244 spin_lock(&fs_info->delayed_iput_lock);
3245 if (!list_empty(&inode->delayed_iput))
3246 run_delayed_iput_locked(fs_info, inode);
3247 spin_unlock(&fs_info->delayed_iput_lock);
3248 }
3249}
3250
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003251void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003252{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003255 while (!list_empty(&fs_info->delayed_iputs)) {
3256 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003257
David Sterba8089fe62015-11-19 14:15:51 +01003258 inode = list_first_entry(&fs_info->delayed_iputs,
3259 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003260 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003261 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003262 }
David Sterba8089fe62015-11-19 14:15:51 +01003263 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264}
3265
Josef Bacik034f7842018-12-03 11:06:52 -05003266/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003267 * Wait for flushing all delayed iputs
3268 *
3269 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003270 *
3271 * This will wait on any delayed iputs that are currently running with KILLABLE
3272 * set. Once they are all done running we will return, unless we are killed in
3273 * which case we return EINTR. This helps in user operations like fallocate etc
3274 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003275 *
3276 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003277 */
3278int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3279{
3280 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3281 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3282 if (ret)
3283 return -EINTR;
3284 return 0;
3285}
3286
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003288 * This creates an orphan entry for the given inode in case something goes wrong
3289 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003290 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003291int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003292 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003293{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003294 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003295
Omar Sandoval27919062018-05-11 13:13:37 -07003296 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3297 if (ret && ret != -EEXIST) {
3298 btrfs_abort_transaction(trans, ret);
3299 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 }
3301
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003303}
3304
3305/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003306 * We have done the delete so we can go ahead and remove the orphan item for
3307 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003308 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003309static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003310 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003311{
Omar Sandoval27919062018-05-11 13:13:37 -07003312 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003313}
3314
3315/*
3316 * this cleans up any orphans that may be left on the list from the last use
3317 * of this root.
3318 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003321 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003322 struct btrfs_path *path;
3323 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003324 struct btrfs_key key, found_key;
3325 struct btrfs_trans_handle *trans;
3326 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003327 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003328 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003329
Yan, Zhengd68fc572010-05-16 10:49:58 -04003330 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003332
3333 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003334 if (!path) {
3335 ret = -ENOMEM;
3336 goto out;
3337 }
David Sterbae4058b52015-11-27 16:31:35 +01003338 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003339
3340 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003341 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003342 key.offset = (u64)-1;
3343
Josef Bacik7b128762008-07-24 12:17:14 -04003344 while (1) {
3345 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003346 if (ret < 0)
3347 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003348
3349 /*
3350 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003351 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003352 * find the key and see if we have stuff that matches
3353 */
3354 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003355 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003356 if (path->slots[0] == 0)
3357 break;
3358 path->slots[0]--;
3359 }
3360
3361 /* pull out the item */
3362 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003363 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3364
3365 /* make sure the item matches what we want */
3366 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3367 break;
David Sterba962a2982014-06-04 18:41:45 +02003368 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003369 break;
3370
3371 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003372 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003373
3374 /*
3375 * this is where we are basically btrfs_lookup, without the
3376 * crossing root thing. we store the inode number in the
3377 * offset of the orphan item.
3378 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003379
3380 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 btrfs_err(fs_info,
3382 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003383 ret = -EINVAL;
3384 goto out;
3385 }
3386
3387 last_objectid = found_key.offset;
3388
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389 found_key.objectid = found_key.offset;
3390 found_key.type = BTRFS_INODE_ITEM_KEY;
3391 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003392 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303393 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003394 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003395 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003397 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003398 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003399 int is_dead_root = 0;
3400
3401 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003402 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003403 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003404 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003405 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003406 * We need to distinguish those two, as the orphan item
3407 * for a root must not get deleted before the deletion
3408 * of the snapshot/subvolume's tree completes.
3409 *
3410 * btrfs_find_orphan_roots() ran before us, which has
3411 * found all deleted roots and loaded them into
3412 * fs_info->fs_roots_radix. So here we can find if an
3413 * orphan item corresponds to a deleted root by looking
3414 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003415 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003416
3417 spin_lock(&fs_info->fs_roots_radix_lock);
3418 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3419 (unsigned long)found_key.objectid);
3420 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3421 is_dead_root = 1;
3422 spin_unlock(&fs_info->fs_roots_radix_lock);
3423
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003424 if (is_dead_root) {
3425 /* prevent this orphan from being found again */
3426 key.offset = found_key.objectid - 1;
3427 continue;
3428 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003429
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003430 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003431
Josef Bacika8c9e572011-09-21 16:55:59 -04003432 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003433 * If we have an inode with links, there are a couple of
3434 * possibilities. Old kernels (before v3.12) used to create an
3435 * orphan item for truncate indicating that there were possibly
3436 * extent items past i_size that needed to be deleted. In v3.12,
3437 * truncate was changed to update i_size in sync with the extent
3438 * items, but the (useless) orphan item was still created. Since
3439 * v4.18, we don't create the orphan item for truncate at all.
3440 *
3441 * So, this item could mean that we need to do a truncate, but
3442 * only if this filesystem was last used on a pre-v3.12 kernel
3443 * and was not cleanly unmounted. The odds of that are quite
3444 * slim, and it's a pain to do the truncate now, so just delete
3445 * the orphan item.
3446 *
3447 * It's also possible that this orphan item was supposed to be
3448 * deleted but wasn't. The inode number may have been reused,
3449 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003450 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003451 if (ret == -ENOENT || inode->i_nlink) {
3452 if (!ret)
3453 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003454 trans = btrfs_start_transaction(root, 1);
3455 if (IS_ERR(trans)) {
3456 ret = PTR_ERR(trans);
3457 goto out;
3458 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 btrfs_debug(fs_info, "auto deleting %Lu",
3460 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003461 ret = btrfs_del_orphan_item(trans, root,
3462 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003463 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003464 if (ret)
3465 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003466 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003467 }
Josef Bacik7b128762008-07-24 12:17:14 -04003468
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003469 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003470
3471 /* this will do delete_inode and everything for us */
3472 iput(inode);
3473 }
Miao Xie3254c872011-11-10 20:45:05 -05003474 /* release the path since we're done with it */
3475 btrfs_release_path(path);
3476
Yan, Zhengd68fc572010-05-16 10:49:58 -04003477 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3478
Omar Sandovala575cee2018-05-11 13:13:38 -07003479 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003480 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003482 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003483 }
Josef Bacik7b128762008-07-24 12:17:14 -04003484
3485 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003486 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487
3488out:
3489 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003490 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003491 btrfs_free_path(path);
3492 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003493}
3494
Chris Masond352ac62008-09-29 15:18:18 -04003495/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003496 * very simple check to peek ahead in the leaf looking for xattrs. If we
3497 * don't find any xattrs, we know there can't be any acls.
3498 *
3499 * slot is the slot the inode is in, objectid is the objectid of the inode
3500 */
3501static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003502 int slot, u64 objectid,
3503 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003504{
3505 u32 nritems = btrfs_header_nritems(leaf);
3506 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003507 static u64 xattr_access = 0;
3508 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003509 int scanned = 0;
3510
Josef Bacikf23b5a52013-06-19 10:16:26 -04003511 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003512 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3513 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3514 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3515 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003516 }
3517
Chris Mason46a53cc2009-04-27 11:47:50 -04003518 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003519 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003520 while (slot < nritems) {
3521 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3522
3523 /* we found a different objectid, there must not be acls */
3524 if (found_key.objectid != objectid)
3525 return 0;
3526
3527 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003528 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003529 if (*first_xattr_slot == -1)
3530 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003531 if (found_key.offset == xattr_access ||
3532 found_key.offset == xattr_default)
3533 return 1;
3534 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003535
3536 /*
3537 * we found a key greater than an xattr key, there can't
3538 * be any acls later on
3539 */
3540 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3541 return 0;
3542
3543 slot++;
3544 scanned++;
3545
3546 /*
3547 * it goes inode, inode backrefs, xattrs, extents,
3548 * so if there are a ton of hard links to an inode there can
3549 * be a lot of backrefs. Don't waste time searching too hard,
3550 * this is just an optimization
3551 */
3552 if (scanned >= 8)
3553 break;
3554 }
3555 /* we hit the end of the leaf before we found an xattr or
3556 * something larger than an xattr. We have to assume the inode
3557 * has acls
3558 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003559 if (*first_xattr_slot == -1)
3560 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003561 return 1;
3562}
3563
3564/*
Chris Masond352ac62008-09-29 15:18:18 -04003565 * read an inode from the btree into the in-memory inode
3566 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003567static int btrfs_read_locked_inode(struct inode *inode,
3568 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003571 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003573 struct btrfs_inode_item *inode_item;
3574 struct btrfs_root *root = BTRFS_I(inode)->root;
3575 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003576 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003577 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003578 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003579 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003580 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003581 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003582
3583 ret = btrfs_fill_inode(inode, &rdev);
3584 if (!ret)
3585 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003586
Filipe Manana4222ea72018-10-24 10:13:03 +01003587 if (!path) {
3588 path = btrfs_alloc_path();
3589 if (!path)
3590 return -ENOMEM;
3591 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003592
Chris Mason39279cc2007-06-12 06:35:45 -04003593 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003594
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003596 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003597 if (path != in_path)
3598 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003599 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003600 }
Chris Mason39279cc2007-06-12 06:35:45 -04003601
Chris Mason5f39d392007-10-15 16:14:19 -04003602 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003603
3604 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003605 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
Chris Mason5f39d392007-10-15 16:14:19 -04003607 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3608 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003609 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003610 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003611 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3612 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003613 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003614 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3615 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbaa937b972014-12-12 17:39:12 +01003617 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3618 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbaa937b972014-12-12 17:39:12 +01003620 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3621 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
David Sterbaa937b972014-12-12 17:39:12 +01003623 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3624 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003625
chandan r9cc97d62012-07-04 12:48:07 +05303626 BTRFS_I(inode)->i_otime.tv_sec =
3627 btrfs_timespec_sec(leaf, &inode_item->otime);
3628 BTRFS_I(inode)->i_otime.tv_nsec =
3629 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003631 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003632 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003633 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3634
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003635 inode_set_iversion_queried(inode,
3636 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003637 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003638 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003639 rdev = btrfs_inode_rdev(leaf, inode_item);
3640
Josef Bacikaec74772008-07-24 12:12:38 -04003641 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003642 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003643
3644cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003645 /*
3646 * If we were modified in the current generation and evicted from memory
3647 * and then re-read we need to do a full sync since we don't have any
3648 * idea about which extents were modified before we were evicted from
3649 * cache.
3650 *
3651 * This is required for both inode re-read from disk and delayed inode
3652 * in delayed_nodes_tree.
3653 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003654 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003655 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3656 &BTRFS_I(inode)->runtime_flags);
3657
Filipe Mananabde6c242015-07-24 00:00:19 +01003658 /*
3659 * We don't persist the id of the transaction where an unlink operation
3660 * against the inode was last made. So here we assume the inode might
3661 * have been evicted, and therefore the exact value of last_unlink_trans
3662 * lost, and set it to last_trans to avoid metadata inconsistencies
3663 * between the inode and its parent if the inode is fsync'ed and the log
3664 * replayed. For example, in the scenario:
3665 *
3666 * touch mydir/foo
3667 * ln mydir/foo mydir/bar
3668 * sync
3669 * unlink mydir/bar
3670 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3671 * xfs_io -c fsync mydir/foo
3672 * <power failure>
3673 * mount fs, triggers fsync log replay
3674 *
3675 * We must make sure that when we fsync our inode foo we also log its
3676 * parent inode, otherwise after log replay the parent still has the
3677 * dentry with the "bar" name but our inode foo has a link count of 1
3678 * and doesn't have an inode ref with the name "bar" anymore.
3679 *
3680 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003681 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003682 * transaction commits on fsync if our inode is a directory, or if our
3683 * inode is not a directory, logging its parent unnecessarily.
3684 */
3685 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3686
Filipe Manana3ebac172020-07-15 12:30:43 +01003687 /*
3688 * Same logic as for last_unlink_trans. We don't persist the generation
3689 * of the last transaction where this inode was used for a reflink
3690 * operation, so after eviction and reloading the inode we must be
3691 * pessimistic and assume the last transaction that modified the inode.
3692 */
3693 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3694
Miao Xie67de1172013-12-26 13:07:06 +08003695 path->slots[0]++;
3696 if (inode->i_nlink != 1 ||
3697 path->slots[0] >= btrfs_header_nritems(leaf))
3698 goto cache_acl;
3699
3700 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003701 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003702 goto cache_acl;
3703
3704 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3705 if (location.type == BTRFS_INODE_REF_KEY) {
3706 struct btrfs_inode_ref *ref;
3707
3708 ref = (struct btrfs_inode_ref *)ptr;
3709 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3710 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3711 struct btrfs_inode_extref *extref;
3712
3713 extref = (struct btrfs_inode_extref *)ptr;
3714 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3715 extref);
3716 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003717cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003718 /*
3719 * try to precache a NULL acl entry for files that don't have
3720 * any xattrs or acls
3721 */
Li Zefan33345d012011-04-20 10:31:50 +08003722 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003723 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003724 if (first_xattr_slot != -1) {
3725 path->slots[0] = first_xattr_slot;
3726 ret = btrfs_load_inode_props(inode, path);
3727 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003728 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003729 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003730 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003731 root->root_key.objectid, ret);
3732 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003733 if (path != in_path)
3734 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003735
Al Viro72c04902009-06-24 16:58:48 -04003736 if (!maybe_acls)
3737 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003738
Chris Mason39279cc2007-06-12 06:35:45 -04003739 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003740 case S_IFREG:
3741 inode->i_mapping->a_ops = &btrfs_aops;
3742 inode->i_fop = &btrfs_file_operations;
3743 inode->i_op = &btrfs_file_inode_operations;
3744 break;
3745 case S_IFDIR:
3746 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003747 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 break;
3749 case S_IFLNK:
3750 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003751 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003752 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003753 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003754 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003755 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003756 init_special_inode(inode, inode->i_mode, rdev);
3757 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003758 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003759
David Sterba7b6a2212018-03-26 18:40:21 +02003760 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003761 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003762}
3763
Chris Masond352ac62008-09-29 15:18:18 -04003764/*
3765 * given a leaf and an inode, copy the inode fields into the leaf
3766 */
Chris Masone02119d2008-09-05 16:13:11 -04003767static void fill_inode_item(struct btrfs_trans_handle *trans,
3768 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003769 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003770 struct inode *inode)
3771{
Liu Bo51fab692012-12-27 09:01:21 +00003772 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003773
David Sterbac82f8232019-08-09 17:48:21 +02003774 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003775
David Sterbacc4c13d2020-04-29 02:15:56 +02003776 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3777 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3778 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3779 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3780 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
David Sterbacc4c13d2020-04-29 02:15:56 +02003782 btrfs_set_token_timespec_sec(&token, &item->atime,
3783 inode->i_atime.tv_sec);
3784 btrfs_set_token_timespec_nsec(&token, &item->atime,
3785 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003786
David Sterbacc4c13d2020-04-29 02:15:56 +02003787 btrfs_set_token_timespec_sec(&token, &item->mtime,
3788 inode->i_mtime.tv_sec);
3789 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3790 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003791
David Sterbacc4c13d2020-04-29 02:15:56 +02003792 btrfs_set_token_timespec_sec(&token, &item->ctime,
3793 inode->i_ctime.tv_sec);
3794 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3795 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003796
David Sterbacc4c13d2020-04-29 02:15:56 +02003797 btrfs_set_token_timespec_sec(&token, &item->otime,
3798 BTRFS_I(inode)->i_otime.tv_sec);
3799 btrfs_set_token_timespec_nsec(&token, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303801
David Sterbacc4c13d2020-04-29 02:15:56 +02003802 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3803 btrfs_set_token_inode_generation(&token, item,
3804 BTRFS_I(inode)->generation);
3805 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3806 btrfs_set_token_inode_transid(&token, item, trans->transid);
3807 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3808 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3809 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003810}
3811
Chris Masond352ac62008-09-29 15:18:18 -04003812/*
3813 * copy everything in the in-memory inode into the btree.
3814 */
Chris Mason21151332011-11-10 20:39:08 -05003815static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003816 struct btrfs_root *root,
3817 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003818{
3819 struct btrfs_inode_item *inode_item;
3820 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003821 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003822 int ret;
3823
3824 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003825 if (!path)
3826 return -ENOMEM;
3827
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003828 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 if (ret) {
3830 if (ret > 0)
3831 ret = -ENOENT;
3832 goto failed;
3833 }
3834
Chris Mason5f39d392007-10-15 16:14:19 -04003835 leaf = path->nodes[0];
3836 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003837 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003838
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003839 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003840 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003841 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 ret = 0;
3843failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003844 btrfs_free_path(path);
3845 return ret;
3846}
3847
Chris Masond352ac62008-09-29 15:18:18 -04003848/*
Chris Mason21151332011-11-10 20:39:08 -05003849 * copy everything in the in-memory inode into the btree.
3850 */
3851noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003852 struct btrfs_root *root,
3853 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003854{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003855 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003856 int ret;
3857
3858 /*
3859 * If the inode is a free space inode, we can deadlock during commit
3860 * if we put it into the delayed code.
3861 *
3862 * The data relocation inode should also be directly updated
3863 * without delay
3864 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003865 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003866 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003867 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003868 btrfs_update_root_times(trans, root);
3869
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003870 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003871 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003872 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003873 return ret;
3874 }
3875
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003876 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003877}
3878
Nikolay Borisov729f7962020-11-02 16:49:06 +02003879int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3880 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003881{
3882 int ret;
3883
Nikolay Borisov729f7962020-11-02 16:49:06 +02003884 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003885 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003886 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003887 return ret;
3888}
3889
3890/*
Chris Masond352ac62008-09-29 15:18:18 -04003891 * unlink helper that gets used here in inode.c and in the tree logging
3892 * recovery code. It remove a link in a directory with a given name, and
3893 * also drops the back refs in the inode to the directory
3894 */
Al Viro92986792011-03-04 17:14:37 +00003895static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003897 struct btrfs_inode *dir,
3898 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003899 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003900{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003901 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003903 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003904 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003905 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003906 u64 ino = btrfs_ino(inode);
3907 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003908
3909 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003910 if (!path) {
3911 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003912 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003913 }
3914
Li Zefan33345d012011-04-20 10:31:50 +08003915 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003916 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003917 if (IS_ERR_OR_NULL(di)) {
3918 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 goto err;
3920 }
Chris Mason39279cc2007-06-12 06:35:45 -04003921 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (ret)
3923 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003924 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003925
Miao Xie67de1172013-12-26 13:07:06 +08003926 /*
3927 * If we don't have dir index, we have to get it by looking up
3928 * the inode ref, since we get the inode ref, remove it directly,
3929 * it is unnecessary to do delayed deletion.
3930 *
3931 * But if we have dir index, needn't search inode ref to get it.
3932 * Since the inode ref is close to the inode item, it is better
3933 * that we delay to delete it, and just do this deletion when
3934 * we update the inode item.
3935 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003936 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003937 ret = btrfs_delayed_delete_inode_ref(inode);
3938 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003939 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003940 goto skip_backref;
3941 }
3942 }
3943
Li Zefan33345d012011-04-20 10:31:50 +08003944 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3945 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003946 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003947 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003948 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003949 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003950 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003951 goto err;
3952 }
Miao Xie67de1172013-12-26 13:07:06 +08003953skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003954 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003955 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003956 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 }
Chris Mason39279cc2007-06-12 06:35:45 -04003959
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003960 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3961 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003963 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 goto err;
3965 }
Chris Masone02119d2008-09-05 16:13:11 -04003966
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003967 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3968 index);
Chris Mason6418c962010-10-30 07:34:24 -04003969 if (ret == -ENOENT)
3970 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003971 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003972 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003973
3974 /*
3975 * If we have a pending delayed iput we could end up with the final iput
3976 * being run in btrfs-cleaner context. If we have enough of these built
3977 * up we can end up burning a lot of time in btrfs-cleaner without any
3978 * way to throttle the unlinks. Since we're currently holding a ref on
3979 * the inode we can run the delayed iput here without any issues as the
3980 * final iput won't be done until after we drop the ref we're currently
3981 * holding.
3982 */
3983 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003984err:
3985 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003986 if (ret)
3987 goto out;
3988
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003989 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003990 inode_inc_iversion(&inode->vfs_inode);
3991 inode_inc_iversion(&dir->vfs_inode);
3992 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3993 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003994 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003995out:
Chris Mason39279cc2007-06-12 06:35:45 -04003996 return ret;
3997}
3998
Al Viro92986792011-03-04 17:14:37 +00003999int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4000 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004001 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004002 const char *name, int name_len)
4003{
4004 int ret;
4005 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4006 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004007 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004008 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004009 }
4010 return ret;
4011}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012
4013/*
4014 * helper to start transaction for unlink and rmdir.
4015 *
Josef Bacikd52be812013-05-29 14:54:47 -04004016 * unlink and rmdir are special in btrfs, they do not always free space, so
4017 * if we cannot make our reservations the normal way try and see if there is
4018 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4019 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 */
Josef Bacikd52be812013-05-29 14:54:47 -04004021static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024
Josef Bacike70bea52011-10-11 14:18:24 -04004025 /*
4026 * 1 for the possible orphan item
4027 * 1 for the dir item
4028 * 1 for the dir index
4029 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004030 * 1 for the inode
4031 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004032 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004033}
4034
Chris Mason39279cc2007-06-12 06:35:45 -04004035static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4036{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004037 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004039 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004040 int ret;
4041
Josef Bacikd52be812013-05-29 14:54:47 -04004042 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004044 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004045
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004046 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4047 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004048
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004049 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4050 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4051 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004052 if (ret)
4053 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004054
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004056 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004057 if (ret)
4058 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004059 }
Josef Bacik7b128762008-07-24 12:17:14 -04004060
Tsutomu Itohb5324022011-07-19 07:27:20 +00004061out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004062 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004063 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 return ret;
4065}
4066
Misono Tomohirof60a2362018-04-18 11:34:52 +09004067static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004068 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004070 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004071 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072 struct btrfs_path *path;
4073 struct extent_buffer *leaf;
4074 struct btrfs_dir_item *di;
4075 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004076 const char *name = dentry->d_name.name;
4077 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004078 u64 index;
4079 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004080 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004081 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082
Josef Bacik045d3962019-12-18 17:20:27 -05004083 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4084 objectid = inode->root->root_key.objectid;
4085 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4086 objectid = inode->location.objectid;
4087 } else {
4088 WARN_ON(1);
4089 return -EINVAL;
4090 }
4091
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 path = btrfs_alloc_path();
4093 if (!path)
4094 return -ENOMEM;
4095
Li Zefan33345d012011-04-20 10:31:50 +08004096 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004099 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 goto out;
4101 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
4103 leaf = path->nodes[0];
4104 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4105 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4106 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004108 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 goto out;
4110 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004111 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
Josef Bacikd49d3282019-12-18 17:20:28 -05004113 /*
4114 * This is a placeholder inode for a subvolume we didn't have a
4115 * reference to at the time of the snapshot creation. In the meantime
4116 * we could have renamed the real subvol link into our snapshot, so
4117 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4118 * Instead simply lookup the dir_index_item for this entry so we can
4119 * remove it. Otherwise we know we have a ref to the root and we can
4120 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4121 */
4122 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004123 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 if (IS_ERR_OR_NULL(di)) {
4126 if (!di)
4127 ret = -ENOENT;
4128 else
4129 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004130 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131 goto out;
4132 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133
4134 leaf = path->nodes[0];
4135 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004136 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004137 btrfs_release_path(path);
4138 } else {
4139 ret = btrfs_del_root_ref(trans, objectid,
4140 root->root_key.objectid, dir_ino,
4141 &index, name, name_len);
4142 if (ret) {
4143 btrfs_abort_transaction(trans, ret);
4144 goto out;
4145 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004146 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147
Lu Fengqi9add2942018-08-01 11:32:26 +08004148 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004150 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151 goto out;
4152 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004153
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004154 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004155 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004156 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004157 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004159 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004161 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004162 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163}
4164
Misono Tomohiroec42f162018-04-18 11:34:13 +09004165/*
4166 * Helper to check if the subvolume references other subvolumes or if it's
4167 * default.
4168 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004169static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004170{
4171 struct btrfs_fs_info *fs_info = root->fs_info;
4172 struct btrfs_path *path;
4173 struct btrfs_dir_item *di;
4174 struct btrfs_key key;
4175 u64 dir_id;
4176 int ret;
4177
4178 path = btrfs_alloc_path();
4179 if (!path)
4180 return -ENOMEM;
4181
4182 /* Make sure this root isn't set as the default subvol */
4183 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4184 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4185 dir_id, "default", 7, 0);
4186 if (di && !IS_ERR(di)) {
4187 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4188 if (key.objectid == root->root_key.objectid) {
4189 ret = -EPERM;
4190 btrfs_err(fs_info,
4191 "deleting default subvolume %llu is not allowed",
4192 key.objectid);
4193 goto out;
4194 }
4195 btrfs_release_path(path);
4196 }
4197
4198 key.objectid = root->root_key.objectid;
4199 key.type = BTRFS_ROOT_REF_KEY;
4200 key.offset = (u64)-1;
4201
4202 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4203 if (ret < 0)
4204 goto out;
4205 BUG_ON(ret == 0);
4206
4207 ret = 0;
4208 if (path->slots[0] > 0) {
4209 path->slots[0]--;
4210 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4211 if (key.objectid == root->root_key.objectid &&
4212 key.type == BTRFS_ROOT_REF_KEY)
4213 ret = -ENOTEMPTY;
4214 }
4215out:
4216 btrfs_free_path(path);
4217 return ret;
4218}
4219
Nikolay Borisov20a68002018-04-27 14:36:24 +03004220/* Delete all dentries for inodes belonging to the root */
4221static void btrfs_prune_dentries(struct btrfs_root *root)
4222{
4223 struct btrfs_fs_info *fs_info = root->fs_info;
4224 struct rb_node *node;
4225 struct rb_node *prev;
4226 struct btrfs_inode *entry;
4227 struct inode *inode;
4228 u64 objectid = 0;
4229
4230 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4231 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4232
4233 spin_lock(&root->inode_lock);
4234again:
4235 node = root->inode_tree.rb_node;
4236 prev = NULL;
4237 while (node) {
4238 prev = node;
4239 entry = rb_entry(node, struct btrfs_inode, rb_node);
4240
David Sterba37508512018-06-29 10:56:40 +02004241 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004242 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004243 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004244 node = node->rb_right;
4245 else
4246 break;
4247 }
4248 if (!node) {
4249 while (prev) {
4250 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004251 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004252 node = prev;
4253 break;
4254 }
4255 prev = rb_next(prev);
4256 }
4257 }
4258 while (node) {
4259 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004260 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004261 inode = igrab(&entry->vfs_inode);
4262 if (inode) {
4263 spin_unlock(&root->inode_lock);
4264 if (atomic_read(&inode->i_count) > 1)
4265 d_prune_aliases(inode);
4266 /*
4267 * btrfs_drop_inode will have it removed from the inode
4268 * cache when its usage count hits zero.
4269 */
4270 iput(inode);
4271 cond_resched();
4272 spin_lock(&root->inode_lock);
4273 goto again;
4274 }
4275
4276 if (cond_resched_lock(&root->inode_lock))
4277 goto again;
4278
4279 node = rb_next(node);
4280 }
4281 spin_unlock(&root->inode_lock);
4282}
4283
Misono Tomohirof60a2362018-04-18 11:34:52 +09004284int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4285{
4286 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4287 struct btrfs_root *root = BTRFS_I(dir)->root;
4288 struct inode *inode = d_inode(dentry);
4289 struct btrfs_root *dest = BTRFS_I(inode)->root;
4290 struct btrfs_trans_handle *trans;
4291 struct btrfs_block_rsv block_rsv;
4292 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004293 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004294
4295 /*
4296 * Don't allow to delete a subvolume with send in progress. This is
4297 * inside the inode lock so the error handling that has to drop the bit
4298 * again is not run concurrently.
4299 */
4300 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004301 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004302 spin_unlock(&dest->root_item_lock);
4303 btrfs_warn(fs_info,
4304 "attempt to delete subvolume %llu during send",
4305 dest->root_key.objectid);
4306 return -EPERM;
4307 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004308 root_flags = btrfs_root_flags(&dest->root_item);
4309 btrfs_set_root_flags(&dest->root_item,
4310 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4311 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004312
4313 down_write(&fs_info->subvol_sem);
4314
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004315 ret = may_destroy_subvol(dest);
4316 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004317 goto out_up_write;
4318
4319 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4320 /*
4321 * One for dir inode,
4322 * two for dir entries,
4323 * two for root ref/backref.
4324 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004325 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4326 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004327 goto out_up_write;
4328
4329 trans = btrfs_start_transaction(root, 0);
4330 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004331 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004332 goto out_release;
4333 }
4334 trans->block_rsv = &block_rsv;
4335 trans->bytes_reserved = block_rsv.size;
4336
4337 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4338
Josef Bacik045d3962019-12-18 17:20:27 -05004339 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004340 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004341 btrfs_abort_transaction(trans, ret);
4342 goto out_end_trans;
4343 }
4344
Josef Bacik2731f512021-03-12 15:25:04 -05004345 ret = btrfs_record_root_in_trans(trans, dest);
4346 if (ret) {
4347 btrfs_abort_transaction(trans, ret);
4348 goto out_end_trans;
4349 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004350
4351 memset(&dest->root_item.drop_progress, 0,
4352 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004353 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004354 btrfs_set_root_refs(&dest->root_item, 0);
4355
4356 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4357 ret = btrfs_insert_orphan_item(trans,
4358 fs_info->tree_root,
4359 dest->root_key.objectid);
4360 if (ret) {
4361 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004362 goto out_end_trans;
4363 }
4364 }
4365
Lu Fengqid1957792018-05-29 15:01:54 +08004366 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004367 BTRFS_UUID_KEY_SUBVOL,
4368 dest->root_key.objectid);
4369 if (ret && ret != -ENOENT) {
4370 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004371 goto out_end_trans;
4372 }
4373 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004374 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004375 dest->root_item.received_uuid,
4376 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4377 dest->root_key.objectid);
4378 if (ret && ret != -ENOENT) {
4379 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004380 goto out_end_trans;
4381 }
4382 }
4383
Qu Wenruo082b6c92020-06-16 10:17:37 +08004384 free_anon_bdev(dest->anon_dev);
4385 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004386out_end_trans:
4387 trans->block_rsv = NULL;
4388 trans->bytes_reserved = 0;
4389 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004390 inode->i_flags |= S_DEAD;
4391out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004392 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004393out_up_write:
4394 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004395 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004396 spin_lock(&dest->root_item_lock);
4397 root_flags = btrfs_root_flags(&dest->root_item);
4398 btrfs_set_root_flags(&dest->root_item,
4399 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4400 spin_unlock(&dest->root_item_lock);
4401 } else {
4402 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004403 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004404 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004405 }
4406
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004407 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004408}
4409
Chris Mason39279cc2007-06-12 06:35:45 -04004410static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4411{
David Howells2b0143b2015-03-17 22:25:59 +00004412 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004413 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004415 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004416 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004417
David Sterbab3ae2442012-09-13 16:04:34 -06004418 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004419 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004420 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004421 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004422
Josef Bacikd52be812013-05-29 14:54:47 -04004423 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004424 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004425 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004426
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004427 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004428 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004429 goto out;
4430 }
4431
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004432 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004433 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004434 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004435
Filipe Manana44f714d2016-06-06 16:11:13 +01004436 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004439 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4440 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4441 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004442 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004443 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004444 /*
4445 * Propagate the last_unlink_trans value of the deleted dir to
4446 * its parent directory. This is to prevent an unrecoverable
4447 * log tree in the case we do something like this:
4448 * 1) create dir foo
4449 * 2) create snapshot under dir foo
4450 * 3) delete the snapshot
4451 * 4) rmdir foo
4452 * 5) mkdir foo
4453 * 6) fsync foo or some file inside foo
4454 */
4455 if (last_unlink_trans >= trans->transid)
4456 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4457 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004458out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004459 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004460 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004461
Chris Mason39279cc2007-06-12 06:35:45 -04004462 return err;
4463}
4464
Josef Bacikddfae632017-10-19 14:16:02 -04004465/*
4466 * Return this if we need to call truncate_block for the last bit of the
4467 * truncate.
4468 */
4469#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004470
Chris Mason323ac952008-10-01 19:05:46 -04004471/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004472 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004473 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004474 * @trans: A transaction handle.
4475 * @root: The root from which to remove items.
4476 * @inode: The inode whose items we want to remove.
4477 * @new_size: The new i_size for the inode. This is only applicable when
4478 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4479 * @min_type: The minimum key type to remove. All keys with a type
4480 * greater than this value are removed and all keys with
4481 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004482 * @extents_found: Output parameter that will contain the number of file
4483 * extent items that were removed or adjusted to the new
4484 * inode i_size. The caller is responsible for initializing
4485 * the counter. Also, it can be NULL if the caller does not
4486 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004487 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004488 * Remove all keys associated with the inode from the given root that have a key
4489 * with a type greater than or equals to @min_type. When @min_type has a value of
4490 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4491 * greater than or equals to @new_size. If a file extent item that starts before
4492 * @new_size and ends after it is found, its length is adjusted.
4493 *
4494 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4495 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004496 */
Yan, Zheng80825102009-11-12 09:35:36 +00004497int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4498 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004499 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004500 u64 new_size, u32 min_type,
4501 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004502{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004504 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004505 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004507 struct btrfs_key key;
4508 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004509 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004510 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004511 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004512 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004513 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004514 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004515 int found_extent;
4516 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 int pending_del_nr = 0;
4518 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004519 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004520 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004521 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004522 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004523 bool be_nice = false;
4524 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004525 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4526 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004527
4528 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004529
Chris Mason28ed1342014-12-17 09:41:04 -08004530 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004531 * For non-free space inodes and non-shareable roots, we want to back
4532 * off from time to time. This means all inodes in subvolume roots,
4533 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004534 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004535 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004536 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004537 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004538
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004539 path = btrfs_alloc_path();
4540 if (!path)
4541 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004542 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004543
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004544 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004545 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004546 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004547
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004548 /*
4549 * We want to drop from the next block forward in case this
4550 * new size is not block aligned since we will be keeping the
4551 * last block of the extent just the way it is.
4552 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004553 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004554 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004555 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004556 }
Yan, Zheng80825102009-11-12 09:35:36 +00004557
Miao Xie16cdcec2011-04-22 18:12:22 +08004558 /*
4559 * This function is also used to drop the items in the log tree before
4560 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004561 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004562 * items.
4563 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004564 if (min_type == 0 && root == inode->root)
4565 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004566
Li Zefan33345d012011-04-20 10:31:50 +08004567 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004568 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004569 key.type = (u8)-1;
4570
Chris Mason85e21ba2008-01-29 15:11:36 -05004571search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004572 /*
4573 * with a 16K leaf size and 128MB extents, you can actually queue
4574 * up a huge file in a single leaf. Most of the time that
4575 * bytes_deleted is > 0, it will be huge by the time we get here
4576 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004577 if (be_nice && bytes_deleted > SZ_32M &&
4578 btrfs_should_end_transaction(trans)) {
4579 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004580 goto out;
4581 }
Chris Masond3977122009-01-05 21:25:51 -05004582
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004583 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4584 if (ret < 0)
4585 goto out;
4586
Chris Mason85e21ba2008-01-29 15:11:36 -05004587 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004588 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004589 /* there are no items in the tree for us to truncate, we're
4590 * done
4591 */
Yan, Zheng80825102009-11-12 09:35:36 +00004592 if (path->slots[0] == 0)
4593 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004594 path->slots[0]--;
4595 }
4596
Chris Masond3977122009-01-05 21:25:51 -05004597 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004598 u64 clear_start = 0, clear_len = 0;
4599
Chris Mason39279cc2007-06-12 06:35:45 -04004600 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004601 leaf = path->nodes[0];
4602 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004603 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004604
Li Zefan33345d012011-04-20 10:31:50 +08004605 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004606 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004607
Chris Mason85e21ba2008-01-29 15:11:36 -05004608 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004609 break;
4610
Chris Mason5f39d392007-10-15 16:14:19 -04004611 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004612 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004613 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004614 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004615 extent_type = btrfs_file_extent_type(leaf, fi);
4616 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004617 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004618 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004619
4620 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004621 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004622 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004623 item_end += btrfs_file_extent_ram_bytes(leaf,
4624 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004625
4626 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004627 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004628 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004629 }
Yan008630c2007-11-07 13:31:09 -05004630 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004631 }
Yan, Zheng80825102009-11-12 09:35:36 +00004632 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004633 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004634 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004635 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004636 break;
4637 if (found_key.offset >= new_size)
4638 del_item = 1;
4639 else
4640 del_item = 0;
4641 }
Chris Mason39279cc2007-06-12 06:35:45 -04004642 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004643 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004644 if (found_type != BTRFS_EXTENT_DATA_KEY)
4645 goto delete;
4646
Filipe Manana0d7d3162021-05-24 11:35:55 +01004647 if (extents_found != NULL)
4648 (*extents_found)++;
4649
Chris Mason179e29e2007-11-01 11:28:41 -04004650 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004651 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004652
4653 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004654 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004655 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004656 u64 orig_num_bytes =
4657 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004658 extent_num_bytes = ALIGN(new_size -
4659 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004660 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004661 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004662 btrfs_set_file_extent_num_bytes(leaf, fi,
4663 extent_num_bytes);
4664 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004665 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004666 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004667 &root->state) &&
4668 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004669 inode_sub_bytes(&inode->vfs_inode,
4670 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004671 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004672 } else {
Chris Masondb945352007-10-15 16:15:53 -04004673 extent_num_bytes =
4674 btrfs_file_extent_disk_num_bytes(leaf,
4675 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004676 extent_offset = found_key.offset -
4677 btrfs_file_extent_offset(leaf, fi);
4678
Chris Mason39279cc2007-06-12 06:35:45 -04004679 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004680 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004681 if (extent_start != 0) {
4682 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004683 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004684 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004685 inode_sub_bytes(&inode->vfs_inode,
4686 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004687 }
4688 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004689 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004690 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004691 /*
4692 * we can't truncate inline items that have had
4693 * special encodings
4694 */
4695 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004696 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004697 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4698 btrfs_file_extent_compression(leaf, fi) == 0) {
4699 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004700
Josef Bacikddfae632017-10-19 14:16:02 -04004701 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4702 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004703 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004704 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004705 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004706 * We have to bail so the last_size is set to
4707 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004708 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004709 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004710 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004711 } else {
4712 /*
4713 * Inline extents are special, we just treat
4714 * them as a full sector worth in the file
4715 * extent tree just for simplicity sake.
4716 */
4717 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004718 }
Josef Bacikddfae632017-10-19 14:16:02 -04004719
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004720 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004721 inode_sub_bytes(&inode->vfs_inode,
4722 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004723 }
Chris Mason179e29e2007-11-01 11:28:41 -04004724delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004725 /*
4726 * We use btrfs_truncate_inode_items() to clean up log trees for
4727 * multiple fsyncs, and in this case we don't want to clear the
4728 * file extent range because it's just the log.
4729 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004730 if (root == inode->root) {
4731 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004732 clear_start, clear_len);
4733 if (ret) {
4734 btrfs_abort_transaction(trans, ret);
4735 break;
4736 }
4737 }
4738
Josef Bacikddfae632017-10-19 14:16:02 -04004739 if (del_item)
4740 last_size = found_key.offset;
4741 else
4742 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004744 if (!pending_del_nr) {
4745 /* no pending yet, add ourselves */
4746 pending_del_slot = path->slots[0];
4747 pending_del_nr = 1;
4748 } else if (pending_del_nr &&
4749 path->slots[0] + 1 == pending_del_slot) {
4750 /* hop on the pending chunk */
4751 pending_del_nr++;
4752 pending_del_slot = path->slots[0];
4753 } else {
Chris Masond3977122009-01-05 21:25:51 -05004754 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004755 }
Chris Mason39279cc2007-06-12 06:35:45 -04004756 } else {
4757 break;
4758 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004759 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004760
Miao Xie27cdeb72014-04-02 19:51:05 +08004761 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004762 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004763 struct btrfs_ref ref = { 0 };
4764
Chris Mason28ed1342014-12-17 09:41:04 -08004765 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004766
4767 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4768 extent_start, extent_num_bytes, 0);
4769 ref.real_root = root->root_key.objectid;
4770 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4771 ino, extent_offset);
4772 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004773 if (ret) {
4774 btrfs_abort_transaction(trans, ret);
4775 break;
4776 }
Chris Mason28f75a02015-02-04 06:59:29 -08004777 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004778 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004779 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004780 }
Chris Mason39279cc2007-06-12 06:35:45 -04004781 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004782
Yan, Zheng80825102009-11-12 09:35:36 +00004783 if (found_type == BTRFS_INODE_ITEM_KEY)
4784 break;
4785
4786 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004787 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004788 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004789 if (pending_del_nr) {
4790 ret = btrfs_del_items(trans, root, path,
4791 pending_del_slot,
4792 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004793 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004794 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004795 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004796 }
Yan, Zheng80825102009-11-12 09:35:36 +00004797 pending_del_nr = 0;
4798 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004799 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004800
Chris Mason28f75a02015-02-04 06:59:29 -08004801 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004802 * We can generate a lot of delayed refs, so we need to
4803 * throttle every once and a while and make sure we're
4804 * adding enough space to keep up with the work we are
4805 * generating. Since we hold a transaction here we
4806 * can't flush, and we don't want to FLUSH_LIMIT because
4807 * we could have generated too many delayed refs to
4808 * actually allocate, so just bail if we're short and
4809 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004810 */
Josef Bacik28bad212018-12-03 10:20:38 -05004811 if (should_throttle) {
4812 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4813 BTRFS_RESERVE_NO_FLUSH);
4814 if (ret) {
4815 ret = -EAGAIN;
4816 break;
4817 }
Chris Mason28f75a02015-02-04 06:59:29 -08004818 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004819 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004820 } else {
4821 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004822 }
Chris Mason39279cc2007-06-12 06:35:45 -04004823 }
Yan, Zheng80825102009-11-12 09:35:36 +00004824out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004825 if (ret >= 0 && pending_del_nr) {
4826 int err;
4827
4828 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004829 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004830 if (err) {
4831 btrfs_abort_transaction(trans, err);
4832 ret = err;
4833 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004834 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004835 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4836 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004837 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004838 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004839 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4840 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4841 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004842 }
Chris Mason28ed1342014-12-17 09:41:04 -08004843
Chris Mason39279cc2007-06-12 06:35:45 -04004844 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004845 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004846}
4847
Chris Masona52d9a82007-08-27 16:49:44 -04004848/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004850 * @inode - inode that we're zeroing
4851 * @from - the offset to start zeroing
4852 * @len - the length to zero, 0 to zero the entire range respective to the
4853 * offset
4854 * @front - zero up to the offset instead of from the offset on
4855 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004857 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004858 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004859int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4860 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004861{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004862 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4863 struct address_space *mapping = inode->vfs_inode.i_mapping;
4864 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004865 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004866 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004867 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004868 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004870 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304871 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004872 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004873 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004874 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004875 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304876 u64 block_start;
4877 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004878
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004879 if (IS_ALIGNED(offset, blocksize) &&
4880 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004881 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304882
Josef Bacik8b62f872017-10-19 14:15:55 -04004883 block_start = round_down(from, blocksize);
4884 block_end = block_start + blocksize - 1;
4885
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004886 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4887 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004888 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004889 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004890 /* For nocow case, no need to reserve data space */
4891 only_release_metadata = true;
4892 } else {
4893 goto out;
4894 }
4895 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004896 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004897 if (ret < 0) {
4898 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004899 btrfs_free_reserved_data_space(inode, data_reserved,
4900 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004901 goto out;
4902 }
Chris Mason211c17f2008-05-15 09:13:45 -04004903again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004904 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004905 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004906 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4907 blocksize, true);
4908 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004909 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004910 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004911 }
Qu Wenruo32443de2021-01-26 16:34:00 +08004912 ret = set_page_extent_mapped(page);
4913 if (ret < 0)
4914 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004915
Chris Masona52d9a82007-08-27 16:49:44 -04004916 if (!PageUptodate(page)) {
4917 ret = btrfs_readpage(NULL, page);
4918 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004919 if (page->mapping != mapping) {
4920 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004921 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004922 goto again;
4923 }
Chris Masona52d9a82007-08-27 16:49:44 -04004924 if (!PageUptodate(page)) {
4925 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004926 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004927 }
4928 }
Chris Mason211c17f2008-05-15 09:13:45 -04004929 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004930
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304931 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004932
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004933 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004934 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004936 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004937 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004938 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004939 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004940 btrfs_put_ordered_extent(ordered);
4941 goto again;
4942 }
4943
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004944 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004945 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4946 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004947
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004948 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004949 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004950 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304951 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004952 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004953 goto out_unlock;
4954 }
4955
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304956 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004957 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304958 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04004959 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07004960 memzero_page(page, (block_start - page_offset(page)),
4961 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004962 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07004963 memzero_page(page, (block_start - page_offset(page)) + offset,
4964 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004965 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004966 }
Chris Mason247e7432008-07-17 12:53:51 -04004967 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004968 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004969 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004970
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004971 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004972 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004973 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004974
Chris Mason89642222008-07-24 09:41:53 -04004975out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004976 if (ret) {
4977 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004978 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004979 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004980 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004981 block_start, blocksize, true);
4982 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004983 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004984 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004985 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004986out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004987 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004988 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004989 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004990 return ret;
4991}
4992
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004993static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004994 u64 offset, u64 len)
4995{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004996 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004997 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004998 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004999 int ret;
5000
5001 /*
5002 * Still need to make sure the inode looks like it's been updated so
5003 * that any holes get logged if we fsync.
5004 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005005 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005006 inode->last_trans = fs_info->generation;
5007 inode->last_sub_trans = root->log_transid;
5008 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04005009 return 0;
5010 }
5011
5012 /*
5013 * 1 - for the one we're dropping
5014 * 1 - for the one we're adding
5015 * 1 - for updating the inode.
5016 */
5017 trans = btrfs_start_transaction(root, 3);
5018 if (IS_ERR(trans))
5019 return PTR_ERR(trans);
5020
Filipe Manana5893dfb2020-11-04 11:07:32 +00005021 drop_args.start = offset;
5022 drop_args.end = offset + len;
5023 drop_args.drop_cache = true;
5024
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005025 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005026 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005027 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005028 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005029 return ret;
5030 }
5031
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005032 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005033 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005034 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005035 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005036 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005037 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5038 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005039 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005040 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005041 return ret;
5042}
5043
Josef Bacik695a0d02011-03-04 15:46:53 -05005044/*
5045 * This function puts in dummy file extents for the area we're creating a hole
5046 * for. So if we are truncating this file to a larger size we need to insert
5047 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5048 * the range between oldsize and size
5049 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005050int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005051{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005052 struct btrfs_root *root = inode->root;
5053 struct btrfs_fs_info *fs_info = root->fs_info;
5054 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005055 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005056 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005057 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005058 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5059 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005060 u64 last_byte;
5061 u64 cur_offset;
5062 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005063 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005064
Josef Bacika71754f2013-06-17 17:14:39 -04005065 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305066 * If our size started in the middle of a block we need to zero out the
5067 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005068 * expose stale data.
5069 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005070 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005071 if (err)
5072 return err;
5073
Yan Zheng9036c102008-10-30 14:19:41 -04005074 if (size <= hole_start)
5075 return 0;
5076
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005077 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5078 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005079 cur_offset = hole_start;
5080 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005081 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005082 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005083 if (IS_ERR(em)) {
5084 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005085 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005086 break;
5087 }
Yan Zheng9036c102008-10-30 14:19:41 -04005088 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005089 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005090 hole_size = last_byte - cur_offset;
5091
Yan, Zheng80825102009-11-12 09:35:36 +00005092 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005093 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005094
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005095 err = maybe_insert_hole(root, inode, cur_offset,
5096 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005097 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005098 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005099
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005100 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005101 cur_offset, hole_size);
5102 if (err)
5103 break;
5104
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005105 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005106 cur_offset + hole_size - 1, 0);
5107 hole_em = alloc_extent_map();
5108 if (!hole_em) {
5109 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005110 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005111 goto next;
5112 }
5113 hole_em->start = cur_offset;
5114 hole_em->len = hole_size;
5115 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005116
Josef Bacik5dc562c2012-08-17 13:14:17 -04005117 hole_em->block_start = EXTENT_MAP_HOLE;
5118 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005119 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005120 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005121 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005122 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005123
5124 while (1) {
5125 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005126 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005127 write_unlock(&em_tree->lock);
5128 if (err != -EEXIST)
5129 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005130 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005131 cur_offset +
5132 hole_size - 1, 0);
5133 }
5134 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005135 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005136 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005137 cur_offset, hole_size);
5138 if (err)
5139 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005140 }
Josef Bacik16e75492013-10-22 12:18:51 -04005141next:
Yan Zheng9036c102008-10-30 14:19:41 -04005142 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005143 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005144 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005145 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005146 break;
5147 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005148 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005149 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005150 return err;
5151}
5152
Eric Sandeen3972f262013-01-12 02:57:22 +00005153static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005154{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005155 struct btrfs_root *root = BTRFS_I(inode)->root;
5156 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005157 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005158 loff_t newsize = attr->ia_size;
5159 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005160 int ret;
5161
Eric Sandeen3972f262013-01-12 02:57:22 +00005162 /*
5163 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5164 * special case where we need to update the times despite not having
5165 * these flags set. For all other operations the VFS set these flags
5166 * explicitly if it wants a timestamp update.
5167 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005168 if (newsize != oldsize) {
5169 inode_inc_iversion(inode);
5170 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5171 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005172 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005173 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005174
Josef Bacika41ad392011-01-31 15:30:16 -05005175 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005176 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005177 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005178 * This is to ensure the snapshot captures a fully consistent
5179 * state of this file - if the snapshot captures this expanding
5180 * truncation, it must capture all writes that happened before
5181 * this truncation.
5182 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005183 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005184 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005185 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005186 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005187 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005188 }
Yan, Zheng80825102009-11-12 09:35:36 +00005189
Miao Xief4a2f4c2011-12-14 20:12:01 -05005190 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005191 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005192 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005193 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005194 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005195
5196 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005197 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305198 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005199 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005200 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005201 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005202 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5204
5205 if (btrfs_is_zoned(fs_info)) {
5206 ret = btrfs_wait_ordered_range(inode,
5207 ALIGN(newsize, fs_info->sectorsize),
5208 (u64)-1);
5209 if (ret)
5210 return ret;
5211 }
Yan, Zheng80825102009-11-12 09:35:36 +00005212
Josef Bacika41ad392011-01-31 15:30:16 -05005213 /*
5214 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005215 * zero. Make sure any new writes to the file get on disk
5216 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005217 */
5218 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005219 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005220 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005221
Josef Bacika41ad392011-01-31 15:30:16 -05005222 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005223
Miao Xie2e60a512013-02-08 07:01:08 +00005224 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005225
Filipe Manana213e8c52018-02-06 20:40:31 +00005226 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005227 if (ret && inode->i_nlink) {
5228 int err;
5229
5230 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005231 * Truncate failed, so fix up the in-memory size. We
5232 * adjusted disk_i_size down as we removed extents, so
5233 * wait for disk_i_size to be stable and then update the
5234 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005235 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005236 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005237 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005238 return err;
5239 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005240 }
Yan, Zheng80825102009-11-12 09:35:36 +00005241 }
5242
Josef Bacika41ad392011-01-31 15:30:16 -05005243 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005244}
5245
Christian Brauner549c7292021-01-21 14:19:43 +01005246static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5247 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005248{
David Howells2b0143b2015-03-17 22:25:59 +00005249 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005250 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005251 int err;
5252
Li Zefanb83cc962010-12-20 16:04:08 +08005253 if (btrfs_root_readonly(root))
5254 return -EROFS;
5255
Christian Brauner2f221d62021-01-21 14:19:26 +01005256 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005257 if (err)
5258 return err;
5259
Chris Mason5a3f23d2009-03-31 13:27:11 -04005260 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005261 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005262 if (err)
5263 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005264 }
Yan Zheng9036c102008-10-30 14:19:41 -04005265
Christoph Hellwig10257742010-06-04 11:30:02 +02005266 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005267 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005268 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005269 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005270
Josef Bacik22c44fe2011-11-30 10:45:38 -05005271 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005272 err = posix_acl_chmod(&init_user_ns, inode,
5273 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005274 }
5275
Chris Mason39279cc2007-06-12 06:35:45 -04005276 return err;
5277}
Chris Mason61295eb2008-01-14 16:24:38 -05005278
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279/*
5280 * While truncating the inode pages during eviction, we get the VFS calling
5281 * btrfs_invalidatepage() against each page of the inode. This is slow because
5282 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5283 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5284 * extent_state structures over and over, wasting lots of time.
5285 *
5286 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5287 * those expensive operations on a per page basis and do only the ordered io
5288 * finishing, while we release here the extent_map and extent_state structures,
5289 * without the excessive merging and splitting.
5290 */
5291static void evict_inode_truncate_pages(struct inode *inode)
5292{
5293 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5294 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5295 struct rb_node *node;
5296
5297 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005298 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005299
5300 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005301 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005302 struct extent_map *em;
5303
Liu Bo07e1ce02018-08-23 03:51:52 +08005304 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005305 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005306 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5307 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005308 remove_extent_mapping(map_tree, em);
5309 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005310 if (need_resched()) {
5311 write_unlock(&map_tree->lock);
5312 cond_resched();
5313 write_lock(&map_tree->lock);
5314 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005315 }
5316 write_unlock(&map_tree->lock);
5317
Filipe Manana6ca07092015-05-26 00:55:42 +01005318 /*
5319 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005320 * We can have ongoing bios started by readahead that have
5321 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005322 * still in progress (unlocked the pages in the bio but did not yet
5323 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005324 * ranges can still be locked and eviction started because before
5325 * submitting those bios, which are executed by a separate task (work
5326 * queue kthread), inode references (inode->i_count) were not taken
5327 * (which would be dropped in the end io callback of each bio).
5328 * Therefore here we effectively end up waiting for those bios and
5329 * anyone else holding locked ranges without having bumped the inode's
5330 * reference count - if we don't do it, when they access the inode's
5331 * io_tree to unlock a range it may be too late, leading to an
5332 * use-after-free issue.
5333 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005334 spin_lock(&io_tree->lock);
5335 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5336 struct extent_state *state;
5337 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005338 u64 start;
5339 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005340 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005341
5342 node = rb_first(&io_tree->state);
5343 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005344 start = state->start;
5345 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005346 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005347 spin_unlock(&io_tree->lock);
5348
David Sterbaff13db42015-12-03 14:30:40 +01005349 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005350
5351 /*
5352 * If still has DELALLOC flag, the extent didn't reach disk,
5353 * and its reserved space won't be freed by delayed_ref.
5354 * So we need to free its reserved space here.
5355 * (Refer to comment in btrfs_invalidatepage, case 2)
5356 *
5357 * Note, end is the bytenr of last byte, so we need + 1 here.
5358 */
Filipe Manana421f0922018-10-12 13:02:48 +01005359 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005360 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5361 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005362
Filipe Manana6ca07092015-05-26 00:55:42 +01005363 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005364 EXTENT_LOCKED | EXTENT_DELALLOC |
5365 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5366 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005367
Filipe Manana7064dd52014-08-08 02:47:05 +01005368 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005369 spin_lock(&io_tree->lock);
5370 }
5371 spin_unlock(&io_tree->lock);
5372}
5373
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005374static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005375 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005376{
5377 struct btrfs_fs_info *fs_info = root->fs_info;
5378 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005379 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005380 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005381 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005382
Josef Bacikd3984c92019-08-01 18:19:37 -04005383 /*
5384 * Eviction should be taking place at some place safe because of our
5385 * delayed iputs. However the normal flushing code will run delayed
5386 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5387 *
5388 * We reserve the delayed_refs_extra here again because we can't use
5389 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5390 * above. We reserve our extra bit here because we generate a ton of
5391 * delayed refs activity by truncating.
5392 *
5393 * If we cannot make our reservation we'll attempt to steal from the
5394 * global reserve, because we really want to be able to free up space.
5395 */
5396 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5397 BTRFS_RESERVE_FLUSH_EVICT);
5398 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005399 /*
5400 * Try to steal from the global reserve if there is space for
5401 * it.
5402 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005403 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5404 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5405 btrfs_warn(fs_info,
5406 "could not allocate space for delete; will truncate on mount");
5407 return ERR_PTR(-ENOSPC);
5408 }
5409 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005410 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005411
5412 trans = btrfs_join_transaction(root);
5413 if (IS_ERR(trans))
5414 return trans;
5415
5416 if (delayed_refs_extra) {
5417 trans->block_rsv = &fs_info->trans_block_rsv;
5418 trans->bytes_reserved = delayed_refs_extra;
5419 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5420 delayed_refs_extra, 1);
5421 }
5422 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005423}
5424
Al Virobd555972010-06-07 11:35:40 -04005425void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005426{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005427 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005428 struct btrfs_trans_handle *trans;
5429 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005430 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005431 int ret;
5432
liubo1abe9b82011-03-24 11:18:59 +00005433 trace_btrfs_inode_evict(inode);
5434
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005435 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005436 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005437 return;
5438 }
5439
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005440 evict_inode_truncate_pages(inode);
5441
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005442 if (inode->i_nlink &&
5443 ((btrfs_root_refs(&root->root_item) != 0 &&
5444 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005445 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005446 goto no_delete;
5447
Omar Sandoval27919062018-05-11 13:13:37 -07005448 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005449 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005450
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005451 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005452
Omar Sandoval7b40b692018-05-11 13:13:33 -07005453 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005454 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005455
Yan, Zheng76dda932009-09-21 16:00:26 -04005456 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005457 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5458 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005459 goto no_delete;
5460 }
5461
Nikolay Borisovaa790212017-01-10 20:35:40 +02005462 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005463 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005464 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005466 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005467 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005468 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005469 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005470 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005471
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005472 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005473
Yan, Zheng80825102009-11-12 09:35:36 +00005474 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005475 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005476 if (IS_ERR(trans))
5477 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005478
5479 trans->block_rsv = rsv;
5480
Nikolay Borisov50743392020-11-02 16:48:55 +02005481 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005482 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005483 trans->block_rsv = &fs_info->trans_block_rsv;
5484 btrfs_end_transaction(trans);
5485 btrfs_btree_balance_dirty(fs_info);
5486 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5487 goto free_rsv;
5488 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005489 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005490 }
5491
Josef Bacik4ef31a42013-08-13 14:10:08 -04005492 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005493 * Errors here aren't a big deal, it just means we leave orphan items in
5494 * the tree. They will be cleaned up on the next mount. If the inode
5495 * number gets reused, cleanup deletes the orphan item without doing
5496 * anything, and unlink reuses the existing orphan item.
5497 *
5498 * If it turns out that we are dropping too many of these, we might want
5499 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005500 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005501 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005502 if (!IS_ERR(trans)) {
5503 trans->block_rsv = rsv;
5504 btrfs_orphan_del(trans, BTRFS_I(inode));
5505 trans->block_rsv = &fs_info->trans_block_rsv;
5506 btrfs_end_transaction(trans);
5507 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005508
Omar Sandoval27919062018-05-11 13:13:37 -07005509free_rsv:
5510 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005511no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005512 /*
5513 * If we didn't successfully delete, the orphan item will still be in
5514 * the tree and we'll retry on the next mount. Again, we might also want
5515 * to retry these periodically in the future.
5516 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005517 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005518 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005519}
5520
5521/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005522 * Return the key found in the dir entry in the location pointer, fill @type
5523 * with BTRFS_FT_*, and return 0.
5524 *
Su Yue005d6712018-03-05 17:13:37 +08005525 * If no dir entries were found, returns -ENOENT.
5526 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005527 */
5528static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005529 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005530{
5531 const char *name = dentry->d_name.name;
5532 int namelen = dentry->d_name.len;
5533 struct btrfs_dir_item *di;
5534 struct btrfs_path *path;
5535 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005536 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005537
5538 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005539 if (!path)
5540 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005541
David Sterbaf85b7372017-01-20 14:54:07 +01005542 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5543 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005544 if (IS_ERR_OR_NULL(di)) {
5545 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005546 goto out;
5547 }
Chris Masond3977122009-01-05 21:25:51 -05005548
Chris Mason5f39d392007-10-15 16:14:19 -04005549 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005550 if (location->type != BTRFS_INODE_ITEM_KEY &&
5551 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005552 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005553 btrfs_warn(root->fs_info,
5554"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5555 __func__, name, btrfs_ino(BTRFS_I(dir)),
5556 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005557 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005558 if (!ret)
5559 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005560out:
Chris Mason39279cc2007-06-12 06:35:45 -04005561 btrfs_free_path(path);
5562 return ret;
5563}
5564
5565/*
5566 * when we hit a tree root in a directory, the btrfs part of the inode
5567 * needs to be changed to reflect the root directory of the tree root. This
5568 * is kind of like crossing a mount point.
5569 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005570static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571 struct inode *dir,
5572 struct dentry *dentry,
5573 struct btrfs_key *location,
5574 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005575{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005576 struct btrfs_path *path;
5577 struct btrfs_root *new_root;
5578 struct btrfs_root_ref *ref;
5579 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005580 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005581 int ret;
5582 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005583
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005584 path = btrfs_alloc_path();
5585 if (!path) {
5586 err = -ENOMEM;
5587 goto out;
5588 }
Chris Mason39279cc2007-06-12 06:35:45 -04005589
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005590 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005591 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5592 key.type = BTRFS_ROOT_REF_KEY;
5593 key.offset = location->objectid;
5594
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005595 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005596 if (ret) {
5597 if (ret < 0)
5598 err = ret;
5599 goto out;
5600 }
Chris Mason39279cc2007-06-12 06:35:45 -04005601
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005602 leaf = path->nodes[0];
5603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005604 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005605 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5606 goto out;
5607
5608 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5609 (unsigned long)(ref + 1),
5610 dentry->d_name.len);
5611 if (ret)
5612 goto out;
5613
David Sterbab3b4aa72011-04-21 01:20:15 +02005614 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005615
David Sterba56e93572020-05-15 19:35:55 +02005616 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005617 if (IS_ERR(new_root)) {
5618 err = PTR_ERR(new_root);
5619 goto out;
5620 }
5621
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622 *sub_root = new_root;
5623 location->objectid = btrfs_root_dirid(&new_root->root_item);
5624 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005625 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626 err = 0;
5627out:
5628 btrfs_free_path(path);
5629 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005630}
5631
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005632static void inode_tree_add(struct inode *inode)
5633{
5634 struct btrfs_root *root = BTRFS_I(inode)->root;
5635 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005636 struct rb_node **p;
5637 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005638 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005639 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640
Al Viro1d3382cb2010-10-23 15:19:20 -04005641 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005642 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005643 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005645 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 while (*p) {
5647 parent = *p;
5648 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5649
David Sterba37508512018-06-29 10:56:40 +02005650 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005651 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005652 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005653 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 else {
5655 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005656 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005657 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005658 RB_CLEAR_NODE(parent);
5659 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005660 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005661 }
5662 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005663 rb_link_node(new, parent, p);
5664 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005665 spin_unlock(&root->inode_lock);
5666}
5667
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005668static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005669{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005670 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005671 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005672
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005673 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005674 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5675 rb_erase(&inode->rb_node, &root->inode_tree);
5676 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005677 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005679 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005680
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005681 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005682 spin_lock(&root->inode_lock);
5683 empty = RB_EMPTY_ROOT(&root->inode_tree);
5684 spin_unlock(&root->inode_lock);
5685 if (empty)
5686 btrfs_add_dead_root(root);
5687 }
5688}
5689
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005690
Chris Masone02119d2008-09-05 16:13:11 -04005691static int btrfs_init_locked_inode(struct inode *inode, void *p)
5692{
5693 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005694
5695 inode->i_ino = args->ino;
5696 BTRFS_I(inode)->location.objectid = args->ino;
5697 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5698 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005699 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5700 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005701 return 0;
5702}
5703
5704static int btrfs_find_actor(struct inode *inode, void *opaque)
5705{
5706 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005707
5708 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005709 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005710}
5711
David Sterba0202e832020-05-15 19:35:59 +02005712static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005713 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005714{
5715 struct inode *inode;
5716 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005717 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005718
David Sterba0202e832020-05-15 19:35:59 +02005719 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005720 args.root = root;
5721
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005722 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005723 btrfs_init_locked_inode,
5724 (void *)&args);
5725 return inode;
5726}
5727
David Sterba4c66e0d2019-10-03 19:09:35 +02005728/*
David Sterba0202e832020-05-15 19:35:59 +02005729 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005730 * Path can be preallocated to prevent recursing back to iget through
5731 * allocator. NULL is also valid but may require an additional allocation
5732 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305733 */
David Sterba0202e832020-05-15 19:35:59 +02005734struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005735 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305736{
5737 struct inode *inode;
5738
David Sterba0202e832020-05-15 19:35:59 +02005739 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305740 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005741 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305742
5743 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005744 int ret;
5745
Filipe Manana4222ea72018-10-24 10:13:03 +01005746 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005747 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005748 inode_tree_add(inode);
5749 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005750 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005751 iget_failed(inode);
5752 /*
5753 * ret > 0 can come from btrfs_search_slot called by
5754 * btrfs_read_locked_inode, this means the inode item
5755 * was not found.
5756 */
5757 if (ret > 0)
5758 ret = -ENOENT;
5759 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005760 }
5761 }
5762
Balaji Rao1a54ef82008-07-21 02:01:04 +05305763 return inode;
5764}
5765
David Sterba0202e832020-05-15 19:35:59 +02005766struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005767{
David Sterba0202e832020-05-15 19:35:59 +02005768 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005769}
5770
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771static struct inode *new_simple_dir(struct super_block *s,
5772 struct btrfs_key *key,
5773 struct btrfs_root *root)
5774{
5775 struct inode *inode = new_inode(s);
5776
5777 if (!inode)
5778 return ERR_PTR(-ENOMEM);
5779
Josef Bacik5c8fd992020-02-14 16:11:43 -05005780 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005781 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005782 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005783
5784 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005785 /*
5786 * We only need lookup, the rest is read-only and there's no inode
5787 * associated with the dentry
5788 */
5789 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005790 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791 inode->i_fop = &simple_dir_operations;
5792 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005793 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305794 inode->i_atime = inode->i_mtime;
5795 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005796 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005797
5798 return inode;
5799}
5800
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005801static inline u8 btrfs_inode_type(struct inode *inode)
5802{
5803 /*
5804 * Compile-time asserts that generic FT_* types still match
5805 * BTRFS_FT_* types
5806 */
5807 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5808 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5809 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5810 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5811 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5812 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5813 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5814 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5815
5816 return fs_umode_to_ftype(inode->i_mode);
5817}
5818
Chris Mason3de45862008-11-17 21:02:50 -05005819struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005820{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005821 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005822 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005823 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 struct btrfs_root *sub_root = root;
5825 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005826 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005827 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005828
5829 if (dentry->d_name.len > BTRFS_NAME_LEN)
5830 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005832 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005833 if (ret < 0)
5834 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005835
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005836 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005837 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005838 if (IS_ERR(inode))
5839 return inode;
5840
5841 /* Do extra check against inode mode with di_type */
5842 if (btrfs_inode_type(inode) != di_type) {
5843 btrfs_crit(fs_info,
5844"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5845 inode->i_mode, btrfs_inode_type(inode),
5846 di_type);
5847 iput(inode);
5848 return ERR_PTR(-EUCLEAN);
5849 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005850 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005852
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005853 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005854 &location, &sub_root);
5855 if (ret < 0) {
5856 if (ret != -ENOENT)
5857 inode = ERR_PTR(ret);
5858 else
5859 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5860 } else {
David Sterba0202e832020-05-15 19:35:59 +02005861 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005862 }
Josef Bacik87270022020-01-24 09:32:31 -05005863 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005864 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005865
Julia Lawall34d19ba2011-01-24 19:55:19 +00005866 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005867 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005868 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005869 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005870 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005871 if (ret) {
5872 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005873 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005874 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005875 }
5876
Chris Mason3de45862008-11-17 21:02:50 -05005877 return inode;
5878}
5879
Nick Pigginfe15ce42011-01-07 17:49:23 +11005880static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005881{
5882 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005883 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005884
Li Zefan848cce02012-02-21 17:04:28 +08005885 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005886 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005887
Li Zefan848cce02012-02-21 17:04:28 +08005888 if (inode) {
5889 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005890 if (btrfs_root_refs(&root->root_item) == 0)
5891 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005892
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005893 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005894 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005895 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005896 return 0;
5897}
5898
Chris Mason3de45862008-11-17 21:02:50 -05005899static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005900 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005901{
Al Viro3837d202018-10-10 16:38:27 -04005902 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005903
Al Viro3837d202018-10-10 16:38:27 -04005904 if (inode == ERR_PTR(-ENOENT))
5905 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005906 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005907}
5908
Josef Bacik23b5ec72017-07-24 15:14:25 -04005909/*
5910 * All this infrastructure exists because dir_emit can fault, and we are holding
5911 * the tree lock when doing readdir. For now just allocate a buffer and copy
5912 * our information into that, and then dir_emit from the buffer. This is
5913 * similar to what NFS does, only we don't keep the buffer around in pagecache
5914 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5915 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5916 * tree lock.
5917 */
5918static int btrfs_opendir(struct inode *inode, struct file *file)
5919{
5920 struct btrfs_file_private *private;
5921
5922 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5923 if (!private)
5924 return -ENOMEM;
5925 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5926 if (!private->filldir_buf) {
5927 kfree(private);
5928 return -ENOMEM;
5929 }
5930 file->private_data = private;
5931 return 0;
5932}
5933
5934struct dir_entry {
5935 u64 ino;
5936 u64 offset;
5937 unsigned type;
5938 int name_len;
5939};
5940
5941static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5942{
5943 while (entries--) {
5944 struct dir_entry *entry = addr;
5945 char *name = (char *)(entry + 1);
5946
David Sterba92d32172018-04-16 21:10:14 +02005947 ctx->pos = get_unaligned(&entry->offset);
5948 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5949 get_unaligned(&entry->ino),
5950 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005951 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005952 addr += sizeof(struct dir_entry) +
5953 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005954 ctx->pos++;
5955 }
5956 return 0;
5957}
5958
Al Viro9cdda8d2013-05-22 16:48:09 -04005959static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005960{
Al Viro9cdda8d2013-05-22 16:48:09 -04005961 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005963 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005964 struct btrfs_dir_item *di;
5965 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005966 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005967 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005968 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005969 struct list_head ins_list;
5970 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005971 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005972 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005974 char *name_ptr;
5975 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005976 int entries = 0;
5977 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005978 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005980
Al Viro9cdda8d2013-05-22 16:48:09 -04005981 if (!dir_emit_dots(file, ctx))
5982 return 0;
5983
David Woodhouse49593bf2008-08-17 17:08:36 +01005984 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005985 if (!path)
5986 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005987
Josef Bacik23b5ec72017-07-24 15:14:25 -04005988 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005989 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005990
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005991 INIT_LIST_HEAD(&ins_list);
5992 INIT_LIST_HEAD(&del_list);
5993 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005994
Josef Bacik23b5ec72017-07-24 15:14:25 -04005995again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005996 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005997 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005998 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005999
Chris Mason39279cc2007-06-12 06:35:45 -04006000 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6001 if (ret < 0)
6002 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006003
6004 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006005 struct dir_entry *entry;
6006
Chris Mason5f39d392007-10-15 16:14:19 -04006007 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006008 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006009 if (slot >= btrfs_header_nritems(leaf)) {
6010 ret = btrfs_next_leaf(root, path);
6011 if (ret < 0)
6012 goto err;
6013 else if (ret > 0)
6014 break;
6015 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006016 }
Chris Mason3de45862008-11-17 21:02:50 -05006017
Chris Mason5f39d392007-10-15 16:14:19 -04006018 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6019
6020 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006021 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006022 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006023 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006024 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006025 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006026 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006027 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006028 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006029 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006030 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6031 PAGE_SIZE) {
6032 btrfs_release_path(path);
6033 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6034 if (ret)
6035 goto nopos;
6036 addr = private->filldir_buf;
6037 entries = 0;
6038 total_len = 0;
6039 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006040 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006041
6042 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006043 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006044 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006045 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6046 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006047 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006048 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006049 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006050 put_unaligned(location.objectid, &entry->ino);
6051 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006052 entries++;
6053 addr += sizeof(struct dir_entry) + name_len;
6054 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006055next:
6056 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006057 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006058 btrfs_release_path(path);
6059
6060 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6061 if (ret)
6062 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006063
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006064 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006065 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006066 goto nopos;
6067
Zach Browndb62efb2013-07-11 16:19:42 -07006068 /*
6069 * Stop new entries from being returned after we return the last
6070 * entry.
6071 *
6072 * New directory entries are assigned a strictly increasing
6073 * offset. This means that new entries created during readdir
6074 * are *guaranteed* to be seen in the future by that readdir.
6075 * This has broken buggy programs which operate on names as
6076 * they're returned by readdir. Until we re-use freed offsets
6077 * we have this hack to stop new entries from being returned
6078 * under the assumption that they'll never reach this huge
6079 * offset.
6080 *
6081 * This is being careful not to overflow 32bit loff_t unless the
6082 * last entry requires it because doing so has broken 32bit apps
6083 * in the past.
6084 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006085 if (ctx->pos >= INT_MAX)
6086 ctx->pos = LLONG_MAX;
6087 else
6088 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006089nopos:
6090 ret = 0;
6091err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006092 if (put)
6093 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006094 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006095 return ret;
6096}
6097
Chris Mason39279cc2007-06-12 06:35:45 -04006098/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006099 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006100 * inode changes. But, it is most likely to find the inode in cache.
6101 * FIXME, needs more benchmarking...there are no reasons other than performance
6102 * to keep or drop this code.
6103 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006104static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006105{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006107 struct btrfs_root *root = BTRFS_I(inode)->root;
6108 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006109 int ret;
6110
Josef Bacik72ac3c02012-05-23 14:13:11 -04006111 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006112 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006113
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006114 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115 if (IS_ERR(trans))
6116 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006117
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006118 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006119 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006120 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006121 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006122 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006123 if (IS_ERR(trans))
6124 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006125
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006126 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006127 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006128 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006129 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006130 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006131
6132 return ret;
6133}
6134
6135/*
6136 * This is a copy of file_update_time. We need this so we can return error on
6137 * ENOSPC for updating the inode in the case of file write and mmap writes.
6138 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006139static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006140 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006141{
Alexander Block2bc5565282012-06-15 09:49:33 +02006142 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006143 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006144
6145 if (btrfs_root_readonly(root))
6146 return -EROFS;
6147
Josef Bacike41f9412012-03-26 09:46:47 -04006148 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006149 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006150 if (flags & S_CTIME)
6151 inode->i_ctime = *now;
6152 if (flags & S_MTIME)
6153 inode->i_mtime = *now;
6154 if (flags & S_ATIME)
6155 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006156 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006157}
6158
Chris Masond352ac62008-09-29 15:18:18 -04006159/*
6160 * find the highest existing sequence number in a directory
6161 * and then set the in-memory index_cnt variable to reflect
6162 * free sequence numbers
6163 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006164static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006165{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006167 struct btrfs_key key, found_key;
6168 struct btrfs_path *path;
6169 struct extent_buffer *leaf;
6170 int ret;
6171
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006172 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006173 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006174 key.offset = (u64)-1;
6175
6176 path = btrfs_alloc_path();
6177 if (!path)
6178 return -ENOMEM;
6179
6180 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6181 if (ret < 0)
6182 goto out;
6183 /* FIXME: we should be able to handle this */
6184 if (ret == 0)
6185 goto out;
6186 ret = 0;
6187
6188 /*
6189 * MAGIC NUMBER EXPLANATION:
6190 * since we search a directory based on f_pos we have to start at 2
6191 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6192 * else has to start at 2
6193 */
6194 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006195 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006196 goto out;
6197 }
6198
6199 path->slots[0]--;
6200
6201 leaf = path->nodes[0];
6202 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6203
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006204 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006205 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006206 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006207 goto out;
6208 }
6209
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006210 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006211out:
6212 btrfs_free_path(path);
6213 return ret;
6214}
6215
Chris Masond352ac62008-09-29 15:18:18 -04006216/*
6217 * helper to find a free sequence number in a given directory. This current
6218 * code is very simple, later versions will do smarter things in the btree
6219 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006220int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006221{
6222 int ret = 0;
6223
Nikolay Borisov877574e2017-02-20 13:50:33 +02006224 if (dir->index_cnt == (u64)-1) {
6225 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006226 if (ret) {
6227 ret = btrfs_set_inode_index_count(dir);
6228 if (ret)
6229 return ret;
6230 }
Josef Bacikaec74772008-07-24 12:12:38 -04006231 }
6232
Nikolay Borisov877574e2017-02-20 13:50:33 +02006233 *index = dir->index_cnt;
6234 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006235
6236 return ret;
6237}
6238
Chris Masonb0d5d102014-09-08 13:08:51 -07006239static int btrfs_insert_inode_locked(struct inode *inode)
6240{
6241 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006242
6243 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006244 args.root = BTRFS_I(inode)->root;
6245
6246 return insert_inode_locked4(inode,
6247 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6248 btrfs_find_actor, &args);
6249}
6250
Anand Jain19aee8d2017-07-18 17:37:05 +08006251/*
6252 * Inherit flags from the parent inode.
6253 *
6254 * Currently only the compression flags and the cow flags are inherited.
6255 */
6256static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6257{
6258 unsigned int flags;
6259
6260 if (!dir)
6261 return;
6262
6263 flags = BTRFS_I(dir)->flags;
6264
6265 if (flags & BTRFS_INODE_NOCOMPRESS) {
6266 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6267 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6268 } else if (flags & BTRFS_INODE_COMPRESS) {
6269 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6270 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6271 }
6272
6273 if (flags & BTRFS_INODE_NODATACOW) {
6274 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6275 if (S_ISREG(inode->i_mode))
6276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6277 }
6278
David Sterba7b6a2212018-03-26 18:40:21 +02006279 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006280}
6281
Chris Mason39279cc2007-06-12 06:35:45 -04006282static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6283 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006284 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006285 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006286 u64 ref_objectid, u64 objectid,
6287 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006289 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006290 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006291 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006292 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006293 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006294 struct btrfs_inode_ref *ref;
6295 struct btrfs_key key[2];
6296 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006297 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006298 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006299 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006300 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006301
Chris Mason5f39d392007-10-15 16:14:19 -04006302 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006303 if (!path)
6304 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006305
Josef Bacik11a19a92019-09-09 10:12:04 -04006306 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006307 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006308 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006309 if (!inode) {
6310 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006312 }
Chris Mason39279cc2007-06-12 06:35:45 -04006313
Li Zefan581bb052011-04-20 10:06:11 +08006314 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006315 * O_TMPFILE, set link count to 0, so that after this point,
6316 * we fill in an inode item with the correct link count.
6317 */
6318 if (!name)
6319 set_nlink(inode, 0);
6320
6321 /*
Li Zefan581bb052011-04-20 10:06:11 +08006322 * we have to initialize this early, so we can reclaim the inode
6323 * number if we fail afterwards in this function.
6324 */
6325 inode->i_ino = objectid;
6326
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006327 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006328 trace_btrfs_inode_request(dir);
6329
Nikolay Borisov877574e2017-02-20 13:50:33 +02006330 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006331 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006332 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006333 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006334 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006335 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006336 } else if (dir) {
6337 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006338 }
6339 /*
6340 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006341 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006342 * number
6343 */
6344 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006345 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006346 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006347 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006348 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006349
Josef Bacik5dc562c2012-08-17 13:14:17 -04006350 /*
6351 * We could have gotten an inode number from somebody who was fsynced
6352 * and then removed in this same transaction, so let's just set full
6353 * sync since it will be a full sync anyway and this will blow away the
6354 * old info in the log.
6355 */
6356 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6357
Chris Mason9c583092008-01-29 15:15:18 -05006358 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006359 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006360 key[0].offset = 0;
6361
Chris Mason9c583092008-01-29 15:15:18 -05006362 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006363
6364 if (name) {
6365 /*
6366 * Start new inodes with an inode_ref. This is slightly more
6367 * efficient for small numbers of hard links since they will
6368 * be packed into one item. Extended refs will kick in if we
6369 * add more hard links than can fit in the ref item.
6370 */
6371 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006372 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006373 key[1].offset = ref_objectid;
6374
6375 sizes[1] = name_len + sizeof(*ref);
6376 }
Chris Mason9c583092008-01-29 15:15:18 -05006377
Chris Masonb0d5d102014-09-08 13:08:51 -07006378 location = &BTRFS_I(inode)->location;
6379 location->objectid = objectid;
6380 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006381 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006382
6383 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006384 if (ret < 0) {
6385 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006386 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006387 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006388
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006389 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006390 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006391 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006392
Christian Brauner21cb47b2021-01-21 14:19:25 +01006393 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006394 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306395
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006396 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306397 inode->i_atime = inode->i_mtime;
6398 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006399 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306400
Chris Mason5f39d392007-10-15 16:14:19 -04006401 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6402 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006403 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006404 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006405 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006406
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006407 if (name) {
6408 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6409 struct btrfs_inode_ref);
6410 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6411 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6412 ptr = (unsigned long)(ref + 1);
6413 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6414 }
Chris Mason9c583092008-01-29 15:15:18 -05006415
Chris Mason5f39d392007-10-15 16:14:19 -04006416 btrfs_mark_buffer_dirty(path->nodes[0]);
6417 btrfs_free_path(path);
6418
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006419 btrfs_inherit_iflags(inode, dir);
6420
Al Viro569254b2011-07-24 17:08:40 -04006421 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006422 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006423 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006424 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006425 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6426 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006427 }
6428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006429 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006430
6431 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006432 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006433
Alexander Block8ea05e32012-07-25 17:35:53 +02006434 btrfs_update_root_times(trans, root);
6435
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006436 ret = btrfs_inode_inherit_props(trans, inode, dir);
6437 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006438 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006439 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006440 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006441
Chris Mason39279cc2007-06-12 06:35:45 -04006442 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443
6444fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006445 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006446fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006447 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006448 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006449 btrfs_free_path(path);
6450 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006451}
6452
Chris Masond352ac62008-09-29 15:18:18 -04006453/*
6454 * utility function to add 'inode' into 'parent_inode' with
6455 * a give name and a given sequence number.
6456 * if 'add_backref' is true, also insert a backref from the
6457 * inode to the parent directory.
6458 */
Chris Masone02119d2008-09-05 16:13:11 -04006459int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006460 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006461 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006462{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006464 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006465 struct btrfs_root *root = parent_inode->root;
6466 u64 ino = btrfs_ino(inode);
6467 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006468
Li Zefan33345d012011-04-20 10:31:50 +08006469 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006471 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006472 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006473 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006474 key.offset = 0;
6475 }
Chris Mason39279cc2007-06-12 06:35:45 -04006476
Li Zefan33345d012011-04-20 10:31:50 +08006477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006478 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 root->root_key.objectid, parent_ino,
6480 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006481 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006482 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6483 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006484 }
6485
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006486 /* Nothing to clean up yet */
6487 if (ret)
6488 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489
Lu Fengqi684572d2018-08-04 21:10:57 +08006490 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006491 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006492 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006493 goto fail_dir_item;
6494 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006495 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006496 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006497 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006499 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006502 /*
6503 * If we are replaying a log tree, we do not want to update the mtime
6504 * and ctime of the parent directory with the current time, since the
6505 * log replay procedure is responsible for setting them to their correct
6506 * values (the ones it had when the fsync was done).
6507 */
6508 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6509 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6510
6511 parent_inode->vfs_inode.i_mtime = now;
6512 parent_inode->vfs_inode.i_ctime = now;
6513 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006514 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006515 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006516 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006517 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006518
6519fail_dir_item:
6520 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6521 u64 local_index;
6522 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006523 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006524 root->root_key.objectid, parent_ino,
6525 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006526 if (err)
6527 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006528 } else if (add_backref) {
6529 u64 local_index;
6530 int err;
6531
6532 err = btrfs_del_inode_ref(trans, root, name, name_len,
6533 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006534 if (err)
6535 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006536 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006537
6538 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006539 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006540}
6541
6542static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006543 struct btrfs_inode *dir, struct dentry *dentry,
6544 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006545{
Josef Bacika1b075d2010-11-19 20:36:11 +00006546 int err = btrfs_add_link(trans, dir, inode,
6547 dentry->d_name.name, dentry->d_name.len,
6548 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 if (err > 0)
6550 err = -EEXIST;
6551 return err;
6552}
6553
Christian Brauner549c7292021-01-21 14:19:43 +01006554static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6555 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006556{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006558 struct btrfs_trans_handle *trans;
6559 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006560 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006561 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006562 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006563 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006564
Josef Bacik9ed74f22009-09-11 16:12:44 -04006565 /*
6566 * 2 for inode item and ref
6567 * 2 for dir items
6568 * 1 for xattr if selinux is on
6569 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 trans = btrfs_start_transaction(root, 5);
6571 if (IS_ERR(trans))
6572 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006573
Nikolay Borisov543068a2020-12-07 17:32:33 +02006574 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006575 if (err)
6576 goto out_unlock;
6577
Josef Bacikaec74772008-07-24 12:12:38 -04006578 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006579 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6580 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006581 if (IS_ERR(inode)) {
6582 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006583 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006585 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006586
Casey Schauflerad19db72011-12-15 10:09:07 -05006587 /*
6588 * If the active LSM wants to access the inode during
6589 * d_instantiate it needs these. Smack checks to see
6590 * if the filesystem supports xattrs by looking at the
6591 * ops vector.
6592 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006593 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006594 init_special_inode(inode, inode->i_mode, rdev);
6595
6596 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006597 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006598 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006599
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006600 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6601 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006602 if (err)
6603 goto out_unlock;
6604
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006605 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006606 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006607
Josef Bacik618e21d2007-07-11 10:18:17 -04006608out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006609 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006610 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006611 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006612 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006613 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006614 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006615 return err;
6616}
6617
Christian Brauner549c7292021-01-21 14:19:43 +01006618static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6619 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006620{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006621 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006622 struct btrfs_trans_handle *trans;
6623 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006624 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006625 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006626 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006627 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006628
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629 /*
6630 * 2 for inode item and ref
6631 * 2 for dir items
6632 * 1 for xattr if selinux is on
6633 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006634 trans = btrfs_start_transaction(root, 5);
6635 if (IS_ERR(trans))
6636 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637
Nikolay Borisov543068a2020-12-07 17:32:33 +02006638 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006639 if (err)
6640 goto out_unlock;
6641
Josef Bacikaec74772008-07-24 12:12:38 -04006642 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006643 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6644 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006645 if (IS_ERR(inode)) {
6646 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006647 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006648 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006649 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006650 /*
6651 * If the active LSM wants to access the inode during
6652 * d_instantiate it needs these. Smack checks to see
6653 * if the filesystem supports xattrs by looking at the
6654 * ops vector.
6655 */
6656 inode->i_fop = &btrfs_file_operations;
6657 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006658 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006659
6660 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6661 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006662 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006663
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006664 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006665 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006666 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006667
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006668 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6669 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006670 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006671 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006672
Al Viro1e2e5472018-05-04 08:23:01 -04006673 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006674
Chris Mason39279cc2007-06-12 06:35:45 -04006675out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006676 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006677 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006678 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006679 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006681 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006682 return err;
6683}
6684
6685static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6686 struct dentry *dentry)
6687{
Filipe Manana271dba452016-01-05 16:24:05 +00006688 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006689 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006690 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006691 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006692 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006693 int err;
6694 int drop_inode = 0;
6695
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006696 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006697 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006698 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006699
Mark Fashehf1863732012-08-08 11:32:27 -07006700 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006701 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006702
Nikolay Borisov877574e2017-02-20 13:50:33 +02006703 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006704 if (err)
6705 goto fail;
6706
Yan, Zhenga22285a2010-05-16 10:48:46 -04006707 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006708 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006709 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006710 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006711 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006713 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 if (IS_ERR(trans)) {
6715 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006716 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006717 goto fail;
6718 }
Chris Mason5f39d392007-10-15 16:14:19 -04006719
Miao Xie67de1172013-12-26 13:07:06 +08006720 /* There are several dir indexes for this inode, clear the cache. */
6721 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006722 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006723 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006724 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006725 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006726 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006727
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006728 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6729 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006730
Yan, Zhenga5719522009-09-24 09:17:31 -04006731 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006732 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006734 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006735
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006736 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006737 if (err)
6738 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006739 if (inode->i_nlink == 1) {
6740 /*
6741 * If new hard link count is 1, it's a file created
6742 * with open(2) O_TMPFILE flag.
6743 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006744 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006745 if (err)
6746 goto fail;
6747 }
Al Viro08c422c2011-12-23 07:58:13 -05006748 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006749 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006750 }
Chris Mason39279cc2007-06-12 06:35:45 -04006751
Chris Mason1832a6d2007-12-21 16:27:21 -05006752fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006753 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006754 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006755 if (drop_inode) {
6756 inode_dec_link_count(inode);
6757 iput(inode);
6758 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006759 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006760 return err;
6761}
6762
Christian Brauner549c7292021-01-21 14:19:43 +01006763static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6764 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006765{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006766 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006767 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006768 struct btrfs_trans_handle *trans;
6769 struct btrfs_root *root = BTRFS_I(dir)->root;
6770 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006771 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006772 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006773
Josef Bacik9ed74f22009-09-11 16:12:44 -04006774 /*
6775 * 2 items for inode and ref
6776 * 2 items for dir items
6777 * 1 for xattr if selinux is on
6778 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006779 trans = btrfs_start_transaction(root, 5);
6780 if (IS_ERR(trans))
6781 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006782
Nikolay Borisov543068a2020-12-07 17:32:33 +02006783 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006784 if (err)
6785 goto out_fail;
6786
Josef Bacikaec74772008-07-24 12:12:38 -04006787 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006788 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6789 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 if (IS_ERR(inode)) {
6791 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006792 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006793 goto out_fail;
6794 }
Chris Mason5f39d392007-10-15 16:14:19 -04006795
Chris Masonb0d5d102014-09-08 13:08:51 -07006796 /* these must be set before we unlock the inode */
6797 inode->i_op = &btrfs_dir_inode_operations;
6798 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006799
Eric Paris2a7dba32011-02-01 11:05:39 -05006800 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006801 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006802 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006803
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006804 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006805 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006806 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006807 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006808
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006809 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6810 dentry->d_name.name,
6811 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006812 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006813 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006814
Al Viro1e2e5472018-05-04 08:23:01 -04006815 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006816
6817out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006818 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006819 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006820 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006821 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006822 }
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
Chris Masonc8b97812008-10-29 14:49:59 -04006827static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006828 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006829 size_t pg_offset, u64 extent_offset,
6830 struct btrfs_file_extent_item *item)
6831{
6832 int ret;
6833 struct extent_buffer *leaf = path->nodes[0];
6834 char *tmp;
6835 size_t max_size;
6836 unsigned long inline_size;
6837 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006838 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006839
6840 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006841 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006842 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6843 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006844 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006845 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006846 if (!tmp)
6847 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006848 ptr = btrfs_file_extent_inline_start(item);
6849
6850 read_extent_buffer(leaf, tmp, ptr, inline_size);
6851
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006852 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006853 ret = btrfs_decompress(compress_type, tmp, page,
6854 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006855
6856 /*
6857 * decompression code contains a memset to fill in any space between the end
6858 * of the uncompressed data and the end of max_size in case the decompressed
6859 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6860 * the end of an inline extent and the beginning of the next block, so we
6861 * cover that region here.
6862 */
6863
Ira Weinyd048b9c2021-05-04 18:40:07 -07006864 if (max_size + pg_offset < PAGE_SIZE)
6865 memzero_page(page, pg_offset + max_size,
6866 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006867 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006868 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006869}
6870
Omar Sandoval39b07b52019-12-02 17:34:23 -08006871/**
6872 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6873 * @inode: file to search in
6874 * @page: page to read extent data into if the extent is inline
6875 * @pg_offset: offset into @page to copy to
6876 * @start: file offset
6877 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006878 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006879 * This returns the first &struct extent_map which overlaps with the given
6880 * range, reading it from the B-tree and caching it if necessary. Note that
6881 * there may be more extents which overlap the given range after the returned
6882 * extent_map.
6883 *
6884 * If @page is not NULL and the extent is inline, this also reads the extent
6885 * data directly into the page and marks the extent up to date in the io_tree.
6886 *
6887 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006888 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006889struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006890 struct page *page, size_t pg_offset,
6891 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006892{
David Sterba3ffbd682018-06-29 10:56:42 +02006893 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006894 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006895 u64 extent_start = 0;
6896 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006897 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006898 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006899 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006900 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006901 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006902 struct extent_buffer *leaf;
6903 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006904 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006905 struct extent_map_tree *em_tree = &inode->extent_tree;
6906 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006907
Chris Mason890871b2009-09-02 16:24:52 -04006908 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006909 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006910 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006911
Chris Masona52d9a82007-08-27 16:49:44 -04006912 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006913 if (em->start > start || em->start + em->len <= start)
6914 free_extent_map(em);
6915 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006916 free_extent_map(em);
6917 else
6918 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006919 }
David Sterba172ddd62011-04-21 00:48:27 +02006920 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006921 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006922 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006923 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 }
Chris Masond1310b22008-01-24 16:13:08 -05006925 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006926 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006927 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006928 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006929
Liu Bobee6ec82018-08-17 05:05:28 +08006930 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006931 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006932 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006933 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006934 }
6935
Liu Bobee6ec82018-08-17 05:05:28 +08006936 /* Chances are we'll be called again, so go ahead and do readahead */
6937 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006938
6939 /*
6940 * The same explanation in load_free_space_cache applies here as well,
6941 * we only read when we're loading the free space cache, and at that
6942 * point the commit_root has everything we need.
6943 */
6944 if (btrfs_is_free_space_inode(inode)) {
6945 path->search_commit_root = 1;
6946 path->skip_locking = 1;
6947 }
Josef Bacik51899412020-08-20 11:46:01 -04006948
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006949 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006950 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006951 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006952 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006953 if (path->slots[0] == 0)
6954 goto not_found;
6955 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006956 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006957 }
6958
Chris Mason5f39d392007-10-15 16:14:19 -04006959 leaf = path->nodes[0];
6960 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006961 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006962 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006963 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006964 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006965 /*
6966 * If we backup past the first extent we want to move forward
6967 * and see if there is an extent in front of us, otherwise we'll
6968 * say there is a hole for our whole search range which can
6969 * cause problems.
6970 */
6971 extent_end = start;
6972 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006973 }
6974
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006975 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006976 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006977 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006978 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6979 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006980 /* Only regular file could have regular/prealloc extent */
6981 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006982 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006983 btrfs_crit(fs_info,
6984 "regular/prealloc extent found for non-regular inode %llu",
6985 btrfs_ino(inode));
6986 goto out;
6987 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006988 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6989 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006990 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006991 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6992 path->slots[0],
6993 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006994 }
Josef Bacik25a50342013-10-14 12:08:38 -04006995next:
Yan Zheng9036c102008-10-30 14:19:41 -04006996 if (start >= extent_end) {
6997 path->slots[0]++;
6998 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6999 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007000 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007001 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007002 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007003 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007004
Yan Zheng9036c102008-10-30 14:19:41 -04007005 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007006 }
Yan Zheng9036c102008-10-30 14:19:41 -04007007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7008 if (found_key.objectid != objectid ||
7009 found_key.type != BTRFS_EXTENT_DATA_KEY)
7010 goto not_found;
7011 if (start + len <= found_key.offset)
7012 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007013 if (start > found_key.offset)
7014 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007015
7016 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007017 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007018 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007019 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007020 em->block_start = EXTENT_MAP_HOLE;
7021 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007022 }
7023
Omar Sandoval39b07b52019-12-02 17:34:23 -08007024 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007025
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007026 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7027 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007028 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007029 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007030 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007031 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007032 size_t size;
7033 size_t extent_offset;
7034 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007035
Omar Sandoval39b07b52019-12-02 17:34:23 -08007036 if (!page)
Yan689f9342007-10-29 11:41:07 -04007037 goto out;
Yan689f9342007-10-29 11:41:07 -04007038
Qu Wenruoe41ca582018-06-06 15:41:49 +08007039 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007040 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007041 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7042 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007043 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007044 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007045 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007046 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007047 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007048
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007049 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007050 if (btrfs_file_extent_compression(leaf, item) !=
7051 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007052 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007053 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007054 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007055 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007056 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007057 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007058 read_extent_buffer(leaf, map + pg_offset, ptr,
7059 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007060 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007061 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007062 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007063 copy_size);
7064 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007065 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007066 }
Chris Mason179e29e2007-11-01 11:28:41 -04007067 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007068 }
Chris Masond1310b22008-01-24 16:13:08 -05007069 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007070 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007071 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007072 }
7073not_found:
7074 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007075 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007076 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007077 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007078insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007079 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007080 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007081 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007082 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007083 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7084 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007085 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007086 goto out;
7087 }
Chris Masond1310b22008-01-24 16:13:08 -05007088
Chris Mason890871b2009-09-02 16:24:52 -04007089 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007090 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007091 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007092out:
Liu Boc6414282018-08-23 07:36:17 +08007093 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007094
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007095 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007096
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007097 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007098 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007099 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007100 }
7101 return em;
7102}
7103
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007104struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007105 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007106{
7107 struct extent_map *em;
7108 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007109 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007110 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007111 u64 delalloc_len;
7112 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007113 int err = 0;
7114
Omar Sandoval39b07b52019-12-02 17:34:23 -08007115 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007116 if (IS_ERR(em))
7117 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007118 /*
7119 * If our em maps to:
7120 * - a hole or
7121 * - a pre-alloc extent,
7122 * there might actually be delalloc bytes behind it.
7123 */
7124 if (em->block_start != EXTENT_MAP_HOLE &&
7125 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7126 return em;
7127 else
7128 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007129
7130 /* check to see if we've wrapped (len == -1 or similar) */
7131 end = start + len;
7132 if (end < start)
7133 end = (u64)-1;
7134 else
7135 end -= 1;
7136
7137 em = NULL;
7138
7139 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007140 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007141 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007142 delalloc_end = delalloc_start + delalloc_len;
7143 if (delalloc_end < delalloc_start)
7144 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007145
7146 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007147 * We didn't find anything useful, return the original results from
7148 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007149 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007150 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007151 em = hole_em;
7152 hole_em = NULL;
7153 goto out;
7154 }
7155
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007156 /*
7157 * Adjust the delalloc_start to make sure it doesn't go backwards from
7158 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007159 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007160 delalloc_start = max(start, delalloc_start);
7161 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007162
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007163 if (delalloc_len > 0) {
7164 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007165 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007166 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007167
David Sterba172ddd62011-04-21 00:48:27 +02007168 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007169 if (!em) {
7170 err = -ENOMEM;
7171 goto out;
7172 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007173
7174 ASSERT(hole_em);
7175 /*
7176 * When btrfs_get_extent can't find anything it returns one
7177 * huge hole
7178 *
7179 * Make sure what it found really fits our range, and adjust to
7180 * make sure it is based on the start from the caller
7181 */
7182 if (hole_end <= start || hole_em->start > end) {
7183 free_extent_map(hole_em);
7184 hole_em = NULL;
7185 } else {
7186 hole_start = max(hole_em->start, start);
7187 hole_len = hole_end - hole_start;
7188 }
7189
7190 if (hole_em && delalloc_start > hole_start) {
7191 /*
7192 * Our hole starts before our delalloc, so we have to
7193 * return just the parts of the hole that go until the
7194 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007195 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007196 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007197 em->start = hole_start;
7198 em->orig_start = hole_start;
7199 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007200 * Don't adjust block start at all, it is fixed at
7201 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007202 */
7203 em->block_start = hole_em->block_start;
7204 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007205 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7206 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007207 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007208 /*
7209 * Hole is out of passed range or it starts after
7210 * delalloc range
7211 */
7212 em->start = delalloc_start;
7213 em->len = delalloc_len;
7214 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007215 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007216 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007217 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007218 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007219 return hole_em;
7220 }
7221out:
7222
7223 free_extent_map(hole_em);
7224 if (err) {
7225 free_extent_map(em);
7226 return ERR_PTR(err);
7227 }
7228 return em;
7229}
7230
Nikolay Borisov64f54182020-06-03 08:55:31 +03007231static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007232 const u64 start,
7233 const u64 len,
7234 const u64 orig_start,
7235 const u64 block_start,
7236 const u64 block_len,
7237 const u64 orig_block_len,
7238 const u64 ram_bytes,
7239 const int type)
7240{
7241 struct extent_map *em = NULL;
7242 int ret;
7243
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007244 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007245 em = create_io_em(inode, start, len, orig_start, block_start,
7246 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007247 BTRFS_COMPRESS_NONE, /* compress_type */
7248 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007249 if (IS_ERR(em))
7250 goto out;
7251 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007252 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7253 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007254 if (ret) {
7255 if (em) {
7256 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007257 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007258 }
7259 em = ERR_PTR(ret);
7260 }
7261 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007262
7263 return em;
7264}
7265
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007266static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 u64 start, u64 len)
7268{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007269 struct btrfs_root *root = inode->root;
7270 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007271 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272 struct btrfs_key ins;
7273 u64 alloc_hint;
7274 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007276 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007277 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007278 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007279 if (ret)
7280 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007282 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007283 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007284 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007285 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007286 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007287 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7288 1);
Josef Bacik00361582013-08-14 14:02:47 -04007289
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290 return em;
7291}
7292
Anand Jainf4639632021-02-10 21:25:16 -08007293static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007294{
7295 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007296 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007297
7298 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7299 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007300 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007301 if (block_group)
7302 btrfs_put_block_group(block_group);
7303 return readonly;
7304}
7305
Chris Mason46bfbb52010-05-26 11:04:10 -04007306/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007307 * Check if we can do nocow write into the range [@offset, @offset + @len)
7308 *
7309 * @offset: File offset
7310 * @len: The length to write, will be updated to the nocow writeable
7311 * range
7312 * @orig_start: (optional) Return the original file offset of the file extent
7313 * @orig_len: (optional) Return the original on-disk length of the file extent
7314 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007315 * @strict: if true, omit optimizations that might force us into unnecessary
7316 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007317 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007318 * Return:
7319 * >0 and update @len if we can do nocow write
7320 * 0 if we can't do nocow write
7321 * <0 if error happened
7322 *
7323 * NOTE: This only checks the file extents, caller is responsible to wait for
7324 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007325 */
Josef Bacik00361582013-08-14 14:02:47 -04007326noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007327 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007328 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007329{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007331 struct btrfs_path *path;
7332 int ret;
7333 struct extent_buffer *leaf;
7334 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007335 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007336 struct btrfs_file_extent_item *fi;
7337 struct btrfs_key key;
7338 u64 disk_bytenr;
7339 u64 backref_offset;
7340 u64 extent_end;
7341 u64 num_bytes;
7342 int slot;
7343 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007344 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007345
Chris Mason46bfbb52010-05-26 11:04:10 -04007346 path = btrfs_alloc_path();
7347 if (!path)
7348 return -ENOMEM;
7349
David Sterbaf85b7372017-01-20 14:54:07 +01007350 ret = btrfs_lookup_file_extent(NULL, root, path,
7351 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007352 if (ret < 0)
7353 goto out;
7354
7355 slot = path->slots[0];
7356 if (ret == 1) {
7357 if (slot == 0) {
7358 /* can't find the item, must cow */
7359 ret = 0;
7360 goto out;
7361 }
7362 slot--;
7363 }
7364 ret = 0;
7365 leaf = path->nodes[0];
7366 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007367 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007368 key.type != BTRFS_EXTENT_DATA_KEY) {
7369 /* not our file or wrong item type, must cow */
7370 goto out;
7371 }
7372
7373 if (key.offset > offset) {
7374 /* Wrong offset, must cow */
7375 goto out;
7376 }
7377
7378 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7379 found_type = btrfs_file_extent_type(leaf, fi);
7380 if (found_type != BTRFS_FILE_EXTENT_REG &&
7381 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7382 /* not a regular extent, must cow */
7383 goto out;
7384 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007385
7386 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7387 goto out;
7388
Miao Xiee77751a2013-12-27 21:11:50 +08007389 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7390 if (extent_end <= offset)
7391 goto out;
7392
Chris Mason46bfbb52010-05-26 11:04:10 -04007393 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007394 if (disk_bytenr == 0)
7395 goto out;
7396
7397 if (btrfs_file_extent_compression(leaf, fi) ||
7398 btrfs_file_extent_encryption(leaf, fi) ||
7399 btrfs_file_extent_other_encoding(leaf, fi))
7400 goto out;
7401
Ethan Lien78d42952018-05-17 14:58:29 +08007402 /*
7403 * Do the same check as in btrfs_cross_ref_exist but without the
7404 * unnecessary search.
7405 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007406 if (!strict &&
7407 (btrfs_file_extent_generation(leaf, fi) <=
7408 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007409 goto out;
7410
Chris Mason46bfbb52010-05-26 11:04:10 -04007411 backref_offset = btrfs_file_extent_offset(leaf, fi);
7412
Josef Bacik7ee9e442013-06-21 16:37:03 -04007413 if (orig_start) {
7414 *orig_start = key.offset - backref_offset;
7415 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7416 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7417 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007418
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007419 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007420 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007421
7422 num_bytes = min(offset + *len, extent_end) - offset;
7423 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7424 u64 range_end;
7425
Jeff Mahoneyda170662016-06-15 09:22:56 -04007426 range_end = round_up(offset + num_bytes,
7427 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007428 ret = test_range_bit(io_tree, offset, range_end,
7429 EXTENT_DELALLOC, 0, NULL);
7430 if (ret) {
7431 ret = -EAGAIN;
7432 goto out;
7433 }
7434 }
7435
Josef Bacik1bda19e2013-10-18 12:10:36 -04007436 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007437
7438 /*
7439 * look for other files referencing this extent, if we
7440 * find any we must cow
7441 */
Josef Bacik00361582013-08-14 14:02:47 -04007442
Liu Boe4c3b2d2017-01-30 12:25:28 -08007443 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007444 key.offset - backref_offset, disk_bytenr,
7445 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007446 if (ret) {
7447 ret = 0;
7448 goto out;
7449 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007450
7451 /*
7452 * adjust disk_bytenr and num_bytes to cover just the bytes
7453 * in this extent we are about to write. If there
7454 * are any csums in that range we have to cow in order
7455 * to keep the csums correct
7456 */
7457 disk_bytenr += backref_offset;
7458 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007459 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7460 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007461 /*
7462 * all of the above have passed, it is safe to overwrite this extent
7463 * without cow
7464 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007465 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007466 ret = 1;
7467out:
7468 btrfs_free_path(path);
7469 return ret;
7470}
7471
Josef Bacikeb838e72012-07-31 16:28:48 -04007472static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007473 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007474{
7475 struct btrfs_ordered_extent *ordered;
7476 int ret = 0;
7477
7478 while (1) {
7479 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007480 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 /*
7482 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007483 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007484 * extents in this range.
7485 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007486 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 lockend - lockstart + 1);
7488
7489 /*
7490 * We need to make sure there are no buffered pages in this
7491 * range either, we could have raced between the invalidate in
7492 * generic_file_direct_write and locking the extent. The
7493 * invalidate needs to happen so that reads after a write do not
7494 * get stale data.
7495 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007496 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007497 (!writing || !filemap_range_has_page(inode->i_mapping,
7498 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007499 break;
7500
7501 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007502 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007503
7504 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007505 /*
7506 * If we are doing a DIO read and the ordered extent we
7507 * found is for a buffered write, we can not wait for it
7508 * to complete and retry, because if we do so we can
7509 * deadlock with concurrent buffered writes on page
7510 * locks. This happens only if our DIO read covers more
7511 * than one extent map, if at this point has already
7512 * created an ordered extent for a previous extent map
7513 * and locked its range in the inode's io tree, and a
7514 * concurrent write against that previous extent map's
7515 * range and this range started (we unlock the ranges
7516 * in the io tree only when the bios complete and
7517 * buffered writes always lock pages before attempting
7518 * to lock range in the io tree).
7519 */
7520 if (writing ||
7521 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007522 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007523 else
7524 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007525 btrfs_put_ordered_extent(ordered);
7526 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007527 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007528 * We could trigger writeback for this range (and wait
7529 * for it to complete) and then invalidate the pages for
7530 * this range (through invalidate_inode_pages2_range()),
7531 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007532 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007533 * triggered a readahead) on a page lock due to an
7534 * ordered dio extent we created before but did not have
7535 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007536 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007537 * ordered extent to complete while holding a lock on
7538 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007540 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 }
7542
Filipe Mananaade77022016-02-18 14:28:55 +00007543 if (ret)
7544 break;
7545
Josef Bacikeb838e72012-07-31 16:28:48 -04007546 cond_resched();
7547 }
7548
7549 return ret;
7550}
7551
Liu Bo6f9994d2017-01-31 07:50:22 -08007552/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007553static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7554 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007555 u64 block_len, u64 orig_block_len,
7556 u64 ram_bytes, int compress_type,
7557 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007558{
7559 struct extent_map_tree *em_tree;
7560 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007561 int ret;
7562
Liu Bo6f9994d2017-01-31 07:50:22 -08007563 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7564 type == BTRFS_ORDERED_COMPRESSED ||
7565 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007566 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007567
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007568 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007569 em = alloc_extent_map();
7570 if (!em)
7571 return ERR_PTR(-ENOMEM);
7572
7573 em->start = start;
7574 em->orig_start = orig_start;
7575 em->len = len;
7576 em->block_len = block_len;
7577 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007578 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007579 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007580 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007581 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007582 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007583 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007584 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007585 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7586 em->compress_type = compress_type;
7587 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007588
7589 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007590 btrfs_drop_extent_cache(inode, em->start,
7591 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007592 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007593 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007594 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007595 /*
7596 * The caller has taken lock_extent(), who could race with us
7597 * to add em?
7598 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007599 } while (ret == -EEXIST);
7600
7601 if (ret) {
7602 free_extent_map(em);
7603 return ERR_PTR(ret);
7604 }
7605
Liu Bo6f9994d2017-01-31 07:50:22 -08007606 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007607 return em;
7608}
7609
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007610
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007611static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007612 struct inode *inode,
7613 struct btrfs_dio_data *dio_data,
7614 u64 start, u64 len)
7615{
7616 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7617 struct extent_map *em = *map;
7618 int ret = 0;
7619
7620 /*
7621 * We don't allocate a new extent in the following cases
7622 *
7623 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7624 * existing extent.
7625 * 2) The extent is marked as PREALLOC. We're good to go here and can
7626 * just use the extent.
7627 *
7628 */
7629 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7630 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7631 em->block_start != EXTENT_MAP_HOLE)) {
7632 int type;
7633 u64 block_start, orig_start, orig_block_len, ram_bytes;
7634
7635 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7636 type = BTRFS_ORDERED_PREALLOC;
7637 else
7638 type = BTRFS_ORDERED_NOCOW;
7639 len = min(len, em->len - (start - em->start));
7640 block_start = em->block_start + (start - em->start);
7641
7642 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007643 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007644 btrfs_inc_nocow_writers(fs_info, block_start)) {
7645 struct extent_map *em2;
7646
Nikolay Borisov64f54182020-06-03 08:55:31 +03007647 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007648 orig_start, block_start,
7649 len, orig_block_len,
7650 ram_bytes, type);
7651 btrfs_dec_nocow_writers(fs_info, block_start);
7652 if (type == BTRFS_ORDERED_PREALLOC) {
7653 free_extent_map(em);
7654 *map = em = em2;
7655 }
7656
7657 if (em2 && IS_ERR(em2)) {
7658 ret = PTR_ERR(em2);
7659 goto out;
7660 }
7661 /*
7662 * For inode marked NODATACOW or extent marked PREALLOC,
7663 * use the existing or preallocated extent, so does not
7664 * need to adjust btrfs_space_info's bytes_may_use.
7665 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007666 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007667 goto skip_cow;
7668 }
7669 }
7670
7671 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007672 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007673 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007674 if (IS_ERR(em)) {
7675 ret = PTR_ERR(em);
7676 goto out;
7677 }
7678
7679 len = min(len, em->len - (start - em->start));
7680
7681skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007682 /*
7683 * Need to update the i_size under the extent lock so buffered
7684 * readers will get the updated i_size when we unlock.
7685 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007686 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007687 i_size_write(inode, start + len);
7688
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007689 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007690out:
7691 return ret;
7692}
7693
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007694static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7695 loff_t length, unsigned int flags, struct iomap *iomap,
7696 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007700 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307701 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007702 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007703 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007704 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007705 u64 len = length;
7706 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007707
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007708 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007709 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007710
Josef Bacikc3298612012-08-03 16:49:19 -04007711 lockstart = start;
7712 lockend = start + len - 1;
7713
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007714 /*
7715 * The generic stuff only does filemap_write_and_wait_range, which
7716 * isn't enough if we've written compressed pages to this area, so we
7717 * need to flush the dirty pages again to make absolutely sure that any
7718 * outstanding dirty pages are on disk.
7719 */
7720 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7721 &BTRFS_I(inode)->runtime_flags)) {
7722 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7723 start + length - 1);
7724 if (ret)
7725 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007726 }
7727
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007728 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7729 if (!dio_data)
7730 return -ENOMEM;
7731
7732 dio_data->length = length;
7733 if (write) {
7734 dio_data->reserve = round_up(length, fs_info->sectorsize);
7735 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7736 &dio_data->data_reserved,
7737 start, dio_data->reserve);
7738 if (ret) {
7739 extent_changeset_free(dio_data->data_reserved);
7740 kfree(dio_data);
7741 return ret;
7742 }
7743 }
7744 iomap->private = dio_data;
7745
7746
Josef Bacikeb838e72012-07-31 16:28:48 -04007747 /*
7748 * If this errors out it's because we couldn't invalidate pagecache for
7749 * this range and we need to fallback to buffered.
7750 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007751 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007752 ret = -ENOTBLK;
7753 goto err;
7754 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007755
Omar Sandoval39b07b52019-12-02 17:34:23 -08007756 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 if (IS_ERR(em)) {
7758 ret = PTR_ERR(em);
7759 goto unlock_err;
7760 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007761
7762 /*
7763 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7764 * io. INLINE is special, and we could probably kludge it in here, but
7765 * it's still buffered so for safety lets just fall back to the generic
7766 * buffered path.
7767 *
7768 * For COMPRESSED we _have_ to read the entire extent in so we can
7769 * decompress it, so there will be buffering required no matter what we
7770 * do, so go ahead and fallback to buffered.
7771 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007772 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 * to buffered IO. Don't blame me, this is the price we pay for using
7774 * the generic code.
7775 */
7776 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7777 em->block_start == EXTENT_MAP_INLINE) {
7778 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 ret = -ENOTBLK;
7780 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 }
7782
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007783 len = min(len, em->len - (start - em->start));
7784 if (write) {
7785 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7786 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007787 if (ret < 0)
7788 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007789 unlock_extents = true;
7790 /* Recalc len in case the new em is smaller than requested */
7791 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007792 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007793 /*
7794 * We need to unlock only the end area that we aren't using.
7795 * The rest is going to be unlocked by the endio routine.
7796 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007797 lockstart = start + len;
7798 if (lockstart < lockend)
7799 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 }
7801
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007802 if (unlock_extents)
7803 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7804 lockstart, lockend, &cached_state);
7805 else
7806 free_extent_state(cached_state);
7807
7808 /*
7809 * Translate extent map information to iomap.
7810 * We trim the extents (and move the addr) even though iomap code does
7811 * that, since we have locked only the parts we are performing I/O in.
7812 */
7813 if ((em->block_start == EXTENT_MAP_HOLE) ||
7814 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7815 iomap->addr = IOMAP_NULL_ADDR;
7816 iomap->type = IOMAP_HOLE;
7817 } else {
7818 iomap->addr = em->block_start + (start - em->start);
7819 iomap->type = IOMAP_MAPPED;
7820 }
7821 iomap->offset = start;
7822 iomap->bdev = fs_info->fs_devices->latest_bdev;
7823 iomap->length = len;
7824
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007825 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007826 iomap->flags |= IOMAP_F_ZONE_APPEND;
7827
Josef Bacik4b46fce2010-05-23 11:00:55 -04007828 free_extent_map(em);
7829
7830 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007831
7832unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007833 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7834 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007835err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007836 if (dio_data) {
7837 btrfs_delalloc_release_space(BTRFS_I(inode),
7838 dio_data->data_reserved, start,
7839 dio_data->reserve, true);
7840 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7841 extent_changeset_free(dio_data->data_reserved);
7842 kfree(dio_data);
7843 }
7844 return ret;
7845}
7846
7847static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7848 ssize_t written, unsigned int flags, struct iomap *iomap)
7849{
7850 int ret = 0;
7851 struct btrfs_dio_data *dio_data = iomap->private;
7852 size_t submitted = dio_data->submitted;
7853 const bool write = !!(flags & IOMAP_WRITE);
7854
7855 if (!write && (iomap->type == IOMAP_HOLE)) {
7856 /* If reading from a hole, unlock and return */
7857 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7858 goto out;
7859 }
7860
7861 if (submitted < length) {
7862 pos += submitted;
7863 length -= submitted;
7864 if (write)
7865 __endio_write_update_ordered(BTRFS_I(inode), pos,
7866 length, false);
7867 else
7868 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7869 pos + length - 1);
7870 ret = -ENOTBLK;
7871 }
7872
7873 if (write) {
7874 if (dio_data->reserve)
7875 btrfs_delalloc_release_space(BTRFS_I(inode),
7876 dio_data->data_reserved, pos,
7877 dio_data->reserve, true);
7878 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7879 extent_changeset_free(dio_data->data_reserved);
7880 }
7881out:
7882 kfree(dio_data);
7883 iomap->private = NULL;
7884
Josef Bacikeb838e72012-07-31 16:28:48 -04007885 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007886}
7887
Omar Sandoval769b4f22020-04-16 14:46:22 -07007888static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7889{
7890 /*
7891 * This implies a barrier so that stores to dio_bio->bi_status before
7892 * this and loads of dio_bio->bi_status after this are fully ordered.
7893 */
7894 if (!refcount_dec_and_test(&dip->refs))
7895 return;
7896
Naohiro Aotacfe94442021-02-04 19:21:59 +09007897 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007898 __endio_write_update_ordered(BTRFS_I(dip->inode),
7899 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007900 dip->bytes,
7901 !dip->dio_bio->bi_status);
7902 } else {
7903 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7904 dip->logical_offset,
7905 dip->logical_offset + dip->bytes - 1);
7906 }
7907
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007908 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007909 kfree(dip);
7910}
7911
Omar Sandoval77d5d682020-04-16 14:46:25 -07007912static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7913 int mirror_num,
7914 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007915{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007916 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007919
Mike Christie37226b22016-06-05 14:31:52 -05007920 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007921
Omar Sandoval5c047a62020-04-16 14:46:24 -07007922 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007923 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007924 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007925
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007926 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007927 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007928 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007929 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007930 return ret;
7931}
7932
Omar Sandoval769b4f22020-04-16 14:46:22 -07007933static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7934 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007935 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007936{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007937 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7938 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007939 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007940 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7941 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007942 struct bio_vec bvec;
7943 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007944 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007945 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007946 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007947
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007948 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7949 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950
Liu Bo17347ce2017-05-15 15:33:27 -07007951 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7952 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007953 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007954 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007955 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007956 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07007957 bio_offset, bvec.bv_page,
7958 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007959 clean_io_failure(fs_info, failure_tree, io_tree,
7960 start, bvec.bv_page,
7961 btrfs_ino(BTRFS_I(inode)),
7962 pgoff);
7963 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08007964 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007965
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007966 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08007967 ret = btrfs_repair_one_sector(inode,
7968 &io_bio->bio,
7969 start - io_bio->logical,
7970 bvec.bv_page, pgoff,
7971 start, io_bio->mirror_num,
7972 submit_dio_repair_bio);
7973 if (ret)
7974 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007975 }
7976 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007977 ASSERT(bio_offset + sectorsize > bio_offset);
7978 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307980 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007981 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007982 return err;
7983}
7984
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007985static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007986 const u64 offset, const u64 bytes,
7987 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007988{
Qu Wenruoe65f1522021-04-01 15:15:06 +08007989 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
7990 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00007991}
7992
Qu Wenruo8896a082020-10-21 14:24:53 +08007993static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08007994 struct bio *bio,
7995 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007996{
Qu Wenruo1941b642020-12-02 14:47:57 +08007997 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007998}
7999
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008000static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008001{
8002 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008003 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008004
Miao Xie8b110e32014-09-12 18:44:03 +08008005 if (err)
8006 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008007 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008008 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008009 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008010 bio->bi_iter.bi_size, err);
8011
Omar Sandoval769b4f22020-04-16 14:46:22 -07008012 if (bio_op(bio) == REQ_OP_READ) {
8013 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008014 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008015 }
8016
Omar Sandoval769b4f22020-04-16 14:46:22 -07008017 if (err)
8018 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008019
Naohiro Aota544d24f2021-02-04 19:22:06 +09008020 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8021
Miao Xiee65e1532010-11-22 03:04:43 +00008022 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008023 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008024}
8025
David Sterbad0ee3932018-03-08 14:35:48 +01008026static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8027 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008030 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008031 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008032 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008033
Liu Bo4c274bc2017-11-01 17:19:27 -06008034 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008035 if (async_submit)
8036 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8037
Josef Bacik5fd02042012-05-02 14:00:54 -04008038 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008039 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008040 if (ret)
8041 goto err;
8042 }
Miao Xiee65e1532010-11-22 03:04:43 +00008043
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008044 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008045 goto map;
8046
8047 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008048 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008049 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008050 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008051 } else if (write) {
8052 /*
8053 * If we aren't doing async submit, calculate the csum of the
8054 * bio now.
8055 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008056 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008057 if (ret)
8058 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008059 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008060 u64 csum_offset;
8061
8062 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008063 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008064 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008065 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008066 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008067map:
Chris Mason08635ba2019-07-10 12:28:14 -07008068 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008069err:
Miao Xiee65e1532010-11-22 03:04:43 +00008070 return ret;
8071}
8072
Omar Sandovalc36cac22020-04-16 14:46:13 -07008073/*
8074 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8075 * or ordered extents whether or not we submit any bios.
8076 */
8077static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8078 struct inode *inode,
8079 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008080{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008081 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008082 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8083 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008084 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008085
Omar Sandoval85879572020-04-16 14:46:21 -07008086 dip_size = sizeof(*dip);
8087 if (!write && csum) {
8088 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008089 size_t nblocks;
8090
David Sterba265fdfa2020-07-01 21:19:09 +02008091 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008092 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008093 }
8094
8095 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008096 if (!dip)
8097 return NULL;
8098
Omar Sandovalc36cac22020-04-16 14:46:13 -07008099 dip->inode = inode;
8100 dip->logical_offset = file_offset;
8101 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008102 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008103 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008104 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008105 return dip;
8106}
8107
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008108static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8109 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008110{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008111 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008112 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008113 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8114 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008115 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008116 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008117 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008118 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008119 u64 submit_len;
8120 int clone_offset = 0;
8121 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008122 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308123 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008124 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008125 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008126 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008127 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008128
Omar Sandovalc36cac22020-04-16 14:46:13 -07008129 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8130 if (!dip) {
8131 if (!write) {
8132 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8133 file_offset + dio_bio->bi_iter.bi_size - 1);
8134 }
8135 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008136 bio_endio(dio_bio);
8137 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008138 }
8139
Josef Bacik334c16d2020-10-16 11:29:14 -04008140 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008141 /*
8142 * Load the csums up front to reduce csum tree searches and
8143 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008144 *
8145 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008146 */
Qu Wenruo62751932020-12-02 14:48:06 +08008147 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008148 if (status != BLK_STS_OK)
8149 goto out_err;
8150 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008151
Omar Sandoval769b4f22020-04-16 14:46:22 -07008152 start_sector = dio_bio->bi_iter.bi_sector;
8153 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008154
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008155 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008156 logical = start_sector << 9;
8157 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8158 if (IS_ERR(em)) {
8159 status = errno_to_blk_status(PTR_ERR(em));
8160 em = NULL;
8161 goto out_err_em;
8162 }
8163 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008164 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008165 if (ret) {
8166 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008167 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008168 }
8169 ASSERT(geom.len <= INT_MAX);
8170
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008171 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008172
Liu Bo725130b2017-05-16 09:51:39 -07008173 /*
8174 * This will never fail as it's passing GPF_NOFS and
8175 * the allocation is backed by btrfs_bioset.
8176 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008177 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008178 bio->bi_private = dip;
8179 bio->bi_end_io = btrfs_end_dio_bio;
8180 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008181
Naohiro Aota544d24f2021-02-04 19:22:06 +09008182 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8183 status = extract_ordered_extent(BTRFS_I(inode), bio,
8184 file_offset);
8185 if (status) {
8186 bio_put(bio);
8187 goto out_err;
8188 }
8189 }
8190
Liu Bo725130b2017-05-16 09:51:39 -07008191 ASSERT(submit_len >= clone_len);
8192 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008193
Liu Bo725130b2017-05-16 09:51:39 -07008194 /*
8195 * Increase the count before we submit the bio so we know
8196 * the end IO handler won't happen before we increase the
8197 * count. Otherwise, the dip might get freed before we're
8198 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008199 *
8200 * We transfer the initial reference to the last bio, so we
8201 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008202 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008203 if (submit_len > 0) {
8204 refcount_inc(&dip->refs);
8205 /*
8206 * If we are submitting more than one bio, submit them
8207 * all asynchronously. The exception is RAID 5 or 6, as
8208 * asynchronous checksums make it difficult to collect
8209 * full stripe writes.
8210 */
8211 if (!raid56)
8212 async_submit = 1;
8213 }
Miao Xiee65e1532010-11-22 03:04:43 +00008214
David Sterbad0ee3932018-03-08 14:35:48 +01008215 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008216 async_submit);
8217 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008218 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008219 if (submit_len > 0)
8220 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008221 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008222 }
Liu Bo725130b2017-05-16 09:51:39 -07008223
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008224 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008225 clone_offset += clone_len;
8226 start_sector += clone_len >> 9;
8227 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008228
8229 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008230 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008231 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008232
Michal Rostecki42034312021-01-27 14:57:27 +01008233out_err_em:
8234 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008235out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008236 dip->dio_bio->bi_status = status;
8237 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008238
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008239 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008240}
8241
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008242const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008243 .iomap_begin = btrfs_dio_iomap_begin,
8244 .iomap_end = btrfs_dio_iomap_end,
8245};
8246
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008247const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008248 .submit_io = btrfs_submit_direct,
8249};
8250
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008251static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008252 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008253{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008254 int ret;
8255
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008256 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008257 if (ret)
8258 return ret;
8259
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008260 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008261}
8262
Chris Mason9ebefb182007-06-15 13:50:00 -04008263int btrfs_readpage(struct file *file, struct page *page)
8264{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008265 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8266 u64 start = page_offset(page);
8267 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008268 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008269 int ret;
8270
Nikolay Borisov0f208812020-09-14 14:39:16 +03008271 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8272
Qu Wenruo390ed292021-04-14 16:42:15 +08008273 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8274 if (bio_ctrl.bio)
8275 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008276 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008277}
Chris Mason1832a6d2007-12-21 16:27:21 -05008278
Chris Mason39279cc2007-06-12 06:35:45 -04008279static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8280{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008281 struct inode *inode = page->mapping->host;
8282 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008283
8284 if (current->flags & PF_MEMALLOC) {
8285 redirty_page_for_writepage(wbc, page);
8286 unlock_page(page);
8287 return 0;
8288 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008289
8290 /*
8291 * If we are under memory pressure we will call this directly from the
8292 * VM, we need to make sure we have the inode referenced for the ordered
8293 * extent. If not just return like we didn't do anything.
8294 */
8295 if (!igrab(inode)) {
8296 redirty_page_for_writepage(wbc, page);
8297 return AOP_WRITEPAGE_ACTIVATE;
8298 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008299 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008300 btrfs_add_delayed_iput(inode);
8301 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008302}
Chris Mason39279cc2007-06-12 06:35:45 -04008303
Eric Sandeen48a3b632013-04-25 20:41:01 +00008304static int btrfs_writepages(struct address_space *mapping,
8305 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008306{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008307 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008308}
8309
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008310static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008311{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008312 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008313}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008314
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008316{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008317 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008318 if (ret == 1)
Qu Wenruo32443de2021-01-26 16:34:00 +08008319 clear_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008320 return ret;
8321}
Chris Mason39279cc2007-06-12 06:35:45 -04008322
Chris Masone6dcd2d2008-07-17 12:53:50 -04008323static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8324{
Chris Mason98509cf2008-09-11 15:51:43 -04008325 if (PageWriteback(page) || PageDirty(page))
8326 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008327 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008328}
8329
Roman Gushchinf8e66082020-03-04 16:57:35 -08008330#ifdef CONFIG_MIGRATION
8331static int btrfs_migratepage(struct address_space *mapping,
8332 struct page *newpage, struct page *page,
8333 enum migrate_mode mode)
8334{
8335 int ret;
8336
8337 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8338 if (ret != MIGRATEPAGE_SUCCESS)
8339 return ret;
8340
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008341 if (page_has_private(page))
8342 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008343
Qu Wenruof57ad932021-04-07 19:22:13 +08008344 if (PageOrdered(page)) {
8345 ClearPageOrdered(page);
8346 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008347 }
8348
8349 if (mode != MIGRATE_SYNC_NO_COPY)
8350 migrate_page_copy(newpage, page);
8351 else
8352 migrate_page_states(newpage, page);
8353 return MIGRATEPAGE_SUCCESS;
8354}
8355#endif
8356
Lukas Czernerd47992f2013-05-21 23:17:23 -04008357static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8358 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008359{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008360 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008361 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008362 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008363 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008364 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008365 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008366 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008367 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008368
Chris Mason8b62b722009-09-02 16:53:46 -04008369 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008370 * We have page locked so no new ordered extent can be created on this
8371 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008372 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008373 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008374 * Furthermore, endio function won't skip page which has Ordered
8375 * (Private2) already cleared, so it's possible for endio and
8376 * invalidatepage to do the same ordered extent accounting twice
8377 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008378 *
8379 * So here we wait for any submitted bios to finish, so that we won't
8380 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008381 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008382 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008383
Chris Masone6dcd2d2008-07-17 12:53:50 -04008384 if (offset) {
8385 btrfs_releasepage(page, GFP_NOFS);
8386 return;
8387 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008388
8389 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008390 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008391
Qu Wenruo3b835842021-04-06 19:54:53 +08008392 cur = page_start;
8393 while (cur < page_end) {
8394 struct btrfs_ordered_extent *ordered;
8395 bool delete_states;
8396 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008397 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008398
8399 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8400 page_end + 1 - cur);
8401 if (!ordered) {
8402 range_end = page_end;
8403 /*
8404 * No ordered extent covering this range, we are safe
8405 * to delete all extent states in the range.
8406 */
8407 delete_states = true;
8408 goto next;
8409 }
8410 if (ordered->file_offset > cur) {
8411 /*
8412 * There is a range between [cur, oe->file_offset) not
8413 * covered by any ordered extent.
8414 * We are safe to delete all extent states, and handle
8415 * the ordered extent in the next iteration.
8416 */
8417 range_end = ordered->file_offset - 1;
8418 delete_states = true;
8419 goto next;
8420 }
8421
8422 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8423 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008424 ASSERT(range_end + 1 - cur < U32_MAX);
8425 range_len = range_end + 1 - cur;
8426 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008427 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008428 * If Ordered (Private2) is cleared, it means endio has
8429 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008430 * We can't delete the extent states as
8431 * btrfs_finish_ordered_io() may still use some of them.
8432 */
8433 delete_states = false;
8434 goto next;
8435 }
Qu Wenruob945a462021-05-31 16:50:46 +08008436 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008437
Chris Masoneb84ae02008-07-17 13:53:27 -04008438 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008439 * IO on this page will never be started, so we need to account
8440 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8441 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008442 *
8443 * This will also unlock the range for incoming
8444 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008445 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008446 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008447 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008448 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008449 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008450 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008451
8452 spin_lock_irq(&inode->ordered_tree.lock);
8453 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8454 ordered->truncated_len = min(ordered->truncated_len,
8455 cur - ordered->file_offset);
8456 spin_unlock_irq(&inode->ordered_tree.lock);
8457
8458 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8459 cur, range_end + 1 - cur, 1)) {
8460 btrfs_finish_ordered_io(ordered);
8461 /*
8462 * The ordered extent has finished, now we're again
8463 * safe to delete all extent states of the range.
8464 */
8465 delete_states = true;
8466 } else {
8467 /*
8468 * btrfs_finish_ordered_io() will get executed by endio
8469 * of other pages, thus we can't delete extent states
8470 * anymore
8471 */
8472 delete_states = false;
8473 }
8474next:
8475 if (ordered)
8476 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008477 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008478 * Qgroup reserved space handler
8479 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008480 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008481 * 1) Already written to disk or bio already finished
8482 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8483 * Qgroup will be handled by its qgroup_record then.
8484 * btrfs_qgroup_free_data() call will do nothing here.
8485 *
8486 * 2) Not written to disk yet
8487 * Then btrfs_qgroup_free_data() call will clear the
8488 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8489 * reserved data space.
8490 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008491 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008492 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008493 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008494 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008495 EXTENT_DELALLOC | EXTENT_UPTODATE |
8496 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008497 delete_states, &cached_state);
8498 }
8499 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008500 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008501 /*
8502 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008503 * should not have Ordered (Private2) anymore, or the above iteration
8504 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008505 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008506 ASSERT(!PageOrdered(page));
Qu Wenruo3b835842021-04-06 19:54:53 +08008507 if (!inode_evicting)
8508 __btrfs_releasepage(page, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04008509 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008510 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008511}
8512
Chris Mason9ebefb182007-06-15 13:50:00 -04008513/*
8514 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8515 * called from a page fault handler when a page is first dirtied. Hence we must
8516 * be careful to check for EOF conditions here. We set the page up correctly
8517 * for a written page which means we get ENOSPC checking when writing into
8518 * holes and correct delalloc and unwritten extent mapping on filesystems that
8519 * support these features.
8520 *
8521 * We are not allowed to take the i_mutex here so we have to play games to
8522 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008523 * truncate_setsize() writes the inode size before removing pages, once we have
8524 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008525 * beyond EOF, then the page is guaranteed safe against truncation until we
8526 * unlock the page.
8527 */
Souptick Joardera528a242018-06-06 19:54:44 +05308528vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008529{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008530 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008531 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008532 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008533 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8534 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008535 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008536 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008537 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008538 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308539 vm_fault_t ret;
8540 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008541 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308542 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008543 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008544 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308545 u64 end;
8546
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008547 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008548
Jan Karab2b5ef52012-06-12 16:20:45 +02008549 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008550 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008551 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308552 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008553
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308554 /*
8555 * Reserving delalloc space after obtaining the page lock can lead to
8556 * deadlock. For example, if a dirty page is locked by this function
8557 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8558 * dirty page write out, then the btrfs_writepage() function could
8559 * end up waiting indefinitely to get a lock on the page currently
8560 * being processed by btrfs_page_mkwrite() function.
8561 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008562 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8563 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308564 if (!ret2) {
8565 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008566 reserved = 1;
8567 }
Souptick Joardera528a242018-06-06 19:54:44 +05308568 if (ret2) {
8569 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008570 if (reserved)
8571 goto out;
8572 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008573 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008574
Nick Piggin56a76f82009-03-31 15:23:23 -07008575 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008576again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008577 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008578 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008579 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008580
Chris Mason9ebefb182007-06-15 13:50:00 -04008581 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008582 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008583 /* page got truncated out from underneath us */
8584 goto out_unlock;
8585 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008586 wait_on_page_writeback(page);
8587
David Sterbaff13db42015-12-03 14:30:40 +01008588 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008589 ret2 = set_page_extent_mapped(page);
8590 if (ret2 < 0) {
8591 ret = vmf_error(ret2);
8592 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8593 goto out_unlock;
8594 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008595
Chris Masoneb84ae02008-07-17 13:53:27 -04008596 /*
8597 * we can't set the delalloc bits if there are pending ordered
8598 * extents. Drop our locks and wait for them to finish
8599 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008600 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8601 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008602 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008603 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008604 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008605 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008606 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008607 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008608 btrfs_put_ordered_extent(ordered);
8609 goto again;
8610 }
8611
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008612 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008613 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008614 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008615 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308616 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008617 btrfs_delalloc_release_space(BTRFS_I(inode),
8618 data_reserved, page_start,
8619 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308620 }
8621 }
8622
Josef Bacikfbf19082009-10-01 17:10:23 -04008623 /*
Liu Bo54160342016-12-13 12:15:19 -08008624 * page_mkwrite gets called when the page is firstly dirtied after it's
8625 * faulted in, but write(2) could also dirty a page and set delalloc
8626 * bits, thus in this case for space account reason, we still need to
8627 * clear any delalloc bits within this page range since we have to
8628 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008629 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308630 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008631 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8632 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008633
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008634 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008635 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308636 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008637 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008638 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008639 ret = VM_FAULT_SIGBUS;
8640 goto out_unlock;
8641 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008642
8643 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008644 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008645 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008646 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008647 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008648
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008649 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008650 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008651 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008652 }
Chris Mason247e7432008-07-17 12:53:51 -04008653 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008654 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008655 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008656
Filipe Mananabc0939f2021-02-23 12:08:48 +00008657 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008658
David Sterbae43bbe52017-12-12 21:43:52 +01008659 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008660 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008661
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008662 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8663 sb_end_pagefault(inode->i_sb);
8664 extent_changeset_free(data_reserved);
8665 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008666
8667out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008668 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008669 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008670out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008671 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008672 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008673 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008674out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008675 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008676 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008677 return ret;
8678}
8679
Filipe Manana213e8c52018-02-06 20:40:31 +00008680static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008681{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008682 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008683 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008684 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008685 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008686 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008687 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008688 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008689 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008690
Filipe Manana213e8c52018-02-06 20:40:31 +00008691 if (!skip_writeback) {
8692 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8693 (u64)-1);
8694 if (ret)
8695 return ret;
8696 }
Chris Mason39279cc2007-06-12 06:35:45 -04008697
Josef Bacikfcb80c22011-05-03 10:40:22 -04008698 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008699 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8700 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008701 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008702 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008703 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008704 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008705 * be free'd up by the truncate operation, but also have some slack
8706 * space reserved in case it uses space during the truncate (thank you
8707 * very much snapshotting).
8708 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008709 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008710 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008711 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008712 * doesn't end up using space reserved for updating the inode. We also
8713 * need to be able to stop the transaction and start a new one, which
8714 * means we need to be able to update the inode several times, and we
8715 * have no idea of knowing how many times that will be, so we can't just
8716 * reserve 1 item for the entirety of the operation, so that has to be
8717 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008718 *
8719 * So that leaves us with
8720 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008721 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008722 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008723 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008724 * updating the inode.
8725 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008726 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008727 if (!rsv)
8728 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008729 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008730 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008731
Josef Bacik907cbce2011-08-08 13:46:15 -04008732 /*
Josef Bacik07127182011-08-19 10:29:59 -04008733 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008734 * 1 for updating the inode.
8735 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008736 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008737 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008738 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008739 goto out;
8740 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008741
Josef Bacik907cbce2011-08-08 13:46:15 -04008742 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008743 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008744 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008745 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008746
Josef Bacikca7e70f2012-08-27 17:48:15 -04008747 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008748
Yan, Zheng80825102009-11-12 09:35:36 +00008749 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008750 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008751 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008752 BTRFS_EXTENT_DATA_KEY,
8753 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008754 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008755 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008756 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008757
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008758 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008759 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008760 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008761
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008762 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008763 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008764
8765 trans = btrfs_start_transaction(root, 2);
8766 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008767 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008768 trans = NULL;
8769 break;
8770 }
8771
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008772 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008773 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008774 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008775 BUG_ON(ret); /* shouldn't happen */
8776 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008777 }
8778
Josef Bacikddfae632017-10-19 14:16:02 -04008779 /*
8780 * We can't call btrfs_truncate_block inside a trans handle as we could
8781 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8782 * we've truncated everything except the last little bit, and can do
8783 * btrfs_truncate_block and then update the disk_i_size.
8784 */
8785 if (ret == NEED_TRUNCATE_BLOCK) {
8786 btrfs_end_transaction(trans);
8787 btrfs_btree_balance_dirty(fs_info);
8788
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008789 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008790 if (ret)
8791 goto out;
8792 trans = btrfs_start_transaction(root, 1);
8793 if (IS_ERR(trans)) {
8794 ret = PTR_ERR(trans);
8795 goto out;
8796 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008797 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008798 }
8799
Chris Mason917c16b2011-11-08 14:49:59 -05008800 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008801 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008802
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008803 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008804 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008805 if (ret2 && !ret)
8806 ret = ret2;
8807
8808 ret2 = btrfs_end_transaction(trans);
8809 if (ret2 && !ret)
8810 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008811 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008812 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008813out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008814 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008815 /*
8816 * So if we truncate and then write and fsync we normally would just
8817 * write the extents that changed, which is a problem if we need to
8818 * first truncate that entire inode. So set this flag so we write out
8819 * all of the extents in the inode to the sync log so we're completely
8820 * safe.
8821 *
8822 * If no extents were dropped or trimmed we don't need to force the next
8823 * fsync to truncate all the inode's items from the log and re-log them
8824 * all. This means the truncate operation did not change the file size,
8825 * or changed it to a smaller size but there was only an implicit hole
8826 * between the old i_size and the new i_size, and there were no prealloc
8827 * extents beyond i_size to drop.
8828 */
8829 if (extents_found > 0)
8830 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008831
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008832 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008833}
8834
Sven Wegener3b963622008-06-09 21:57:42 -04008835/*
Chris Masond352ac62008-09-29 15:18:18 -04008836 * create a new subvolume directory/inode (helper for the ioctl).
8837 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008838int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008839 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008840 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008841{
Chris Mason39279cc2007-06-12 06:35:45 -04008842 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008843 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008844 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008845 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008846
Nikolay Borisov23125102020-12-07 17:32:36 +02008847 err = btrfs_get_free_objectid(new_root, &ino);
8848 if (err < 0)
8849 return err;
8850
8851 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008852 S_IFDIR | (~current_umask() & S_IRWXUGO),
8853 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008854 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008855 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008856 inode->i_op = &btrfs_dir_inode_operations;
8857 inode->i_fop = &btrfs_dir_file_operations;
8858
Miklos Szeredibfe86842011-10-28 14:13:29 +02008859 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008860 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008861 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008862
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008863 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8864 if (err)
8865 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008866 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008867 new_root->root_key.objectid, err);
8868
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008869 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008870
Yan, Zheng76dda932009-09-21 16:00:26 -04008871 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008872 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008873}
8874
Chris Mason39279cc2007-06-12 06:35:45 -04008875struct inode *btrfs_alloc_inode(struct super_block *sb)
8876{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008877 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008878 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008879 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008880
David Sterba712e36c2017-10-31 17:08:27 +01008881 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008882 if (!ei)
8883 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008884
8885 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008886 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008887 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008888 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008889 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008890 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008891 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008892 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008893 ei->disk_i_size = 0;
8894 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008895 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008896 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008897 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008898 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008899 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008900 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008901
Josef Bacik9e0baf62011-07-15 15:16:44 +00008902 spin_lock_init(&ei->lock);
8903 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008904 if (sb->s_magic != BTRFS_TEST_MAGIC)
8905 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8906 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008907 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008908 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008909 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008910
Miao Xie16cdcec2011-04-22 18:12:22 +08008911 ei->delayed_node = NULL;
8912
chandan r9cc97d62012-07-04 12:48:07 +05308913 ei->i_otime.tv_sec = 0;
8914 ei->i_otime.tv_nsec = 0;
8915
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008916 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008917 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008918 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8919 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8920 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008921 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8922 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008923 ei->io_tree.track_uptodate = true;
8924 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008925 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008926 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008927 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008928 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008929 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008930 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05008931 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008932
8933 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008934}
8935
Josef Bacikaaedb552013-10-11 14:44:09 -04008936#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8937void btrfs_test_destroy_inode(struct inode *inode)
8938{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008939 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008940 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8941}
8942#endif
8943
Al Viro26602ca2019-04-10 15:14:41 -04008944void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008945{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008946 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8947}
8948
Nikolay Borisov633cc812020-09-18 12:15:49 +03008949void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008950{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008952 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8953 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008954
Nikolay Borisov633cc812020-09-18 12:15:49 +03008955 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8956 WARN_ON(vfs_inode->i_data.nrpages);
8957 WARN_ON(inode->block_rsv.reserved);
8958 WARN_ON(inode->block_rsv.size);
8959 WARN_ON(inode->outstanding_extents);
8960 WARN_ON(inode->delalloc_bytes);
8961 WARN_ON(inode->new_delalloc_bytes);
8962 WARN_ON(inode->csum_bytes);
8963 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008964
Chris Mason5a3f23d2009-03-31 13:27:11 -04008965 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008966 * This can happen where we create an inode, but somebody else also
8967 * created the same inode and we need to destroy the one we already
8968 * created.
8969 */
8970 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008971 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008972
Chris Masond3977122009-01-05 21:25:51 -05008973 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008974 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 if (!ordered)
8976 break;
8977 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008978 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008979 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008980 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008981 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008982 btrfs_put_ordered_extent(ordered);
8983 btrfs_put_ordered_extent(ordered);
8984 }
8985 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008986 btrfs_qgroup_check_reserved_leak(inode);
8987 inode_tree_del(inode);
8988 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8989 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8990 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008991}
8992
Al Viro45321ac2010-06-07 13:43:19 -04008993int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008994{
8995 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008996
Naohiro Aota6379ef92013-06-06 09:56:34 +00008997 if (root == NULL)
8998 return 1;
8999
Liu Bofa6ac872013-02-20 14:10:23 +00009000 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009001 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009002 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009003 else
Al Viro45321ac2010-06-07 13:43:19 -04009004 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009005}
9006
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009007static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009008{
9009 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9010
9011 inode_init_once(&ei->vfs_inode);
9012}
9013
David Sterbae67c7182018-02-19 17:24:18 +01009014void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009015{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009016 /*
9017 * Make sure all delayed rcu free inodes are flushed before we
9018 * destroy cache.
9019 */
9020 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009021 kmem_cache_destroy(btrfs_inode_cachep);
9022 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009023 kmem_cache_destroy(btrfs_path_cachep);
9024 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009025 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009026}
9027
Liu Bof5c29bd2017-11-02 17:21:50 -06009028int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009029{
David Sterba837e1972012-09-07 03:00:48 -06009030 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009031 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009032 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9033 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009034 if (!btrfs_inode_cachep)
9035 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009036
David Sterba837e1972012-09-07 03:00:48 -06009037 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009038 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009039 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009040 if (!btrfs_trans_handle_cachep)
9041 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009042
David Sterba837e1972012-09-07 03:00:48 -06009043 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009044 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009045 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009046 if (!btrfs_path_cachep)
9047 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009048
David Sterba837e1972012-09-07 03:00:48 -06009049 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009050 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009051 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009052 if (!btrfs_free_space_cachep)
9053 goto fail;
9054
Christophe Leroy3acd4852019-08-21 15:05:55 +00009055 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9056 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009057 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009058 if (!btrfs_free_space_bitmap_cachep)
9059 goto fail;
9060
Chris Mason39279cc2007-06-12 06:35:45 -04009061 return 0;
9062fail:
9063 btrfs_destroy_cachep();
9064 return -ENOMEM;
9065}
9066
Christian Brauner549c7292021-01-21 14:19:43 +01009067static int btrfs_getattr(struct user_namespace *mnt_userns,
9068 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009069 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009070{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009071 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009072 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009073 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009074 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009075 u32 bi_flags = BTRFS_I(inode)->flags;
9076
9077 stat->result_mask |= STATX_BTIME;
9078 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9079 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9080 if (bi_flags & BTRFS_INODE_APPEND)
9081 stat->attributes |= STATX_ATTR_APPEND;
9082 if (bi_flags & BTRFS_INODE_COMPRESS)
9083 stat->attributes |= STATX_ATTR_COMPRESSED;
9084 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9085 stat->attributes |= STATX_ATTR_IMMUTABLE;
9086 if (bi_flags & BTRFS_INODE_NODUMP)
9087 stat->attributes |= STATX_ATTR_NODUMP;
9088
9089 stat->attributes_mask |= (STATX_ATTR_APPEND |
9090 STATX_ATTR_COMPRESSED |
9091 STATX_ATTR_IMMUTABLE |
9092 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009093
Christian Brauner0d56a452021-01-21 14:19:30 +01009094 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009095 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009096
9097 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009098 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009099 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009100 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009101 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009102 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009103 return 0;
9104}
9105
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009106static int btrfs_rename_exchange(struct inode *old_dir,
9107 struct dentry *old_dentry,
9108 struct inode *new_dir,
9109 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009110{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009111 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009112 struct btrfs_trans_handle *trans;
9113 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009114 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009115 struct inode *new_inode = new_dentry->d_inode;
9116 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009117 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009118 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9119 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009120 u64 old_idx = 0;
9121 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009122 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009123 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009124 bool root_log_pinned = false;
9125 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009126 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009127
9128 /* we only allow rename subvolume link between subvolumes */
9129 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9130 return -EXDEV;
9131
9132 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009133 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9134 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009135 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009136
9137 /*
9138 * We want to reserve the absolute worst case amount of items. So if
9139 * both inodes are subvols and we need to unlink them then that would
9140 * require 4 item modifications, but if they are both normal inodes it
9141 * would require 5 item modifications, so we'll assume their normal
9142 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9143 * should cover the worst case number of items we'll modify.
9144 */
9145 trans = btrfs_start_transaction(root, 12);
9146 if (IS_ERR(trans)) {
9147 ret = PTR_ERR(trans);
9148 goto out_notrans;
9149 }
9150
Josef Bacik00aa8e82021-03-12 15:25:02 -05009151 if (dest != root) {
9152 ret = btrfs_record_root_in_trans(trans, dest);
9153 if (ret)
9154 goto out_fail;
9155 }
Josef Bacik3e174092019-11-15 15:43:06 -05009156
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009157 /*
9158 * We need to find a free sequence number both in the source and
9159 * in the destination directory for the exchange.
9160 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009161 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009162 if (ret)
9163 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009164 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009165 if (ret)
9166 goto out_fail;
9167
9168 BTRFS_I(old_inode)->dir_index = 0ULL;
9169 BTRFS_I(new_inode)->dir_index = 0ULL;
9170
9171 /* Reference for the source. */
9172 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9173 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009174 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009175 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009176 btrfs_pin_log_trans(root);
9177 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009178 ret = btrfs_insert_inode_ref(trans, dest,
9179 new_dentry->d_name.name,
9180 new_dentry->d_name.len,
9181 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009182 btrfs_ino(BTRFS_I(new_dir)),
9183 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009184 if (ret)
9185 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009186 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009187 }
9188
9189 /* And now for the dest. */
9190 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9191 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009192 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009193 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009194 btrfs_pin_log_trans(dest);
9195 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009196 ret = btrfs_insert_inode_ref(trans, root,
9197 old_dentry->d_name.name,
9198 old_dentry->d_name.len,
9199 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009200 btrfs_ino(BTRFS_I(old_dir)),
9201 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009202 if (ret) {
9203 if (need_abort)
9204 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009205 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009206 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009207 }
9208
9209 /* Update inode version and ctime/mtime. */
9210 inode_inc_iversion(old_dir);
9211 inode_inc_iversion(new_dir);
9212 inode_inc_iversion(old_inode);
9213 inode_inc_iversion(new_inode);
9214 old_dir->i_ctime = old_dir->i_mtime = ctime;
9215 new_dir->i_ctime = new_dir->i_mtime = ctime;
9216 old_inode->i_ctime = ctime;
9217 new_inode->i_ctime = ctime;
9218
9219 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009220 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9221 BTRFS_I(old_inode), 1);
9222 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9223 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009224 }
9225
9226 /* src is a subvolume */
9227 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009228 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009229 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009230 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9231 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009232 old_dentry->d_name.name,
9233 old_dentry->d_name.len);
9234 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009235 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009236 }
9237 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009238 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009239 goto out_fail;
9240 }
9241
9242 /* dest is a subvolume */
9243 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009244 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009245 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009246 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9247 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009248 new_dentry->d_name.name,
9249 new_dentry->d_name.len);
9250 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009251 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009252 }
9253 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009254 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009255 goto out_fail;
9256 }
9257
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009258 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009259 new_dentry->d_name.name,
9260 new_dentry->d_name.len, 0, old_idx);
9261 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009262 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009263 goto out_fail;
9264 }
9265
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009266 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009267 old_dentry->d_name.name,
9268 old_dentry->d_name.len, 0, new_idx);
9269 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009270 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009271 goto out_fail;
9272 }
9273
9274 if (old_inode->i_nlink == 1)
9275 BTRFS_I(old_inode)->dir_index = old_idx;
9276 if (new_inode->i_nlink == 1)
9277 BTRFS_I(new_inode)->dir_index = new_idx;
9278
Filipe Manana86e8aa02016-05-05 02:02:27 +01009279 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009280 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9281 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009282 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009283 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009284 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009285 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009286 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9287 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009288 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009289 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009290 }
9291out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009292 /*
9293 * If we have pinned a log and an error happened, we unpin tasks
9294 * trying to sync the log and force them to fallback to a transaction
9295 * commit if the log currently contains any of the inodes involved in
9296 * this rename operation (to ensure we do not persist a log with an
9297 * inconsistent state for any of these inodes or leading to any
9298 * inconsistencies when replayed). If the transaction was aborted, the
9299 * abortion reason is propagated to userspace when attempting to commit
9300 * the transaction. If the log does not contain any of these inodes, we
9301 * allow the tasks to sync it.
9302 */
9303 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009304 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9305 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9306 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009307 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009308 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009309 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009310
9311 if (root_log_pinned) {
9312 btrfs_end_log_trans(root);
9313 root_log_pinned = false;
9314 }
9315 if (dest_log_pinned) {
9316 btrfs_end_log_trans(dest);
9317 dest_log_pinned = false;
9318 }
9319 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009320 ret2 = btrfs_end_transaction(trans);
9321 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009322out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009323 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9324 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009325 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009326
9327 return ret;
9328}
9329
9330static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9331 struct btrfs_root *root,
9332 struct inode *dir,
9333 struct dentry *dentry)
9334{
9335 int ret;
9336 struct inode *inode;
9337 u64 objectid;
9338 u64 index;
9339
Nikolay Borisov543068a2020-12-07 17:32:33 +02009340 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341 if (ret)
9342 return ret;
9343
9344 inode = btrfs_new_inode(trans, root, dir,
9345 dentry->d_name.name,
9346 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009347 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009348 objectid,
9349 S_IFCHR | WHITEOUT_MODE,
9350 &index);
9351
9352 if (IS_ERR(inode)) {
9353 ret = PTR_ERR(inode);
9354 return ret;
9355 }
9356
9357 inode->i_op = &btrfs_special_inode_operations;
9358 init_special_inode(inode, inode->i_mode,
9359 WHITEOUT_DEV);
9360
9361 ret = btrfs_init_inode_security(trans, inode, dir,
9362 &dentry->d_name);
9363 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009364 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009365
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009366 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9367 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009368 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009369 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009370
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009371 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009372out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009373 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009374 if (ret)
9375 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009376 iput(inode);
9377
Filipe Mananac9901612016-05-05 01:41:57 +01009378 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009379}
9380
Chris Mason39279cc2007-06-12 06:35:45 -04009381static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009382 struct inode *new_dir, struct dentry *new_dentry,
9383 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009385 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009386 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009387 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009388 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9389 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009390 struct inode *new_inode = d_inode(new_dentry);
9391 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009392 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009393 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009394 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009395 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009396 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009397
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009398 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009399 return -EPERM;
9400
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009401 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009402 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009403 return -EXDEV;
9404
Li Zefan33345d012011-04-20 10:31:50 +08009405 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009406 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009407 return -ENOTEMPTY;
9408
Chris Mason39279cc2007-06-12 06:35:45 -04009409 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009410 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009411 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009412
9413
9414 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009415 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009416 new_dentry->d_name.name,
9417 new_dentry->d_name.len);
9418
9419 if (ret) {
9420 if (ret == -EEXIST) {
9421 /* we shouldn't get
9422 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309423 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009424 return ret;
9425 }
9426 } else {
9427 /* maybe -EOVERFLOW */
9428 return ret;
9429 }
9430 }
9431 ret = 0;
9432
Chris Mason5a3f23d2009-03-31 13:27:11 -04009433 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009434 * we're using rename to replace one file with another. Start IO on it
9435 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009436 */
Chris Mason8d875f92014-08-12 10:47:42 -07009437 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009438 filemap_flush(old_inode->i_mapping);
9439
Yan, Zheng76dda932009-09-21 16:00:26 -04009440 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009441 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009442 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009443 /*
9444 * We want to reserve the absolute worst case amount of items. So if
9445 * both inodes are subvols and we need to unlink them then that would
9446 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009448 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9449 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009450 * If our rename has the whiteout flag, we need more 5 units for the
9451 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9452 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009453 */
Filipe Manana5062af32016-05-05 10:26:26 +01009454 trans_num_items = 11;
9455 if (flags & RENAME_WHITEOUT)
9456 trans_num_items += 5;
9457 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009458 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009459 ret = PTR_ERR(trans);
9460 goto out_notrans;
9461 }
Chris Mason5f39d392007-10-15 16:14:19 -04009462
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009463 if (dest != root) {
9464 ret = btrfs_record_root_in_trans(trans, dest);
9465 if (ret)
9466 goto out_fail;
9467 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009468
Nikolay Borisov877574e2017-02-20 13:50:33 +02009469 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009470 if (ret)
9471 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009472
Miao Xie67de1172013-12-26 13:07:06 +08009473 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009474 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009475 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009476 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009477 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009478 btrfs_pin_log_trans(root);
9479 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009480 ret = btrfs_insert_inode_ref(trans, dest,
9481 new_dentry->d_name.name,
9482 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009483 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009484 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009485 if (ret)
9486 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009487 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009488
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009489 inode_inc_iversion(old_dir);
9490 inode_inc_iversion(new_dir);
9491 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009492 old_dir->i_ctime = old_dir->i_mtime =
9493 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009494 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009495
Chris Mason12fcfd22009-03-24 10:24:20 -04009496 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009497 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9498 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009499
Li Zefan33345d012011-04-20 10:31:50 +08009500 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009501 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009502 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009503 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9504 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009505 old_dentry->d_name.name,
9506 old_dentry->d_name.len);
9507 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009508 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009509 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009510 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009511 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009512 goto out_fail;
9513 }
Chris Mason39279cc2007-06-12 06:35:45 -04009514
9515 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009516 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009517 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009518 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009519 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009520 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009521 BUG_ON(new_inode->i_nlink == 0);
9522 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009523 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9524 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009525 new_dentry->d_name.name,
9526 new_dentry->d_name.len);
9527 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009528 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009529 ret = btrfs_orphan_add(trans,
9530 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009531 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009532 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009533 goto out_fail;
9534 }
Chris Mason39279cc2007-06-12 06:35:45 -04009535 }
Josef Bacikaec74772008-07-24 12:12:38 -04009536
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009537 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009538 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009539 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009540 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009541 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009542 goto out_fail;
9543 }
Chris Mason39279cc2007-06-12 06:35:45 -04009544
Miao Xie67de1172013-12-26 13:07:06 +08009545 if (old_inode->i_nlink == 1)
9546 BTRFS_I(old_inode)->dir_index = index;
9547
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009548 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009549 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9550 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009551 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009552 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009553 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554
9555 if (flags & RENAME_WHITEOUT) {
9556 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9557 old_dentry);
9558
9559 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009560 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 goto out_fail;
9562 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009563 }
Chris Mason39279cc2007-06-12 06:35:45 -04009564out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009565 /*
9566 * If we have pinned the log and an error happened, we unpin tasks
9567 * trying to sync the log and force them to fallback to a transaction
9568 * commit if the log currently contains any of the inodes involved in
9569 * this rename operation (to ensure we do not persist a log with an
9570 * inconsistent state for any of these inodes or leading to any
9571 * inconsistencies when replayed). If the transaction was aborted, the
9572 * abortion reason is propagated to userspace when attempting to commit
9573 * the transaction. If the log does not contain any of these inodes, we
9574 * allow the tasks to sync it.
9575 */
9576 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009577 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9578 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9579 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009580 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009581 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009582 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009583
9584 btrfs_end_log_trans(root);
9585 log_pinned = false;
9586 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009587 ret2 = btrfs_end_transaction(trans);
9588 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009589out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009590 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009591 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009592
Chris Mason39279cc2007-06-12 06:35:45 -04009593 return ret;
9594}
9595
Christian Brauner549c7292021-01-21 14:19:43 +01009596static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9597 struct dentry *old_dentry, struct inode *new_dir,
9598 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009599{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009601 return -EINVAL;
9602
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009603 if (flags & RENAME_EXCHANGE)
9604 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9605 new_dentry);
9606
9607 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009608}
9609
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009610struct btrfs_delalloc_work {
9611 struct inode *inode;
9612 struct completion completion;
9613 struct list_head list;
9614 struct btrfs_work work;
9615};
9616
Miao Xie8ccf6f192012-10-25 09:28:04 +00009617static void btrfs_run_delalloc_work(struct btrfs_work *work)
9618{
9619 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009620 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009621
9622 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9623 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009624 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009625 filemap_flush(inode->i_mapping);
9626 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9627 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009628 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009629
Nikolay Borisov076da912018-04-23 10:54:16 +03009630 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009631 complete(&delalloc_work->completion);
9632}
9633
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009634static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009635{
9636 struct btrfs_delalloc_work *work;
9637
David Sterba100d5702015-12-08 14:39:32 +01009638 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009639 if (!work)
9640 return NULL;
9641
9642 init_completion(&work->completion);
9643 INIT_LIST_HEAD(&work->list);
9644 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009645 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009646
9647 return work;
9648}
9649
Chris Masond352ac62008-09-29 15:18:18 -04009650/*
9651 * some fairly slow code that needs optimization. This walks the list
9652 * of all the inodes with pending delalloc and forces them to disk.
9653 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009654static int start_delalloc_inodes(struct btrfs_root *root,
9655 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009656 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009657{
Chris Masonea8c2812008-08-04 23:17:27 -04009658 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009659 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009660 struct btrfs_delalloc_work *work, *next;
9661 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009662 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009663 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009664 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009665
Miao Xie8ccf6f192012-10-25 09:28:04 +00009666 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009667 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009668
Miao Xie573bfb72014-03-06 13:55:03 +08009669 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009670 spin_lock(&root->delalloc_lock);
9671 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009672 while (!list_empty(&splice)) {
9673 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009674 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009675
Miao Xieeb73c1b2013-05-15 07:48:22 +00009676 list_move_tail(&binode->delalloc_inodes,
9677 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009678
9679 if (in_reclaim_context &&
9680 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9681 continue;
9682
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009683 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009684 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009685 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009686 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009687 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009688 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009689
Ethan Lien3cd24c62018-11-01 14:49:03 +08009690 if (snapshot)
9691 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9692 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009693 if (full_flush) {
9694 work = btrfs_alloc_delalloc_work(inode);
9695 if (!work) {
9696 iput(inode);
9697 ret = -ENOMEM;
9698 goto out;
9699 }
9700 list_add_tail(&work->list, &works);
9701 btrfs_queue_work(root->fs_info->flush_workers,
9702 &work->work);
9703 } else {
9704 ret = sync_inode(inode, wbc);
9705 if (!ret &&
9706 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9707 &BTRFS_I(inode)->runtime_flags))
9708 ret = sync_inode(inode, wbc);
9709 btrfs_add_delayed_iput(inode);
9710 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009711 goto out;
9712 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009713 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009714 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009715 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009716 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009717
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009718out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009719 list_for_each_entry_safe(work, next, &works, list) {
9720 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009721 wait_for_completion(&work->completion);
9722 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009723 }
9724
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009725 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009726 spin_lock(&root->delalloc_lock);
9727 list_splice_tail(&splice, &root->delalloc_inodes);
9728 spin_unlock(&root->delalloc_lock);
9729 }
Miao Xie573bfb72014-03-06 13:55:03 +08009730 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009731 return ret;
9732}
9733
Filipe Mananaf9baa502021-04-22 12:08:05 +01009734int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009735{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009736 struct writeback_control wbc = {
9737 .nr_to_write = LONG_MAX,
9738 .sync_mode = WB_SYNC_NONE,
9739 .range_start = 0,
9740 .range_end = LLONG_MAX,
9741 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009742 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009744 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009745 return -EROFS;
9746
Filipe Mananaf9baa502021-04-22 12:08:05 +01009747 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009748}
9749
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009750int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009751 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009752{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009753 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009754 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009755 .sync_mode = WB_SYNC_NONE,
9756 .range_start = 0,
9757 .range_end = LLONG_MAX,
9758 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009759 struct btrfs_root *root;
9760 struct list_head splice;
9761 int ret;
9762
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009763 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009764 return -EROFS;
9765
9766 INIT_LIST_HEAD(&splice);
9767
Miao Xie573bfb72014-03-06 13:55:03 +08009768 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009769 spin_lock(&fs_info->delalloc_root_lock);
9770 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009771 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009772 /*
9773 * Reset nr_to_write here so we know that we're doing a full
9774 * flush.
9775 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009776 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009777 wbc.nr_to_write = LONG_MAX;
9778
Miao Xieeb73c1b2013-05-15 07:48:22 +00009779 root = list_first_entry(&splice, struct btrfs_root,
9780 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009781 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009782 BUG_ON(!root);
9783 list_move_tail(&root->delalloc_root,
9784 &fs_info->delalloc_roots);
9785 spin_unlock(&fs_info->delalloc_root_lock);
9786
Josef Bacike076ab2a2021-01-07 17:08:30 -05009787 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009788 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009789 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009790 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009791 spin_lock(&fs_info->delalloc_root_lock);
9792 }
9793 spin_unlock(&fs_info->delalloc_root_lock);
9794
Miao Xie6c255e62014-03-06 13:55:01 +08009795 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009796out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009797 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009798 spin_lock(&fs_info->delalloc_root_lock);
9799 list_splice_tail(&splice, &fs_info->delalloc_roots);
9800 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009801 }
Miao Xie573bfb72014-03-06 13:55:03 +08009802 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009803 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009804}
9805
Christian Brauner549c7292021-01-21 14:19:43 +01009806static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9807 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009808{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009809 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009810 struct btrfs_trans_handle *trans;
9811 struct btrfs_root *root = BTRFS_I(dir)->root;
9812 struct btrfs_path *path;
9813 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009814 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009815 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009816 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309817 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009818 int name_len;
9819 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009820 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009821 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009822 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009823
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009824 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009825 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009826 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009827
Josef Bacik9ed74f22009-09-11 16:12:44 -04009828 /*
9829 * 2 items for inode item and ref
9830 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009831 * 1 item for updating parent inode item
9832 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009833 * 1 item for xattr if selinux is on
9834 */
Filipe Manana9269d122015-12-31 18:16:29 +00009835 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009836 if (IS_ERR(trans))
9837 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009838
Nikolay Borisov543068a2020-12-07 17:32:33 +02009839 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009840 if (err)
9841 goto out_unlock;
9842
Josef Bacikaec74772008-07-24 12:12:38 -04009843 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009844 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9845 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009846 if (IS_ERR(inode)) {
9847 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009848 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009849 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009850 }
Chris Mason39279cc2007-06-12 06:35:45 -04009851
Casey Schauflerad19db72011-12-15 10:09:07 -05009852 /*
9853 * If the active LSM wants to access the inode during
9854 * d_instantiate it needs these. Smack checks to see
9855 * if the filesystem supports xattrs by looking at the
9856 * ops vector.
9857 */
9858 inode->i_fop = &btrfs_file_operations;
9859 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009860 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009861
9862 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9863 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009864 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009865
Chris Mason39279cc2007-06-12 06:35:45 -04009866 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009867 if (!path) {
9868 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009869 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009870 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009871 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009872 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009873 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009874 datasize = btrfs_file_extent_calc_inline_size(name_len);
9875 err = btrfs_insert_empty_item(trans, root, path, &key,
9876 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009877 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009878 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009879 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009880 }
Chris Mason5f39d392007-10-15 16:14:19 -04009881 leaf = path->nodes[0];
9882 ei = btrfs_item_ptr(leaf, path->slots[0],
9883 struct btrfs_file_extent_item);
9884 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9885 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009886 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009887 btrfs_set_file_extent_encryption(leaf, ei, 0);
9888 btrfs_set_file_extent_compression(leaf, ei, 0);
9889 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9890 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9891
Chris Mason39279cc2007-06-12 06:35:45 -04009892 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009893 write_extent_buffer(leaf, symname, ptr, name_len);
9894 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009895 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009896
Chris Mason39279cc2007-06-12 06:35:45 -04009897 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009898 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009899 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009900 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009901 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009902 /*
9903 * Last step, add directory indexes for our symlink inode. This is the
9904 * last step to avoid extra cleanup of these indexes if an error happens
9905 * elsewhere above.
9906 */
9907 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009908 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9909 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009910 if (err)
9911 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009912
Al Viro1e2e5472018-05-04 08:23:01 -04009913 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009914
9915out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009916 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009917 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009918 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009919 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009920 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009921 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009922 return err;
9923}
Chris Mason16432982008-04-10 10:23:21 -04009924
Filipe Manana8fccebf2020-09-08 11:27:20 +01009925static struct btrfs_trans_handle *insert_prealloc_file_extent(
9926 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009927 struct btrfs_inode *inode,
9928 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009929 u64 file_offset)
9930{
9931 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009932 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009933 struct btrfs_trans_handle *trans = trans_in;
9934 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009935 u64 start = ins->objectid;
9936 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009937 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +08009938 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009939
9940 memset(&stack_fi, 0, sizeof(stack_fi));
9941
9942 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9943 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9944 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9945 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9946 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9947 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9948 /* Encryption and other encoding is reserved and all 0 */
9949
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009950 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9951 if (qgroup_released < 0)
9952 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009953
9954 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009955 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009956 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009957 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009958 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009959 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009960 return trans;
9961 }
9962
9963 extent_info.disk_offset = start;
9964 extent_info.disk_len = len;
9965 extent_info.data_offset = 0;
9966 extent_info.data_len = len;
9967 extent_info.file_offset = file_offset;
9968 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009969 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +08009970 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009971 extent_info.insertions = 0;
9972
9973 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009974 if (!path) {
9975 ret = -ENOMEM;
9976 goto free_qgroup;
9977 }
Filipe Manana8fccebf2020-09-08 11:27:20 +01009978
Nikolay Borisovbfc78472021-02-17 15:12:47 +02009979 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009980 file_offset + len - 1, &extent_info,
9981 &trans);
9982 btrfs_free_path(path);
9983 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009984 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009985 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +08009986
9987free_qgroup:
9988 /*
9989 * We have released qgroup data range at the beginning of the function,
9990 * and normally qgroup_released bytes will be freed when committing
9991 * transaction.
9992 * But if we error out early, we have to free what we have released
9993 * or we leak qgroup data reservation.
9994 */
9995 btrfs_qgroup_free_refroot(inode->root->fs_info,
9996 inode->root->root_key.objectid, qgroup_released,
9997 BTRFS_QGROUP_RSV_DATA);
9998 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009999}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010000
Josef Bacik0af3d002010-06-21 14:48:16 -040010001static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10002 u64 start, u64 num_bytes, u64 min_size,
10003 loff_t actual_len, u64 *alloc_hint,
10004 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010005{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010007 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10008 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010009 struct btrfs_root *root = BTRFS_I(inode)->root;
10010 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010011 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010012 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010013 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010014 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010015 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010016 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010017 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010018 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010019
Josef Bacik0af3d002010-06-21 14:48:16 -040010020 if (trans)
10021 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010022 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010023 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010024 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010025 /*
10026 * If we are severely fragmented we could end up with really
10027 * small allocations, so if the allocator is returning small
10028 * chunks lets make its job easier by only searching for those
10029 * sized chunks.
10030 */
10031 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010032 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10033 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010034 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010035 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010036
10037 /*
10038 * We've reserved this space, and thus converted it from
10039 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10040 * from here on out we will only need to clear our reservation
10041 * for the remaining unreserved area, so advance our
10042 * clear_offset by our extent size.
10043 */
10044 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010045
Josef Bacik0b670dc2015-09-23 17:11:16 -040010046 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010047 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10048 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010049 /*
10050 * Now that we inserted the prealloc extent we can finally
10051 * decrement the number of reservations in the block group.
10052 * If we did it before, we could race with relocation and have
10053 * relocation miss the reserved extent, making it fail later.
10054 */
10055 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010056 if (IS_ERR(trans)) {
10057 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010058 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010059 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010060 break;
10061 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010062
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010063 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010064 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010065
Josef Bacik5dc562c2012-08-17 13:14:17 -040010066 em = alloc_extent_map();
10067 if (!em) {
10068 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10069 &BTRFS_I(inode)->runtime_flags);
10070 goto next;
10071 }
10072
10073 em->start = cur_offset;
10074 em->orig_start = cur_offset;
10075 em->len = ins.offset;
10076 em->block_start = ins.objectid;
10077 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010078 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010079 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010080 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10081 em->generation = trans->transid;
10082
10083 while (1) {
10084 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010085 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010086 write_unlock(&em_tree->lock);
10087 if (ret != -EEXIST)
10088 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010089 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010090 cur_offset + ins.offset - 1,
10091 0);
10092 }
10093 free_extent_map(em);
10094next:
Yan Zhengd899e052008-10-30 14:25:28 -040010095 num_bytes -= ins.offset;
10096 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010097 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010098
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010099 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010100 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010101 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010102 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010103 (actual_len > inode->i_size) &&
10104 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010105 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010106 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010107 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010108 i_size = cur_offset;
10109 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010110 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010111 }
10112
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010113 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010114
10115 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010116 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010117 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010118 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010119 break;
10120 }
Yan Zhengd899e052008-10-30 14:25:28 -040010121
Filipe Manana8fccebf2020-09-08 11:27:20 +010010122 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010123 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010124 trans = NULL;
10125 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010126 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010127 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010128 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010129 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010130 return ret;
10131}
10132
Josef Bacik0af3d002010-06-21 14:48:16 -040010133int btrfs_prealloc_file_range(struct inode *inode, int mode,
10134 u64 start, u64 num_bytes, u64 min_size,
10135 loff_t actual_len, u64 *alloc_hint)
10136{
10137 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10138 min_size, actual_len, alloc_hint,
10139 NULL);
10140}
10141
10142int btrfs_prealloc_file_range_trans(struct inode *inode,
10143 struct btrfs_trans_handle *trans, int mode,
10144 u64 start, u64 num_bytes, u64 min_size,
10145 loff_t actual_len, u64 *alloc_hint)
10146{
10147 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10148 min_size, actual_len, alloc_hint, trans);
10149}
10150
Chris Masone6dcd2d2008-07-17 12:53:50 -040010151static int btrfs_set_page_dirty(struct page *page)
10152{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010153 return __set_page_dirty_nobuffers(page);
10154}
10155
Christian Brauner549c7292021-01-21 14:19:43 +010010156static int btrfs_permission(struct user_namespace *mnt_userns,
10157 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010158{
Li Zefanb83cc962010-12-20 16:04:08 +080010159 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010160 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010161
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010162 if (mask & MAY_WRITE &&
10163 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10164 if (btrfs_root_readonly(root))
10165 return -EROFS;
10166 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10167 return -EACCES;
10168 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010169 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010170}
Chris Mason39279cc2007-06-12 06:35:45 -040010171
Christian Brauner549c7292021-01-21 14:19:43 +010010172static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10173 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010174{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010175 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010176 struct btrfs_trans_handle *trans;
10177 struct btrfs_root *root = BTRFS_I(dir)->root;
10178 struct inode *inode = NULL;
10179 u64 objectid;
10180 u64 index;
10181 int ret = 0;
10182
10183 /*
10184 * 5 units required for adding orphan entry
10185 */
10186 trans = btrfs_start_transaction(root, 5);
10187 if (IS_ERR(trans))
10188 return PTR_ERR(trans);
10189
Nikolay Borisov543068a2020-12-07 17:32:33 +020010190 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010191 if (ret)
10192 goto out;
10193
10194 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010195 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010196 if (IS_ERR(inode)) {
10197 ret = PTR_ERR(inode);
10198 inode = NULL;
10199 goto out;
10200 }
10201
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010202 inode->i_fop = &btrfs_file_operations;
10203 inode->i_op = &btrfs_file_inode_operations;
10204
10205 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010206
Chris Masonb0d5d102014-09-08 13:08:51 -070010207 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10208 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010209 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010210
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010211 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010212 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010213 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010214 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010215 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010216 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010217
Filipe Manana5762b5c2014-08-01 00:10:32 +010010218 /*
10219 * We set number of links to 0 in btrfs_new_inode(), and here we set
10220 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10221 * through:
10222 *
10223 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10224 */
10225 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010226 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010227 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010228 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010229out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010230 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010231 if (ret && inode)
10232 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010233 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010234 return ret;
10235}
10236
David Sterba5cdc84b2018-07-18 20:32:52 +020010237void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010238{
David Sterba5cdc84b2018-07-18 20:32:52 +020010239 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010240 unsigned long index = start >> PAGE_SHIFT;
10241 unsigned long end_index = end >> PAGE_SHIFT;
10242 struct page *page;
10243
10244 while (index <= end_index) {
10245 page = find_get_page(inode->i_mapping, index);
10246 ASSERT(page); /* Pages should be in the extent_io_tree */
10247 set_page_writeback(page);
10248 put_page(page);
10249 index++;
10250 }
10251}
10252
Omar Sandovaled46ff32016-11-03 10:28:14 -070010253#ifdef CONFIG_SWAP
10254/*
10255 * Add an entry indicating a block group or device which is pinned by a
10256 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10257 * negative errno on failure.
10258 */
10259static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10260 bool is_block_group)
10261{
10262 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10263 struct btrfs_swapfile_pin *sp, *entry;
10264 struct rb_node **p;
10265 struct rb_node *parent = NULL;
10266
10267 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10268 if (!sp)
10269 return -ENOMEM;
10270 sp->ptr = ptr;
10271 sp->inode = inode;
10272 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010273 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010274
10275 spin_lock(&fs_info->swapfile_pins_lock);
10276 p = &fs_info->swapfile_pins.rb_node;
10277 while (*p) {
10278 parent = *p;
10279 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10280 if (sp->ptr < entry->ptr ||
10281 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10282 p = &(*p)->rb_left;
10283 } else if (sp->ptr > entry->ptr ||
10284 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10285 p = &(*p)->rb_right;
10286 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010287 if (is_block_group)
10288 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010289 spin_unlock(&fs_info->swapfile_pins_lock);
10290 kfree(sp);
10291 return 1;
10292 }
10293 }
10294 rb_link_node(&sp->node, parent, p);
10295 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10296 spin_unlock(&fs_info->swapfile_pins_lock);
10297 return 0;
10298}
10299
10300/* Free all of the entries pinned by this swapfile. */
10301static void btrfs_free_swapfile_pins(struct inode *inode)
10302{
10303 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10304 struct btrfs_swapfile_pin *sp;
10305 struct rb_node *node, *next;
10306
10307 spin_lock(&fs_info->swapfile_pins_lock);
10308 node = rb_first(&fs_info->swapfile_pins);
10309 while (node) {
10310 next = rb_next(node);
10311 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10312 if (sp->inode == inode) {
10313 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010314 if (sp->is_block_group) {
10315 btrfs_dec_block_group_swap_extents(sp->ptr,
10316 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010317 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010318 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010319 kfree(sp);
10320 }
10321 node = next;
10322 }
10323 spin_unlock(&fs_info->swapfile_pins_lock);
10324}
10325
10326struct btrfs_swap_info {
10327 u64 start;
10328 u64 block_start;
10329 u64 block_len;
10330 u64 lowest_ppage;
10331 u64 highest_ppage;
10332 unsigned long nr_pages;
10333 int nr_extents;
10334};
10335
10336static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10337 struct btrfs_swap_info *bsi)
10338{
10339 unsigned long nr_pages;
10340 u64 first_ppage, first_ppage_reported, next_ppage;
10341 int ret;
10342
10343 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10344 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10345 PAGE_SIZE) >> PAGE_SHIFT;
10346
10347 if (first_ppage >= next_ppage)
10348 return 0;
10349 nr_pages = next_ppage - first_ppage;
10350
10351 first_ppage_reported = first_ppage;
10352 if (bsi->start == 0)
10353 first_ppage_reported++;
10354 if (bsi->lowest_ppage > first_ppage_reported)
10355 bsi->lowest_ppage = first_ppage_reported;
10356 if (bsi->highest_ppage < (next_ppage - 1))
10357 bsi->highest_ppage = next_ppage - 1;
10358
10359 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10360 if (ret < 0)
10361 return ret;
10362 bsi->nr_extents += ret;
10363 bsi->nr_pages += nr_pages;
10364 return 0;
10365}
10366
10367static void btrfs_swap_deactivate(struct file *file)
10368{
10369 struct inode *inode = file_inode(file);
10370
10371 btrfs_free_swapfile_pins(inode);
10372 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10373}
10374
10375static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10376 sector_t *span)
10377{
10378 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010379 struct btrfs_root *root = BTRFS_I(inode)->root;
10380 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010381 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10382 struct extent_state *cached_state = NULL;
10383 struct extent_map *em = NULL;
10384 struct btrfs_device *device = NULL;
10385 struct btrfs_swap_info bsi = {
10386 .lowest_ppage = (sector_t)-1ULL,
10387 };
10388 int ret = 0;
10389 u64 isize;
10390 u64 start;
10391
10392 /*
10393 * If the swap file was just created, make sure delalloc is done. If the
10394 * file changes again after this, the user is doing something stupid and
10395 * we don't really care.
10396 */
10397 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10398 if (ret)
10399 return ret;
10400
10401 /*
10402 * The inode is locked, so these flags won't change after we check them.
10403 */
10404 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10405 btrfs_warn(fs_info, "swapfile must not be compressed");
10406 return -EINVAL;
10407 }
10408 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10409 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10410 return -EINVAL;
10411 }
10412 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10413 btrfs_warn(fs_info, "swapfile must not be checksummed");
10414 return -EINVAL;
10415 }
10416
10417 /*
10418 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010419 * under us. The exclop protection makes sure they aren't running/won't
10420 * run concurrently while we are mapping the swap extents, and
10421 * fs_info->swapfile_pins prevents them from running while the swap
10422 * file is active and moving the extents. Note that this also prevents
10423 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010424 * really worth the trouble to allow it.
10425 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010426 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010427 btrfs_warn(fs_info,
10428 "cannot activate swapfile while exclusive operation is running");
10429 return -EBUSY;
10430 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010431
10432 /*
10433 * Prevent snapshot creation while we are activating the swap file.
10434 * We do not want to race with snapshot creation. If snapshot creation
10435 * already started before we bumped nr_swapfiles from 0 to 1 and
10436 * completes before the first write into the swap file after it is
10437 * activated, than that write would fallback to COW.
10438 */
10439 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10440 btrfs_exclop_finish(fs_info);
10441 btrfs_warn(fs_info,
10442 "cannot activate swapfile because snapshot creation is in progress");
10443 return -EINVAL;
10444 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010445 /*
10446 * Snapshots can create extents which require COW even if NODATACOW is
10447 * set. We use this counter to prevent snapshots. We must increment it
10448 * before walking the extents because we don't want a concurrent
10449 * snapshot to run after we've already checked the extents.
10450 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010451 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010452
10453 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10454
10455 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10456 start = 0;
10457 while (start < isize) {
10458 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010459 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010460 u64 len = isize - start;
10461
Omar Sandoval39b07b52019-12-02 17:34:23 -080010462 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010463 if (IS_ERR(em)) {
10464 ret = PTR_ERR(em);
10465 goto out;
10466 }
10467
10468 if (em->block_start == EXTENT_MAP_HOLE) {
10469 btrfs_warn(fs_info, "swapfile must not have holes");
10470 ret = -EINVAL;
10471 goto out;
10472 }
10473 if (em->block_start == EXTENT_MAP_INLINE) {
10474 /*
10475 * It's unlikely we'll ever actually find ourselves
10476 * here, as a file small enough to fit inline won't be
10477 * big enough to store more than the swap header, but in
10478 * case something changes in the future, let's catch it
10479 * here rather than later.
10480 */
10481 btrfs_warn(fs_info, "swapfile must not be inline");
10482 ret = -EINVAL;
10483 goto out;
10484 }
10485 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10486 btrfs_warn(fs_info, "swapfile must not be compressed");
10487 ret = -EINVAL;
10488 goto out;
10489 }
10490
10491 logical_block_start = em->block_start + (start - em->start);
10492 len = min(len, em->len - (start - em->start));
10493 free_extent_map(em);
10494 em = NULL;
10495
Boris Burkova84d5d42020-08-18 11:00:05 -070010496 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010497 if (ret < 0) {
10498 goto out;
10499 } else if (ret) {
10500 ret = 0;
10501 } else {
10502 btrfs_warn(fs_info,
10503 "swapfile must not be copy-on-write");
10504 ret = -EINVAL;
10505 goto out;
10506 }
10507
10508 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10509 if (IS_ERR(em)) {
10510 ret = PTR_ERR(em);
10511 goto out;
10512 }
10513
10514 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10515 btrfs_warn(fs_info,
10516 "swapfile must have single data profile");
10517 ret = -EINVAL;
10518 goto out;
10519 }
10520
10521 if (device == NULL) {
10522 device = em->map_lookup->stripes[0].dev;
10523 ret = btrfs_add_swapfile_pin(inode, device, false);
10524 if (ret == 1)
10525 ret = 0;
10526 else if (ret)
10527 goto out;
10528 } else if (device != em->map_lookup->stripes[0].dev) {
10529 btrfs_warn(fs_info, "swapfile must be on one device");
10530 ret = -EINVAL;
10531 goto out;
10532 }
10533
10534 physical_block_start = (em->map_lookup->stripes[0].physical +
10535 (logical_block_start - em->start));
10536 len = min(len, em->len - (logical_block_start - em->start));
10537 free_extent_map(em);
10538 em = NULL;
10539
10540 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10541 if (!bg) {
10542 btrfs_warn(fs_info,
10543 "could not find block group containing swapfile");
10544 ret = -EINVAL;
10545 goto out;
10546 }
10547
Filipe Manana195a49e2021-02-05 12:55:37 +000010548 if (!btrfs_inc_block_group_swap_extents(bg)) {
10549 btrfs_warn(fs_info,
10550 "block group for swapfile at %llu is read-only%s",
10551 bg->start,
10552 atomic_read(&fs_info->scrubs_running) ?
10553 " (scrub running)" : "");
10554 btrfs_put_block_group(bg);
10555 ret = -EINVAL;
10556 goto out;
10557 }
10558
Omar Sandovaled46ff32016-11-03 10:28:14 -070010559 ret = btrfs_add_swapfile_pin(inode, bg, true);
10560 if (ret) {
10561 btrfs_put_block_group(bg);
10562 if (ret == 1)
10563 ret = 0;
10564 else
10565 goto out;
10566 }
10567
10568 if (bsi.block_len &&
10569 bsi.block_start + bsi.block_len == physical_block_start) {
10570 bsi.block_len += len;
10571 } else {
10572 if (bsi.block_len) {
10573 ret = btrfs_add_swap_extent(sis, &bsi);
10574 if (ret)
10575 goto out;
10576 }
10577 bsi.start = start;
10578 bsi.block_start = physical_block_start;
10579 bsi.block_len = len;
10580 }
10581
10582 start += len;
10583 }
10584
10585 if (bsi.block_len)
10586 ret = btrfs_add_swap_extent(sis, &bsi);
10587
10588out:
10589 if (!IS_ERR_OR_NULL(em))
10590 free_extent_map(em);
10591
10592 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10593
10594 if (ret)
10595 btrfs_swap_deactivate(file);
10596
Filipe Mananadd0734f2021-02-05 12:55:38 +000010597 btrfs_drew_write_unlock(&root->snapshot_lock);
10598
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010599 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010600
10601 if (ret)
10602 return ret;
10603
10604 if (device)
10605 sis->bdev = device->bdev;
10606 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10607 sis->max = bsi.nr_pages;
10608 sis->pages = bsi.nr_pages - 1;
10609 sis->highest_bit = bsi.nr_pages - 1;
10610 return bsi.nr_extents;
10611}
10612#else
10613static void btrfs_swap_deactivate(struct file *file)
10614{
10615}
10616
10617static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10618 sector_t *span)
10619{
10620 return -EOPNOTSUPP;
10621}
10622#endif
10623
Filipe Manana2766ff62020-11-04 11:07:34 +000010624/*
10625 * Update the number of bytes used in the VFS' inode. When we replace extents in
10626 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10627 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10628 * always get a correct value.
10629 */
10630void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10631 const u64 add_bytes,
10632 const u64 del_bytes)
10633{
10634 if (add_bytes == del_bytes)
10635 return;
10636
10637 spin_lock(&inode->lock);
10638 if (del_bytes > 0)
10639 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10640 if (add_bytes > 0)
10641 inode_add_bytes(&inode->vfs_inode, add_bytes);
10642 spin_unlock(&inode->lock);
10643}
10644
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010645static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010646 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010647 .lookup = btrfs_lookup,
10648 .create = btrfs_create,
10649 .unlink = btrfs_unlink,
10650 .link = btrfs_link,
10651 .mkdir = btrfs_mkdir,
10652 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010653 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010654 .symlink = btrfs_symlink,
10655 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010656 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010657 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010658 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010659 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010660 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010661 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010662 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010663 .fileattr_get = btrfs_fileattr_get,
10664 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010665};
Yan, Zheng76dda932009-09-21 16:00:26 -040010666
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010667static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010668 .llseek = generic_file_llseek,
10669 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010670 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010671 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010672 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010673#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010674 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010675#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010676 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010677 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010678};
10679
Chris Mason35054392009-01-21 13:11:13 -050010680/*
10681 * btrfs doesn't support the bmap operation because swapfiles
10682 * use bmap to make a mapping of extents in the file. They assume
10683 * these extents won't change over the life of the file and they
10684 * use the bmap result to do IO directly to the drive.
10685 *
10686 * the btrfs bmap call would return logical addresses that aren't
10687 * suitable for IO and they also will change frequently as COW
10688 * operations happen. So, swapfile + btrfs == corruption.
10689 *
10690 * For now we're avoiding this by dropping bmap.
10691 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010692static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010693 .readpage = btrfs_readpage,
10694 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010695 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010696 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010697 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010698 .invalidatepage = btrfs_invalidatepage,
10699 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010700#ifdef CONFIG_MIGRATION
10701 .migratepage = btrfs_migratepage,
10702#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010703 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010704 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010705 .swap_activate = btrfs_swap_activate,
10706 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010707};
10708
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010709static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010710 .getattr = btrfs_getattr,
10711 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010712 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010713 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010714 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010715 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010716 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010717 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010718 .fileattr_get = btrfs_fileattr_get,
10719 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010720};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010721static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010722 .getattr = btrfs_getattr,
10723 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010724 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010725 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010726 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010727 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010728 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010729};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010730static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010731 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010732 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010733 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010734 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010735 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010736 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010737};
Yan, Zheng76dda932009-09-21 16:00:26 -040010738
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010739const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010740 .d_delete = btrfs_dentry_delete,
10741};