blob: c917e311784238e71dc4a3ee32cf653360c4f4b6 [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
David Sterba4c2bf272021-06-15 17:15:38 +0200289 kaddr = page_address(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400290 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonc8b97812008-10-29 14:49:59 -0400291
292 i++;
293 ptr += cur_size;
294 compressed_size -= cur_size;
295 }
296 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800297 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400298 } else {
299 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300300 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400301 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800302 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100303 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400304 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800305 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300306 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400307 }
308 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400310
Yan, Zhengc2167752009-11-12 09:34:21 +0000311 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500312 * We align size to sectorsize for inline extents just for simplicity
313 * sake.
314 */
315 size = ALIGN(size, root->fs_info->sectorsize);
316 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
317 if (ret)
318 goto fail;
319
320 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000321 * we're an inline extent, so nobody can
322 * extend the file past i_size without locking
323 * a page we already have locked.
324 *
325 * We must do any isize and inode updates
326 * before we unlock the pages. Otherwise we
327 * could end up racing with unlink.
328 */
Chris Masonc8b97812008-10-29 14:49:59 -0400329 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400330fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200331 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400332}
333
334
335/*
336 * conditionally insert an inline extent into the file. This
337 * does the checks required to make sure the data is small enough
338 * to fit as an inline extent.
339 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300340static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400341 u64 end, size_t compressed_size,
342 int compress_type,
343 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400344{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000345 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300346 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400347 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400348 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300349 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400350 u64 actual_end = min(end + 1, isize);
351 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400352 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400353 u64 data_len = inline_len;
354 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000355 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400356
357 if (compressed_size)
358 data_len = compressed_size;
359
360 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400361 actual_end > fs_info->sectorsize ||
362 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400363 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400364 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400365 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400366 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400367 return 1;
368 }
369
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000370 path = btrfs_alloc_path();
371 if (!path)
372 return -ENOMEM;
373
Josef Bacik00361582013-08-14 14:02:47 -0400374 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000375 if (IS_ERR(trans)) {
376 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400377 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000378 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300379 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400380
Filipe Manana5893dfb2020-11-04 11:07:32 +0000381 drop_args.path = path;
382 drop_args.start = start;
383 drop_args.end = aligned_end;
384 drop_args.drop_cache = true;
385 drop_args.replace_extent = true;
386
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000387 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000388 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000389 compressed_size);
390 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000391 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000392 inline_len);
393
Filipe Manana5893dfb2020-11-04 11:07:32 +0000394 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400395 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400396 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400397 goto out;
398 }
Chris Masonc8b97812008-10-29 14:49:59 -0400399
400 if (isize > actual_end)
401 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000402 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300403 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400404 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000405 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400406 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400407 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400408 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400409 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400410 ret = 1;
411 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100412 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400413
Filipe Manana2766ff62020-11-04 11:07:34 +0000414 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200415 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000416 if (ret && ret != -ENOSPC) {
417 btrfs_abort_transaction(trans, ret);
418 goto out;
419 } else if (ret == -ENOSPC) {
420 ret = 1;
421 goto out;
422 }
423
Nikolay Borisova0349402020-06-03 08:55:12 +0300424 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400425out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800426 /*
427 * Don't forget to free the reserved space, as for inlined extent
428 * it won't count as data extent, free them directly here.
429 * And at reserve time, it's always aligned to page size, so
430 * just free one page here.
431 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300432 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000433 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400434 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400435 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400436}
437
Chris Mason771ed682008-11-06 22:02:51 -0500438struct async_extent {
439 u64 start;
440 u64 ram_size;
441 u64 compressed_size;
442 struct page **pages;
443 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800444 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500445 struct list_head list;
446};
447
Nikolay Borisov97db1202019-03-12 17:20:24 +0200448struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500449 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500450 struct page *locked_page;
451 u64 start;
452 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600453 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700455 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500456 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200457 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500458};
459
Nikolay Borisov97db1202019-03-12 17:20:24 +0200460struct async_cow {
461 /* Number of chunks in flight; must be first in the structure */
462 atomic_t num_chunks;
463 struct async_chunk chunks[];
464};
465
466static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500467 u64 start, u64 ram_size,
468 u64 compressed_size,
469 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800470 unsigned long nr_pages,
471 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500472{
473 struct async_extent *async_extent;
474
475 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100476 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500477 async_extent->start = start;
478 async_extent->ram_size = ram_size;
479 async_extent->compressed_size = compressed_size;
480 async_extent->pages = pages;
481 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800482 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500483 list_add_tail(&async_extent->list, &cow->extents);
484 return 0;
485}
486
Qu Wenruo42c16da2019-07-01 05:12:46 +0000487/*
488 * Check if the inode has flags compatible with compression
489 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300490static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000491{
Qu Wenruo95ea0482021-07-26 14:35:06 +0800492 /* Subpage doesn't support compression yet */
493 if (inode->root->fs_info->sectorsize < PAGE_SIZE)
494 return false;
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300495 if (inode->flags & BTRFS_INODE_NODATACOW ||
496 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000497 return false;
498 return true;
499}
500
501/*
502 * Check if the inode needs to be submitted to compression, based on mount
503 * options, defragmentation, properties or heuristics.
504 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300505static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
506 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800507{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300508 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800509
Nikolay Borisov808a1292020-06-03 08:55:27 +0300510 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000511 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
512 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300513 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000514 return 0;
515 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800516 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400517 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800518 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200519 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300520 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200521 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800522 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300523 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800524 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400525 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300526 inode->flags & BTRFS_INODE_COMPRESS ||
527 inode->prop_compress)
528 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800529 return 0;
530}
531
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200532static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800533 u64 start, u64 end, u64 num_bytes, u64 small_write)
534{
535 /* If this is a small write inside eof, kick off a defrag */
536 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200537 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800538 btrfs_add_inode_defrag(NULL, inode);
539}
540
Chris Masonc8b97812008-10-29 14:49:59 -0400541/*
Chris Mason771ed682008-11-06 22:02:51 -0500542 * we create compressed extents in two phases. The first
543 * phase compresses a range of pages that have already been
544 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400545 *
Chris Mason771ed682008-11-06 22:02:51 -0500546 * This is done inside an ordered work queue, and the compression
547 * is spread across many cpus. The actual IO submission is step
548 * two, and the ordered work queue takes care of making sure that
549 * happens in the same order things were put onto the queue by
550 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400551 *
Chris Mason771ed682008-11-06 22:02:51 -0500552 * If this code finds it can't get good compression, it puts an
553 * entry onto the work queue to write the uncompressed bytes. This
554 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300555 * are written in the same order that the flusher thread sent them
556 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400557 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300558static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400559{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200560 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400561 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400562 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200563 u64 start = async_chunk->start;
564 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400565 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400566 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400567 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400568 struct page **pages = NULL;
569 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400570 unsigned long total_compressed = 0;
571 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400572 int i;
573 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400574 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300575 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400576 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400577
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200578 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
579 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400580
Josef Bacikd98da492019-10-11 09:03:54 -0400581 /*
582 * We need to save i_size before now because it could change in between
583 * us evaluating the size and assigning it. This is because we lock and
584 * unlock the page in truncate and fallocate, and then modify the i_size
585 * later on.
586 *
587 * The barriers are to emulate READ_ONCE, remove that once i_size_read
588 * does that for us.
589 */
590 barrier();
591 i_size = i_size_read(inode);
592 barrier();
593 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400594again:
595 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100597 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
598 nr_pages = min_t(unsigned long, nr_pages,
599 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400600
Chris Masonf03d9301f2009-02-04 09:31:06 -0500601 /*
602 * we don't want to send crud past the end of i_size through
603 * compression, that's just a waste of CPU time. So, if the
604 * end of the file is before the start of our current
605 * requested range of bytes, we bail out to the uncompressed
606 * cleanup code that can deal with all of this.
607 *
608 * It isn't really the fastest way to fix things, but this is a
609 * very uncommon corner.
610 */
611 if (actual_end <= start)
612 goto cleanup_and_bail_uncompressed;
613
Chris Masonc8b97812008-10-29 14:49:59 -0400614 total_compressed = actual_end - start;
615
Shilong Wang4bcbb332014-10-07 18:44:35 -0400616 /*
617 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400618 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400619 */
620 if (total_compressed <= blocksize &&
621 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
622 goto cleanup_and_bail_uncompressed;
623
David Sterba069eac72017-02-14 19:36:54 +0100624 total_compressed = min_t(unsigned long, total_compressed,
625 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400626 total_in = 0;
627 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400628
Chris Mason771ed682008-11-06 22:02:51 -0500629 /*
630 * we do compression for mount -o compress and when the
631 * inode has not been flagged as nocompress. This flag can
632 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400633 */
David Sterbaf2165622021-06-14 12:45:18 +0200634 if (nr_pages > 1 && inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400635 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100636 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800637 if (!pages) {
638 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100639 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800640 goto cont;
641 }
Chris Mason179e29e2007-11-01 11:28:41 -0400642
David Sterbaeec63c62017-07-17 19:41:31 +0200643 if (BTRFS_I(inode)->defrag_compress)
644 compress_type = BTRFS_I(inode)->defrag_compress;
645 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200646 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800647
Chris Mason4adaa612013-03-26 13:07:00 -0400648 /*
649 * we need to call clear_page_dirty_for_io on each
650 * page in the range. Otherwise applications with the file
651 * mmap'd can wander in and change the page contents while
652 * we are compressing them.
653 *
654 * If the compression fails for any reason, we set the pages
655 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300656 *
657 * Note that the remaining part is redirtied, the start pointer
658 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400659 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300660 if (!redirty) {
661 extent_range_clear_dirty_for_io(inode, start, end);
662 redirty = 1;
663 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200664
665 /* Compression level is applied here and only here */
666 ret = btrfs_compress_pages(
667 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800668 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100669 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100670 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800671 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100672 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400673
674 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100675 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100676 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400677
678 /* zero the tail end of the last page, we might be
679 * sending it down to disk
680 */
Ira Weinyd048b9c2021-05-04 18:40:07 -0700681 if (offset)
682 memzero_page(page, offset, PAGE_SIZE - offset);
Chris Masonc8b97812008-10-29 14:49:59 -0400683 will_compress = 1;
684 }
685 }
Li Zefan560f7d72011-09-08 10:22:01 +0800686cont:
Qu Wenruo73672532021-07-26 14:34:59 +0800687 /*
688 * Check cow_file_range() for why we don't even try to create inline
689 * extent for subpage case.
690 */
691 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400692 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300693 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400694 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500695 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400696 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300697 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
698 0, BTRFS_COMPRESS_NONE,
699 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400700 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500701 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300702 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000703 total_compressed,
704 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400705 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100706 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400707 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400708 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
709 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100710 unsigned long page_error_op;
711
Filipe Mananae6eb4312014-10-10 10:45:12 +0100712 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400713
Chris Mason771ed682008-11-06 22:02:51 -0500714 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100715 * inline extent creation worked or returned error,
716 * we don't need to create any more async work items.
717 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400718 *
719 * We use DO_ACCOUNTING here because we need the
720 * delalloc_release_metadata to be done _after_ we drop
721 * our outstanding extent for clearing delalloc for this
722 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500723 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300724 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
725 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300726 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800727 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800728 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100729 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400730 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300731
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800732 /*
733 * Ensure we only free the compressed pages if we have
734 * them allocated, as we can still reach here with
735 * inode_need_compress() == false.
736 */
737 if (pages) {
738 for (i = 0; i < nr_pages; i++) {
739 WARN_ON(pages[i]->mapping);
740 put_page(pages[i]);
741 }
742 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300743 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300744 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400745 }
746 }
747
748 if (will_compress) {
749 /*
750 * we aren't doing an inline extent round the compressed size
751 * up to a block size boundary so the allocator does sane
752 * things
753 */
Qu Wenruofda28322013-02-26 08:10:22 +0000754 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400755
756 /*
757 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300758 * win, compare the page count read with the blocks on disk,
759 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400760 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300762 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300763 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700764
765 /*
766 * The async work queues will take care of doing actual
767 * allocation on disk for these compressed pages, and
768 * will submit them to the elevator.
769 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200770 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100771 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700772 compress_type);
773
Timofey Titovets11708622017-10-03 18:06:01 +0300774 if (start + total_in < end) {
775 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700776 pages = NULL;
777 cond_resched();
778 goto again;
779 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300780 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400781 }
782 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700783 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400784 /*
785 * the compression code ran but failed to make things smaller,
786 * free any pages it allocated and our page pointer array
787 */
David Sterba4d3a8002017-02-14 19:04:07 +0100788 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400789 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300790 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400791 }
792 kfree(pages);
793 pages = NULL;
794 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100795 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400796
797 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400798 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200799 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500800 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500801 }
Chris Masonc8b97812008-10-29 14:49:59 -0400802 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500803cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700804 /*
805 * No compression, but we still need to write the pages in the file
806 * we've been given so far. redirty the locked page if it corresponds
807 * to our extent and set things up for the async work queue to run
808 * cow_file_range to do the normal delalloc dance.
809 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700810 if (async_chunk->locked_page &&
811 (page_offset(async_chunk->locked_page) >= start &&
812 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200813 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700814 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700815 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700816
817 if (redirty)
818 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200819 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700820 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300821 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500822
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300823 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500824}
Chris Mason771ed682008-11-06 22:02:51 -0500825
Filipe Manana40ae8372014-10-06 22:14:24 +0100826static void free_async_extent_pages(struct async_extent *async_extent)
827{
828 int i;
829
830 if (!async_extent->pages)
831 return;
832
833 for (i = 0; i < async_extent->nr_pages; i++) {
834 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300835 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100836 }
837 kfree(async_extent->pages);
838 async_extent->nr_pages = 0;
839 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500840}
841
842/*
843 * phase two of compressed writeback. This is the ordered portion
844 * of the code, which only gets called in the order the work was
845 * queued. We walk all the async extents created by compress_file_range
846 * and send them down to the disk.
847 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200848static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500849{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300850 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
851 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500852 struct async_extent *async_extent;
853 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500854 struct btrfs_key ins;
855 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300856 struct btrfs_root *root = inode->root;
857 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500858 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500859
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500860again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200861 while (!list_empty(&async_chunk->extents)) {
862 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500863 struct async_extent, list);
864 list_del(&async_extent->list);
865
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500866retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200867 lock_extent(io_tree, async_extent->start,
868 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500869 /* did the compression code fall back to uncompressed IO? */
870 if (!async_extent->pages) {
871 int page_started = 0;
872 unsigned long nr_written = 0;
873
Chris Mason771ed682008-11-06 22:02:51 -0500874 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300875 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500876 async_extent->start,
877 async_extent->start +
878 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300879 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500880
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100881 /* JDM XXX */
882
Chris Mason771ed682008-11-06 22:02:51 -0500883 /*
884 * if page_started, cow_file_range inserted an
885 * inline extent and took care of all the unlocking
886 * and IO for us. Otherwise, we need to submit
887 * all those pages down to the drive.
888 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500889 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300890 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200891 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500892 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500893 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500894 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700895 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200896 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500897 kfree(async_extent);
898 cond_resched();
899 continue;
900 }
901
Wang Xiaoguang18513092016-07-25 15:51:40 +0800902 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500903 async_extent->compressed_size,
904 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800905 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500906 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100907 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500908
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400909 if (ret == -ENOSPC) {
910 unlock_extent(io_tree, async_extent->start,
911 async_extent->start +
912 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800913
914 /*
915 * we need to redirty the pages if we decide to
916 * fallback to uncompressed IO, otherwise we
917 * will not submit these pages down to lower
918 * layers.
919 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300920 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800921 async_extent->start,
922 async_extent->start +
923 async_extent->ram_size - 1);
924
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400926 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500927 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500928 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000929 /*
930 * here we're doing allocation and writeback of the
931 * compressed pages
932 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300933 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800934 async_extent->ram_size, /* len */
935 async_extent->start, /* orig_start */
936 ins.objectid, /* block_start */
937 ins.offset, /* block_len */
938 ins.offset, /* orig_block_len */
939 async_extent->ram_size, /* ram_bytes */
940 async_extent->compress_type,
941 BTRFS_ORDERED_COMPRESSED);
942 if (IS_ERR(em))
943 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500944 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800945 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500946
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300947 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800948 async_extent->start,
949 ins.objectid,
950 async_extent->ram_size,
951 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800952 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100953 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300954 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100955 async_extent->start +
956 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500957 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100958 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500960
Chris Mason771ed682008-11-06 22:02:51 -0500961 /*
962 * clear dirty, set writeback and unlock the pages.
963 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300964 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400965 async_extent->start +
966 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400967 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800968 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300969 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500970 async_extent->ram_size,
971 ins.objectid,
972 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600973 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700974 async_chunk->write_flags,
975 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100976 struct page *p = async_extent->pages[0];
977 const u64 start = async_extent->start;
978 const u64 end = start + async_extent->ram_size - 1;
979
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300980 p->mapping = inode->vfs_inode.i_mapping;
Qu Wenruo38a39ac72021-04-08 20:32:27 +0800981 btrfs_writepage_endio_finish_ordered(inode, p, start,
David Sterba25c12522021-07-26 14:15:08 +0200982 end, false);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200983
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100984 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300985 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100986 PAGE_END_WRITEBACK |
987 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100988 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100989 }
Chris Mason771ed682008-11-06 22:02:51 -0500990 alloc_hint = ins.objectid + ins.offset;
991 kfree(async_extent);
992 cond_resched();
993 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100994 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500995out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400996 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400997 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300999 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001000 async_extent->start +
1001 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001002 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001003 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -04001004 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001005 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1006 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +01001007 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -05001009 goto again;
Chris Mason771ed682008-11-06 22:02:51 -05001010}
1011
Nikolay Borisov43c69842020-06-03 08:55:02 +03001012static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04001013 u64 num_bytes)
1014{
Nikolay Borisov43c69842020-06-03 08:55:02 +03001015 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -04001016 struct extent_map *em;
1017 u64 alloc_hint = 0;
1018
1019 read_lock(&em_tree->lock);
1020 em = search_extent_mapping(em_tree, start, num_bytes);
1021 if (em) {
1022 /*
1023 * if block start isn't an actual block number then find the
1024 * first block in this inode and use that as a hint. If that
1025 * block is also bogus then just don't worry about it.
1026 */
1027 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1028 free_extent_map(em);
1029 em = search_extent_mapping(em_tree, 0, 0);
1030 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1031 alloc_hint = em->block_start;
1032 if (em)
1033 free_extent_map(em);
1034 } else {
1035 alloc_hint = em->block_start;
1036 free_extent_map(em);
1037 }
1038 }
1039 read_unlock(&em_tree->lock);
1040
1041 return alloc_hint;
1042}
1043
Chris Mason771ed682008-11-06 22:02:51 -05001044/*
1045 * when extent_io.c finds a delayed allocation range in the file,
1046 * the call backs end up in this code. The basic idea is to
1047 * allocate extents on disk for the range, and create ordered data structs
1048 * in ram to track those extents.
1049 *
1050 * locked_page is the page that writepage had locked already. We use
1051 * it to make sure we don't do extra locks or unlocks.
1052 *
1053 * *page_started is set to one if we unlock locked_page and do everything
1054 * required to start IO on it. It may be clean and already done with
1055 * IO when we return.
1056 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001057static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001058 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001059 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001060 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001061{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001062 struct btrfs_root *root = inode->root;
1063 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001064 u64 alloc_hint = 0;
1065 u64 num_bytes;
1066 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001067 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001068 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001069 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001070 struct btrfs_key ins;
1071 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 unsigned clear_bits;
1073 unsigned long page_ops;
1074 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001075 int ret = 0;
1076
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001077 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001078 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001079 ret = -EINVAL;
1080 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001081 }
Chris Mason771ed682008-11-06 22:02:51 -05001082
Qu Wenruofda28322013-02-26 08:10:22 +00001083 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001084 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001085 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001086
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001087 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001088
Qu Wenruo73672532021-07-26 14:34:59 +08001089 /*
1090 * Due to the page size limit, for subpage we can only trigger the
1091 * writeback for the dirty sectors of page, that means data writeback
1092 * is doing more writeback than what we want.
1093 *
1094 * This is especially unexpected for some call sites like fallocate,
1095 * where we only increase i_size after everything is done.
1096 * This means we can trigger inline extent even if we didn't want to.
1097 * So here we skip inline extent creation completely.
1098 */
1099 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
Chris Mason771ed682008-11-06 22:02:51 -05001100 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001101 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001102 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001103 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001104 /*
1105 * We use DO_ACCOUNTING here because we need the
1106 * delalloc_release_metadata to be run _after_ we drop
1107 * our outstanding extent for clearing delalloc for this
1108 * range.
1109 */
Qu Wenruo4750af32021-05-31 16:50:48 +08001110 extent_clear_unlock_delalloc(inode, start, end,
1111 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001112 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001113 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1114 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001115 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001116 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001117 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001118 *page_started = 1;
Qu Wenruo4750af32021-05-31 16:50:48 +08001119 /*
1120 * locked_page is locked by the caller of
1121 * writepage_delalloc(), not locked by
1122 * __process_pages_contig().
1123 *
1124 * We can't let __process_pages_contig() to unlock it,
1125 * as it doesn't have any subpage::writers recorded.
1126 *
1127 * Here we manually unlock the page, since the caller
1128 * can't use page_started to determine if it's an
1129 * inline extent or a compressed extent.
1130 */
1131 unlock_page(locked_page);
Chris Mason771ed682008-11-06 22:02:51 -05001132 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001133 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001135 }
1136 }
Chris Masonc8b97812008-10-29 14:49:59 -04001137
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001138 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1139 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001140
Filipe Manana432cd2a2020-06-08 13:32:55 +01001141 /*
1142 * Relocation relies on the relocated extents to have exactly the same
1143 * size as the original extents. Normally writeback for relocation data
1144 * extents follows a NOCOW path because relocation preallocates the
1145 * extents. However, due to an operation such as scrub turning a block
1146 * group to RO mode, it may fallback to COW mode, so we must make sure
1147 * an extent allocated during COW has exactly the requested size and can
1148 * not be split into smaller extents, otherwise relocation breaks and
1149 * fails during the stage where it updates the bytenr of file extent
1150 * items.
1151 */
1152 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1153 min_alloc_size = num_bytes;
1154 else
1155 min_alloc_size = fs_info->sectorsize;
1156
Anand Jain3752d222018-02-15 12:29:38 +08001157 while (num_bytes > 0) {
1158 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001159 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001160 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001161 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001162 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001163 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001164 cur_alloc_size = ins.offset;
1165 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001166
Chris Mason771ed682008-11-06 22:02:51 -05001167 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001169 start, /* orig_start */
1170 ins.objectid, /* block_start */
1171 ins.offset, /* block_len */
1172 ins.offset, /* orig_block_len */
1173 ram_size, /* ram_bytes */
1174 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001175 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001176 if (IS_ERR(em)) {
1177 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001178 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001179 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001180 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001181
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001182 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001183 ram_size, cur_alloc_size,
1184 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001185 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001186 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001187
Yan Zheng17d217f2008-12-12 10:03:38 -05001188 if (root->root_key.objectid ==
1189 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001190 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001191 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001192 /*
1193 * Only drop cache here, and process as normal.
1194 *
1195 * We must not allow extent_clear_unlock_delalloc()
1196 * at out_unlock label to free meta of this ordered
1197 * extent, as its meta should be freed by
1198 * btrfs_finish_ordered_io().
1199 *
1200 * So we must continue until @start is increased to
1201 * skip current ordered extent.
1202 */
Josef Bacik00361582013-08-14 14:02:47 -04001203 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001204 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001205 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001206 }
1207
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001208 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001209
Qu Wenruof57ad932021-04-07 19:22:13 +08001210 /*
1211 * We're not doing compressed IO, don't unlock the first page
1212 * (which the caller expects to stay locked), don't clear any
1213 * dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001214 *
Qu Wenruof57ad932021-04-07 19:22:13 +08001215 * Do set the Ordered (Private2) bit so we know this page was
1216 * properly setup for writepage.
Chris Masonc8b97812008-10-29 14:49:59 -04001217 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001218 page_ops = unlock ? PAGE_UNLOCK : 0;
Qu Wenruof57ad932021-04-07 19:22:13 +08001219 page_ops |= PAGE_SET_ORDERED;
Chris Masona791e352009-10-08 11:27:10 -04001220
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001221 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001222 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001223 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001224 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001225 if (num_bytes < cur_alloc_size)
1226 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001227 else
Anand Jain3752d222018-02-15 12:29:38 +08001228 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001229 alloc_hint = ins.objectid + ins.offset;
1230 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001231 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001232
1233 /*
1234 * btrfs_reloc_clone_csums() error, since start is increased
1235 * extent_clear_unlock_delalloc() at out_unlock label won't
1236 * free metadata of current ordered extent, we're OK to exit.
1237 */
1238 if (ret)
1239 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001240 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001241out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001242 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001243
Filipe Mananad9f85962014-08-25 10:43:00 +01001244out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001245 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001246out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001247 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001248 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001250 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1251 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001252 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001253 /*
1254 * If we reserved an extent for our delalloc range (or a subrange) and
1255 * failed to create the respective ordered extent, then it means that
1256 * when we reserved the extent we decremented the extent's size from
1257 * the data space_info's bytes_may_use counter and incremented the
1258 * space_info's bytes_reserved counter by the same amount. We must make
1259 * sure extent_clear_unlock_delalloc() does not try to decrement again
1260 * the data space_info's bytes_may_use counter, therefore we do not pass
1261 * it the flag EXTENT_CLEAR_DATA_RESV.
1262 */
1263 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001264 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001265 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001266 locked_page,
1267 clear_bits,
1268 page_ops);
1269 start += cur_alloc_size;
1270 if (start >= end)
1271 goto out;
1272 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001273 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001274 clear_bits | EXTENT_CLEAR_DATA_RESV,
1275 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001276 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001277}
Chris Masonc8b97812008-10-29 14:49:59 -04001278
Chris Mason771ed682008-11-06 22:02:51 -05001279/*
1280 * work queue call back to started compression on a file and pages
1281 */
1282static noinline void async_cow_start(struct btrfs_work *work)
1283{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001284 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001285 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001286
Nikolay Borisovb5326272019-03-12 17:20:25 +02001287 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001288
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001289 compressed_extents = compress_file_range(async_chunk);
1290 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001291 btrfs_add_delayed_iput(async_chunk->inode);
1292 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001293 }
Chris Mason771ed682008-11-06 22:02:51 -05001294}
1295
1296/*
1297 * work queue call back to submit previously compressed pages
1298 */
1299static noinline void async_cow_submit(struct btrfs_work *work)
1300{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001301 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1302 work);
1303 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001304 unsigned long nr_pages;
1305
Nikolay Borisovb5326272019-03-12 17:20:25 +02001306 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001307 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001308
Nikolay Borisov4546d172019-01-03 10:50:03 +02001309 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001310 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001311 * in which case we don't have anything to submit, yet we need to
1312 * always adjust ->async_delalloc_pages as its paired with the init
1313 * happening in cow_file_range_async
1314 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001315 if (async_chunk->inode)
1316 submit_compressed_extents(async_chunk);
Josef Bacikac981412021-07-14 14:47:17 -04001317
1318 /* atomic_sub_return implies a barrier */
1319 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1320 5 * SZ_1M)
1321 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001322}
Chris Masonc8b97812008-10-29 14:49:59 -04001323
Chris Mason771ed682008-11-06 22:02:51 -05001324static noinline void async_cow_free(struct btrfs_work *work)
1325{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001326 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001327
Nikolay Borisovb5326272019-03-12 17:20:25 +02001328 async_chunk = container_of(work, struct async_chunk, work);
1329 if (async_chunk->inode)
1330 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001331 if (async_chunk->blkcg_css)
1332 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001333 /*
1334 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001335 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001336 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001337 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001338 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001339}
1340
Nikolay Borisov751b6432020-06-03 08:55:22 +03001341static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001342 struct writeback_control *wbc,
1343 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001344 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001345 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001346{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001347 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001348 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001349 struct async_cow *ctx;
1350 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001351 unsigned long nr_pages;
1352 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001353 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1354 int i;
1355 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001356 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001357 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001358
Nikolay Borisov751b6432020-06-03 08:55:22 +03001359 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001360
Nikolay Borisov751b6432020-06-03 08:55:22 +03001361 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001362 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1363 num_chunks = 1;
1364 should_compress = false;
1365 } else {
1366 should_compress = true;
1367 }
1368
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001369 nofs_flag = memalloc_nofs_save();
1370 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1371 memalloc_nofs_restore(nofs_flag);
1372
Nikolay Borisov97db1202019-03-12 17:20:24 +02001373 if (!ctx) {
1374 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1375 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1376 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001377 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1378 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001379
Nikolay Borisov751b6432020-06-03 08:55:22 +03001380 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1381 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001382 return -ENOMEM;
1383 }
1384
1385 async_chunk = ctx->chunks;
1386 atomic_set(&ctx->num_chunks, num_chunks);
1387
1388 for (i = 0; i < num_chunks; i++) {
1389 if (should_compress)
1390 cur_end = min(end, start + SZ_512K - 1);
1391 else
1392 cur_end = end;
1393
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001394 /*
1395 * igrab is called higher up in the call chain, take only the
1396 * lightweight reference for the callback lifetime
1397 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001398 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001399 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001400 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001401 async_chunk[i].start = start;
1402 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001403 async_chunk[i].write_flags = write_flags;
1404 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001405
Chris Mason1d53c9e2019-07-10 12:28:16 -07001406 /*
1407 * The locked_page comes all the way from writepage and its
1408 * the original page we were actually given. As we spread
1409 * this large delalloc region across multiple async_chunk
1410 * structs, only the first struct needs a pointer to locked_page
1411 *
1412 * This way we don't need racey decisions about who is supposed
1413 * to unlock it.
1414 */
1415 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001416 /*
1417 * Depending on the compressibility, the pages might or
1418 * might not go through async. We want all of them to
1419 * be accounted against wbc once. Let's do it here
1420 * before the paths diverge. wbc accounting is used
1421 * only for foreign writeback detection and doesn't
1422 * need full accuracy. Just account the whole thing
1423 * against the first page.
1424 */
1425 wbc_account_cgroup_owner(wbc, locked_page,
1426 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001427 async_chunk[i].locked_page = locked_page;
1428 locked_page = NULL;
1429 } else {
1430 async_chunk[i].locked_page = NULL;
1431 }
1432
Chris Masonec39f762019-07-10 12:28:17 -07001433 if (blkcg_css != blkcg_root_css) {
1434 css_get(blkcg_css);
1435 async_chunk[i].blkcg_css = blkcg_css;
1436 } else {
1437 async_chunk[i].blkcg_css = NULL;
1438 }
1439
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001440 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1441 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001442
Nikolay Borisov97db1202019-03-12 17:20:24 +02001443 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001444 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001445
Nikolay Borisov97db1202019-03-12 17:20:24 +02001446 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001447
Chris Mason771ed682008-11-06 22:02:51 -05001448 *nr_written += nr_pages;
1449 start = cur_end + 1;
1450 }
1451 *page_started = 1;
1452 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001453}
1454
Naohiro Aota42c01102021-02-04 19:22:07 +09001455static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1456 struct page *locked_page, u64 start,
1457 u64 end, int *page_started,
1458 unsigned long *nr_written)
1459{
1460 int ret;
1461
1462 ret = cow_file_range(inode, locked_page, start, end, page_started,
1463 nr_written, 0);
1464 if (ret)
1465 return ret;
1466
1467 if (*page_started)
1468 return 0;
1469
1470 __set_page_dirty_nobuffers(locked_page);
1471 account_page_redirty(locked_page);
1472 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1473 *page_started = 1;
1474
1475 return 0;
1476}
1477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001478static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001479 u64 bytenr, u64 num_bytes)
1480{
1481 int ret;
1482 struct btrfs_ordered_sum *sums;
1483 LIST_HEAD(list);
1484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001485 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001486 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001487 if (ret == 0 && list_empty(&list))
1488 return 0;
1489
1490 while (!list_empty(&list)) {
1491 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1492 list_del(&sums->list);
1493 kfree(sums);
1494 }
Liu Bo58113752018-01-31 17:09:13 -07001495 if (ret < 0)
1496 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001497 return 1;
1498}
1499
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001500static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001501 const u64 start, const u64 end,
1502 int *page_started, unsigned long *nr_written)
1503{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001504 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1505 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001506 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001507 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001508 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001509 u64 range_start = start;
1510 u64 count;
1511
1512 /*
1513 * If EXTENT_NORESERVE is set it means that when the buffered write was
1514 * made we had not enough available data space and therefore we did not
1515 * reserve data space for it, since we though we could do NOCOW for the
1516 * respective file range (either there is prealloc extent or the inode
1517 * has the NOCOW bit set).
1518 *
1519 * However when we need to fallback to COW mode (because for example the
1520 * block group for the corresponding extent was turned to RO mode by a
1521 * scrub or relocation) we need to do the following:
1522 *
1523 * 1) We increment the bytes_may_use counter of the data space info.
1524 * If COW succeeds, it allocates a new data extent and after doing
1525 * that it decrements the space info's bytes_may_use counter and
1526 * increments its bytes_reserved counter by the same amount (we do
1527 * this at btrfs_add_reserved_bytes()). So we need to increment the
1528 * bytes_may_use counter to compensate (when space is reserved at
1529 * buffered write time, the bytes_may_use counter is incremented);
1530 *
1531 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1532 * that if the COW path fails for any reason, it decrements (through
1533 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1534 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001535 *
1536 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001537 * space cache inode or an inode of the data relocation tree, we must
1538 * also increment bytes_may_use of the data space_info for the same
1539 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001540 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001541 * group that contains that extent to RO mode and therefore force COW
1542 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001543 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001544 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001545 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001546 if (count > 0 || is_space_ino || is_reloc_ino) {
1547 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001548 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001549 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1550
Filipe Manana6bd335b2020-06-08 13:33:05 +01001551 if (is_space_ino || is_reloc_ino)
1552 bytes = range_bytes;
1553
Filipe Manana467dc472020-05-27 11:16:07 +01001554 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001555 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001556 spin_unlock(&sinfo->lock);
1557
Filipe Manana2166e5e2020-05-27 11:16:19 +01001558 if (count > 0)
1559 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1560 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001561 }
1562
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001563 return cow_file_range(inode, locked_page, start, end, page_started,
1564 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * when nowcow writeback call back. This checks for snapshots or COW copies
1569 * of the extents that exist in the file, and COWs the file as required.
1570 *
1571 * If no cow copies or snapshots exist, we write directly to the existing
1572 * blocks on disk
1573 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001574static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001575 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001576 const u64 start, const u64 end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001577 int *page_started,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001578 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001579{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001580 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1581 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001582 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001583 u64 cow_start = (u64)-1;
1584 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001585 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001586 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001587 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1588 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001589 bool nocow = false;
1590 u64 disk_bytenr = 0;
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001591 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
Chris Masonbe20aa92007-12-17 20:14:01 -05001592
1593 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001594 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001595 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001596 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001597 EXTENT_DO_ACCOUNTING |
1598 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001599 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001600 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001601 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001602 }
Li Zefan82d59022011-04-20 10:33:24 +08001603
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001605 struct btrfs_key found_key;
1606 struct btrfs_file_extent_item *fi;
1607 struct extent_buffer *leaf;
1608 u64 extent_end;
1609 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001610 u64 num_bytes = 0;
1611 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001612 u64 ram_bytes;
1613 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001614
1615 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001616
Liu Boe4c3b2d2017-01-30 12:25:28 -08001617 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001618 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001619 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001620 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001621
1622 /*
1623 * If there is no extent for our range when doing the initial
1624 * search, then go back to the previous slot as it will be the
1625 * one containing the search offset
1626 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001627 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1628 leaf = path->nodes[0];
1629 btrfs_item_key_to_cpu(leaf, &found_key,
1630 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001631 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001632 found_key.type == BTRFS_EXTENT_DATA_KEY)
1633 path->slots[0]--;
1634 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001635 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001636next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001637 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001638 leaf = path->nodes[0];
1639 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1640 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001641 if (ret < 0) {
1642 if (cow_start != (u64)-1)
1643 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001644 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001645 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001646 if (ret > 0)
1647 break;
1648 leaf = path->nodes[0];
1649 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001650
Yan Zheng80ff3852008-10-30 14:20:02 -04001651 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001652
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001653 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001654 if (found_key.objectid > ino)
1655 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001656 /*
1657 * Keep searching until we find an EXTENT_ITEM or there are no
1658 * more extents for this inode
1659 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001660 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1661 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1662 path->slots[0]++;
1663 goto next_slot;
1664 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001665
1666 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001667 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001668 found_key.offset > end)
1669 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001670
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001671 /*
1672 * If the found extent starts after requested offset, then
1673 * adjust extent_end to be right before this extent begins
1674 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001675 if (found_key.offset > cur_offset) {
1676 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001677 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 goto out_check;
1679 }
Chris Masonc31f8832008-01-08 15:46:31 -05001680
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001681 /*
1682 * Found extent which begins before our range and potentially
1683 * intersect it
1684 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001685 fi = btrfs_item_ptr(leaf, path->slots[0],
1686 struct btrfs_file_extent_item);
1687 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001688
Josef Bacikcc95bef2013-04-04 14:31:27 -04001689 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001690 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1691 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001694 extent_end = found_key.offset +
1695 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001696 disk_num_bytes =
1697 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001698 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001699 * If the extent we got ends before our current offset,
1700 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001701 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001702 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001703 path->slots[0]++;
1704 goto next_slot;
1705 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001706 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001707 if (disk_bytenr == 0)
1708 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001709 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001710 if (btrfs_file_extent_compression(leaf, fi) ||
1711 btrfs_file_extent_encryption(leaf, fi) ||
1712 btrfs_file_extent_other_encoding(leaf, fi))
1713 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001714 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001715 * If extent is created before the last volume's snapshot
1716 * this implies the extent is shared, hence we can't do
1717 * nocow. This is the same check as in
1718 * btrfs_cross_ref_exist but without calling
1719 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001720 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001721 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001722 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001723 btrfs_root_last_snapshot(&root->root_item))
1724 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001725 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1726 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001727
1728 /*
1729 * The following checks can be expensive, as they need to
1730 * take other locks and do btree or rbtree searches, so
1731 * release the path to avoid blocking other tasks for too
1732 * long.
1733 */
1734 btrfs_release_path(path);
1735
Liu Bo58113752018-01-31 17:09:13 -07001736 ret = btrfs_cross_ref_exist(root, ino,
1737 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001738 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001739 if (ret) {
1740 /*
1741 * ret could be -EIO if the above fails to read
1742 * metadata.
1743 */
1744 if (ret < 0) {
1745 if (cow_start != (u64)-1)
1746 cur_offset = cow_start;
1747 goto error;
1748 }
1749
Nikolay Borisov3e024842019-08-21 10:42:03 +03001750 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001751 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001752 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001753 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001754 disk_bytenr += cur_offset - found_key.offset;
1755 num_bytes = min(end + 1, extent_end) - cur_offset;
1756 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001757 * If there are pending snapshots for this root, we
1758 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001759 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001760 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001761 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001762 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001763 * force cow if csum exists in the range.
1764 * this ensure that csum for a given extent are
1765 * either valid or do not exist.
1766 */
Liu Bo58113752018-01-31 17:09:13 -07001767 ret = csum_exist_in_range(fs_info, disk_bytenr,
1768 num_bytes);
1769 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001770 /*
1771 * ret could be -EIO if the above fails to read
1772 * metadata.
1773 */
1774 if (ret < 0) {
1775 if (cow_start != (u64)-1)
1776 cur_offset = cow_start;
1777 goto error;
1778 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001779 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001780 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001781 }
Filipe Manana20903032021-02-05 12:55:36 +00001782 /* If the extent's block group is RO, we must COW */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001783 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001784 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001785 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001786 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001787 extent_end = found_key.offset + ram_bytes;
1788 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001789 /* Skip extents outside of our requested range */
1790 if (extent_end <= start) {
1791 path->slots[0]++;
1792 goto next_slot;
1793 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001794 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001795 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001796 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001797 }
1798out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001799 /*
1800 * If nocow is false then record the beginning of the range
1801 * that needs to be COWed
1802 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001803 if (!nocow) {
1804 if (cow_start == (u64)-1)
1805 cow_start = cur_offset;
1806 cur_offset = extent_end;
1807 if (cur_offset > end)
1808 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001809 if (!path->nodes[0])
1810 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001811 path->slots[0]++;
1812 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001813 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001814
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001815 /*
1816 * COW range from cow_start to found_key.offset - 1. As the key
1817 * will contain the beginning of the first extent that can be
1818 * NOCOW, following one which needs to be COW'ed
1819 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001820 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001821 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001822 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001823 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001824 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001825 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001826 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001827 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001828
Yan Zhengd899e052008-10-30 14:25:28 -04001829 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001830 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001831 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001832
Nikolay Borisov968322c2020-06-03 08:55:21 +03001833 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001834 orig_start,
1835 disk_bytenr, /* block_start */
1836 num_bytes, /* block_len */
1837 disk_num_bytes, /* orig_block_len */
1838 ram_bytes, BTRFS_COMPRESS_NONE,
1839 BTRFS_ORDERED_PREALLOC);
1840 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001841 ret = PTR_ERR(em);
1842 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001843 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001844 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001845 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001846 disk_bytenr, num_bytes,
1847 num_bytes,
1848 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001849 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001850 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001851 cur_offset + num_bytes - 1,
1852 0);
1853 goto error;
1854 }
Yan Zhengd899e052008-10-30 14:25:28 -04001855 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001856 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001857 disk_bytenr, num_bytes,
1858 num_bytes,
1859 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001860 if (ret)
1861 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001862 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001863
Filipe Mananaf78c4362016-05-09 13:15:41 +01001864 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001866 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001867
Yan, Zhengefa56462010-05-16 10:49:59 -04001868 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001869 BTRFS_DATA_RELOC_TREE_OBJECTID)
1870 /*
1871 * Error handled later, as we must prevent
1872 * extent_clear_unlock_delalloc() in error handler
1873 * from freeing metadata of created ordered extent.
1874 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001875 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001876 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001877
Nikolay Borisov968322c2020-06-03 08:55:21 +03001878 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001879 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001880 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001881 EXTENT_DELALLOC |
1882 EXTENT_CLEAR_DATA_RESV,
Qu Wenruof57ad932021-04-07 19:22:13 +08001883 PAGE_UNLOCK | PAGE_SET_ORDERED);
Wang Xiaoguang18513092016-07-25 15:51:40 +08001884
Yan Zheng80ff3852008-10-30 14:20:02 -04001885 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001886
1887 /*
1888 * btrfs_reloc_clone_csums() error, now we're OK to call error
1889 * handler, as metadata for created ordered extent will only
1890 * be freed by btrfs_finish_ordered_io().
1891 */
1892 if (ret)
1893 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001894 if (cur_offset > end)
1895 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001896 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001897 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001898
Robbie Ko506481b2018-10-30 18:04:04 +08001899 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001900 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001901
Yan Zheng80ff3852008-10-30 14:20:02 -04001902 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001903 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001904 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1905 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001906 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001907 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001908 }
1909
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001910error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001911 if (nocow)
1912 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1913
Josef Bacik17ca04a2012-05-31 15:58:55 -04001914 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001915 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001916 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001917 EXTENT_DELALLOC | EXTENT_DEFRAG |
1918 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001919 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001920 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001921 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001923}
1924
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001925static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001926{
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001927 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1928 if (inode->defrag_bytes &&
1929 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1930 0, NULL))
1931 return false;
1932 return true;
1933 }
1934 return false;
Wang Shilong47059d92014-07-03 18:22:07 +08001935}
1936
Chris Masond352ac62008-09-29 15:18:18 -04001937/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001938 * Function to process delayed allocation (create CoW) for ranges which are
1939 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001940 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001941int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001942 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1943 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001944{
Chris Masonbe20aa92007-12-17 20:14:01 -05001945 int ret;
Naohiro Aota42c01102021-02-04 19:22:07 +09001946 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
Chris Masona2135012008-06-25 16:01:30 -04001947
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001948 if (should_nocow(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001949 ASSERT(!zoned);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001950 ret = run_delalloc_nocow(inode, locked_page, start, end,
Goldwyn Rodrigues6e65ae72021-03-04 09:06:25 -06001951 page_started, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001952 } else if (!inode_can_compress(inode) ||
1953 !inode_need_compress(inode, start, end)) {
Naohiro Aota42c01102021-02-04 19:22:07 +09001954 if (zoned)
1955 ret = run_delalloc_zoned(inode, locked_page, start, end,
1956 page_started, nr_written);
1957 else
1958 ret = cow_file_range(inode, locked_page, start, end,
1959 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001960 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001961 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1962 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001963 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001964 }
Qu Wenruo524272602017-03-08 10:25:52 +08001965 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001966 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001967 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001968 return ret;
1969}
1970
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001971void btrfs_split_delalloc_extent(struct inode *inode,
1972 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001973{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001974 u64 size;
1975
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001976 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001977 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001978 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001979
Josef Bacikdcab6a32015-02-11 15:08:59 -05001980 size = orig->end - orig->start + 1;
1981 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001982 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001983 u64 new_size;
1984
1985 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001986 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001987 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001988 */
1989 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001990 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001991 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001992 num_extents += count_max_extents(new_size);
1993 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001994 return;
1995 }
1996
Josef Bacik9e0baf62011-07-15 15:16:44 +00001997 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001998 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001999 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002000}
2001
2002/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02002003 * Handle merged delayed allocation extents so we can keep track of new extents
2004 * that are just merged onto old extents, such as when we are doing sequential
2005 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04002006 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02002007void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2008 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04002009{
Josef Bacikdcab6a32015-02-11 15:08:59 -05002010 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01002011 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002012
Josef Bacik9ed74f22009-09-11 16:12:44 -04002013 /* not delalloc, ignore it */
2014 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00002015 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002016
Josef Bacik8461a3d2015-03-13 15:12:08 -04002017 if (new->start > other->start)
2018 new_size = new->end - other->start + 1;
2019 else
2020 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05002021
2022 /* we're not bigger than the max, unreserve the space and go */
2023 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2024 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002025 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05002026 spin_unlock(&BTRFS_I(inode)->lock);
2027 return;
2028 }
2029
2030 /*
Josef Bacikba117212015-03-13 15:01:24 -04002031 * We have to add up either side to figure out how many extents were
2032 * accounted for before we merged into one big extent. If the number of
2033 * extents we accounted for is <= the amount we need for the new range
2034 * then we can return, otherwise drop. Think of it like this
2035 *
2036 * [ 4k][MAX_SIZE]
2037 *
2038 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2039 * need 2 outstanding extents, on one side we have 1 and the other side
2040 * we have 1 so they are == and we can return. But in this case
2041 *
2042 * [MAX_SIZE+4k][MAX_SIZE+4k]
2043 *
2044 * Each range on their own accounts for 2 extents, but merged together
2045 * they are only 3 extents worth of accounting, so we need to drop in
2046 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05002047 */
Josef Bacikba117212015-03-13 15:01:24 -04002048 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002049 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04002050 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01002051 num_extents += count_max_extents(old_size);
2052 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05002053 return;
2054
Josef Bacik9e0baf62011-07-15 15:16:44 +00002055 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04002056 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00002057 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002058}
2059
Miao Xieeb73c1b2013-05-15 07:48:22 +00002060static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2061 struct inode *inode)
2062{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2064
Miao Xieeb73c1b2013-05-15 07:48:22 +00002065 spin_lock(&root->delalloc_lock);
2066 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2067 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2068 &root->delalloc_inodes);
2069 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2070 &BTRFS_I(inode)->runtime_flags);
2071 root->nr_delalloc_inodes++;
2072 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002073 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002074 BUG_ON(!list_empty(&root->delalloc_root));
2075 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002076 &fs_info->delalloc_roots);
2077 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002078 }
2079 }
2080 spin_unlock(&root->delalloc_lock);
2081}
2082
Nikolay Borisov2b877332018-04-27 12:21:51 +03002083
2084void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2085 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002086{
David Sterba3ffbd682018-06-29 10:56:42 +02002087 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002088
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002089 if (!list_empty(&inode->delalloc_inodes)) {
2090 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002091 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002092 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002093 root->nr_delalloc_inodes--;
2094 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002095 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002096 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002097 BUG_ON(list_empty(&root->delalloc_root));
2098 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002099 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002100 }
2101 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002102}
2103
2104static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2105 struct btrfs_inode *inode)
2106{
2107 spin_lock(&root->delalloc_lock);
2108 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002109 spin_unlock(&root->delalloc_lock);
2110}
2111
Chris Masond352ac62008-09-29 15:18:18 -04002112/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002113 * Properly track delayed allocation bytes in the inode and to maintain the
2114 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002115 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002116void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2117 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2120
Wang Shilong47059d92014-07-03 18:22:07 +08002121 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2122 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002123 /*
2124 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002125 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002126 * bit, which is only set or cleared with irqs on
2127 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002128 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002129 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002130 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002131 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002132 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002133
Josef Bacik8b62f872017-10-19 14:15:55 -04002134 spin_lock(&BTRFS_I(inode)->lock);
2135 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2136 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002137
Josef Bacik6a3891c2015-03-16 17:38:52 -04002138 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002139 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002140 return;
2141
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002142 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2143 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002144 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002145 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002146 if (*bits & EXTENT_DEFRAG)
2147 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002148 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002149 &BTRFS_I(inode)->runtime_flags))
2150 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002151 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002152 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002153
2154 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2155 (*bits & EXTENT_DELALLOC_NEW)) {
2156 spin_lock(&BTRFS_I(inode)->lock);
2157 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2158 state->start;
2159 spin_unlock(&BTRFS_I(inode)->lock);
2160 }
Chris Mason291d6732008-01-29 15:55:23 -05002161}
2162
Chris Masond352ac62008-09-29 15:18:18 -04002163/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002164 * Once a range is no longer delalloc this function ensures that proper
2165 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002166 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002167void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2168 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002169{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002170 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2171 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002172 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002173 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002174
Filipe Manana4a4b9642017-07-27 19:52:55 +01002175 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2176 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002177 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002178 spin_unlock(&inode->lock);
2179 }
Wang Shilong47059d92014-07-03 18:22:07 +08002180
Chris Mason75eff682008-12-15 15:54:40 -05002181 /*
2182 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002183 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002184 * bit, which is only set or cleared with irqs on
2185 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002186 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002187 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002188 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002189
Josef Bacik8b62f872017-10-19 14:15:55 -04002190 spin_lock(&inode->lock);
2191 btrfs_mod_outstanding_extents(inode, -num_extents);
2192 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002193
Josef Bacikb6d08f02013-09-27 14:57:43 -04002194 /*
2195 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002196 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002197 * error.
2198 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002199 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002200 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002201 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002202
Josef Bacik6a3891c2015-03-16 17:38:52 -04002203 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002204 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002205 return;
2206
Filipe Mananaa315e682017-03-06 23:04:20 +00002207 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2208 do_list && !(state->state & EXTENT_NORESERVE) &&
2209 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002210 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002211
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002212 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2213 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002214 spin_lock(&inode->lock);
2215 inode->delalloc_bytes -= len;
2216 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002217 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002218 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002219 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002220 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002221 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002222
2223 if ((state->state & EXTENT_DELALLOC_NEW) &&
2224 (*bits & EXTENT_DELALLOC_NEW)) {
2225 spin_lock(&inode->lock);
2226 ASSERT(inode->new_delalloc_bytes >= len);
2227 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002228 if (*bits & EXTENT_ADD_INODE_BYTES)
2229 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002230 spin_unlock(&inode->lock);
2231 }
Chris Mason291d6732008-01-29 15:55:23 -05002232}
2233
Chris Masond352ac62008-09-29 15:18:18 -04002234/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002235 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2236 * in a chunk's stripe. This function ensures that bios do not span a
2237 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002238 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002239 * @page - The page we are about to add to the bio
2240 * @size - size we want to add to the bio
2241 * @bio - bio we want to ensure is smaller than a stripe
2242 * @bio_flags - flags of the bio
2243 *
2244 * return 1 if page cannot be added to the bio
2245 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002246 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002247 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002248int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2249 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002251 struct inode *inode = page->mapping->host;
2252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002253 u64 logical = bio->bi_iter.bi_sector << 9;
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002254 u32 bio_len = bio->bi_iter.bi_size;
Michal Rostecki42034312021-01-27 14:57:27 +01002255 struct extent_map *em;
Michal Rostecki42034312021-01-27 14:57:27 +01002256 int ret = 0;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002257 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002258
Chris Mason771ed682008-11-06 22:02:51 -05002259 if (bio_flags & EXTENT_BIO_COMPRESSED)
2260 return 0;
2261
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002262 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
Michal Rostecki42034312021-01-27 14:57:27 +01002263 if (IS_ERR(em))
2264 return PTR_ERR(em);
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08002265 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002266 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002267 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002268
Qu Wenruo1a0b5c42021-04-13 18:00:47 +08002269 if (geom.len < bio_len + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002270 ret = 1;
2271out:
2272 free_extent_map(em);
2273 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002274}
2275
Chris Masond352ac62008-09-29 15:18:18 -04002276/*
2277 * in order to insert checksums into the metadata in large chunks,
2278 * we wait until bio submission time. All the pages in the bio are
2279 * checksummed and sums are attached onto the ordered extent record.
2280 *
2281 * At IO completion time the cums attached on the ordered extent record
2282 * are inserted into the btree
2283 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002284static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002285 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002286{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002287 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002288}
Chris Masone0156402008-04-16 11:15:20 -04002289
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002290/*
2291 * Split an extent_map at [start, start + len]
2292 *
2293 * This function is intended to be used only for extract_ordered_extent().
2294 */
2295static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2296 u64 pre, u64 post)
2297{
2298 struct extent_map_tree *em_tree = &inode->extent_tree;
2299 struct extent_map *em;
2300 struct extent_map *split_pre = NULL;
2301 struct extent_map *split_mid = NULL;
2302 struct extent_map *split_post = NULL;
2303 int ret = 0;
2304 int modified;
2305 unsigned long flags;
2306
2307 /* Sanity check */
2308 if (pre == 0 && post == 0)
2309 return 0;
2310
2311 split_pre = alloc_extent_map();
2312 if (pre)
2313 split_mid = alloc_extent_map();
2314 if (post)
2315 split_post = alloc_extent_map();
2316 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2317 ret = -ENOMEM;
2318 goto out;
2319 }
2320
2321 ASSERT(pre + post < len);
2322
2323 lock_extent(&inode->io_tree, start, start + len - 1);
2324 write_lock(&em_tree->lock);
2325 em = lookup_extent_mapping(em_tree, start, len);
2326 if (!em) {
2327 ret = -EIO;
2328 goto out_unlock;
2329 }
2330
2331 ASSERT(em->len == len);
2332 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2333 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
2334
2335 flags = em->flags;
2336 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2337 clear_bit(EXTENT_FLAG_LOGGING, &flags);
2338 modified = !list_empty(&em->list);
2339
2340 /* First, replace the em with a new extent_map starting from * em->start */
2341 split_pre->start = em->start;
2342 split_pre->len = (pre ? pre : em->len - post);
2343 split_pre->orig_start = split_pre->start;
2344 split_pre->block_start = em->block_start;
2345 split_pre->block_len = split_pre->len;
2346 split_pre->orig_block_len = split_pre->block_len;
2347 split_pre->ram_bytes = split_pre->len;
2348 split_pre->flags = flags;
2349 split_pre->compress_type = em->compress_type;
2350 split_pre->generation = em->generation;
2351
2352 replace_extent_mapping(em_tree, em, split_pre, modified);
2353
2354 /*
2355 * Now we only have an extent_map at:
2356 * [em->start, em->start + pre] if pre != 0
2357 * [em->start, em->start + em->len - post] if pre == 0
2358 */
2359
2360 if (pre) {
2361 /* Insert the middle extent_map */
2362 split_mid->start = em->start + pre;
2363 split_mid->len = em->len - pre - post;
2364 split_mid->orig_start = split_mid->start;
2365 split_mid->block_start = em->block_start + pre;
2366 split_mid->block_len = split_mid->len;
2367 split_mid->orig_block_len = split_mid->block_len;
2368 split_mid->ram_bytes = split_mid->len;
2369 split_mid->flags = flags;
2370 split_mid->compress_type = em->compress_type;
2371 split_mid->generation = em->generation;
2372 add_extent_mapping(em_tree, split_mid, modified);
2373 }
2374
2375 if (post) {
2376 split_post->start = em->start + em->len - post;
2377 split_post->len = post;
2378 split_post->orig_start = split_post->start;
2379 split_post->block_start = em->block_start + em->len - post;
2380 split_post->block_len = split_post->len;
2381 split_post->orig_block_len = split_post->block_len;
2382 split_post->ram_bytes = split_post->len;
2383 split_post->flags = flags;
2384 split_post->compress_type = em->compress_type;
2385 split_post->generation = em->generation;
2386 add_extent_mapping(em_tree, split_post, modified);
2387 }
2388
2389 /* Once for us */
2390 free_extent_map(em);
2391 /* Once for the tree */
2392 free_extent_map(em);
2393
2394out_unlock:
2395 write_unlock(&em_tree->lock);
2396 unlock_extent(&inode->io_tree, start, start + len - 1);
2397out:
2398 free_extent_map(split_pre);
2399 free_extent_map(split_mid);
2400 free_extent_map(split_post);
2401
2402 return ret;
2403}
2404
Naohiro Aotad22002f2021-02-04 19:22:00 +09002405static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2406 struct bio *bio, loff_t file_offset)
2407{
2408 struct btrfs_ordered_extent *ordered;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002409 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002410 u64 file_len;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002411 u64 len = bio->bi_iter.bi_size;
2412 u64 end = start + len;
2413 u64 ordered_end;
2414 u64 pre, post;
2415 int ret = 0;
2416
2417 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2418 if (WARN_ON_ONCE(!ordered))
2419 return BLK_STS_IOERR;
2420
2421 /* No need to split */
2422 if (ordered->disk_num_bytes == len)
2423 goto out;
2424
2425 /* We cannot split once end_bio'd ordered extent */
2426 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2427 ret = -EINVAL;
2428 goto out;
2429 }
2430
2431 /* We cannot split a compressed ordered extent */
2432 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2433 ret = -EINVAL;
2434 goto out;
2435 }
2436
2437 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2438 /* bio must be in one ordered extent */
2439 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2440 ret = -EINVAL;
2441 goto out;
2442 }
2443
2444 /* Checksum list should be empty */
2445 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2446 ret = -EINVAL;
2447 goto out;
2448 }
2449
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002450 file_len = ordered->num_bytes;
Naohiro Aotad22002f2021-02-04 19:22:00 +09002451 pre = start - ordered->disk_bytenr;
2452 post = ordered_end - end;
2453
2454 ret = btrfs_split_ordered_extent(ordered, pre, post);
2455 if (ret)
2456 goto out;
Naohiro Aotaabb99cf2021-06-28 17:57:28 +09002457 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
Naohiro Aotad22002f2021-02-04 19:22:00 +09002458
2459out:
Naohiro Aotad22002f2021-02-04 19:22:00 +09002460 btrfs_put_ordered_extent(ordered);
2461
2462 return errno_to_blk_status(ret);
2463}
2464
Chris Mason4a69a412008-11-06 22:03:00 -05002465/*
Chris Masoncad321a2008-12-17 14:51:42 -05002466 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002467 * on write, or reading the csums from the tree before a read.
2468 *
2469 * Rules about async/sync submit,
2470 * a) read: sync submit
2471 *
2472 * b) write without checksum: sync submit
2473 *
2474 * c) write with checksum:
2475 * c-1) if bio is issued by fsync: sync submit
2476 * (sync_writers != 0)
2477 *
2478 * c-2) if root is reloc root: sync submit
2479 * (only in case of buffered IO)
2480 *
2481 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002482 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002483blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2484 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002485
Chris Mason44b8bd72008-04-16 11:14:51 -04002486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002488 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302489 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002490 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002491 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002492 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002493
Josef Bacik42437a62020-10-16 11:29:18 -04002494 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2495 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002496
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002497 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302498 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002499
Naohiro Aotad22002f2021-02-04 19:22:00 +09002500 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2501 struct page *page = bio_first_bvec_all(bio)->bv_page;
2502 loff_t file_offset = page_offset(page);
2503
2504 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2505 if (ret)
2506 goto out;
2507 }
2508
Naohiro Aotacfe94442021-02-04 19:21:59 +09002509 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002510 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002511 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002512 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002513
Chris Masond20f7042008-12-08 16:58:54 -05002514 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002515 ret = btrfs_submit_compressed_read(inode, bio,
2516 mirror_num,
2517 bio_flags);
2518 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002519 } else {
2520 /*
2521 * Lookup bio sums does extra checks around whether we
2522 * need to csum or not, which is why we ignore skip_sum
2523 * here.
2524 */
Qu Wenruo62751932020-12-02 14:48:06 +08002525 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002526 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002527 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002528 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002529 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002530 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002531 /* csum items have already been cloned */
2532 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2533 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002534 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002535 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2536 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002537 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002538 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002539 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002540 if (ret)
2541 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002542 }
2543
Chris Mason0b86a832008-03-24 15:01:56 -04002544mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002545 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002546
2547out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002548 if (ret) {
2549 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002550 bio_endio(bio);
2551 }
Stefan Behrens61891922012-11-05 18:51:52 +01002552 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002553}
Chris Mason6885f302008-02-20 16:11:05 -05002554
Chris Masond352ac62008-09-29 15:18:18 -04002555/*
2556 * given a list of ordered sums record them in the inode. This happens
2557 * at IO completion time based on sums calculated at bio submission time.
2558 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002559static int add_pending_csums(struct btrfs_trans_handle *trans,
2560 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002561{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002562 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002563 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002564
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002565 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002566 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002567 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002568 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002569 if (ret)
2570 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002571 }
2572 return 0;
2573}
2574
Filipe Mananac3347302020-11-04 11:07:31 +00002575static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2576 const u64 start,
2577 const u64 len,
2578 struct extent_state **cached_state)
2579{
2580 u64 search_start = start;
2581 const u64 end = start + len - 1;
2582
2583 while (search_start < end) {
2584 const u64 search_len = end - search_start + 1;
2585 struct extent_map *em;
2586 u64 em_len;
2587 int ret = 0;
2588
2589 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2590 if (IS_ERR(em))
2591 return PTR_ERR(em);
2592
2593 if (em->block_start != EXTENT_MAP_HOLE)
2594 goto next;
2595
2596 em_len = em->len;
2597 if (em->start < search_start)
2598 em_len -= search_start - em->start;
2599 if (em_len > search_len)
2600 em_len = search_len;
2601
2602 ret = set_extent_bit(&inode->io_tree, search_start,
2603 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002604 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2605 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002606next:
2607 search_start = extent_map_end(em);
2608 free_extent_map(em);
2609 if (ret)
2610 return ret;
2611 }
2612 return 0;
2613}
2614
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002615int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002616 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002617 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002618{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002619 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002620
2621 if (start >= i_size_read(&inode->vfs_inode) &&
2622 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2623 /*
2624 * There can't be any extents following eof in this case so just
2625 * set the delalloc new bit for the range directly.
2626 */
2627 extra_bits |= EXTENT_DELALLOC_NEW;
2628 } else {
2629 int ret;
2630
2631 ret = btrfs_find_new_delalloc_bytes(inode, start,
2632 end + 1 - start,
2633 cached_state);
2634 if (ret)
2635 return ret;
2636 }
2637
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002638 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2639 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002640}
2641
Chris Masond352ac62008-09-29 15:18:18 -04002642/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002643struct btrfs_writepage_fixup {
2644 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002645 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002646 struct btrfs_work work;
2647};
2648
Christoph Hellwigb2950862008-12-02 09:54:17 -05002649static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002650{
2651 struct btrfs_writepage_fixup *fixup;
2652 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002653 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002654 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002655 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002656 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002657 u64 page_start;
2658 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002659 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002660 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002661
2662 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2663 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002664 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002665 page_start = page_offset(page);
2666 page_end = page_offset(page) + PAGE_SIZE - 1;
2667
2668 /*
2669 * This is similar to page_mkwrite, we need to reserve the space before
2670 * we take the page lock.
2671 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002672 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2673 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002674again:
Chris Mason247e7432008-07-17 12:53:51 -04002675 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002676
Chris Mason25f3c502020-01-21 11:51:42 -05002677 /*
2678 * Before we queued this fixup, we took a reference on the page.
2679 * page->mapping may go NULL, but it shouldn't be moved to a different
2680 * address space.
2681 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002682 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2683 /*
2684 * Unfortunately this is a little tricky, either
2685 *
2686 * 1) We got here and our page had already been dealt with and
2687 * we reserved our space, thus ret == 0, so we need to just
2688 * drop our space reservation and bail. This can happen the
2689 * first time we come into the fixup worker, or could happen
2690 * while waiting for the ordered extent.
2691 * 2) Our page was already dealt with, but we happened to get an
2692 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2693 * this case we obviously don't have anything to release, but
2694 * because the page was already dealt with we don't want to
2695 * mark the page with an error, so make sure we're resetting
2696 * ret to 0. This is why we have this check _before_ the ret
2697 * check, because we do not want to have a surprise ENOSPC
2698 * when the page was already properly dealt with.
2699 */
2700 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002701 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2702 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002703 page_start, PAGE_SIZE,
2704 true);
2705 }
2706 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002707 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002708 }
Chris Mason25f3c502020-01-21 11:51:42 -05002709
2710 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002711 * We can't mess with the page state unless it is locked, so now that
2712 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002713 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002714 if (ret)
2715 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002716
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002717 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002718
2719 /* already ordered? We're done */
Qu Wenruof57ad932021-04-07 19:22:13 +08002720 if (PageOrdered(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002721 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002722
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002723 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002724 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002725 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2726 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002727 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002728 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002729 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002730 goto again;
2731 }
Chris Mason247e7432008-07-17 12:53:51 -04002732
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002733 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002734 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002735 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002736 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002737
Chris Mason25f3c502020-01-21 11:51:42 -05002738 /*
2739 * Everything went as planned, we're now the owner of a dirty page with
2740 * delayed allocation bits set and space reserved for our COW
2741 * destination.
2742 *
2743 * The page was dirty when we started, nothing should have cleaned it.
2744 */
2745 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002746 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002747out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002748 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002749 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002750 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2751 PAGE_SIZE, true);
2752 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002753 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002754out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002755 if (ret) {
2756 /*
2757 * We hit ENOSPC or other errors. Update the mapping and page
2758 * to reflect the errors and clean the page.
2759 */
2760 mapping_set_error(page->mapping, ret);
2761 end_extent_writepage(page, ret, page_start, page_end);
2762 clear_page_dirty_for_io(page);
2763 SetPageError(page);
2764 }
2765 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002766 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002767 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002768 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002769 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002770 /*
2771 * As a precaution, do a delayed iput in case it would be the last iput
2772 * that could need flushing space. Recursing back to fixup worker would
2773 * deadlock.
2774 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002775 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002776}
2777
2778/*
2779 * There are a few paths in the higher layers of the kernel that directly
2780 * set the page dirty bit without asking the filesystem if it is a
2781 * good idea. This causes problems because we want to make sure COW
2782 * properly happens and the data=ordered rules are followed.
2783 *
Chris Masonc8b97812008-10-29 14:49:59 -04002784 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002785 * hasn't been properly setup for IO. We kick off an async process
2786 * to fix it up. The async helper will wait for ordered extents, set
2787 * the delalloc bit and make it safe to write the page.
2788 */
Qu Wenruoa129ffb2021-07-27 13:41:32 +08002789int btrfs_writepage_cow_fixup(struct page *page)
Chris Mason247e7432008-07-17 12:53:51 -04002790{
2791 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002792 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002793 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002794
Qu Wenruof57ad932021-04-07 19:22:13 +08002795 /* This page has ordered extent covering it already */
2796 if (PageOrdered(page))
Chris Mason247e7432008-07-17 12:53:51 -04002797 return 0;
2798
Chris Mason25f3c502020-01-21 11:51:42 -05002799 /*
2800 * PageChecked is set below when we create a fixup worker for this page,
2801 * don't try to create another one if we're already PageChecked()
2802 *
2803 * The extent_io writepage code will redirty the page if we send back
2804 * EAGAIN.
2805 */
Chris Mason247e7432008-07-17 12:53:51 -04002806 if (PageChecked(page))
2807 return -EAGAIN;
2808
2809 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2810 if (!fixup)
2811 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002812
Josef Bacikf4b13632020-01-21 14:34:52 -05002813 /*
2814 * We are already holding a reference to this inode from
2815 * write_cache_pages. We need to hold it because the space reservation
2816 * takes place outside of the page lock, and we can't trust
2817 * page->mapping outside of the page lock.
2818 */
2819 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002820 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002821 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002822 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002823 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002824 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002825 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002826
2827 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002828}
2829
Yan Zhengd899e052008-10-30 14:25:28 -04002830static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002831 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002832 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002833 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002834 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002835{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002836 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002837 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002838 struct btrfs_path *path;
2839 struct extent_buffer *leaf;
2840 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002841 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2842 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2843 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2844 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002845 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002846 int ret;
2847
2848 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002849 if (!path)
2850 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002851
Chris Masona1ed8352009-09-11 12:27:37 -04002852 /*
2853 * we may be replacing one extent in the tree with another.
2854 * The new extent is pinned in the extent map, and we don't want
2855 * to drop it from the cache until it is completely in the btree.
2856 *
2857 * So, tell btrfs_drop_extents to leave this extent in the cache.
2858 * the caller is expected to unpin it and allow it to be merged
2859 * with the others.
2860 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002861 drop_args.path = path;
2862 drop_args.start = file_pos;
2863 drop_args.end = file_pos + num_bytes;
2864 drop_args.replace_extent = true;
2865 drop_args.extent_item_size = sizeof(*stack_fi);
2866 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002867 if (ret)
2868 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002869
Filipe Manana5893dfb2020-11-04 11:07:32 +00002870 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002871 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002872 ins.offset = file_pos;
2873 ins.type = BTRFS_EXTENT_DATA_KEY;
2874
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002875 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002876 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002877 if (ret)
2878 goto out;
2879 }
Yan Zhengd899e052008-10-30 14:25:28 -04002880 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002881 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2882 write_extent_buffer(leaf, stack_fi,
2883 btrfs_item_ptr_offset(leaf, path->slots[0]),
2884 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002885
Yan Zhengd899e052008-10-30 14:25:28 -04002886 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002887 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002888
Filipe Manana2766ff62020-11-04 11:07:34 +00002889 /*
2890 * If we dropped an inline extent here, we know the range where it is
2891 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
David Sterba1a9fd412021-05-21 17:42:23 +02002892 * number of bytes only for that range containing the inline extent.
Filipe Manana2766ff62020-11-04 11:07:34 +00002893 * The remaining of the range will be processed when clearning the
2894 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2895 */
2896 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2897 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2898
2899 inline_size = drop_args.bytes_found - inline_size;
2900 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2901 drop_args.bytes_found -= inline_size;
2902 num_bytes -= sectorsize;
2903 }
2904
2905 if (update_inode_bytes)
2906 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002907
2908 ins.objectid = disk_bytenr;
2909 ins.offset = disk_num_bytes;
2910 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002911
Nikolay Borisovc553f942020-06-03 08:55:19 +03002912 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002913 if (ret)
2914 goto out;
2915
Nikolay Borisovc553f942020-06-03 08:55:19 +03002916 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002917 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002918out:
Yan Zhengd899e052008-10-30 14:25:28 -04002919 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002920
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002921 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002922}
2923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002924static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002925 u64 start, u64 len)
2926{
David Sterba32da53862019-10-29 19:20:18 +01002927 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002928
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002930 ASSERT(cache);
2931
2932 spin_lock(&cache->lock);
2933 cache->delalloc_bytes -= len;
2934 spin_unlock(&cache->lock);
2935
2936 btrfs_put_block_group(cache);
2937}
2938
Qu Wenruo203f44c52020-06-10 09:04:40 +08002939static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002940 struct btrfs_ordered_extent *oe)
2941{
2942 struct btrfs_file_extent_item stack_fi;
2943 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002944 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002945
2946 memset(&stack_fi, 0, sizeof(stack_fi));
2947 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2948 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2949 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2950 oe->disk_num_bytes);
2951 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2952 logical_len = oe->truncated_len;
2953 else
2954 logical_len = oe->num_bytes;
2955 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2956 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2957 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2958 /* Encryption and other encoding is reserved and all 0 */
2959
Filipe Manana2766ff62020-11-04 11:07:34 +00002960 /*
2961 * For delalloc, when completing an ordered extent we update the inode's
2962 * bytes when clearing the range in the inode's io tree, so pass false
2963 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2964 * except if the ordered extent was truncated.
2965 */
2966 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2967 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2968
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002969 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2970 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002971 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002972}
2973
2974/*
2975 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002976 * an ordered extent if the range of bytes in the file it covers are
2977 * fully written.
2978 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002979static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002980{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002981 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2982 struct btrfs_root *root = inode->root;
2983 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002984 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002985 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002986 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002987 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002988 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002989 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002990 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002991 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002992 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002993 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002994 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002995
Omar Sandovalbffe6332019-12-02 17:34:19 -08002996 start = ordered_extent->file_offset;
2997 end = start + ordered_extent->num_bytes - 1;
2998
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002999 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3000 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3001 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00003002 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003003
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003004 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003005
Josef Bacik5fd02042012-05-02 14:00:54 -04003006 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3007 ret = -EIO;
3008 goto out;
3009 }
3010
Christoph Hellwigc7c3a6d2021-07-22 09:53:59 +02003011 if (ordered_extent->bdev)
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09003012 btrfs_rewrite_logical_zoned(ordered_extent);
3013
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003014 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08003015
Josef Bacik77cef2e2013-08-29 13:57:21 -04003016 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3017 truncated = true;
3018 logical_len = ordered_extent->truncated_len;
3019 /* Truncated the entire extent, don't bother adding */
3020 if (!logical_len)
3021 goto out;
3022 }
3023
Yan, Zhengc2167752009-11-12 09:34:21 +00003024 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003025 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003026
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003027 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03003028 if (freespace_inode)
3029 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05003030 else
3031 trans = btrfs_join_transaction(root);
3032 if (IS_ERR(trans)) {
3033 ret = PTR_ERR(trans);
3034 trans = NULL;
3035 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003036 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003037 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02003038 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003039 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003040 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003041 goto out;
3042 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003043
Filipe Manana2766ff62020-11-04 11:07:34 +00003044 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08003045 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046
Nikolay Borisov8d510122019-10-08 20:43:06 +03003047 if (freespace_inode)
3048 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003049 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003050 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 if (IS_ERR(trans)) {
3052 ret = PTR_ERR(trans);
3053 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003054 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003055 }
Chris Masona79b7d42014-05-22 16:18:52 -07003056
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003057 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003058
Chris Masonc8b97812008-10-29 14:49:59 -04003059 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003060 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003061 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003062 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003063 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04003064 ordered_extent->file_offset,
3065 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003066 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003067 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003068 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03003069 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04003070 if (!ret) {
3071 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003072 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003073 ordered_extent->disk_bytenr,
3074 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04003075 }
Yan Zhengd899e052008-10-30 14:25:28 -04003076 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003077 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003078 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003079 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003081 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003083
Nikolay Borisov510f85e2020-09-18 12:15:52 +03003084 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02003085 if (ret) {
3086 btrfs_abort_transaction(trans, ret);
3087 goto out;
3088 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089
Filipe Manana2766ff62020-11-04 11:07:34 +00003090 /*
3091 * If this is a new delalloc range, clear its new delalloc flag to
3092 * update the inode's number of bytes. This needs to be done first
3093 * before updating the inode item.
3094 */
3095 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3096 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003097 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00003098 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3099 0, 0, &cached_state);
3100
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003101 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003102 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05003103 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003104 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003105 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003106 }
3107 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003108out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003109 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003110 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08003111 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003112
Miao Xiea698d0752012-09-20 01:51:59 -06003113 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003114 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003115
Josef Bacik77cef2e2013-08-29 13:57:21 -04003116 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08003117 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003118
Josef Bacikd61bec02021-05-19 09:38:27 -04003119 /*
3120 * If we failed to finish this ordered extent for any reason we
3121 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3122 * extent, and mark the inode with the error if it wasn't
3123 * already set. Any error during writeback would have already
3124 * set the mapping error, so we need to set it if we're the ones
3125 * marking this ordered extent as failed.
3126 */
3127 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3128 &ordered_extent->flags))
3129 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3130
Josef Bacik77cef2e2013-08-29 13:57:21 -04003131 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08003132 unwritten_start += logical_len;
3133 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003134
3135 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003136 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003137
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 /*
3139 * If the ordered extent had an IOERR or something else went
3140 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003141 * back to the allocator. We only free the extent in the
3142 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003143 *
3144 * If we made it past insert_reserved_file_extent before we
3145 * errored out then we don't need to do this as the accounting
3146 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003147 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003148 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003149 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003150 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003151 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3152 /*
3153 * Discard the range before returning it back to the
3154 * free space pool
3155 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08003156 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003157 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003158 ordered_extent->disk_bytenr,
3159 ordered_extent->disk_num_bytes,
3160 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003161 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08003162 ordered_extent->disk_bytenr,
3163 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02003164 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003165 }
3166
Josef Bacik5fd02042012-05-02 14:00:54 -04003167 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003168 * This needs to be done to make sure anybody waiting knows we are done
3169 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003170 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02003171 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04003172
Chris Masone6dcd2d2008-07-17 12:53:50 -04003173 /* once for us */
3174 btrfs_put_ordered_extent(ordered_extent);
3175 /* once for the tree */
3176 btrfs_put_ordered_extent(ordered_extent);
3177
Josef Bacik5fd02042012-05-02 14:00:54 -04003178 return ret;
3179}
3180
3181static void finish_ordered_fn(struct btrfs_work *work)
3182{
3183 struct btrfs_ordered_extent *ordered_extent;
3184 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3185 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003186}
3187
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003188void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3189 struct page *page, u64 start,
David Sterba25c12522021-07-26 14:15:08 +02003190 u64 end, bool uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003191{
Qu Wenruo38a39ac72021-04-08 20:32:27 +08003192 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
liubo1abe9b82011-03-24 11:18:59 +00003193
Qu Wenruoe65f1522021-04-01 15:15:06 +08003194 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3195 finish_ordered_fn, uptodate);
Chris Mason211f90e2008-07-18 11:56:15 -04003196}
3197
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003198/*
3199 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003200 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003201 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003202 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003203 * @page: page where is the data to be verified
3204 * @pgoff: offset inside the page
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003205 * @start: logical offset in the file
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003206 *
3207 * The length of such check is always one sector size.
3208 */
Omar Sandoval47df7762020-04-16 14:46:17 -07003209static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003210 u32 bio_offset, struct page *page, u32 pgoff,
3211 u64 start)
Miao Xiedc380ae2014-09-12 18:43:55 +08003212{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003213 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3214 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003216 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02003217 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003218 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003219 u8 *csum_expected;
3220 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003221
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003222 ASSERT(pgoff + len <= PAGE_SIZE);
3223
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003224 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3225 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003226
3227 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003228 shash->tfm = fs_info->csum_shash;
3229
Eric Biggersfd080012020-04-30 23:51:59 -07003230 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003231
3232 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003233 goto zeroit;
3234
3235 kunmap_atomic(kaddr);
3236 return 0;
3237zeroit:
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003238 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3239 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03003240 if (io_bio->device)
3241 btrfs_dev_stat_inc_and_print(io_bio->device,
3242 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08003243 memset(kaddr + pgoff, 1, len);
3244 flush_dcache_page(page);
3245 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003246 return -EIO;
3247}
3248
Chris Masond352ac62008-09-29 15:18:18 -04003249/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003250 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003251 * if there's a match, we allow the bio to finish. If not, the code in
3252 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003253 *
3254 * @bio_offset: offset to the beginning of the bio (in bytes)
3255 * @start: file offset of the range start
3256 * @end: file offset of the range end (inclusive)
Qu Wenruo08508fe2021-05-03 10:08:54 +08003257 *
3258 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3259 * csum match.
Chris Masond352ac62008-09-29 15:18:18 -04003260 */
Qu Wenruo08508fe2021-05-03 10:08:54 +08003261unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3262 struct page *page, u64 start, u64 end)
Chris Mason07157aa2007-08-30 08:50:51 -04003263{
Chris Mason07157aa2007-08-30 08:50:51 -04003264 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003265 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003266 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003267 const u32 sectorsize = root->fs_info->sectorsize;
3268 u32 pg_off;
Qu Wenruo08508fe2021-05-03 10:08:54 +08003269 unsigned int result = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003270
Chris Masond20f7042008-12-08 16:58:54 -05003271 if (PageChecked(page)) {
3272 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003273 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003275
Qu Wenruo3670e642021-07-26 14:34:51 +08003276 /*
3277 * For subpage case, above PageChecked is not safe as it's not subpage
3278 * compatible.
3279 * But for now only cow fixup and compressed read utilize PageChecked
3280 * flag, while in this context we can easily use io_bio->csum to
3281 * determine if we really need to do csum verification.
3282 *
3283 * So for now, just exit if io_bio->csum is NULL, as it means it's
3284 * compressed read, and its compressed data csum has already been
3285 * verified.
3286 */
3287 if (io_bio->csum == NULL)
3288 return 0;
3289
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003290 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003291 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003292
Josef Bacik42437a62020-10-16 11:29:18 -04003293 if (!root->fs_info->csum_root)
3294 return 0;
3295
Qu Wenruof44cf412020-12-02 14:47:59 +08003296 ASSERT(page_offset(page) <= start &&
3297 end <= page_offset(page) + PAGE_SIZE - 1);
3298 for (pg_off = offset_in_page(start);
3299 pg_off < offset_in_page(end);
3300 pg_off += sectorsize, bio_offset += sectorsize) {
Qu Wenruoe3c62322021-07-26 14:35:04 +08003301 u64 file_offset = pg_off + page_offset(page);
Qu Wenruof44cf412020-12-02 14:47:59 +08003302 int ret;
3303
Qu Wenruoe3c62322021-07-26 14:35:04 +08003304 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3305 test_range_bit(io_tree, file_offset,
3306 file_offset + sectorsize - 1,
3307 EXTENT_NODATASUM, 1, NULL)) {
3308 /* Skip the range without csum for data reloc inode */
3309 clear_extent_bits(io_tree, file_offset,
3310 file_offset + sectorsize - 1,
3311 EXTENT_NODATASUM);
3312 continue;
3313 }
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07003314 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3315 page_offset(page) + pg_off);
Qu Wenruo08508fe2021-05-03 10:08:54 +08003316 if (ret < 0) {
3317 const int nr_bit = (pg_off - offset_in_page(start)) >>
3318 root->fs_info->sectorsize_bits;
3319
3320 result |= (1U << nr_bit);
3321 }
Qu Wenruof44cf412020-12-02 14:47:59 +08003322 }
Qu Wenruo08508fe2021-05-03 10:08:54 +08003323 return result;
Chris Mason07157aa2007-08-30 08:50:51 -04003324}
Chris Masonb888db22007-08-27 16:49:44 -04003325
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003326/*
3327 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3328 *
3329 * @inode: The inode we want to perform iput on
3330 *
3331 * This function uses the generic vfs_inode::i_count to track whether we should
3332 * just decrement it (in case it's > 1) or if this is the last iput then link
3333 * the inode to the delayed iput machinery. Delayed iputs are processed at
3334 * transaction commit time/superblock commit/cleaner kthread.
3335 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003336void btrfs_add_delayed_iput(struct inode *inode)
3337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003339 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003340
3341 if (atomic_add_unless(&inode->i_count, -1, 1))
3342 return;
3343
Josef Bacik034f7842018-12-03 11:06:52 -05003344 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003345 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003346 ASSERT(list_empty(&binode->delayed_iput));
3347 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003348 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003349 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3350 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003351}
3352
Josef Bacik63611e72019-06-18 10:59:18 -04003353static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3354 struct btrfs_inode *inode)
3355{
3356 list_del_init(&inode->delayed_iput);
3357 spin_unlock(&fs_info->delayed_iput_lock);
3358 iput(&inode->vfs_inode);
3359 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3360 wake_up(&fs_info->delayed_iputs_wait);
3361 spin_lock(&fs_info->delayed_iput_lock);
3362}
3363
3364static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3365 struct btrfs_inode *inode)
3366{
3367 if (!list_empty(&inode->delayed_iput)) {
3368 spin_lock(&fs_info->delayed_iput_lock);
3369 if (!list_empty(&inode->delayed_iput))
3370 run_delayed_iput_locked(fs_info, inode);
3371 spin_unlock(&fs_info->delayed_iput_lock);
3372 }
3373}
3374
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003375void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003376{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003377
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003378 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003379 while (!list_empty(&fs_info->delayed_iputs)) {
3380 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003381
David Sterba8089fe62015-11-19 14:15:51 +01003382 inode = list_first_entry(&fs_info->delayed_iputs,
3383 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003384 run_delayed_iput_locked(fs_info, inode);
Josef Bacik71795ee2021-04-29 10:51:34 -04003385 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003386 }
David Sterba8089fe62015-11-19 14:15:51 +01003387 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003388}
3389
Josef Bacik034f7842018-12-03 11:06:52 -05003390/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003391 * Wait for flushing all delayed iputs
3392 *
3393 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003394 *
3395 * This will wait on any delayed iputs that are currently running with KILLABLE
3396 * set. Once they are all done running we will return, unless we are killed in
3397 * which case we return EINTR. This helps in user operations like fallocate etc
3398 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003399 *
3400 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003401 */
3402int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3403{
3404 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3405 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3406 if (ret)
3407 return -EINTR;
3408 return 0;
3409}
3410
Yan, Zhengd68fc572010-05-16 10:49:58 -04003411/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003412 * This creates an orphan entry for the given inode in case something goes wrong
3413 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003414 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003415int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003416 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003417{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003418 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003419
Omar Sandoval27919062018-05-11 13:13:37 -07003420 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3421 if (ret && ret != -EEXIST) {
3422 btrfs_abort_transaction(trans, ret);
3423 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003424 }
3425
Yan, Zhengd68fc572010-05-16 10:49:58 -04003426 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003427}
3428
3429/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003430 * We have done the delete so we can go ahead and remove the orphan item for
3431 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003432 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003433static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003434 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003435{
Omar Sandoval27919062018-05-11 13:13:37 -07003436 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003437}
3438
3439/*
3440 * this cleans up any orphans that may be left on the list from the last use
3441 * of this root.
3442 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003443int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003445 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003446 struct btrfs_path *path;
3447 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003448 struct btrfs_key key, found_key;
3449 struct btrfs_trans_handle *trans;
3450 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003451 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003452 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003453
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003455 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003456
3457 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003458 if (!path) {
3459 ret = -ENOMEM;
3460 goto out;
3461 }
David Sterbae4058b52015-11-27 16:31:35 +01003462 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003463
3464 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003465 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 key.offset = (u64)-1;
3467
Josef Bacik7b128762008-07-24 12:17:14 -04003468 while (1) {
3469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470 if (ret < 0)
3471 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003472
3473 /*
3474 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003475 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003476 * find the key and see if we have stuff that matches
3477 */
3478 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003479 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003480 if (path->slots[0] == 0)
3481 break;
3482 path->slots[0]--;
3483 }
3484
3485 /* pull out the item */
3486 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3488
3489 /* make sure the item matches what we want */
3490 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3491 break;
David Sterba962a2982014-06-04 18:41:45 +02003492 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003493 break;
3494
3495 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003496 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003497
3498 /*
3499 * this is where we are basically btrfs_lookup, without the
3500 * crossing root thing. we store the inode number in the
3501 * offset of the orphan item.
3502 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003503
3504 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003505 btrfs_err(fs_info,
3506 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003507 ret = -EINVAL;
3508 goto out;
3509 }
3510
3511 last_objectid = found_key.offset;
3512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003513 found_key.objectid = found_key.offset;
3514 found_key.type = BTRFS_INODE_ITEM_KEY;
3515 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003516 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303517 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003518 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003519 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003522 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003523 int is_dead_root = 0;
3524
3525 /*
Filipe Manana0c0218e2021-03-16 16:54:13 +00003526 * This is an orphan in the tree root. Currently these
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003527 * could come from 2 sources:
Filipe Manana0c0218e2021-03-16 16:54:13 +00003528 * a) a root (snapshot/subvolume) deletion in progress
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003529 * b) a free space cache inode
Filipe Manana0c0218e2021-03-16 16:54:13 +00003530 * We need to distinguish those two, as the orphan item
3531 * for a root must not get deleted before the deletion
3532 * of the snapshot/subvolume's tree completes.
3533 *
3534 * btrfs_find_orphan_roots() ran before us, which has
3535 * found all deleted roots and loaded them into
3536 * fs_info->fs_roots_radix. So here we can find if an
3537 * orphan item corresponds to a deleted root by looking
3538 * up the root from that radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003539 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003540
3541 spin_lock(&fs_info->fs_roots_radix_lock);
3542 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3543 (unsigned long)found_key.objectid);
3544 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3545 is_dead_root = 1;
3546 spin_unlock(&fs_info->fs_roots_radix_lock);
3547
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003548 if (is_dead_root) {
3549 /* prevent this orphan from being found again */
3550 key.offset = found_key.objectid - 1;
3551 continue;
3552 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003553
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003554 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003555
Josef Bacika8c9e572011-09-21 16:55:59 -04003556 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003557 * If we have an inode with links, there are a couple of
3558 * possibilities. Old kernels (before v3.12) used to create an
3559 * orphan item for truncate indicating that there were possibly
3560 * extent items past i_size that needed to be deleted. In v3.12,
3561 * truncate was changed to update i_size in sync with the extent
3562 * items, but the (useless) orphan item was still created. Since
3563 * v4.18, we don't create the orphan item for truncate at all.
3564 *
3565 * So, this item could mean that we need to do a truncate, but
3566 * only if this filesystem was last used on a pre-v3.12 kernel
3567 * and was not cleanly unmounted. The odds of that are quite
3568 * slim, and it's a pain to do the truncate now, so just delete
3569 * the orphan item.
3570 *
3571 * It's also possible that this orphan item was supposed to be
3572 * deleted but wasn't. The inode number may have been reused,
3573 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003574 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003575 if (ret == -ENOENT || inode->i_nlink) {
3576 if (!ret)
3577 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003578 trans = btrfs_start_transaction(root, 1);
3579 if (IS_ERR(trans)) {
3580 ret = PTR_ERR(trans);
3581 goto out;
3582 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003583 btrfs_debug(fs_info, "auto deleting %Lu",
3584 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003585 ret = btrfs_del_orphan_item(trans, root,
3586 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003587 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003588 if (ret)
3589 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003590 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003591 }
Josef Bacik7b128762008-07-24 12:17:14 -04003592
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003593 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003594
3595 /* this will do delete_inode and everything for us */
3596 iput(inode);
3597 }
Miao Xie3254c872011-11-10 20:45:05 -05003598 /* release the path since we're done with it */
3599 btrfs_release_path(path);
3600
Yan, Zhengd68fc572010-05-16 10:49:58 -04003601 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3602
Omar Sandovala575cee2018-05-11 13:13:38 -07003603 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003604 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003605 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003606 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003607 }
Josef Bacik7b128762008-07-24 12:17:14 -04003608
3609 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003610 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003611
3612out:
3613 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003615 btrfs_free_path(path);
3616 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003617}
3618
Chris Masond352ac62008-09-29 15:18:18 -04003619/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003620 * very simple check to peek ahead in the leaf looking for xattrs. If we
3621 * don't find any xattrs, we know there can't be any acls.
3622 *
3623 * slot is the slot the inode is in, objectid is the objectid of the inode
3624 */
3625static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003626 int slot, u64 objectid,
3627 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003628{
3629 u32 nritems = btrfs_header_nritems(leaf);
3630 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003631 static u64 xattr_access = 0;
3632 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003633 int scanned = 0;
3634
Josef Bacikf23b5a52013-06-19 10:16:26 -04003635 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003636 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3637 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3638 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3639 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003640 }
3641
Chris Mason46a53cc2009-04-27 11:47:50 -04003642 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003643 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003644 while (slot < nritems) {
3645 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3646
3647 /* we found a different objectid, there must not be acls */
3648 if (found_key.objectid != objectid)
3649 return 0;
3650
3651 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003652 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003653 if (*first_xattr_slot == -1)
3654 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003655 if (found_key.offset == xattr_access ||
3656 found_key.offset == xattr_default)
3657 return 1;
3658 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003659
3660 /*
3661 * we found a key greater than an xattr key, there can't
3662 * be any acls later on
3663 */
3664 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3665 return 0;
3666
3667 slot++;
3668 scanned++;
3669
3670 /*
3671 * it goes inode, inode backrefs, xattrs, extents,
3672 * so if there are a ton of hard links to an inode there can
3673 * be a lot of backrefs. Don't waste time searching too hard,
3674 * this is just an optimization
3675 */
3676 if (scanned >= 8)
3677 break;
3678 }
3679 /* we hit the end of the leaf before we found an xattr or
3680 * something larger than an xattr. We have to assume the inode
3681 * has acls
3682 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003683 if (*first_xattr_slot == -1)
3684 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003685 return 1;
3686}
3687
3688/*
Chris Masond352ac62008-09-29 15:18:18 -04003689 * read an inode from the btree into the in-memory inode
3690 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003691static int btrfs_read_locked_inode(struct inode *inode,
3692 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003695 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003696 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 struct btrfs_inode_item *inode_item;
3698 struct btrfs_root *root = BTRFS_I(inode)->root;
3699 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003700 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003701 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003702 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003703 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003704 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003705 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003706
3707 ret = btrfs_fill_inode(inode, &rdev);
3708 if (!ret)
3709 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003710
Filipe Manana4222ea72018-10-24 10:13:03 +01003711 if (!path) {
3712 path = btrfs_alloc_path();
3713 if (!path)
3714 return -ENOMEM;
3715 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003716
Chris Mason39279cc2007-06-12 06:35:45 -04003717 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003718
Chris Mason39279cc2007-06-12 06:35:45 -04003719 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003720 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003721 if (path != in_path)
3722 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003723 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003724 }
Chris Mason39279cc2007-06-12 06:35:45 -04003725
Chris Mason5f39d392007-10-15 16:14:19 -04003726 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003727
3728 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003729 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003730
Chris Mason5f39d392007-10-15 16:14:19 -04003731 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3732 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003733 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003734 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003735 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3736 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003737 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003738 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3739 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003740
David Sterbaa937b972014-12-12 17:39:12 +01003741 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3742 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003743
David Sterbaa937b972014-12-12 17:39:12 +01003744 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3745 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003746
David Sterbaa937b972014-12-12 17:39:12 +01003747 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3748 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003749
chandan r9cc97d62012-07-04 12:48:07 +05303750 BTRFS_I(inode)->i_otime.tv_sec =
3751 btrfs_timespec_sec(leaf, &inode_item->otime);
3752 BTRFS_I(inode)->i_otime.tv_nsec =
3753 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003755 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003756 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003757 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3758
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003759 inode_set_iversion_queried(inode,
3760 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003761 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003762 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003763 rdev = btrfs_inode_rdev(leaf, inode_item);
3764
Josef Bacikaec74772008-07-24 12:12:38 -04003765 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003766 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003767
3768cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003769 /*
3770 * If we were modified in the current generation and evicted from memory
3771 * and then re-read we need to do a full sync since we don't have any
3772 * idea about which extents were modified before we were evicted from
3773 * cache.
3774 *
3775 * This is required for both inode re-read from disk and delayed inode
3776 * in delayed_nodes_tree.
3777 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003778 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003779 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3780 &BTRFS_I(inode)->runtime_flags);
3781
Filipe Mananabde6c242015-07-24 00:00:19 +01003782 /*
3783 * We don't persist the id of the transaction where an unlink operation
3784 * against the inode was last made. So here we assume the inode might
3785 * have been evicted, and therefore the exact value of last_unlink_trans
3786 * lost, and set it to last_trans to avoid metadata inconsistencies
3787 * between the inode and its parent if the inode is fsync'ed and the log
3788 * replayed. For example, in the scenario:
3789 *
3790 * touch mydir/foo
3791 * ln mydir/foo mydir/bar
3792 * sync
3793 * unlink mydir/bar
3794 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3795 * xfs_io -c fsync mydir/foo
3796 * <power failure>
3797 * mount fs, triggers fsync log replay
3798 *
3799 * We must make sure that when we fsync our inode foo we also log its
3800 * parent inode, otherwise after log replay the parent still has the
3801 * dentry with the "bar" name but our inode foo has a link count of 1
3802 * and doesn't have an inode ref with the name "bar" anymore.
3803 *
3804 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003805 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003806 * transaction commits on fsync if our inode is a directory, or if our
3807 * inode is not a directory, logging its parent unnecessarily.
3808 */
3809 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3810
Filipe Manana3ebac172020-07-15 12:30:43 +01003811 /*
3812 * Same logic as for last_unlink_trans. We don't persist the generation
3813 * of the last transaction where this inode was used for a reflink
3814 * operation, so after eviction and reloading the inode we must be
3815 * pessimistic and assume the last transaction that modified the inode.
3816 */
3817 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3818
Miao Xie67de1172013-12-26 13:07:06 +08003819 path->slots[0]++;
3820 if (inode->i_nlink != 1 ||
3821 path->slots[0] >= btrfs_header_nritems(leaf))
3822 goto cache_acl;
3823
3824 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003825 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003826 goto cache_acl;
3827
3828 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3829 if (location.type == BTRFS_INODE_REF_KEY) {
3830 struct btrfs_inode_ref *ref;
3831
3832 ref = (struct btrfs_inode_ref *)ptr;
3833 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3834 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3835 struct btrfs_inode_extref *extref;
3836
3837 extref = (struct btrfs_inode_extref *)ptr;
3838 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3839 extref);
3840 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003841cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003842 /*
3843 * try to precache a NULL acl entry for files that don't have
3844 * any xattrs or acls
3845 */
Li Zefan33345d012011-04-20 10:31:50 +08003846 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003847 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003848 if (first_xattr_slot != -1) {
3849 path->slots[0] = first_xattr_slot;
3850 ret = btrfs_load_inode_props(inode, path);
3851 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003852 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003853 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003854 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003855 root->root_key.objectid, ret);
3856 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003857 if (path != in_path)
3858 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003859
Al Viro72c04902009-06-24 16:58:48 -04003860 if (!maybe_acls)
3861 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003862
Chris Mason39279cc2007-06-12 06:35:45 -04003863 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003864 case S_IFREG:
3865 inode->i_mapping->a_ops = &btrfs_aops;
3866 inode->i_fop = &btrfs_file_operations;
3867 inode->i_op = &btrfs_file_inode_operations;
3868 break;
3869 case S_IFDIR:
3870 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003871 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 break;
3873 case S_IFLNK:
3874 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003875 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003876 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003878 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003879 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003880 init_special_inode(inode, inode->i_mode, rdev);
3881 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003882 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003883
David Sterba7b6a2212018-03-26 18:40:21 +02003884 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003885 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003886}
3887
Chris Masond352ac62008-09-29 15:18:18 -04003888/*
3889 * given a leaf and an inode, copy the inode fields into the leaf
3890 */
Chris Masone02119d2008-09-05 16:13:11 -04003891static void fill_inode_item(struct btrfs_trans_handle *trans,
3892 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003893 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003894 struct inode *inode)
3895{
Liu Bo51fab692012-12-27 09:01:21 +00003896 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003897
David Sterbac82f8232019-08-09 17:48:21 +02003898 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003899
David Sterbacc4c13d2020-04-29 02:15:56 +02003900 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3901 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3902 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3903 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3904 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003905
David Sterbacc4c13d2020-04-29 02:15:56 +02003906 btrfs_set_token_timespec_sec(&token, &item->atime,
3907 inode->i_atime.tv_sec);
3908 btrfs_set_token_timespec_nsec(&token, &item->atime,
3909 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003910
David Sterbacc4c13d2020-04-29 02:15:56 +02003911 btrfs_set_token_timespec_sec(&token, &item->mtime,
3912 inode->i_mtime.tv_sec);
3913 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3914 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003915
David Sterbacc4c13d2020-04-29 02:15:56 +02003916 btrfs_set_token_timespec_sec(&token, &item->ctime,
3917 inode->i_ctime.tv_sec);
3918 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3919 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003920
David Sterbacc4c13d2020-04-29 02:15:56 +02003921 btrfs_set_token_timespec_sec(&token, &item->otime,
3922 BTRFS_I(inode)->i_otime.tv_sec);
3923 btrfs_set_token_timespec_nsec(&token, &item->otime,
3924 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303925
David Sterbacc4c13d2020-04-29 02:15:56 +02003926 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3927 btrfs_set_token_inode_generation(&token, item,
3928 BTRFS_I(inode)->generation);
3929 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3930 btrfs_set_token_inode_transid(&token, item, trans->transid);
3931 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3932 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3933 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003934}
3935
Chris Masond352ac62008-09-29 15:18:18 -04003936/*
3937 * copy everything in the in-memory inode into the btree.
3938 */
Chris Mason21151332011-11-10 20:39:08 -05003939static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003940 struct btrfs_root *root,
3941 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003942{
3943 struct btrfs_inode_item *inode_item;
3944 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003945 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003946 int ret;
3947
3948 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003949 if (!path)
3950 return -ENOMEM;
3951
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003952 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 if (ret) {
3954 if (ret > 0)
3955 ret = -ENOENT;
3956 goto failed;
3957 }
3958
Chris Mason5f39d392007-10-15 16:14:19 -04003959 leaf = path->nodes[0];
3960 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003961 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003962
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003963 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003964 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003965 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003966 ret = 0;
3967failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003968 btrfs_free_path(path);
3969 return ret;
3970}
3971
Chris Masond352ac62008-09-29 15:18:18 -04003972/*
Chris Mason21151332011-11-10 20:39:08 -05003973 * copy everything in the in-memory inode into the btree.
3974 */
3975noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003976 struct btrfs_root *root,
3977 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003978{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003979 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003980 int ret;
3981
3982 /*
3983 * If the inode is a free space inode, we can deadlock during commit
3984 * if we put it into the delayed code.
3985 *
3986 * The data relocation inode should also be directly updated
3987 * without delay
3988 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003989 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003990 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003991 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003992 btrfs_update_root_times(trans, root);
3993
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003994 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003995 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003996 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003997 return ret;
3998 }
3999
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004000 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004001}
4002
Nikolay Borisov729f7962020-11-02 16:49:06 +02004003int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4004 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004005{
4006 int ret;
4007
Nikolay Borisov729f7962020-11-02 16:49:06 +02004008 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004009 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02004010 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05004011 return ret;
4012}
4013
4014/*
Chris Masond352ac62008-09-29 15:18:18 -04004015 * unlink helper that gets used here in inode.c and in the tree logging
4016 * recovery code. It remove a link in a directory with a given name, and
4017 * also drops the back refs in the inode to the directory
4018 */
Al Viro92986792011-03-04 17:14:37 +00004019static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4020 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004021 struct btrfs_inode *dir,
4022 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004023 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004024{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004025 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004026 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004027 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004028 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004029 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004030 u64 ino = btrfs_ino(inode);
4031 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004032
4033 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004034 if (!path) {
4035 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004036 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004037 }
4038
Li Zefan33345d012011-04-20 10:31:50 +08004039 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004040 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004041 if (IS_ERR_OR_NULL(di)) {
4042 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 goto err;
4044 }
Chris Mason39279cc2007-06-12 06:35:45 -04004045 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 if (ret)
4047 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004048 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004049
Miao Xie67de1172013-12-26 13:07:06 +08004050 /*
4051 * If we don't have dir index, we have to get it by looking up
4052 * the inode ref, since we get the inode ref, remove it directly,
4053 * it is unnecessary to do delayed deletion.
4054 *
4055 * But if we have dir index, needn't search inode ref to get it.
4056 * Since the inode ref is close to the inode item, it is better
4057 * that we delay to delete it, and just do this deletion when
4058 * we update the inode item.
4059 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004060 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004061 ret = btrfs_delayed_delete_inode_ref(inode);
4062 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004064 goto skip_backref;
4065 }
4066 }
4067
Li Zefan33345d012011-04-20 10:31:50 +08004068 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4069 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004070 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004071 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004072 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004073 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004074 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004075 goto err;
4076 }
Miao Xie67de1172013-12-26 13:07:06 +08004077skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004078 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004080 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004081 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 }
Chris Mason39279cc2007-06-12 06:35:45 -04004083
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004084 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4085 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004087 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 goto err;
4089 }
Chris Masone02119d2008-09-05 16:13:11 -04004090
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004091 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4092 index);
Chris Mason6418c962010-10-30 07:34:24 -04004093 if (ret == -ENOENT)
4094 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004095 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004097
4098 /*
4099 * If we have a pending delayed iput we could end up with the final iput
4100 * being run in btrfs-cleaner context. If we have enough of these built
4101 * up we can end up burning a lot of time in btrfs-cleaner without any
4102 * way to throttle the unlinks. Since we're currently holding a ref on
4103 * the inode we can run the delayed iput here without any issues as the
4104 * final iput won't be done until after we drop the ref we're currently
4105 * holding.
4106 */
4107 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004108err:
4109 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004110 if (ret)
4111 goto out;
4112
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004113 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004114 inode_inc_iversion(&inode->vfs_inode);
4115 inode_inc_iversion(&dir->vfs_inode);
4116 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4117 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004118 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004119out:
Chris Mason39279cc2007-06-12 06:35:45 -04004120 return ret;
4121}
4122
Al Viro92986792011-03-04 17:14:37 +00004123int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4124 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004125 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004126 const char *name, int name_len)
4127{
4128 int ret;
4129 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4130 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02004132 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00004133 }
4134 return ret;
4135}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004136
4137/*
4138 * helper to start transaction for unlink and rmdir.
4139 *
Josef Bacikd52be812013-05-29 14:54:47 -04004140 * unlink and rmdir are special in btrfs, they do not always free space, so
4141 * if we cannot make our reservations the normal way try and see if there is
4142 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4143 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004144 */
Josef Bacikd52be812013-05-29 14:54:47 -04004145static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004147 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004148
Josef Bacike70bea52011-10-11 14:18:24 -04004149 /*
4150 * 1 for the possible orphan item
4151 * 1 for the dir item
4152 * 1 for the dir index
4153 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004154 * 1 for the inode
4155 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04004156 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004157}
4158
Chris Mason39279cc2007-06-12 06:35:45 -04004159static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4160{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004162 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004163 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004164 int ret;
4165
Josef Bacikd52be812013-05-29 14:54:47 -04004166 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004168 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004169
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004170 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4171 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004172
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004173 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4174 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4175 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004176 if (ret)
4177 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004178
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004180 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004181 if (ret)
4182 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183 }
Josef Bacik7b128762008-07-24 12:17:14 -04004184
Tsutomu Itohb5324022011-07-19 07:27:20 +00004185out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004186 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004187 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004188 return ret;
4189}
4190
Misono Tomohirof60a2362018-04-18 11:34:52 +09004191static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05004192 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004193{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004194 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05004195 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004196 struct btrfs_path *path;
4197 struct extent_buffer *leaf;
4198 struct btrfs_dir_item *di;
4199 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05004200 const char *name = dentry->d_name.name;
4201 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 u64 index;
4203 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05004204 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004205 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206
Josef Bacik045d3962019-12-18 17:20:27 -05004207 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4208 objectid = inode->root->root_key.objectid;
4209 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4210 objectid = inode->location.objectid;
4211 } else {
4212 WARN_ON(1);
4213 return -EINVAL;
4214 }
4215
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004216 path = btrfs_alloc_path();
4217 if (!path)
4218 return -ENOMEM;
4219
Li Zefan33345d012011-04-20 10:31:50 +08004220 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004222 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004223 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 goto out;
4225 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226
4227 leaf = path->nodes[0];
4228 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4229 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4230 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 goto out;
4234 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004235 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
Josef Bacikd49d3282019-12-18 17:20:28 -05004237 /*
4238 * This is a placeholder inode for a subvolume we didn't have a
4239 * reference to at the time of the snapshot creation. In the meantime
4240 * we could have renamed the real subvol link into our snapshot, so
David Sterba1a9fd412021-05-21 17:42:23 +02004241 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
Josef Bacikd49d3282019-12-18 17:20:28 -05004242 * Instead simply lookup the dir_index_item for this entry so we can
4243 * remove it. Otherwise we know we have a ref to the root and we can
4244 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4245 */
4246 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08004247 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004248 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 if (IS_ERR_OR_NULL(di)) {
4250 if (!di)
4251 ret = -ENOENT;
4252 else
4253 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004254 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 goto out;
4256 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257
4258 leaf = path->nodes[0];
4259 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05004261 btrfs_release_path(path);
4262 } else {
4263 ret = btrfs_del_root_ref(trans, objectid,
4264 root->root_key.objectid, dir_ino,
4265 &index, name, name_len);
4266 if (ret) {
4267 btrfs_abort_transaction(trans, ret);
4268 goto out;
4269 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004270 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271
Lu Fengqi9add2942018-08-01 11:32:26 +08004272 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004274 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275 goto out;
4276 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004277
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004278 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004279 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004280 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02004281 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004283 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004284out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004285 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287}
4288
Misono Tomohiroec42f162018-04-18 11:34:13 +09004289/*
4290 * Helper to check if the subvolume references other subvolumes or if it's
4291 * default.
4292 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004293static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004294{
4295 struct btrfs_fs_info *fs_info = root->fs_info;
4296 struct btrfs_path *path;
4297 struct btrfs_dir_item *di;
4298 struct btrfs_key key;
4299 u64 dir_id;
4300 int ret;
4301
4302 path = btrfs_alloc_path();
4303 if (!path)
4304 return -ENOMEM;
4305
4306 /* Make sure this root isn't set as the default subvol */
4307 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4308 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4309 dir_id, "default", 7, 0);
4310 if (di && !IS_ERR(di)) {
4311 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4312 if (key.objectid == root->root_key.objectid) {
4313 ret = -EPERM;
4314 btrfs_err(fs_info,
4315 "deleting default subvolume %llu is not allowed",
4316 key.objectid);
4317 goto out;
4318 }
4319 btrfs_release_path(path);
4320 }
4321
4322 key.objectid = root->root_key.objectid;
4323 key.type = BTRFS_ROOT_REF_KEY;
4324 key.offset = (u64)-1;
4325
4326 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4327 if (ret < 0)
4328 goto out;
4329 BUG_ON(ret == 0);
4330
4331 ret = 0;
4332 if (path->slots[0] > 0) {
4333 path->slots[0]--;
4334 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4335 if (key.objectid == root->root_key.objectid &&
4336 key.type == BTRFS_ROOT_REF_KEY)
4337 ret = -ENOTEMPTY;
4338 }
4339out:
4340 btrfs_free_path(path);
4341 return ret;
4342}
4343
Nikolay Borisov20a68002018-04-27 14:36:24 +03004344/* Delete all dentries for inodes belonging to the root */
4345static void btrfs_prune_dentries(struct btrfs_root *root)
4346{
4347 struct btrfs_fs_info *fs_info = root->fs_info;
4348 struct rb_node *node;
4349 struct rb_node *prev;
4350 struct btrfs_inode *entry;
4351 struct inode *inode;
4352 u64 objectid = 0;
4353
4354 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4355 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4356
4357 spin_lock(&root->inode_lock);
4358again:
4359 node = root->inode_tree.rb_node;
4360 prev = NULL;
4361 while (node) {
4362 prev = node;
4363 entry = rb_entry(node, struct btrfs_inode, rb_node);
4364
David Sterba37508512018-06-29 10:56:40 +02004365 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004366 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004367 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004368 node = node->rb_right;
4369 else
4370 break;
4371 }
4372 if (!node) {
4373 while (prev) {
4374 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004375 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004376 node = prev;
4377 break;
4378 }
4379 prev = rb_next(prev);
4380 }
4381 }
4382 while (node) {
4383 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004384 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004385 inode = igrab(&entry->vfs_inode);
4386 if (inode) {
4387 spin_unlock(&root->inode_lock);
4388 if (atomic_read(&inode->i_count) > 1)
4389 d_prune_aliases(inode);
4390 /*
4391 * btrfs_drop_inode will have it removed from the inode
4392 * cache when its usage count hits zero.
4393 */
4394 iput(inode);
4395 cond_resched();
4396 spin_lock(&root->inode_lock);
4397 goto again;
4398 }
4399
4400 if (cond_resched_lock(&root->inode_lock))
4401 goto again;
4402
4403 node = rb_next(node);
4404 }
4405 spin_unlock(&root->inode_lock);
4406}
4407
Misono Tomohirof60a2362018-04-18 11:34:52 +09004408int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4409{
4410 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4411 struct btrfs_root *root = BTRFS_I(dir)->root;
4412 struct inode *inode = d_inode(dentry);
4413 struct btrfs_root *dest = BTRFS_I(inode)->root;
4414 struct btrfs_trans_handle *trans;
4415 struct btrfs_block_rsv block_rsv;
4416 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004417 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004418
4419 /*
4420 * Don't allow to delete a subvolume with send in progress. This is
4421 * inside the inode lock so the error handling that has to drop the bit
4422 * again is not run concurrently.
4423 */
4424 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004425 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004426 spin_unlock(&dest->root_item_lock);
4427 btrfs_warn(fs_info,
4428 "attempt to delete subvolume %llu during send",
4429 dest->root_key.objectid);
4430 return -EPERM;
4431 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004432 root_flags = btrfs_root_flags(&dest->root_item);
4433 btrfs_set_root_flags(&dest->root_item,
4434 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4435 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004436
4437 down_write(&fs_info->subvol_sem);
4438
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004439 ret = may_destroy_subvol(dest);
4440 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004441 goto out_up_write;
4442
4443 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4444 /*
4445 * One for dir inode,
4446 * two for dir entries,
4447 * two for root ref/backref.
4448 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004449 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4450 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004451 goto out_up_write;
4452
4453 trans = btrfs_start_transaction(root, 0);
4454 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004455 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004456 goto out_release;
4457 }
4458 trans->block_rsv = &block_rsv;
4459 trans->bytes_reserved = block_rsv.size;
4460
4461 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4462
Josef Bacik045d3962019-12-18 17:20:27 -05004463 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004464 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004465 btrfs_abort_transaction(trans, ret);
4466 goto out_end_trans;
4467 }
4468
Josef Bacik2731f512021-03-12 15:25:04 -05004469 ret = btrfs_record_root_in_trans(trans, dest);
4470 if (ret) {
4471 btrfs_abort_transaction(trans, ret);
4472 goto out_end_trans;
4473 }
Misono Tomohirof60a2362018-04-18 11:34:52 +09004474
4475 memset(&dest->root_item.drop_progress, 0,
4476 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004477 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004478 btrfs_set_root_refs(&dest->root_item, 0);
4479
4480 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4481 ret = btrfs_insert_orphan_item(trans,
4482 fs_info->tree_root,
4483 dest->root_key.objectid);
4484 if (ret) {
4485 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004486 goto out_end_trans;
4487 }
4488 }
4489
Lu Fengqid1957792018-05-29 15:01:54 +08004490 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004491 BTRFS_UUID_KEY_SUBVOL,
4492 dest->root_key.objectid);
4493 if (ret && ret != -ENOENT) {
4494 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004495 goto out_end_trans;
4496 }
4497 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004498 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004499 dest->root_item.received_uuid,
4500 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4501 dest->root_key.objectid);
4502 if (ret && ret != -ENOENT) {
4503 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004504 goto out_end_trans;
4505 }
4506 }
4507
Qu Wenruo082b6c92020-06-16 10:17:37 +08004508 free_anon_bdev(dest->anon_dev);
4509 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004510out_end_trans:
4511 trans->block_rsv = NULL;
4512 trans->bytes_reserved = 0;
4513 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004514 inode->i_flags |= S_DEAD;
4515out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004516 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004517out_up_write:
4518 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004519 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004520 spin_lock(&dest->root_item_lock);
4521 root_flags = btrfs_root_flags(&dest->root_item);
4522 btrfs_set_root_flags(&dest->root_item,
4523 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4524 spin_unlock(&dest->root_item_lock);
4525 } else {
4526 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004527 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004528 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004529 }
4530
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004531 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004532}
4533
Chris Mason39279cc2007-06-12 06:35:45 -04004534static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4535{
David Howells2b0143b2015-03-17 22:25:59 +00004536 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004537 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004538 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004539 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004540 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004541
David Sterbab3ae2442012-09-13 16:04:34 -06004542 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004543 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004544 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004545 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004546
Josef Bacikd52be812013-05-29 14:54:47 -04004547 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004548 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004549 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004550
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004551 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004552 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004553 goto out;
4554 }
4555
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004556 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004557 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004558 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004559
Filipe Manana44f714d2016-06-06 16:11:13 +01004560 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4561
Chris Mason39279cc2007-06-12 06:35:45 -04004562 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004563 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4564 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4565 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004566 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004567 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004568 /*
4569 * Propagate the last_unlink_trans value of the deleted dir to
4570 * its parent directory. This is to prevent an unrecoverable
4571 * log tree in the case we do something like this:
4572 * 1) create dir foo
4573 * 2) create snapshot under dir foo
4574 * 3) delete the snapshot
4575 * 4) rmdir foo
4576 * 5) mkdir foo
4577 * 6) fsync foo or some file inside foo
4578 */
4579 if (last_unlink_trans >= trans->transid)
4580 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4581 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004582out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004583 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004584 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004585
Chris Mason39279cc2007-06-12 06:35:45 -04004586 return err;
4587}
4588
Josef Bacikddfae632017-10-19 14:16:02 -04004589/*
4590 * Return this if we need to call truncate_block for the last bit of the
4591 * truncate.
4592 */
4593#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004594
Chris Mason323ac952008-10-01 19:05:46 -04004595/*
Filipe Manana4f7e6732021-05-24 11:35:54 +01004596 * Remove inode items from a given root.
Chris Mason39279cc2007-06-12 06:35:45 -04004597 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004598 * @trans: A transaction handle.
4599 * @root: The root from which to remove items.
4600 * @inode: The inode whose items we want to remove.
4601 * @new_size: The new i_size for the inode. This is only applicable when
4602 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4603 * @min_type: The minimum key type to remove. All keys with a type
4604 * greater than this value are removed and all keys with
4605 * this type are removed only if their offset is >= @new_size.
Filipe Manana0d7d3162021-05-24 11:35:55 +01004606 * @extents_found: Output parameter that will contain the number of file
4607 * extent items that were removed or adjusted to the new
4608 * inode i_size. The caller is responsible for initializing
4609 * the counter. Also, it can be NULL if the caller does not
4610 * need this counter.
Josef Bacik7b128762008-07-24 12:17:14 -04004611 *
Filipe Manana4f7e6732021-05-24 11:35:54 +01004612 * Remove all keys associated with the inode from the given root that have a key
4613 * with a type greater than or equals to @min_type. When @min_type has a value of
4614 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4615 * greater than or equals to @new_size. If a file extent item that starts before
4616 * @new_size and ends after it is found, its length is adjusted.
4617 *
4618 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4619 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
Chris Mason39279cc2007-06-12 06:35:45 -04004620 */
Yan, Zheng80825102009-11-12 09:35:36 +00004621int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4622 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004623 struct btrfs_inode *inode,
Filipe Manana0d7d3162021-05-24 11:35:55 +01004624 u64 new_size, u32 min_type,
4625 u64 *extents_found)
Chris Mason39279cc2007-06-12 06:35:45 -04004626{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004627 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004628 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004629 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004630 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004631 struct btrfs_key key;
4632 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004633 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004634 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004635 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004636 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004637 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004638 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004639 int found_extent;
4640 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004641 int pending_del_nr = 0;
4642 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004643 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004644 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004645 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004646 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004647 bool be_nice = false;
4648 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004649 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4650 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004651
4652 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004653
Chris Mason28ed1342014-12-17 09:41:04 -08004654 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004655 * For non-free space inodes and non-shareable roots, we want to back
4656 * off from time to time. This means all inodes in subvolume roots,
4657 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004658 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004659 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004660 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004661 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004662
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004663 path = btrfs_alloc_path();
4664 if (!path)
4665 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004666 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004667
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004668 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004669 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004670 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004671
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004672 /*
4673 * We want to drop from the next block forward in case this
4674 * new size is not block aligned since we will be keeping the
4675 * last block of the extent just the way it is.
4676 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004677 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004678 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004679 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004680 }
Yan, Zheng80825102009-11-12 09:35:36 +00004681
Miao Xie16cdcec2011-04-22 18:12:22 +08004682 /*
4683 * This function is also used to drop the items in the log tree before
4684 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004685 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004686 * items.
4687 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004688 if (min_type == 0 && root == inode->root)
4689 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004690
Li Zefan33345d012011-04-20 10:31:50 +08004691 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004692 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004693 key.type = (u8)-1;
4694
Chris Mason85e21ba2008-01-29 15:11:36 -05004695search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004696 /*
4697 * with a 16K leaf size and 128MB extents, you can actually queue
4698 * up a huge file in a single leaf. Most of the time that
4699 * bytes_deleted is > 0, it will be huge by the time we get here
4700 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004701 if (be_nice && bytes_deleted > SZ_32M &&
4702 btrfs_should_end_transaction(trans)) {
4703 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 goto out;
4705 }
Chris Masond3977122009-01-05 21:25:51 -05004706
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004707 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4708 if (ret < 0)
4709 goto out;
4710
Chris Mason85e21ba2008-01-29 15:11:36 -05004711 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004712 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004713 /* there are no items in the tree for us to truncate, we're
4714 * done
4715 */
Yan, Zheng80825102009-11-12 09:35:36 +00004716 if (path->slots[0] == 0)
4717 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 path->slots[0]--;
4719 }
4720
Chris Masond3977122009-01-05 21:25:51 -05004721 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004722 u64 clear_start = 0, clear_len = 0;
4723
Chris Mason39279cc2007-06-12 06:35:45 -04004724 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004725 leaf = path->nodes[0];
4726 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004727 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004728
Li Zefan33345d012011-04-20 10:31:50 +08004729 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004730 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004731
Chris Mason85e21ba2008-01-29 15:11:36 -05004732 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004733 break;
4734
Chris Mason5f39d392007-10-15 16:14:19 -04004735 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004736 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004737 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004738 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004739 extent_type = btrfs_file_extent_type(leaf, fi);
4740 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004741 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004742 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004743
4744 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004745 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004746 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004747 item_end += btrfs_file_extent_ram_bytes(leaf,
4748 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004749
4750 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004751 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004752 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004753 }
Yan008630c2007-11-07 13:31:09 -05004754 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004755 }
Yan, Zheng80825102009-11-12 09:35:36 +00004756 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004757 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004758 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004759 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004760 break;
4761 if (found_key.offset >= new_size)
4762 del_item = 1;
4763 else
4764 del_item = 0;
4765 }
Chris Mason39279cc2007-06-12 06:35:45 -04004766 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004767 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004768 if (found_type != BTRFS_EXTENT_DATA_KEY)
4769 goto delete;
4770
Filipe Manana0d7d3162021-05-24 11:35:55 +01004771 if (extents_found != NULL)
4772 (*extents_found)++;
4773
Chris Mason179e29e2007-11-01 11:28:41 -04004774 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004775 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004776
4777 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004778 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004779 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004780 u64 orig_num_bytes =
4781 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004782 extent_num_bytes = ALIGN(new_size -
4783 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004784 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004785 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004786 btrfs_set_file_extent_num_bytes(leaf, fi,
4787 extent_num_bytes);
4788 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004789 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004790 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004791 &root->state) &&
4792 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004793 inode_sub_bytes(&inode->vfs_inode,
4794 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004795 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004796 } else {
Chris Masondb945352007-10-15 16:15:53 -04004797 extent_num_bytes =
4798 btrfs_file_extent_disk_num_bytes(leaf,
4799 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004800 extent_offset = found_key.offset -
4801 btrfs_file_extent_offset(leaf, fi);
4802
Chris Mason39279cc2007-06-12 06:35:45 -04004803 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004804 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004805 if (extent_start != 0) {
4806 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004807 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004808 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004809 inode_sub_bytes(&inode->vfs_inode,
4810 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004811 }
4812 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004813 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004814 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004815 /*
4816 * we can't truncate inline items that have had
4817 * special encodings
4818 */
4819 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004820 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004821 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4822 btrfs_file_extent_compression(leaf, fi) == 0) {
4823 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004824
Josef Bacikddfae632017-10-19 14:16:02 -04004825 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4826 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004827 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004828 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004829 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004830 * We have to bail so the last_size is set to
4831 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004832 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004833 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004834 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004835 } else {
4836 /*
4837 * Inline extents are special, we just treat
4838 * them as a full sector worth in the file
4839 * extent tree just for simplicity sake.
4840 */
4841 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004842 }
Josef Bacikddfae632017-10-19 14:16:02 -04004843
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004844 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004845 inode_sub_bytes(&inode->vfs_inode,
4846 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004847 }
Chris Mason179e29e2007-11-01 11:28:41 -04004848delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004849 /*
4850 * We use btrfs_truncate_inode_items() to clean up log trees for
4851 * multiple fsyncs, and in this case we don't want to clear the
4852 * file extent range because it's just the log.
4853 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004854 if (root == inode->root) {
4855 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004856 clear_start, clear_len);
4857 if (ret) {
4858 btrfs_abort_transaction(trans, ret);
4859 break;
4860 }
4861 }
4862
Josef Bacikddfae632017-10-19 14:16:02 -04004863 if (del_item)
4864 last_size = found_key.offset;
4865 else
4866 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004867 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004868 if (!pending_del_nr) {
4869 /* no pending yet, add ourselves */
4870 pending_del_slot = path->slots[0];
4871 pending_del_nr = 1;
4872 } else if (pending_del_nr &&
4873 path->slots[0] + 1 == pending_del_slot) {
4874 /* hop on the pending chunk */
4875 pending_del_nr++;
4876 pending_del_slot = path->slots[0];
4877 } else {
Chris Masond3977122009-01-05 21:25:51 -05004878 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004879 }
Chris Mason39279cc2007-06-12 06:35:45 -04004880 } else {
4881 break;
4882 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004883 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004884
Miao Xie27cdeb72014-04-02 19:51:05 +08004885 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004886 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004887 struct btrfs_ref ref = { 0 };
4888
Chris Mason28ed1342014-12-17 09:41:04 -08004889 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004890
4891 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4892 extent_start, extent_num_bytes, 0);
4893 ref.real_root = root->root_key.objectid;
4894 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4895 ino, extent_offset);
4896 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004897 if (ret) {
4898 btrfs_abort_transaction(trans, ret);
4899 break;
4900 }
Chris Mason28f75a02015-02-04 06:59:29 -08004901 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004902 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004903 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004904 }
Chris Mason39279cc2007-06-12 06:35:45 -04004905 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004906
Yan, Zheng80825102009-11-12 09:35:36 +00004907 if (found_type == BTRFS_INODE_ITEM_KEY)
4908 break;
4909
4910 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004911 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004912 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004913 if (pending_del_nr) {
4914 ret = btrfs_del_items(trans, root, path,
4915 pending_del_slot,
4916 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004917 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004918 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004919 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004920 }
Yan, Zheng80825102009-11-12 09:35:36 +00004921 pending_del_nr = 0;
4922 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004923 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004924
Chris Mason28f75a02015-02-04 06:59:29 -08004925 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004926 * We can generate a lot of delayed refs, so we need to
4927 * throttle every once and a while and make sure we're
4928 * adding enough space to keep up with the work we are
4929 * generating. Since we hold a transaction here we
4930 * can't flush, and we don't want to FLUSH_LIMIT because
4931 * we could have generated too many delayed refs to
4932 * actually allocate, so just bail if we're short and
4933 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004934 */
Josef Bacik28bad212018-12-03 10:20:38 -05004935 if (should_throttle) {
4936 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4937 BTRFS_RESERVE_NO_FLUSH);
4938 if (ret) {
4939 ret = -EAGAIN;
4940 break;
4941 }
Chris Mason28f75a02015-02-04 06:59:29 -08004942 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004943 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004944 } else {
4945 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004946 }
Chris Mason39279cc2007-06-12 06:35:45 -04004947 }
Yan, Zheng80825102009-11-12 09:35:36 +00004948out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004949 if (ret >= 0 && pending_del_nr) {
4950 int err;
4951
4952 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004953 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004954 if (err) {
4955 btrfs_abort_transaction(trans, err);
4956 ret = err;
4957 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004958 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004959 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4960 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004961 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004962 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004963 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4964 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4965 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004966 }
Chris Mason28ed1342014-12-17 09:41:04 -08004967
Chris Mason39279cc2007-06-12 06:35:45 -04004968 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004969 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004970}
4971
Chris Masona52d9a82007-08-27 16:49:44 -04004972/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304973 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004974 * @inode - inode that we're zeroing
4975 * @from - the offset to start zeroing
4976 * @len - the length to zero, 0 to zero the entire range respective to the
4977 * offset
4978 * @front - zero up to the offset instead of from the offset on
4979 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304980 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004981 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004982 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004983int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4984 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004985{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004986 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4987 struct address_space *mapping = inode->vfs_inode.i_mapping;
4988 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004989 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004990 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004991 struct extent_changeset *data_reserved = NULL;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004992 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004993 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004994 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304995 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004996 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004997 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004998 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004999 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305000 u64 block_start;
5001 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04005002
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02005003 if (IS_ALIGNED(offset, blocksize) &&
5004 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04005005 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305006
Josef Bacik8b62f872017-10-19 14:15:55 -04005007 block_start = round_down(from, blocksize);
5008 block_end = block_start + blocksize - 1;
5009
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005010 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5011 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005012 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005013 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005014 /* For nocow case, no need to reserve data space */
5015 only_release_metadata = true;
5016 } else {
5017 goto out;
5018 }
5019 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005020 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005021 if (ret < 0) {
5022 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005023 btrfs_free_reserved_data_space(inode, data_reserved,
5024 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005025 goto out;
5026 }
Chris Mason211c17f2008-05-15 09:13:45 -04005027again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04005028 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005029 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005030 btrfs_delalloc_release_space(inode, data_reserved, block_start,
5031 blocksize, true);
5032 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00005033 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04005034 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04005035 }
Qu Wenruo32443de2021-01-26 16:34:00 +08005036 ret = set_page_extent_mapped(page);
5037 if (ret < 0)
5038 goto out_unlock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005039
Chris Masona52d9a82007-08-27 16:49:44 -04005040 if (!PageUptodate(page)) {
5041 ret = btrfs_readpage(NULL, page);
5042 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005043 if (page->mapping != mapping) {
5044 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005045 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04005046 goto again;
5047 }
Chris Masona52d9a82007-08-27 16:49:44 -04005048 if (!PageUptodate(page)) {
5049 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04005050 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04005051 }
5052 }
Chris Mason211c17f2008-05-15 09:13:45 -04005053 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005054
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305055 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005056
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005057 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005058 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305059 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005060 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005061 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005062 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03005063 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005064 btrfs_put_ordered_extent(ordered);
5065 goto again;
5066 }
5067
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005068 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07005069 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5070 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04005071
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005072 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03005073 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005074 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305075 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01005076 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005077 goto out_unlock;
5078 }
5079
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305080 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005081 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305082 len = blocksize - offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04005083 if (front)
Ira Weinyd048b9c2021-05-04 18:40:07 -07005084 memzero_page(page, (block_start - page_offset(page)),
5085 offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005086 else
Ira Weinyd048b9c2021-05-04 18:40:07 -07005087 memzero_page(page, (block_start - page_offset(page)) + offset,
5088 len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005089 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005090 }
Chris Mason247e7432008-07-17 12:53:51 -04005091 ClearPageChecked(page);
Qu Wenruo6c9ac8b2021-05-31 16:50:51 +08005092 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
David Sterbae43bbe52017-12-12 21:43:52 +01005093 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005094
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005095 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005096 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02005097 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005098
Chris Mason89642222008-07-24 09:41:53 -04005099out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005100 if (ret) {
5101 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005102 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005103 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005104 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005105 block_start, blocksize, true);
5106 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005107 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04005108 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005109 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005110out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08005111 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02005112 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08005113 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005114 return ret;
5115}
5116
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005117static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04005118 u64 offset, u64 len)
5119{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005120 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04005121 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00005122 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04005123 int ret;
5124
5125 /*
Filipe Mananacceaa892021-07-20 16:03:40 +01005126 * If NO_HOLES is enabled, we don't need to do anything.
5127 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5128 * or btrfs_update_inode() will be called, which guarantee that the next
5129 * fsync will know this inode was changed and needs to be logged.
Josef Bacik16e75492013-10-22 12:18:51 -04005130 */
Filipe Mananacceaa892021-07-20 16:03:40 +01005131 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04005132 return 0;
Josef Bacik16e75492013-10-22 12:18:51 -04005133
5134 /*
5135 * 1 - for the one we're dropping
5136 * 1 - for the one we're adding
5137 * 1 - for updating the inode.
5138 */
5139 trans = btrfs_start_transaction(root, 3);
5140 if (IS_ERR(trans))
5141 return PTR_ERR(trans);
5142
Filipe Manana5893dfb2020-11-04 11:07:32 +00005143 drop_args.start = offset;
5144 drop_args.end = offset + len;
5145 drop_args.drop_cache = true;
5146
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005147 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04005148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005149 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005150 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005151 return ret;
5152 }
5153
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005154 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01005155 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00005156 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005157 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00005158 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02005159 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5160 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00005161 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005162 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005163 return ret;
5164}
5165
Josef Bacik695a0d02011-03-04 15:46:53 -05005166/*
5167 * This function puts in dummy file extents for the area we're creating a hole
5168 * for. So if we are truncating this file to a larger size we need to insert
5169 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5170 * the range between oldsize and size
5171 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005172int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005173{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005174 struct btrfs_root *root = inode->root;
5175 struct btrfs_fs_info *fs_info = root->fs_info;
5176 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005177 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005178 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005179 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005180 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5181 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005182 u64 last_byte;
5183 u64 cur_offset;
5184 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005185 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005186
Josef Bacika71754f2013-06-17 17:14:39 -04005187 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305188 * If our size started in the middle of a block we need to zero out the
5189 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005190 * expose stale data.
5191 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005192 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005193 if (err)
5194 return err;
5195
Yan Zheng9036c102008-10-30 14:19:41 -04005196 if (size <= hole_start)
5197 return 0;
5198
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005199 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5200 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005201 cur_offset = hole_start;
5202 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005203 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08005204 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005205 if (IS_ERR(em)) {
5206 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005207 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005208 break;
5209 }
Yan Zheng9036c102008-10-30 14:19:41 -04005210 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005211 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005212 hole_size = last_byte - cur_offset;
5213
Yan, Zheng80825102009-11-12 09:35:36 +00005214 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005215 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00005216
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005217 err = maybe_insert_hole(root, inode, cur_offset,
5218 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04005219 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005220 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05005221
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005222 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005223 cur_offset, hole_size);
5224 if (err)
5225 break;
5226
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005227 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005228 cur_offset + hole_size - 1, 0);
5229 hole_em = alloc_extent_map();
5230 if (!hole_em) {
5231 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005232 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005233 goto next;
5234 }
5235 hole_em->start = cur_offset;
5236 hole_em->len = hole_size;
5237 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005238
Josef Bacik5dc562c2012-08-17 13:14:17 -04005239 hole_em->block_start = EXTENT_MAP_HOLE;
5240 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005241 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005242 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005243 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005244 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005245
5246 while (1) {
5247 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005248 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005249 write_unlock(&em_tree->lock);
5250 if (err != -EEXIST)
5251 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005252 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005253 cur_offset +
5254 hole_size - 1, 0);
5255 }
5256 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05005257 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005258 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05005259 cur_offset, hole_size);
5260 if (err)
5261 break;
Yan Zheng9036c102008-10-30 14:19:41 -04005262 }
Josef Bacik16e75492013-10-22 12:18:51 -04005263next:
Yan Zheng9036c102008-10-30 14:19:41 -04005264 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005265 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005266 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005267 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005268 break;
5269 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005270 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005271 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005272 return err;
5273}
5274
Eric Sandeen3972f262013-01-12 02:57:22 +00005275static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005276{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005277 struct btrfs_root *root = BTRFS_I(inode)->root;
5278 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005279 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005280 loff_t newsize = attr->ia_size;
5281 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005282 int ret;
5283
Eric Sandeen3972f262013-01-12 02:57:22 +00005284 /*
5285 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5286 * special case where we need to update the times despite not having
5287 * these flags set. For all other operations the VFS set these flags
5288 * explicitly if it wants a timestamp update.
5289 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005290 if (newsize != oldsize) {
5291 inode_inc_iversion(inode);
5292 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5293 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005294 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005295 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005296
Josef Bacika41ad392011-01-31 15:30:16 -05005297 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005298 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005299 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005300 * This is to ensure the snapshot captures a fully consistent
5301 * state of this file - if the snapshot captures this expanding
5302 * truncation, it must capture all writes that happened before
5303 * this truncation.
5304 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005305 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02005306 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005307 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005308 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005309 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005310 }
Yan, Zheng80825102009-11-12 09:35:36 +00005311
Miao Xief4a2f4c2011-12-14 20:12:01 -05005312 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005313 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005314 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005315 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005316 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005317
5318 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005319 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305320 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005321 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005322 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005323 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005324 } else {
Naohiro Aota24c0a722021-02-04 19:22:09 +09005325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5326
5327 if (btrfs_is_zoned(fs_info)) {
5328 ret = btrfs_wait_ordered_range(inode,
5329 ALIGN(newsize, fs_info->sectorsize),
5330 (u64)-1);
5331 if (ret)
5332 return ret;
5333 }
Yan, Zheng80825102009-11-12 09:35:36 +00005334
Josef Bacika41ad392011-01-31 15:30:16 -05005335 /*
5336 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005337 * zero. Make sure any new writes to the file get on disk
5338 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005339 */
5340 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005341 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005342 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005343
Josef Bacika41ad392011-01-31 15:30:16 -05005344 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005345
Miao Xie2e60a512013-02-08 07:01:08 +00005346 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005347
Filipe Manana213e8c52018-02-06 20:40:31 +00005348 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005349 if (ret && inode->i_nlink) {
5350 int err;
5351
5352 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005353 * Truncate failed, so fix up the in-memory size. We
5354 * adjusted disk_i_size down as we removed extents, so
5355 * wait for disk_i_size to be stable and then update the
5356 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005357 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005358 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005359 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005360 return err;
5361 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005362 }
Yan, Zheng80825102009-11-12 09:35:36 +00005363 }
5364
Josef Bacika41ad392011-01-31 15:30:16 -05005365 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005366}
5367
Christian Brauner549c7292021-01-21 14:19:43 +01005368static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5369 struct iattr *attr)
Chris Mason39279cc2007-06-12 06:35:45 -04005370{
David Howells2b0143b2015-03-17 22:25:59 +00005371 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005372 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005373 int err;
5374
Li Zefanb83cc962010-12-20 16:04:08 +08005375 if (btrfs_root_readonly(root))
5376 return -EROFS;
5377
Christian Brauner2f221d62021-01-21 14:19:26 +01005378 err = setattr_prepare(&init_user_ns, dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005379 if (err)
5380 return err;
5381
Chris Mason5a3f23d2009-03-31 13:27:11 -04005382 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005383 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005384 if (err)
5385 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005386 }
Yan Zheng9036c102008-10-30 14:19:41 -04005387
Christoph Hellwig10257742010-06-04 11:30:02 +02005388 if (attr->ia_valid) {
Christian Brauner2f221d62021-01-21 14:19:26 +01005389 setattr_copy(&init_user_ns, inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005390 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005391 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005392
Josef Bacik22c44fe2011-11-30 10:45:38 -05005393 if (!err && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01005394 err = posix_acl_chmod(&init_user_ns, inode,
5395 inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005396 }
5397
Chris Mason39279cc2007-06-12 06:35:45 -04005398 return err;
5399}
Chris Mason61295eb2008-01-14 16:24:38 -05005400
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005401/*
5402 * While truncating the inode pages during eviction, we get the VFS calling
5403 * btrfs_invalidatepage() against each page of the inode. This is slow because
5404 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5405 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5406 * extent_state structures over and over, wasting lots of time.
5407 *
5408 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5409 * those expensive operations on a per page basis and do only the ordered io
5410 * finishing, while we release here the extent_map and extent_state structures,
5411 * without the excessive merging and splitting.
5412 */
5413static void evict_inode_truncate_pages(struct inode *inode)
5414{
5415 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5416 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5417 struct rb_node *node;
5418
5419 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005420 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005421
5422 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005423 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005424 struct extent_map *em;
5425
Liu Bo07e1ce02018-08-23 03:51:52 +08005426 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005427 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005428 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5429 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005430 remove_extent_mapping(map_tree, em);
5431 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005432 if (need_resched()) {
5433 write_unlock(&map_tree->lock);
5434 cond_resched();
5435 write_lock(&map_tree->lock);
5436 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005437 }
5438 write_unlock(&map_tree->lock);
5439
Filipe Manana6ca07092015-05-26 00:55:42 +01005440 /*
5441 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005442 * We can have ongoing bios started by readahead that have
5443 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005444 * still in progress (unlocked the pages in the bio but did not yet
5445 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005446 * ranges can still be locked and eviction started because before
5447 * submitting those bios, which are executed by a separate task (work
5448 * queue kthread), inode references (inode->i_count) were not taken
5449 * (which would be dropped in the end io callback of each bio).
5450 * Therefore here we effectively end up waiting for those bios and
5451 * anyone else holding locked ranges without having bumped the inode's
5452 * reference count - if we don't do it, when they access the inode's
5453 * io_tree to unlock a range it may be too late, leading to an
5454 * use-after-free issue.
5455 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005456 spin_lock(&io_tree->lock);
5457 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5458 struct extent_state *state;
5459 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005460 u64 start;
5461 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005462 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005463
5464 node = rb_first(&io_tree->state);
5465 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005466 start = state->start;
5467 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005468 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005469 spin_unlock(&io_tree->lock);
5470
David Sterbaff13db42015-12-03 14:30:40 +01005471 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005472
5473 /*
5474 * If still has DELALLOC flag, the extent didn't reach disk,
5475 * and its reserved space won't be freed by delayed_ref.
5476 * So we need to free its reserved space here.
5477 * (Refer to comment in btrfs_invalidatepage, case 2)
5478 *
5479 * Note, end is the bytenr of last byte, so we need + 1 here.
5480 */
Filipe Manana421f0922018-10-12 13:02:48 +01005481 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005482 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5483 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005484
Filipe Manana6ca07092015-05-26 00:55:42 +01005485 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005486 EXTENT_LOCKED | EXTENT_DELALLOC |
5487 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5488 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005489
Filipe Manana7064dd52014-08-08 02:47:05 +01005490 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005491 spin_lock(&io_tree->lock);
5492 }
5493 spin_unlock(&io_tree->lock);
5494}
5495
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005496static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005497 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005498{
5499 struct btrfs_fs_info *fs_info = root->fs_info;
5500 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005501 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005502 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005503 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005504
Josef Bacikd3984c92019-08-01 18:19:37 -04005505 /*
5506 * Eviction should be taking place at some place safe because of our
5507 * delayed iputs. However the normal flushing code will run delayed
5508 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5509 *
5510 * We reserve the delayed_refs_extra here again because we can't use
5511 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5512 * above. We reserve our extra bit here because we generate a ton of
5513 * delayed refs activity by truncating.
5514 *
5515 * If we cannot make our reservation we'll attempt to steal from the
5516 * global reserve, because we really want to be able to free up space.
5517 */
5518 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5519 BTRFS_RESERVE_FLUSH_EVICT);
5520 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005521 /*
5522 * Try to steal from the global reserve if there is space for
5523 * it.
5524 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005525 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5526 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5527 btrfs_warn(fs_info,
5528 "could not allocate space for delete; will truncate on mount");
5529 return ERR_PTR(-ENOSPC);
5530 }
5531 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005532 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005533
5534 trans = btrfs_join_transaction(root);
5535 if (IS_ERR(trans))
5536 return trans;
5537
5538 if (delayed_refs_extra) {
5539 trans->block_rsv = &fs_info->trans_block_rsv;
5540 trans->bytes_reserved = delayed_refs_extra;
5541 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5542 delayed_refs_extra, 1);
5543 }
5544 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005545}
5546
Al Virobd555972010-06-07 11:35:40 -04005547void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005549 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005550 struct btrfs_trans_handle *trans;
5551 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005552 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005553 int ret;
5554
liubo1abe9b82011-03-24 11:18:59 +00005555 trace_btrfs_inode_evict(inode);
5556
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005557 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005558 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005559 return;
5560 }
5561
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005562 evict_inode_truncate_pages(inode);
5563
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005564 if (inode->i_nlink &&
5565 ((btrfs_root_refs(&root->root_item) != 0 &&
5566 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005567 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005568 goto no_delete;
5569
Omar Sandoval27919062018-05-11 13:13:37 -07005570 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005571 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005572
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005573 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005574
Omar Sandoval7b40b692018-05-11 13:13:33 -07005575 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005576 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005577
Yan, Zheng76dda932009-09-21 16:00:26 -04005578 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005579 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5580 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005581 goto no_delete;
5582 }
5583
Nikolay Borisovaa790212017-01-10 20:35:40 +02005584 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005585 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005586 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005587
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005588 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005589 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005590 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005591 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005592 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005593
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005594 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005595
Yan, Zheng80825102009-11-12 09:35:36 +00005596 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005597 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005598 if (IS_ERR(trans))
5599 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005600
5601 trans->block_rsv = rsv;
5602
Nikolay Borisov50743392020-11-02 16:48:55 +02005603 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Filipe Manana0d7d3162021-05-24 11:35:55 +01005604 0, 0, NULL);
Omar Sandoval27919062018-05-11 13:13:37 -07005605 trans->block_rsv = &fs_info->trans_block_rsv;
5606 btrfs_end_transaction(trans);
5607 btrfs_btree_balance_dirty(fs_info);
5608 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5609 goto free_rsv;
5610 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005611 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005612 }
5613
Josef Bacik4ef31a42013-08-13 14:10:08 -04005614 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005615 * Errors here aren't a big deal, it just means we leave orphan items in
5616 * the tree. They will be cleaned up on the next mount. If the inode
5617 * number gets reused, cleanup deletes the orphan item without doing
5618 * anything, and unlink reuses the existing orphan item.
5619 *
5620 * If it turns out that we are dropping too many of these, we might want
5621 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005622 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005623 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005624 if (!IS_ERR(trans)) {
5625 trans->block_rsv = rsv;
5626 btrfs_orphan_del(trans, BTRFS_I(inode));
5627 trans->block_rsv = &fs_info->trans_block_rsv;
5628 btrfs_end_transaction(trans);
5629 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005630
Omar Sandoval27919062018-05-11 13:13:37 -07005631free_rsv:
5632 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005633no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005634 /*
5635 * If we didn't successfully delete, the orphan item will still be in
5636 * the tree and we'll retry on the next mount. Again, we might also want
5637 * to retry these periodically in the future.
5638 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005639 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005640 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005641}
5642
5643/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005644 * Return the key found in the dir entry in the location pointer, fill @type
5645 * with BTRFS_FT_*, and return 0.
5646 *
Su Yue005d6712018-03-05 17:13:37 +08005647 * If no dir entries were found, returns -ENOENT.
5648 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005649 */
5650static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005651 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005652{
5653 const char *name = dentry->d_name.name;
5654 int namelen = dentry->d_name.len;
5655 struct btrfs_dir_item *di;
5656 struct btrfs_path *path;
5657 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005658 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005659
5660 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005661 if (!path)
5662 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005663
David Sterbaf85b7372017-01-20 14:54:07 +01005664 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5665 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005666 if (IS_ERR_OR_NULL(di)) {
5667 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005668 goto out;
5669 }
Chris Masond3977122009-01-05 21:25:51 -05005670
Chris Mason5f39d392007-10-15 16:14:19 -04005671 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005672 if (location->type != BTRFS_INODE_ITEM_KEY &&
5673 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005674 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005675 btrfs_warn(root->fs_info,
5676"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5677 __func__, name, btrfs_ino(BTRFS_I(dir)),
5678 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005679 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005680 if (!ret)
5681 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005682out:
Chris Mason39279cc2007-06-12 06:35:45 -04005683 btrfs_free_path(path);
5684 return ret;
5685}
5686
5687/*
5688 * when we hit a tree root in a directory, the btrfs part of the inode
5689 * needs to be changed to reflect the root directory of the tree root. This
5690 * is kind of like crossing a mount point.
5691 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005692static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005693 struct inode *dir,
5694 struct dentry *dentry,
5695 struct btrfs_key *location,
5696 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005697{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005698 struct btrfs_path *path;
5699 struct btrfs_root *new_root;
5700 struct btrfs_root_ref *ref;
5701 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005702 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005703 int ret;
5704 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005705
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005706 path = btrfs_alloc_path();
5707 if (!path) {
5708 err = -ENOMEM;
5709 goto out;
5710 }
Chris Mason39279cc2007-06-12 06:35:45 -04005711
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005713 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5714 key.type = BTRFS_ROOT_REF_KEY;
5715 key.offset = location->objectid;
5716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005717 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718 if (ret) {
5719 if (ret < 0)
5720 err = ret;
5721 goto out;
5722 }
Chris Mason39279cc2007-06-12 06:35:45 -04005723
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724 leaf = path->nodes[0];
5725 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005726 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5728 goto out;
5729
5730 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5731 (unsigned long)(ref + 1),
5732 dentry->d_name.len);
5733 if (ret)
5734 goto out;
5735
David Sterbab3b4aa72011-04-21 01:20:15 +02005736 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737
David Sterba56e93572020-05-15 19:35:55 +02005738 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005739 if (IS_ERR(new_root)) {
5740 err = PTR_ERR(new_root);
5741 goto out;
5742 }
5743
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744 *sub_root = new_root;
5745 location->objectid = btrfs_root_dirid(&new_root->root_item);
5746 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005747 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748 err = 0;
5749out:
5750 btrfs_free_path(path);
5751 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005752}
5753
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005754static void inode_tree_add(struct inode *inode)
5755{
5756 struct btrfs_root *root = BTRFS_I(inode)->root;
5757 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005758 struct rb_node **p;
5759 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005760 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005761 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005762
Al Viro1d3382cb2010-10-23 15:19:20 -04005763 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005764 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005765 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005766 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005767 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005768 while (*p) {
5769 parent = *p;
5770 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5771
David Sterba37508512018-06-29 10:56:40 +02005772 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005773 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005774 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005775 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005776 else {
5777 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005778 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005779 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005780 RB_CLEAR_NODE(parent);
5781 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005782 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005783 }
5784 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005785 rb_link_node(new, parent, p);
5786 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005787 spin_unlock(&root->inode_lock);
5788}
5789
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005790static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005791{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005792 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005793 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005794
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005795 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005796 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5797 rb_erase(&inode->rb_node, &root->inode_tree);
5798 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005799 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005800 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005801 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005802
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005803 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005804 spin_lock(&root->inode_lock);
5805 empty = RB_EMPTY_ROOT(&root->inode_tree);
5806 spin_unlock(&root->inode_lock);
5807 if (empty)
5808 btrfs_add_dead_root(root);
5809 }
5810}
5811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005812
Chris Masone02119d2008-09-05 16:13:11 -04005813static int btrfs_init_locked_inode(struct inode *inode, void *p)
5814{
5815 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005816
5817 inode->i_ino = args->ino;
5818 BTRFS_I(inode)->location.objectid = args->ino;
5819 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5820 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005821 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5822 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 return 0;
5824}
5825
5826static int btrfs_find_actor(struct inode *inode, void *opaque)
5827{
5828 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005829
5830 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005831 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005832}
5833
David Sterba0202e832020-05-15 19:35:59 +02005834static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005835 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005836{
5837 struct inode *inode;
5838 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005839 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005840
David Sterba0202e832020-05-15 19:35:59 +02005841 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005842 args.root = root;
5843
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005844 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005845 btrfs_init_locked_inode,
5846 (void *)&args);
5847 return inode;
5848}
5849
David Sterba4c66e0d2019-10-03 19:09:35 +02005850/*
David Sterba0202e832020-05-15 19:35:59 +02005851 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005852 * Path can be preallocated to prevent recursing back to iget through
5853 * allocator. NULL is also valid but may require an additional allocation
5854 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305855 */
David Sterba0202e832020-05-15 19:35:59 +02005856struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005857 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305858{
5859 struct inode *inode;
5860
David Sterba0202e832020-05-15 19:35:59 +02005861 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305862 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005863 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305864
5865 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005866 int ret;
5867
Filipe Manana4222ea72018-10-24 10:13:03 +01005868 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005869 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005870 inode_tree_add(inode);
5871 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005872 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005873 iget_failed(inode);
5874 /*
5875 * ret > 0 can come from btrfs_search_slot called by
5876 * btrfs_read_locked_inode, this means the inode item
5877 * was not found.
5878 */
5879 if (ret > 0)
5880 ret = -ENOENT;
5881 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005882 }
5883 }
5884
Balaji Rao1a54ef82008-07-21 02:01:04 +05305885 return inode;
5886}
5887
David Sterba0202e832020-05-15 19:35:59 +02005888struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005889{
David Sterba0202e832020-05-15 19:35:59 +02005890 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005891}
5892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005893static struct inode *new_simple_dir(struct super_block *s,
5894 struct btrfs_key *key,
5895 struct btrfs_root *root)
5896{
5897 struct inode *inode = new_inode(s);
5898
5899 if (!inode)
5900 return ERR_PTR(-ENOMEM);
5901
Josef Bacik5c8fd992020-02-14 16:11:43 -05005902 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005903 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005904 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005905
5906 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005907 /*
5908 * We only need lookup, the rest is read-only and there's no inode
5909 * associated with the dentry
5910 */
5911 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005912 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005913 inode->i_fop = &simple_dir_operations;
5914 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005915 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305916 inode->i_atime = inode->i_mtime;
5917 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005918 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005919
5920 return inode;
5921}
5922
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005923static inline u8 btrfs_inode_type(struct inode *inode)
5924{
5925 /*
5926 * Compile-time asserts that generic FT_* types still match
5927 * BTRFS_FT_* types
5928 */
5929 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5930 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5931 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5932 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5933 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5934 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5935 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5936 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5937
5938 return fs_umode_to_ftype(inode->i_mode);
5939}
5940
Chris Mason3de45862008-11-17 21:02:50 -05005941struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005943 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005944 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005945 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 struct btrfs_root *sub_root = root;
5947 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005948 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005949 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005950
5951 if (dentry->d_name.len > BTRFS_NAME_LEN)
5952 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005954 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005955 if (ret < 0)
5956 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005957
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005958 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005959 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005960 if (IS_ERR(inode))
5961 return inode;
5962
5963 /* Do extra check against inode mode with di_type */
5964 if (btrfs_inode_type(inode) != di_type) {
5965 btrfs_crit(fs_info,
5966"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5967 inode->i_mode, btrfs_inode_type(inode),
5968 di_type);
5969 iput(inode);
5970 return ERR_PTR(-EUCLEAN);
5971 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005972 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005974
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005975 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005976 &location, &sub_root);
5977 if (ret < 0) {
5978 if (ret != -ENOENT)
5979 inode = ERR_PTR(ret);
5980 else
5981 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5982 } else {
David Sterba0202e832020-05-15 19:35:59 +02005983 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005984 }
Josef Bacik87270022020-01-24 09:32:31 -05005985 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005986 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005987
Julia Lawall34d19ba2011-01-24 19:55:19 +00005988 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005989 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005990 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005991 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005992 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005993 if (ret) {
5994 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005995 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005996 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005997 }
5998
Chris Mason3de45862008-11-17 21:02:50 -05005999 return inode;
6000}
6001
Nick Pigginfe15ce42011-01-07 17:49:23 +11006002static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04006003{
6004 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00006005 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04006006
Li Zefan848cce02012-02-21 17:04:28 +08006007 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00006008 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04006009
Li Zefan848cce02012-02-21 17:04:28 +08006010 if (inode) {
6011 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04006012 if (btrfs_root_refs(&root->root_item) == 0)
6013 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08006014
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006015 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08006016 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04006017 }
Yan, Zheng76dda932009-09-21 16:00:26 -04006018 return 0;
6019}
6020
Chris Mason3de45862008-11-17 21:02:50 -05006021static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04006022 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05006023{
Al Viro3837d202018-10-10 16:38:27 -04006024 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04006025
Al Viro3837d202018-10-10 16:38:27 -04006026 if (inode == ERR_PTR(-ENOENT))
6027 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04006028 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04006029}
6030
Josef Bacik23b5ec72017-07-24 15:14:25 -04006031/*
6032 * All this infrastructure exists because dir_emit can fault, and we are holding
6033 * the tree lock when doing readdir. For now just allocate a buffer and copy
6034 * our information into that, and then dir_emit from the buffer. This is
6035 * similar to what NFS does, only we don't keep the buffer around in pagecache
6036 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6037 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6038 * tree lock.
6039 */
6040static int btrfs_opendir(struct inode *inode, struct file *file)
6041{
6042 struct btrfs_file_private *private;
6043
6044 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6045 if (!private)
6046 return -ENOMEM;
6047 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6048 if (!private->filldir_buf) {
6049 kfree(private);
6050 return -ENOMEM;
6051 }
6052 file->private_data = private;
6053 return 0;
6054}
6055
6056struct dir_entry {
6057 u64 ino;
6058 u64 offset;
6059 unsigned type;
6060 int name_len;
6061};
6062
6063static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6064{
6065 while (entries--) {
6066 struct dir_entry *entry = addr;
6067 char *name = (char *)(entry + 1);
6068
David Sterba92d32172018-04-16 21:10:14 +02006069 ctx->pos = get_unaligned(&entry->offset);
6070 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6071 get_unaligned(&entry->ino),
6072 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04006073 return 1;
David Sterba92d32172018-04-16 21:10:14 +02006074 addr += sizeof(struct dir_entry) +
6075 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006076 ctx->pos++;
6077 }
6078 return 0;
6079}
6080
Al Viro9cdda8d2013-05-22 16:48:09 -04006081static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04006082{
Al Viro9cdda8d2013-05-22 16:48:09 -04006083 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04006084 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006085 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04006086 struct btrfs_dir_item *di;
6087 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04006088 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04006089 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006090 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08006091 struct list_head ins_list;
6092 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04006093 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04006094 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04006095 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04006096 char *name_ptr;
6097 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04006098 int entries = 0;
6099 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006100 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006101 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04006102
Al Viro9cdda8d2013-05-22 16:48:09 -04006103 if (!dir_emit_dots(file, ctx))
6104 return 0;
6105
David Woodhouse49593bf2008-08-17 17:08:36 +01006106 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08006107 if (!path)
6108 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04006109
Josef Bacik23b5ec72017-07-24 15:14:25 -04006110 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006111 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006112
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006113 INIT_LIST_HEAD(&ins_list);
6114 INIT_LIST_HEAD(&del_list);
6115 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006116
Josef Bacik23b5ec72017-07-24 15:14:25 -04006117again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006118 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006119 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006120 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006121
Chris Mason39279cc2007-06-12 06:35:45 -04006122 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6123 if (ret < 0)
6124 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006125
6126 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006127 struct dir_entry *entry;
6128
Chris Mason5f39d392007-10-15 16:14:19 -04006129 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006130 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006131 if (slot >= btrfs_header_nritems(leaf)) {
6132 ret = btrfs_next_leaf(root, path);
6133 if (ret < 0)
6134 goto err;
6135 else if (ret > 0)
6136 break;
6137 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006138 }
Chris Mason3de45862008-11-17 21:02:50 -05006139
Chris Mason5f39d392007-10-15 16:14:19 -04006140 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6141
6142 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006143 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006144 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006145 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006146 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006147 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006148 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006149 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006150 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006151 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006152 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6153 PAGE_SIZE) {
6154 btrfs_release_path(path);
6155 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6156 if (ret)
6157 goto nopos;
6158 addr = private->filldir_buf;
6159 entries = 0;
6160 total_len = 0;
6161 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006162 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006163
6164 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006165 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006166 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006167 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6168 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006169 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006170 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006171 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006172 put_unaligned(location.objectid, &entry->ino);
6173 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006174 entries++;
6175 addr += sizeof(struct dir_entry) + name_len;
6176 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006177next:
6178 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006179 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006180 btrfs_release_path(path);
6181
6182 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6183 if (ret)
6184 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006185
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006186 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006187 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006188 goto nopos;
6189
Zach Browndb62efb2013-07-11 16:19:42 -07006190 /*
6191 * Stop new entries from being returned after we return the last
6192 * entry.
6193 *
6194 * New directory entries are assigned a strictly increasing
6195 * offset. This means that new entries created during readdir
6196 * are *guaranteed* to be seen in the future by that readdir.
6197 * This has broken buggy programs which operate on names as
6198 * they're returned by readdir. Until we re-use freed offsets
6199 * we have this hack to stop new entries from being returned
6200 * under the assumption that they'll never reach this huge
6201 * offset.
6202 *
6203 * This is being careful not to overflow 32bit loff_t unless the
6204 * last entry requires it because doing so has broken 32bit apps
6205 * in the past.
6206 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006207 if (ctx->pos >= INT_MAX)
6208 ctx->pos = LLONG_MAX;
6209 else
6210 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006211nopos:
6212 ret = 0;
6213err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006214 if (put)
6215 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006216 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006217 return ret;
6218}
6219
Chris Mason39279cc2007-06-12 06:35:45 -04006220/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006221 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006222 * inode changes. But, it is most likely to find the inode in cache.
6223 * FIXME, needs more benchmarking...there are no reasons other than performance
6224 * to keep or drop this code.
6225 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006226static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006227{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006228 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006229 struct btrfs_root *root = BTRFS_I(inode)->root;
6230 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006231 int ret;
6232
Josef Bacik72ac3c02012-05-23 14:13:11 -04006233 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006234 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006235
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006236 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006237 if (IS_ERR(trans))
6238 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006239
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006240 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisov4d14c5c2021-02-22 18:40:44 +02006241 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04006242 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006243 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006244 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006245 if (IS_ERR(trans))
6246 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006247
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006248 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04006249 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006250 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006251 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006252 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006253
6254 return ret;
6255}
6256
6257/*
6258 * This is a copy of file_update_time. We need this so we can return error on
6259 * ENOSPC for updating the inode in the case of file write and mmap writes.
6260 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006261static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006262 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006263{
Alexander Block2bc5565282012-06-15 09:49:33 +02006264 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006265 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006266
6267 if (btrfs_root_readonly(root))
6268 return -EROFS;
6269
Josef Bacike41f9412012-03-26 09:46:47 -04006270 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006271 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006272 if (flags & S_CTIME)
6273 inode->i_ctime = *now;
6274 if (flags & S_MTIME)
6275 inode->i_mtime = *now;
6276 if (flags & S_ATIME)
6277 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006278 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006279}
6280
Chris Masond352ac62008-09-29 15:18:18 -04006281/*
6282 * find the highest existing sequence number in a directory
6283 * and then set the in-memory index_cnt variable to reflect
6284 * free sequence numbers
6285 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006286static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006287{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006288 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006289 struct btrfs_key key, found_key;
6290 struct btrfs_path *path;
6291 struct extent_buffer *leaf;
6292 int ret;
6293
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006294 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006295 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006296 key.offset = (u64)-1;
6297
6298 path = btrfs_alloc_path();
6299 if (!path)
6300 return -ENOMEM;
6301
6302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6303 if (ret < 0)
6304 goto out;
6305 /* FIXME: we should be able to handle this */
6306 if (ret == 0)
6307 goto out;
6308 ret = 0;
6309
6310 /*
6311 * MAGIC NUMBER EXPLANATION:
6312 * since we search a directory based on f_pos we have to start at 2
6313 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6314 * else has to start at 2
6315 */
6316 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006317 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006318 goto out;
6319 }
6320
6321 path->slots[0]--;
6322
6323 leaf = path->nodes[0];
6324 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6325
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006326 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006327 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006328 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006329 goto out;
6330 }
6331
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006332 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006333out:
6334 btrfs_free_path(path);
6335 return ret;
6336}
6337
Chris Masond352ac62008-09-29 15:18:18 -04006338/*
6339 * helper to find a free sequence number in a given directory. This current
6340 * code is very simple, later versions will do smarter things in the btree
6341 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006342int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006343{
6344 int ret = 0;
6345
Nikolay Borisov877574e2017-02-20 13:50:33 +02006346 if (dir->index_cnt == (u64)-1) {
6347 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006348 if (ret) {
6349 ret = btrfs_set_inode_index_count(dir);
6350 if (ret)
6351 return ret;
6352 }
Josef Bacikaec74772008-07-24 12:12:38 -04006353 }
6354
Nikolay Borisov877574e2017-02-20 13:50:33 +02006355 *index = dir->index_cnt;
6356 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006357
6358 return ret;
6359}
6360
Chris Masonb0d5d102014-09-08 13:08:51 -07006361static int btrfs_insert_inode_locked(struct inode *inode)
6362{
6363 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006364
6365 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006366 args.root = BTRFS_I(inode)->root;
6367
6368 return insert_inode_locked4(inode,
6369 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6370 btrfs_find_actor, &args);
6371}
6372
Anand Jain19aee8d2017-07-18 17:37:05 +08006373/*
6374 * Inherit flags from the parent inode.
6375 *
6376 * Currently only the compression flags and the cow flags are inherited.
6377 */
6378static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6379{
6380 unsigned int flags;
6381
6382 if (!dir)
6383 return;
6384
6385 flags = BTRFS_I(dir)->flags;
6386
6387 if (flags & BTRFS_INODE_NOCOMPRESS) {
6388 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6389 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6390 } else if (flags & BTRFS_INODE_COMPRESS) {
6391 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6392 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6393 }
6394
6395 if (flags & BTRFS_INODE_NODATACOW) {
6396 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6397 if (S_ISREG(inode->i_mode))
6398 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6399 }
6400
David Sterba7b6a2212018-03-26 18:40:21 +02006401 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006402}
6403
Chris Mason39279cc2007-06-12 06:35:45 -04006404static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6405 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006406 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006407 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006408 u64 ref_objectid, u64 objectid,
6409 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006412 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006413 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006414 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006415 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006416 struct btrfs_inode_ref *ref;
6417 struct btrfs_key key[2];
6418 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006419 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006420 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006421 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006422 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006423
Chris Mason5f39d392007-10-15 16:14:19 -04006424 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006425 if (!path)
6426 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006427
Josef Bacik11a19a92019-09-09 10:12:04 -04006428 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006429 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006430 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006431 if (!inode) {
6432 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006433 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006434 }
Chris Mason39279cc2007-06-12 06:35:45 -04006435
Li Zefan581bb052011-04-20 10:06:11 +08006436 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006437 * O_TMPFILE, set link count to 0, so that after this point,
6438 * we fill in an inode item with the correct link count.
6439 */
6440 if (!name)
6441 set_nlink(inode, 0);
6442
6443 /*
Li Zefan581bb052011-04-20 10:06:11 +08006444 * we have to initialize this early, so we can reclaim the inode
6445 * number if we fail afterwards in this function.
6446 */
6447 inode->i_ino = objectid;
6448
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006449 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006450 trace_btrfs_inode_request(dir);
6451
Nikolay Borisov877574e2017-02-20 13:50:33 +02006452 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006453 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006454 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006455 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006456 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006457 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006458 } else if (dir) {
6459 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006460 }
6461 /*
6462 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006463 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006464 * number
6465 */
6466 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006467 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006468 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006469 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006470 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006471
Josef Bacik5dc562c2012-08-17 13:14:17 -04006472 /*
6473 * We could have gotten an inode number from somebody who was fsynced
6474 * and then removed in this same transaction, so let's just set full
6475 * sync since it will be a full sync anyway and this will blow away the
6476 * old info in the log.
6477 */
6478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6479
Chris Mason9c583092008-01-29 15:15:18 -05006480 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006481 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006482 key[0].offset = 0;
6483
Chris Mason9c583092008-01-29 15:15:18 -05006484 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006485
6486 if (name) {
6487 /*
6488 * Start new inodes with an inode_ref. This is slightly more
6489 * efficient for small numbers of hard links since they will
6490 * be packed into one item. Extended refs will kick in if we
6491 * add more hard links than can fit in the ref item.
6492 */
6493 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006494 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006495 key[1].offset = ref_objectid;
6496
6497 sizes[1] = name_len + sizeof(*ref);
6498 }
Chris Mason9c583092008-01-29 15:15:18 -05006499
Chris Masonb0d5d102014-09-08 13:08:51 -07006500 location = &BTRFS_I(inode)->location;
6501 location->objectid = objectid;
6502 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006503 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006504
6505 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006506 if (ret < 0) {
6507 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006508 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006509 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006510
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006511 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006512 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006513 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006514
Christian Brauner21cb47b2021-01-21 14:19:25 +01006515 inode_init_owner(&init_user_ns, inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006516 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306517
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006518 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306519 inode->i_atime = inode->i_mtime;
6520 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006521 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306522
Chris Mason5f39d392007-10-15 16:14:19 -04006523 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6524 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006525 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006526 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006527 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006528
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006529 if (name) {
6530 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6531 struct btrfs_inode_ref);
6532 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6533 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6534 ptr = (unsigned long)(ref + 1);
6535 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6536 }
Chris Mason9c583092008-01-29 15:15:18 -05006537
Chris Mason5f39d392007-10-15 16:14:19 -04006538 btrfs_mark_buffer_dirty(path->nodes[0]);
6539 btrfs_free_path(path);
6540
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006541 btrfs_inherit_iflags(inode, dir);
6542
Al Viro569254b2011-07-24 17:08:40 -04006543 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006544 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006545 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006546 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006547 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6548 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006549 }
6550
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006551 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006552
6553 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006554 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006555
Alexander Block8ea05e32012-07-25 17:35:53 +02006556 btrfs_update_root_times(trans, root);
6557
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006558 ret = btrfs_inode_inherit_props(trans, inode, dir);
6559 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006560 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006561 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006562 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006563
Chris Mason39279cc2007-06-12 06:35:45 -04006564 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006565
6566fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006567 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006568fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006569 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006570 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006571 btrfs_free_path(path);
6572 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006573}
6574
Chris Masond352ac62008-09-29 15:18:18 -04006575/*
6576 * utility function to add 'inode' into 'parent_inode' with
6577 * a give name and a given sequence number.
6578 * if 'add_backref' is true, also insert a backref from the
6579 * inode to the parent directory.
6580 */
Chris Masone02119d2008-09-05 16:13:11 -04006581int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006582 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006583 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006584{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006585 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006586 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006587 struct btrfs_root *root = parent_inode->root;
6588 u64 ino = btrfs_ino(inode);
6589 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006590
Li Zefan33345d012011-04-20 10:31:50 +08006591 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006592 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006593 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006594 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006595 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006596 key.offset = 0;
6597 }
Chris Mason39279cc2007-06-12 06:35:45 -04006598
Li Zefan33345d012011-04-20 10:31:50 +08006599 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006600 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006601 root->root_key.objectid, parent_ino,
6602 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006603 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006604 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6605 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006606 }
6607
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006608 /* Nothing to clean up yet */
6609 if (ret)
6610 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006611
Lu Fengqi684572d2018-08-04 21:10:57 +08006612 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006613 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006614 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006615 goto fail_dir_item;
6616 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006617 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006618 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006619 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006620
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006621 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006622 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006623 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006624 /*
6625 * If we are replaying a log tree, we do not want to update the mtime
6626 * and ctime of the parent directory with the current time, since the
6627 * log replay procedure is responsible for setting them to their correct
6628 * values (the ones it had when the fsync was done).
6629 */
6630 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6631 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6632
6633 parent_inode->vfs_inode.i_mtime = now;
6634 parent_inode->vfs_inode.i_ctime = now;
6635 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006636 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006637 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006638 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006639 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006640
6641fail_dir_item:
6642 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6643 u64 local_index;
6644 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006645 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006646 root->root_key.objectid, parent_ino,
6647 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006648 if (err)
6649 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006650 } else if (add_backref) {
6651 u64 local_index;
6652 int err;
6653
6654 err = btrfs_del_inode_ref(trans, root, name, name_len,
6655 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006656 if (err)
6657 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006658 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006659
6660 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006661 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006662}
6663
6664static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006665 struct btrfs_inode *dir, struct dentry *dentry,
6666 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006667{
Josef Bacika1b075d2010-11-19 20:36:11 +00006668 int err = btrfs_add_link(trans, dir, inode,
6669 dentry->d_name.name, dentry->d_name.len,
6670 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006671 if (err > 0)
6672 err = -EEXIST;
6673 return err;
6674}
6675
Christian Brauner549c7292021-01-21 14:19:43 +01006676static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6677 struct dentry *dentry, umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006678{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006679 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006680 struct btrfs_trans_handle *trans;
6681 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006682 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006683 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006684 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006685 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006686
Josef Bacik9ed74f22009-09-11 16:12:44 -04006687 /*
6688 * 2 for inode item and ref
6689 * 2 for dir items
6690 * 1 for xattr if selinux is on
6691 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006692 trans = btrfs_start_transaction(root, 5);
6693 if (IS_ERR(trans))
6694 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006695
Nikolay Borisov543068a2020-12-07 17:32:33 +02006696 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006697 if (err)
6698 goto out_unlock;
6699
Josef Bacikaec74772008-07-24 12:12:38 -04006700 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006701 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6702 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006703 if (IS_ERR(inode)) {
6704 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006705 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006706 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006707 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006708
Casey Schauflerad19db72011-12-15 10:09:07 -05006709 /*
6710 * If the active LSM wants to access the inode during
6711 * d_instantiate it needs these. Smack checks to see
6712 * if the filesystem supports xattrs by looking at the
6713 * ops vector.
6714 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006715 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006716 init_special_inode(inode, inode->i_mode, rdev);
6717
6718 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006719 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006720 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006721
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006722 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6723 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006724 if (err)
6725 goto out_unlock;
6726
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006727 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006728 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006729
Josef Bacik618e21d2007-07-11 10:18:17 -04006730out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006731 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006732 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006733 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006734 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006735 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006736 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006737 return err;
6738}
6739
Christian Brauner549c7292021-01-21 14:19:43 +01006740static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6741 struct dentry *dentry, umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006743 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006744 struct btrfs_trans_handle *trans;
6745 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006746 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006747 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006748 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006749 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006750
Josef Bacik9ed74f22009-09-11 16:12:44 -04006751 /*
6752 * 2 for inode item and ref
6753 * 2 for dir items
6754 * 1 for xattr if selinux is on
6755 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006756 trans = btrfs_start_transaction(root, 5);
6757 if (IS_ERR(trans))
6758 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006759
Nikolay Borisov543068a2020-12-07 17:32:33 +02006760 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006761 if (err)
6762 goto out_unlock;
6763
Josef Bacikaec74772008-07-24 12:12:38 -04006764 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006765 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6766 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006767 if (IS_ERR(inode)) {
6768 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006769 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006770 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006771 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006772 /*
6773 * If the active LSM wants to access the inode during
6774 * d_instantiate it needs these. Smack checks to see
6775 * if the filesystem supports xattrs by looking at the
6776 * ops vector.
6777 */
6778 inode->i_fop = &btrfs_file_operations;
6779 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006781
6782 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6783 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006784 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006785
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006786 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006787 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006788 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006789
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006790 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6791 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006793 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006794
Al Viro1e2e5472018-05-04 08:23:01 -04006795 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006796
Chris Mason39279cc2007-06-12 06:35:45 -04006797out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006798 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006799 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006800 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006801 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006802 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006803 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 return err;
6805}
6806
6807static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6808 struct dentry *dentry)
6809{
Filipe Manana271dba452016-01-05 16:24:05 +00006810 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006811 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006812 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006814 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006815 int err;
6816 int drop_inode = 0;
6817
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006818 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006819 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006820 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006821
Mark Fashehf1863732012-08-08 11:32:27 -07006822 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006823 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006824
Nikolay Borisov877574e2017-02-20 13:50:33 +02006825 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006826 if (err)
6827 goto fail;
6828
Yan, Zhenga22285a2010-05-16 10:48:46 -04006829 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006830 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006831 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006832 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006833 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006834 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006835 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006836 if (IS_ERR(trans)) {
6837 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006838 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006839 goto fail;
6840 }
Chris Mason5f39d392007-10-15 16:14:19 -04006841
Miao Xie67de1172013-12-26 13:07:06 +08006842 /* There are several dir indexes for this inode, clear the cache. */
6843 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006844 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006845 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006846 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006847 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006848 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006849
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006850 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6851 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006852
Yan, Zhenga5719522009-09-24 09:17:31 -04006853 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006854 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006855 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006856 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006857
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006858 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006859 if (err)
6860 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006861 if (inode->i_nlink == 1) {
6862 /*
6863 * If new hard link count is 1, it's a file created
6864 * with open(2) O_TMPFILE flag.
6865 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006866 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006867 if (err)
6868 goto fail;
6869 }
Al Viro08c422c2011-12-23 07:58:13 -05006870 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006871 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006872 }
Chris Mason39279cc2007-06-12 06:35:45 -04006873
Chris Mason1832a6d2007-12-21 16:27:21 -05006874fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006875 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006876 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006877 if (drop_inode) {
6878 inode_dec_link_count(inode);
6879 iput(inode);
6880 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006881 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006882 return err;
6883}
6884
Christian Brauner549c7292021-01-21 14:19:43 +01006885static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6886 struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006887{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006888 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006889 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006890 struct btrfs_trans_handle *trans;
6891 struct btrfs_root *root = BTRFS_I(dir)->root;
6892 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006893 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006894 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006895
Josef Bacik9ed74f22009-09-11 16:12:44 -04006896 /*
6897 * 2 items for inode and ref
6898 * 2 items for dir items
6899 * 1 for xattr if selinux is on
6900 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006901 trans = btrfs_start_transaction(root, 5);
6902 if (IS_ERR(trans))
6903 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006904
Nikolay Borisov543068a2020-12-07 17:32:33 +02006905 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006906 if (err)
6907 goto out_fail;
6908
Josef Bacikaec74772008-07-24 12:12:38 -04006909 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006910 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6911 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006912 if (IS_ERR(inode)) {
6913 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006914 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006915 goto out_fail;
6916 }
Chris Mason5f39d392007-10-15 16:14:19 -04006917
Chris Masonb0d5d102014-09-08 13:08:51 -07006918 /* these must be set before we unlock the inode */
6919 inode->i_op = &btrfs_dir_inode_operations;
6920 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006921
Eric Paris2a7dba32011-02-01 11:05:39 -05006922 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006923 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006924 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006925
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006926 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006927 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006928 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006929 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006930
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006931 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6932 dentry->d_name.name,
6933 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006934 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006935 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006936
Al Viro1e2e5472018-05-04 08:23:01 -04006937 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006938
6939out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006940 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006941 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006942 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006943 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006944 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006945 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006946 return err;
6947}
6948
Chris Masonc8b97812008-10-29 14:49:59 -04006949static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006950 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006951 size_t pg_offset, u64 extent_offset,
6952 struct btrfs_file_extent_item *item)
6953{
6954 int ret;
6955 struct extent_buffer *leaf = path->nodes[0];
6956 char *tmp;
6957 size_t max_size;
6958 unsigned long inline_size;
6959 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006960 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006961
6962 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006963 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006964 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6965 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006966 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006967 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006968 if (!tmp)
6969 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006970 ptr = btrfs_file_extent_inline_start(item);
6971
6972 read_extent_buffer(leaf, tmp, ptr, inline_size);
6973
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006974 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006975 ret = btrfs_decompress(compress_type, tmp, page,
6976 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006977
6978 /*
6979 * decompression code contains a memset to fill in any space between the end
6980 * of the uncompressed data and the end of max_size in case the decompressed
6981 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6982 * the end of an inline extent and the beginning of the next block, so we
6983 * cover that region here.
6984 */
6985
Ira Weinyd048b9c2021-05-04 18:40:07 -07006986 if (max_size + pg_offset < PAGE_SIZE)
6987 memzero_page(page, pg_offset + max_size,
6988 PAGE_SIZE - max_size - pg_offset);
Chris Masonc8b97812008-10-29 14:49:59 -04006989 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006990 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006991}
6992
Omar Sandoval39b07b52019-12-02 17:34:23 -08006993/**
6994 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6995 * @inode: file to search in
6996 * @page: page to read extent data into if the extent is inline
6997 * @pg_offset: offset into @page to copy to
6998 * @start: file offset
6999 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04007000 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08007001 * This returns the first &struct extent_map which overlaps with the given
7002 * range, reading it from the B-tree and caching it if necessary. Note that
7003 * there may be more extents which overlap the given range after the returned
7004 * extent_map.
7005 *
7006 * If @page is not NULL and the extent is inline, this also reads the extent
7007 * data directly into the page and marks the extent up to date in the io_tree.
7008 *
7009 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04007010 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007011struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08007012 struct page *page, size_t pg_offset,
7013 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04007014{
David Sterba3ffbd682018-06-29 10:56:42 +02007015 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007016 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007017 u64 extent_start = 0;
7018 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007019 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07007020 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04007021 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007022 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04007023 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04007024 struct extent_buffer *leaf;
7025 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04007026 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007027 struct extent_map_tree *em_tree = &inode->extent_tree;
7028 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007029
Chris Mason890871b2009-09-02 16:24:52 -04007030 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007031 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007032 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05007033
Chris Masona52d9a82007-08-27 16:49:44 -04007034 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04007035 if (em->start > start || em->start + em->len <= start)
7036 free_extent_map(em);
7037 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05007038 free_extent_map(em);
7039 else
7040 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007041 }
David Sterba172ddd62011-04-21 00:48:27 +02007042 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04007043 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007044 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05007045 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007046 }
Chris Masond1310b22008-01-24 16:13:08 -05007047 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05007048 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05007049 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04007050 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04007051
Liu Bobee6ec82018-08-17 05:05:28 +08007052 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04007053 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007054 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08007055 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04007056 }
7057
Liu Bobee6ec82018-08-17 05:05:28 +08007058 /* Chances are we'll be called again, so go ahead and do readahead */
7059 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04007060
7061 /*
7062 * The same explanation in load_free_space_cache applies here as well,
7063 * we only read when we're loading the free space cache, and at that
7064 * point the commit_root has everything we need.
7065 */
7066 if (btrfs_is_free_space_inode(inode)) {
7067 path->search_commit_root = 1;
7068 path->skip_locking = 1;
7069 }
Josef Bacik51899412020-08-20 11:46:01 -04007070
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02007071 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04007072 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007073 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007074 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04007075 if (path->slots[0] == 0)
7076 goto not_found;
7077 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007078 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007079 }
7080
Chris Mason5f39d392007-10-15 16:14:19 -04007081 leaf = path->nodes[0];
7082 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007083 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04007084 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04007085 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007086 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007087 /*
7088 * If we backup past the first extent we want to move forward
7089 * and see if there is an extent in front of us, otherwise we'll
7090 * say there is a hole for our whole search range which can
7091 * cause problems.
7092 */
7093 extent_end = start;
7094 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007095 }
7096
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007097 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04007098 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00007099 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007100 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7101 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007102 /* Only regular file could have regular/prealloc extent */
7103 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007104 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08007105 btrfs_crit(fs_info,
7106 "regular/prealloc extent found for non-regular inode %llu",
7107 btrfs_ino(inode));
7108 goto out;
7109 }
Liu Bo09ed2f12017-03-10 11:09:48 -08007110 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7111 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007112 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08007113 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7114 path->slots[0],
7115 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007116 }
Josef Bacik25a50342013-10-14 12:08:38 -04007117next:
Yan Zheng9036c102008-10-30 14:19:41 -04007118 if (start >= extent_end) {
7119 path->slots[0]++;
7120 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7121 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007122 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007123 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007124 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04007125 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007126
Yan Zheng9036c102008-10-30 14:19:41 -04007127 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007128 }
Yan Zheng9036c102008-10-30 14:19:41 -04007129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7130 if (found_key.objectid != objectid ||
7131 found_key.type != BTRFS_EXTENT_DATA_KEY)
7132 goto not_found;
7133 if (start + len <= found_key.offset)
7134 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007135 if (start > found_key.offset)
7136 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007137
7138 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007139 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007140 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007141 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007142 em->block_start = EXTENT_MAP_HOLE;
7143 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007144 }
7145
Omar Sandoval39b07b52019-12-02 17:34:23 -08007146 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007147
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007148 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7149 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007150 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007151 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007152 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007153 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007154 size_t size;
7155 size_t extent_offset;
7156 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007157
Omar Sandoval39b07b52019-12-02 17:34:23 -08007158 if (!page)
Yan689f9342007-10-29 11:41:07 -04007159 goto out;
Yan689f9342007-10-29 11:41:07 -04007160
Qu Wenruoe41ca582018-06-06 15:41:49 +08007161 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007162 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007163 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7164 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007165 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007166 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007167 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007168 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007169 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007170
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007171 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007172 if (btrfs_file_extent_compression(leaf, item) !=
7173 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007174 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007175 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007176 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04007177 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04007178 } else {
Ira Weiny58c1a352021-02-16 18:48:23 -08007179 map = kmap_local_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -04007180 read_extent_buffer(leaf, map + pg_offset, ptr,
7181 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007182 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007183 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007184 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007185 copy_size);
7186 }
Ira Weiny58c1a352021-02-16 18:48:23 -08007187 kunmap_local(map);
Chris Masonc8b97812008-10-29 14:49:59 -04007188 }
Chris Mason179e29e2007-11-01 11:28:41 -04007189 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007190 }
Chris Masond1310b22008-01-24 16:13:08 -05007191 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007192 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007193 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007194 }
7195not_found:
7196 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007197 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007198 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007199 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007200insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007201 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02007202 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007203 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007204 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007205 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7206 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007207 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04007208 goto out;
7209 }
Chris Masond1310b22008-01-24 16:13:08 -05007210
Chris Mason890871b2009-09-02 16:24:52 -04007211 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007212 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007213 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007214out:
Liu Boc6414282018-08-23 07:36:17 +08007215 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007216
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007217 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007218
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007219 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04007220 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03007221 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04007222 }
7223 return em;
7224}
7225
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007226struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007227 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007228{
7229 struct extent_map *em;
7230 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007231 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007232 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007233 u64 delalloc_len;
7234 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007235 int err = 0;
7236
Omar Sandoval39b07b52019-12-02 17:34:23 -08007237 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05007238 if (IS_ERR(em))
7239 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007240 /*
7241 * If our em maps to:
7242 * - a hole or
7243 * - a pre-alloc extent,
7244 * there might actually be delalloc bytes behind it.
7245 */
7246 if (em->block_start != EXTENT_MAP_HOLE &&
7247 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7248 return em;
7249 else
7250 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007251
7252 /* check to see if we've wrapped (len == -1 or similar) */
7253 end = start + len;
7254 if (end < start)
7255 end = (u64)-1;
7256 else
7257 end -= 1;
7258
7259 em = NULL;
7260
7261 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007262 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007263 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007264 delalloc_end = delalloc_start + delalloc_len;
7265 if (delalloc_end < delalloc_start)
7266 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007267
7268 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007269 * We didn't find anything useful, return the original results from
7270 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007271 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007272 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007273 em = hole_em;
7274 hole_em = NULL;
7275 goto out;
7276 }
7277
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007278 /*
7279 * Adjust the delalloc_start to make sure it doesn't go backwards from
7280 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007281 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007282 delalloc_start = max(start, delalloc_start);
7283 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007284
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007285 if (delalloc_len > 0) {
7286 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007287 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007288 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007289
David Sterba172ddd62011-04-21 00:48:27 +02007290 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007291 if (!em) {
7292 err = -ENOMEM;
7293 goto out;
7294 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007295
7296 ASSERT(hole_em);
7297 /*
7298 * When btrfs_get_extent can't find anything it returns one
7299 * huge hole
7300 *
7301 * Make sure what it found really fits our range, and adjust to
7302 * make sure it is based on the start from the caller
7303 */
7304 if (hole_end <= start || hole_em->start > end) {
7305 free_extent_map(hole_em);
7306 hole_em = NULL;
7307 } else {
7308 hole_start = max(hole_em->start, start);
7309 hole_len = hole_end - hole_start;
7310 }
7311
7312 if (hole_em && delalloc_start > hole_start) {
7313 /*
7314 * Our hole starts before our delalloc, so we have to
7315 * return just the parts of the hole that go until the
7316 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007317 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007318 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007319 em->start = hole_start;
7320 em->orig_start = hole_start;
7321 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007322 * Don't adjust block start at all, it is fixed at
7323 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007324 */
7325 em->block_start = hole_em->block_start;
7326 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007327 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7328 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007329 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007330 /*
7331 * Hole is out of passed range or it starts after
7332 * delalloc range
7333 */
7334 em->start = delalloc_start;
7335 em->len = delalloc_len;
7336 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007337 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007338 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007339 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007340 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007341 return hole_em;
7342 }
7343out:
7344
7345 free_extent_map(hole_em);
7346 if (err) {
7347 free_extent_map(em);
7348 return ERR_PTR(err);
7349 }
7350 return em;
7351}
7352
Nikolay Borisov64f54182020-06-03 08:55:31 +03007353static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007354 const u64 start,
7355 const u64 len,
7356 const u64 orig_start,
7357 const u64 block_start,
7358 const u64 block_len,
7359 const u64 orig_block_len,
7360 const u64 ram_bytes,
7361 const int type)
7362{
7363 struct extent_map *em = NULL;
7364 int ret;
7365
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007366 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007367 em = create_io_em(inode, start, len, orig_start, block_start,
7368 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007369 BTRFS_COMPRESS_NONE, /* compress_type */
7370 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007371 if (IS_ERR(em))
7372 goto out;
7373 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007374 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7375 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007376 if (ret) {
7377 if (em) {
7378 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007379 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007380 }
7381 em = ERR_PTR(ret);
7382 }
7383 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007384
7385 return em;
7386}
7387
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007388static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007389 u64 start, u64 len)
7390{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007391 struct btrfs_root *root = inode->root;
7392 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007393 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007394 struct btrfs_key ins;
7395 u64 alloc_hint;
7396 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007397
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007398 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007399 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007400 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007401 if (ret)
7402 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007403
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007404 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007405 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007406 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007407 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007408 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007409 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7410 1);
Josef Bacik00361582013-08-14 14:02:47 -04007411
Josef Bacik4b46fce2010-05-23 11:00:55 -04007412 return em;
7413}
7414
Anand Jainf4639632021-02-10 21:25:16 -08007415static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Anand Jain05947ae2021-02-10 21:25:15 -08007416{
7417 struct btrfs_block_group *block_group;
Anand Jainf4639632021-02-10 21:25:16 -08007418 bool readonly = false;
Anand Jain05947ae2021-02-10 21:25:15 -08007419
7420 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7421 if (!block_group || block_group->ro)
Anand Jainf4639632021-02-10 21:25:16 -08007422 readonly = true;
Anand Jain05947ae2021-02-10 21:25:15 -08007423 if (block_group)
7424 btrfs_put_block_group(block_group);
7425 return readonly;
7426}
7427
Chris Mason46bfbb52010-05-26 11:04:10 -04007428/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007429 * Check if we can do nocow write into the range [@offset, @offset + @len)
7430 *
7431 * @offset: File offset
7432 * @len: The length to write, will be updated to the nocow writeable
7433 * range
7434 * @orig_start: (optional) Return the original file offset of the file extent
7435 * @orig_len: (optional) Return the original on-disk length of the file extent
7436 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007437 * @strict: if true, omit optimizations that might force us into unnecessary
7438 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007439 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007440 * Return:
7441 * >0 and update @len if we can do nocow write
7442 * 0 if we can't do nocow write
7443 * <0 if error happened
7444 *
7445 * NOTE: This only checks the file extents, caller is responsible to wait for
7446 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007447 */
Josef Bacik00361582013-08-14 14:02:47 -04007448noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007449 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007450 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007451{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007452 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007453 struct btrfs_path *path;
7454 int ret;
7455 struct extent_buffer *leaf;
7456 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007457 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007458 struct btrfs_file_extent_item *fi;
7459 struct btrfs_key key;
7460 u64 disk_bytenr;
7461 u64 backref_offset;
7462 u64 extent_end;
7463 u64 num_bytes;
7464 int slot;
7465 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007466 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007467
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 path = btrfs_alloc_path();
7469 if (!path)
7470 return -ENOMEM;
7471
David Sterbaf85b7372017-01-20 14:54:07 +01007472 ret = btrfs_lookup_file_extent(NULL, root, path,
7473 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007474 if (ret < 0)
7475 goto out;
7476
7477 slot = path->slots[0];
7478 if (ret == 1) {
7479 if (slot == 0) {
7480 /* can't find the item, must cow */
7481 ret = 0;
7482 goto out;
7483 }
7484 slot--;
7485 }
7486 ret = 0;
7487 leaf = path->nodes[0];
7488 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007489 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 key.type != BTRFS_EXTENT_DATA_KEY) {
7491 /* not our file or wrong item type, must cow */
7492 goto out;
7493 }
7494
7495 if (key.offset > offset) {
7496 /* Wrong offset, must cow */
7497 goto out;
7498 }
7499
7500 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7501 found_type = btrfs_file_extent_type(leaf, fi);
7502 if (found_type != BTRFS_FILE_EXTENT_REG &&
7503 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7504 /* not a regular extent, must cow */
7505 goto out;
7506 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007507
7508 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7509 goto out;
7510
Miao Xiee77751a2013-12-27 21:11:50 +08007511 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7512 if (extent_end <= offset)
7513 goto out;
7514
Chris Mason46bfbb52010-05-26 11:04:10 -04007515 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007516 if (disk_bytenr == 0)
7517 goto out;
7518
7519 if (btrfs_file_extent_compression(leaf, fi) ||
7520 btrfs_file_extent_encryption(leaf, fi) ||
7521 btrfs_file_extent_other_encoding(leaf, fi))
7522 goto out;
7523
Ethan Lien78d42952018-05-17 14:58:29 +08007524 /*
7525 * Do the same check as in btrfs_cross_ref_exist but without the
7526 * unnecessary search.
7527 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007528 if (!strict &&
7529 (btrfs_file_extent_generation(leaf, fi) <=
7530 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007531 goto out;
7532
Chris Mason46bfbb52010-05-26 11:04:10 -04007533 backref_offset = btrfs_file_extent_offset(leaf, fi);
7534
Josef Bacik7ee9e442013-06-21 16:37:03 -04007535 if (orig_start) {
7536 *orig_start = key.offset - backref_offset;
7537 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7538 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7539 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007540
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007541 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007542 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007543
7544 num_bytes = min(offset + *len, extent_end) - offset;
7545 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7546 u64 range_end;
7547
Jeff Mahoneyda170662016-06-15 09:22:56 -04007548 range_end = round_up(offset + num_bytes,
7549 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007550 ret = test_range_bit(io_tree, offset, range_end,
7551 EXTENT_DELALLOC, 0, NULL);
7552 if (ret) {
7553 ret = -EAGAIN;
7554 goto out;
7555 }
7556 }
7557
Josef Bacik1bda19e2013-10-18 12:10:36 -04007558 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007559
7560 /*
7561 * look for other files referencing this extent, if we
7562 * find any we must cow
7563 */
Josef Bacik00361582013-08-14 14:02:47 -04007564
Liu Boe4c3b2d2017-01-30 12:25:28 -08007565 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007566 key.offset - backref_offset, disk_bytenr,
7567 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007568 if (ret) {
7569 ret = 0;
7570 goto out;
7571 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007572
7573 /*
7574 * adjust disk_bytenr and num_bytes to cover just the bytes
7575 * in this extent we are about to write. If there
7576 * are any csums in that range we have to cow in order
7577 * to keep the csums correct
7578 */
7579 disk_bytenr += backref_offset;
7580 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007581 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7582 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007583 /*
7584 * all of the above have passed, it is safe to overwrite this extent
7585 * without cow
7586 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007587 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007588 ret = 1;
7589out:
7590 btrfs_free_path(path);
7591 return ret;
7592}
7593
Josef Bacikeb838e72012-07-31 16:28:48 -04007594static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007595 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007596{
7597 struct btrfs_ordered_extent *ordered;
7598 int ret = 0;
7599
7600 while (1) {
7601 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007602 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007603 /*
7604 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007605 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007606 * extents in this range.
7607 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007608 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 lockend - lockstart + 1);
7610
7611 /*
7612 * We need to make sure there are no buffered pages in this
7613 * range either, we could have raced between the invalidate in
7614 * generic_file_direct_write and locking the extent. The
7615 * invalidate needs to happen so that reads after a write do not
7616 * get stale data.
7617 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007618 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007619 (!writing || !filemap_range_has_page(inode->i_mapping,
7620 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007621 break;
7622
7623 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007624 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007625
7626 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007627 /*
7628 * If we are doing a DIO read and the ordered extent we
7629 * found is for a buffered write, we can not wait for it
7630 * to complete and retry, because if we do so we can
7631 * deadlock with concurrent buffered writes on page
7632 * locks. This happens only if our DIO read covers more
7633 * than one extent map, if at this point has already
7634 * created an ordered extent for a previous extent map
7635 * and locked its range in the inode's io tree, and a
7636 * concurrent write against that previous extent map's
7637 * range and this range started (we unlock the ranges
7638 * in the io tree only when the bios complete and
7639 * buffered writes always lock pages before attempting
7640 * to lock range in the io tree).
7641 */
7642 if (writing ||
7643 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007644 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007645 else
7646 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007647 btrfs_put_ordered_extent(ordered);
7648 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007649 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007650 * We could trigger writeback for this range (and wait
7651 * for it to complete) and then invalidate the pages for
7652 * this range (through invalidate_inode_pages2_range()),
7653 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007654 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007655 * triggered a readahead) on a page lock due to an
7656 * ordered dio extent we created before but did not have
7657 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007658 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007659 * ordered extent to complete while holding a lock on
7660 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007662 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 }
7664
Filipe Mananaade77022016-02-18 14:28:55 +00007665 if (ret)
7666 break;
7667
Josef Bacikeb838e72012-07-31 16:28:48 -04007668 cond_resched();
7669 }
7670
7671 return ret;
7672}
7673
Liu Bo6f9994d2017-01-31 07:50:22 -08007674/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007675static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7676 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007677 u64 block_len, u64 orig_block_len,
7678 u64 ram_bytes, int compress_type,
7679 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007680{
7681 struct extent_map_tree *em_tree;
7682 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007683 int ret;
7684
Liu Bo6f9994d2017-01-31 07:50:22 -08007685 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7686 type == BTRFS_ORDERED_COMPRESSED ||
7687 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007688 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007689
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007690 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007691 em = alloc_extent_map();
7692 if (!em)
7693 return ERR_PTR(-ENOMEM);
7694
7695 em->start = start;
7696 em->orig_start = orig_start;
7697 em->len = len;
7698 em->block_len = block_len;
7699 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007700 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007701 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007702 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007704 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007705 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007706 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007707 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7708 em->compress_type = compress_type;
7709 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007710
7711 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007712 btrfs_drop_extent_cache(inode, em->start,
7713 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007714 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007715 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007716 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007717 /*
7718 * The caller has taken lock_extent(), who could race with us
7719 * to add em?
7720 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007721 } while (ret == -EEXIST);
7722
7723 if (ret) {
7724 free_extent_map(em);
7725 return ERR_PTR(ret);
7726 }
7727
Liu Bo6f9994d2017-01-31 07:50:22 -08007728 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007729 return em;
7730}
7731
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007732
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007733static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007734 struct inode *inode,
7735 struct btrfs_dio_data *dio_data,
7736 u64 start, u64 len)
7737{
7738 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7739 struct extent_map *em = *map;
7740 int ret = 0;
7741
7742 /*
7743 * We don't allocate a new extent in the following cases
7744 *
7745 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7746 * existing extent.
7747 * 2) The extent is marked as PREALLOC. We're good to go here and can
7748 * just use the extent.
7749 *
7750 */
7751 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7752 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7753 em->block_start != EXTENT_MAP_HOLE)) {
7754 int type;
7755 u64 block_start, orig_start, orig_block_len, ram_bytes;
7756
7757 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7758 type = BTRFS_ORDERED_PREALLOC;
7759 else
7760 type = BTRFS_ORDERED_NOCOW;
7761 len = min(len, em->len - (start - em->start));
7762 block_start = em->block_start + (start - em->start);
7763
7764 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007765 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007766 btrfs_inc_nocow_writers(fs_info, block_start)) {
7767 struct extent_map *em2;
7768
Nikolay Borisov64f54182020-06-03 08:55:31 +03007769 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007770 orig_start, block_start,
7771 len, orig_block_len,
7772 ram_bytes, type);
7773 btrfs_dec_nocow_writers(fs_info, block_start);
7774 if (type == BTRFS_ORDERED_PREALLOC) {
7775 free_extent_map(em);
7776 *map = em = em2;
7777 }
7778
7779 if (em2 && IS_ERR(em2)) {
7780 ret = PTR_ERR(em2);
7781 goto out;
7782 }
7783 /*
7784 * For inode marked NODATACOW or extent marked PREALLOC,
7785 * use the existing or preallocated extent, so does not
7786 * need to adjust btrfs_space_info's bytes_may_use.
7787 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007788 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007789 goto skip_cow;
7790 }
7791 }
7792
7793 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007794 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007795 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007796 if (IS_ERR(em)) {
7797 ret = PTR_ERR(em);
7798 goto out;
7799 }
7800
7801 len = min(len, em->len - (start - em->start));
7802
7803skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007804 /*
7805 * Need to update the i_size under the extent lock so buffered
7806 * readers will get the updated i_size when we unlock.
7807 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007808 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007809 i_size_write(inode, start + len);
7810
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007811 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007812out:
7813 return ret;
7814}
7815
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007816static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7817 loff_t length, unsigned int flags, struct iomap *iomap,
7818 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007821 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007822 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307823 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007824 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007825 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007826 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007827 u64 len = length;
7828 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007829
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007830 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007831 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007832
Josef Bacikc3298612012-08-03 16:49:19 -04007833 lockstart = start;
7834 lockend = start + len - 1;
7835
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007836 /*
7837 * The generic stuff only does filemap_write_and_wait_range, which
7838 * isn't enough if we've written compressed pages to this area, so we
7839 * need to flush the dirty pages again to make absolutely sure that any
7840 * outstanding dirty pages are on disk.
7841 */
7842 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7843 &BTRFS_I(inode)->runtime_flags)) {
7844 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7845 start + length - 1);
7846 if (ret)
7847 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007848 }
7849
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007850 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7851 if (!dio_data)
7852 return -ENOMEM;
7853
7854 dio_data->length = length;
7855 if (write) {
7856 dio_data->reserve = round_up(length, fs_info->sectorsize);
7857 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7858 &dio_data->data_reserved,
7859 start, dio_data->reserve);
7860 if (ret) {
7861 extent_changeset_free(dio_data->data_reserved);
7862 kfree(dio_data);
7863 return ret;
7864 }
7865 }
7866 iomap->private = dio_data;
7867
7868
Josef Bacikeb838e72012-07-31 16:28:48 -04007869 /*
7870 * If this errors out it's because we couldn't invalidate pagecache for
7871 * this range and we need to fallback to buffered.
7872 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007873 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007874 ret = -ENOTBLK;
7875 goto err;
7876 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007877
Omar Sandoval39b07b52019-12-02 17:34:23 -08007878 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007879 if (IS_ERR(em)) {
7880 ret = PTR_ERR(em);
7881 goto unlock_err;
7882 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883
7884 /*
7885 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7886 * io. INLINE is special, and we could probably kludge it in here, but
7887 * it's still buffered so for safety lets just fall back to the generic
7888 * buffered path.
7889 *
7890 * For COMPRESSED we _have_ to read the entire extent in so we can
7891 * decompress it, so there will be buffering required no matter what we
7892 * do, so go ahead and fallback to buffered.
7893 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007894 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007895 * to buffered IO. Don't blame me, this is the price we pay for using
7896 * the generic code.
7897 */
7898 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7899 em->block_start == EXTENT_MAP_INLINE) {
7900 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007901 ret = -ENOTBLK;
7902 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007903 }
7904
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007905 len = min(len, em->len - (start - em->start));
7906 if (write) {
7907 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7908 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007909 if (ret < 0)
7910 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007911 unlock_extents = true;
7912 /* Recalc len in case the new em is smaller than requested */
7913 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007914 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007915 /*
7916 * We need to unlock only the end area that we aren't using.
7917 * The rest is going to be unlocked by the endio routine.
7918 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007919 lockstart = start + len;
7920 if (lockstart < lockend)
7921 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922 }
7923
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007924 if (unlock_extents)
7925 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7926 lockstart, lockend, &cached_state);
7927 else
7928 free_extent_state(cached_state);
7929
7930 /*
7931 * Translate extent map information to iomap.
7932 * We trim the extents (and move the addr) even though iomap code does
7933 * that, since we have locked only the parts we are performing I/O in.
7934 */
7935 if ((em->block_start == EXTENT_MAP_HOLE) ||
7936 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7937 iomap->addr = IOMAP_NULL_ADDR;
7938 iomap->type = IOMAP_HOLE;
7939 } else {
7940 iomap->addr = em->block_start + (start - em->start);
7941 iomap->type = IOMAP_MAPPED;
7942 }
7943 iomap->offset = start;
7944 iomap->bdev = fs_info->fs_devices->latest_bdev;
7945 iomap->length = len;
7946
Johannes Thumshirne380adf2021-05-19 00:40:27 +09007947 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
Naohiro Aota544d24f2021-02-04 19:22:06 +09007948 iomap->flags |= IOMAP_F_ZONE_APPEND;
7949
Josef Bacik4b46fce2010-05-23 11:00:55 -04007950 free_extent_map(em);
7951
7952 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007953
7954unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007955 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7956 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007957err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007958 if (dio_data) {
7959 btrfs_delalloc_release_space(BTRFS_I(inode),
7960 dio_data->data_reserved, start,
7961 dio_data->reserve, true);
7962 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7963 extent_changeset_free(dio_data->data_reserved);
7964 kfree(dio_data);
7965 }
7966 return ret;
7967}
7968
7969static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7970 ssize_t written, unsigned int flags, struct iomap *iomap)
7971{
7972 int ret = 0;
7973 struct btrfs_dio_data *dio_data = iomap->private;
7974 size_t submitted = dio_data->submitted;
7975 const bool write = !!(flags & IOMAP_WRITE);
7976
7977 if (!write && (iomap->type == IOMAP_HOLE)) {
7978 /* If reading from a hole, unlock and return */
7979 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7980 goto out;
7981 }
7982
7983 if (submitted < length) {
7984 pos += submitted;
7985 length -= submitted;
7986 if (write)
7987 __endio_write_update_ordered(BTRFS_I(inode), pos,
7988 length, false);
7989 else
7990 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7991 pos + length - 1);
7992 ret = -ENOTBLK;
7993 }
7994
7995 if (write) {
7996 if (dio_data->reserve)
7997 btrfs_delalloc_release_space(BTRFS_I(inode),
7998 dio_data->data_reserved, pos,
7999 dio_data->reserve, true);
8000 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
8001 extent_changeset_free(dio_data->data_reserved);
8002 }
8003out:
8004 kfree(dio_data);
8005 iomap->private = NULL;
8006
Josef Bacikeb838e72012-07-31 16:28:48 -04008007 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008008}
8009
Omar Sandoval769b4f22020-04-16 14:46:22 -07008010static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8011{
8012 /*
8013 * This implies a barrier so that stores to dio_bio->bi_status before
8014 * this and loads of dio_bio->bi_status after this are fully ordered.
8015 */
8016 if (!refcount_dec_and_test(&dip->refs))
8017 return;
8018
Naohiro Aotacfe94442021-02-04 19:21:59 +09008019 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008020 __endio_write_update_ordered(BTRFS_I(dip->inode),
8021 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07008022 dip->bytes,
8023 !dip->dio_bio->bi_status);
8024 } else {
8025 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
8026 dip->logical_offset,
8027 dip->logical_offset + dip->bytes - 1);
8028 }
8029
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008030 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008031 kfree(dip);
8032}
8033
Omar Sandoval77d5d682020-04-16 14:46:25 -07008034static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8035 int mirror_num,
8036 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08008037{
Omar Sandoval77d5d682020-04-16 14:46:25 -07008038 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008039 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008040 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008041
Mike Christie37226b22016-06-05 14:31:52 -05008042 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008043
Omar Sandoval5c047a62020-04-16 14:46:24 -07008044 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08008045 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02008046 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008047
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008048 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07008049 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07008050 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008051 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08008052 return ret;
8053}
8054
Omar Sandoval769b4f22020-04-16 14:46:22 -07008055static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
8056 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008057 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08008058{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008059 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8060 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04008061 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8063 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07008064 struct bio_vec bvec;
8065 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008066 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008067 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008068 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008069
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008070 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
8071 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308072
Liu Bo17347ce2017-05-15 15:33:27 -07008073 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8074 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008075 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07008076 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008077 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008078 (!csum || !check_data_csum(inode, io_bio,
Omar Sandovalc1d6abd2021-03-16 12:43:00 -07008079 bio_offset, bvec.bv_page,
8080 pgoff, start))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008081 clean_io_failure(fs_info, failure_tree, io_tree,
8082 start, bvec.bv_page,
8083 btrfs_ino(BTRFS_I(inode)),
8084 pgoff);
8085 } else {
Qu Wenruo150e4b02021-05-03 10:08:55 +08008086 int ret;
Miao Xie8b110e32014-09-12 18:44:03 +08008087
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008088 ASSERT((start - io_bio->logical) < UINT_MAX);
Qu Wenruo150e4b02021-05-03 10:08:55 +08008089 ret = btrfs_repair_one_sector(inode,
8090 &io_bio->bio,
8091 start - io_bio->logical,
8092 bvec.bv_page, pgoff,
8093 start, io_bio->mirror_num,
8094 submit_dio_repair_bio);
8095 if (ret)
8096 err = errno_to_blk_status(ret);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008097 }
8098 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08008099 ASSERT(bio_offset + sectorsize > bio_offset);
8100 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008103 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008104 return err;
8105}
8106
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03008107static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08008108 const u64 offset, const u64 bytes,
8109 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008110{
Qu Wenruoe65f1522021-04-01 15:15:06 +08008111 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8112 finish_ordered_fn, uptodate);
Filipe Manana14543772015-11-24 16:23:54 +00008113}
8114
Qu Wenruo8896a082020-10-21 14:24:53 +08008115static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08008116 struct bio *bio,
8117 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008118{
Qu Wenruo1941b642020-12-02 14:47:57 +08008119 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04008120}
8121
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008122static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008123{
8124 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008125 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008126
Miao Xie8b110e32014-09-12 18:44:03 +08008127 if (err)
8128 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008129 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008130 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01008131 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08008132 bio->bi_iter.bi_size, err);
8133
Omar Sandoval769b4f22020-04-16 14:46:22 -07008134 if (bio_op(bio) == REQ_OP_READ) {
8135 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07008136 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00008137 }
8138
Omar Sandoval769b4f22020-04-16 14:46:22 -07008139 if (err)
8140 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00008141
Naohiro Aota544d24f2021-02-04 19:22:06 +09008142 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8143
Miao Xiee65e1532010-11-22 03:04:43 +00008144 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008145 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08008146}
8147
David Sterbad0ee3932018-03-08 14:35:48 +01008148static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8149 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008150{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008151 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008152 struct btrfs_dio_private *dip = bio->bi_private;
Naohiro Aotacfe94442021-02-04 19:21:59 +09008153 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008154 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008155
Liu Bo4c274bc2017-11-01 17:19:27 -06008156 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008157 if (async_submit)
8158 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8159
Josef Bacik5fd02042012-05-02 14:00:54 -04008160 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008161 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008162 if (ret)
8163 goto err;
8164 }
Miao Xiee65e1532010-11-22 03:04:43 +00008165
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008166 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008167 goto map;
8168
8169 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08008170 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02008171 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008172 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008173 } else if (write) {
8174 /*
8175 * If we aren't doing async submit, calculate the csum of the
8176 * bio now.
8177 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03008178 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008179 if (ret)
8180 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008181 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07008182 u64 csum_offset;
8183
8184 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02008185 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02008186 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07008187 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008188 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008189map:
Chris Mason08635ba2019-07-10 12:28:14 -07008190 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008191err:
Miao Xiee65e1532010-11-22 03:04:43 +00008192 return ret;
8193}
8194
Omar Sandovalc36cac22020-04-16 14:46:13 -07008195/*
8196 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8197 * or ordered extents whether or not we submit any bios.
8198 */
8199static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8200 struct inode *inode,
8201 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00008202{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008203 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07008204 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8205 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008206 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008207
Omar Sandoval85879572020-04-16 14:46:21 -07008208 dip_size = sizeof(*dip);
8209 if (!write && csum) {
8210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07008211 size_t nblocks;
8212
David Sterba265fdfa2020-07-01 21:19:09 +02008213 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02008214 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07008215 }
8216
8217 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008218 if (!dip)
8219 return NULL;
8220
Omar Sandovalc36cac22020-04-16 14:46:13 -07008221 dip->inode = inode;
8222 dip->logical_offset = file_offset;
8223 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01008224 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008225 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07008226 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008227 return dip;
8228}
8229
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008230static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
8231 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008232{
Naohiro Aotacfe94442021-02-04 19:21:59 +09008233 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008235 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8236 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07008237 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00008238 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07008239 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04008240 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008241 u64 submit_len;
8242 int clone_offset = 0;
8243 int clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008244 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308245 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008246 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008247 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008248 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01008249 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00008250
Omar Sandovalc36cac22020-04-16 14:46:13 -07008251 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8252 if (!dip) {
8253 if (!write) {
8254 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8255 file_offset + dio_bio->bi_iter.bi_size - 1);
8256 }
8257 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008258 bio_endio(dio_bio);
8259 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04008260 }
8261
Josef Bacik334c16d2020-10-16 11:29:14 -04008262 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07008263 /*
8264 * Load the csums up front to reduce csum tree searches and
8265 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04008266 *
8267 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07008268 */
Qu Wenruo62751932020-12-02 14:48:06 +08008269 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07008270 if (status != BLK_STS_OK)
8271 goto out_err;
8272 }
David Woodhouse53b381b2013-01-29 18:40:14 -05008273
Omar Sandoval769b4f22020-04-16 14:46:22 -07008274 start_sector = dio_bio->bi_iter.bi_sector;
8275 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00008276
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008277 do {
Michal Rostecki42034312021-01-27 14:57:27 +01008278 logical = start_sector << 9;
8279 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8280 if (IS_ERR(em)) {
8281 status = errno_to_blk_status(PTR_ERR(em));
8282 em = NULL;
8283 goto out_err_em;
8284 }
8285 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
Qu Wenruo43c0d1a2021-04-13 17:58:48 +08008286 logical, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008287 if (ret) {
8288 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01008289 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07008290 }
8291 ASSERT(geom.len <= INT_MAX);
8292
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008293 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008294
Liu Bo725130b2017-05-16 09:51:39 -07008295 /*
8296 * This will never fail as it's passing GPF_NOFS and
8297 * the allocation is backed by btrfs_bioset.
8298 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008299 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07008300 bio->bi_private = dip;
8301 bio->bi_end_io = btrfs_end_dio_bio;
8302 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008303
Naohiro Aota544d24f2021-02-04 19:22:06 +09008304 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8305 status = extract_ordered_extent(BTRFS_I(inode), bio,
8306 file_offset);
8307 if (status) {
8308 bio_put(bio);
8309 goto out_err;
8310 }
8311 }
8312
Liu Bo725130b2017-05-16 09:51:39 -07008313 ASSERT(submit_len >= clone_len);
8314 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008315
Liu Bo725130b2017-05-16 09:51:39 -07008316 /*
8317 * Increase the count before we submit the bio so we know
8318 * the end IO handler won't happen before we increase the
8319 * count. Otherwise, the dip might get freed before we're
8320 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008321 *
8322 * We transfer the initial reference to the last bio, so we
8323 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008324 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008325 if (submit_len > 0) {
8326 refcount_inc(&dip->refs);
8327 /*
8328 * If we are submitting more than one bio, submit them
8329 * all asynchronously. The exception is RAID 5 or 6, as
8330 * asynchronous checksums make it difficult to collect
8331 * full stripe writes.
8332 */
8333 if (!raid56)
8334 async_submit = 1;
8335 }
Miao Xiee65e1532010-11-22 03:04:43 +00008336
David Sterbad0ee3932018-03-08 14:35:48 +01008337 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008338 async_submit);
8339 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008340 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008341 if (submit_len > 0)
8342 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008343 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008344 }
Liu Bo725130b2017-05-16 09:51:39 -07008345
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008346 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008347 clone_offset += clone_len;
8348 start_sector += clone_len >> 9;
8349 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008350
8351 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008352 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008353 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008354
Michal Rostecki42034312021-01-27 14:57:27 +01008355out_err_em:
8356 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008357out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008358 dip->dio_bio->bi_status = status;
8359 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008360
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008361 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008362}
8363
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008364const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008365 .iomap_begin = btrfs_dio_iomap_begin,
8366 .iomap_end = btrfs_dio_iomap_end,
8367};
8368
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008369const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008370 .submit_io = btrfs_submit_direct,
8371};
8372
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008373static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008374 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008375{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008376 int ret;
8377
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008378 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008379 if (ret)
8380 return ret;
8381
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008382 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008383}
8384
Chris Mason9ebefb182007-06-15 13:50:00 -04008385int btrfs_readpage(struct file *file, struct page *page)
8386{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008387 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8388 u64 start = page_offset(page);
8389 u64 end = start + PAGE_SIZE - 1;
Qu Wenruo390ed292021-04-14 16:42:15 +08008390 struct btrfs_bio_ctrl bio_ctrl = { 0 };
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008391 int ret;
8392
Nikolay Borisov0f208812020-09-14 14:39:16 +03008393 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8394
Qu Wenruo390ed292021-04-14 16:42:15 +08008395 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8396 if (bio_ctrl.bio)
8397 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008398 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008399}
Chris Mason1832a6d2007-12-21 16:27:21 -05008400
Chris Mason39279cc2007-06-12 06:35:45 -04008401static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8402{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008403 struct inode *inode = page->mapping->host;
8404 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008405
8406 if (current->flags & PF_MEMALLOC) {
8407 redirty_page_for_writepage(wbc, page);
8408 unlock_page(page);
8409 return 0;
8410 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008411
8412 /*
8413 * If we are under memory pressure we will call this directly from the
8414 * VM, we need to make sure we have the inode referenced for the ordered
8415 * extent. If not just return like we didn't do anything.
8416 */
8417 if (!igrab(inode)) {
8418 redirty_page_for_writepage(wbc, page);
8419 return AOP_WRITEPAGE_ACTIVATE;
8420 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008421 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008422 btrfs_add_delayed_iput(inode);
8423 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008424}
Chris Mason39279cc2007-06-12 06:35:45 -04008425
Eric Sandeen48a3b632013-04-25 20:41:01 +00008426static int btrfs_writepages(struct address_space *mapping,
8427 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008428{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008429 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008430}
8431
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008432static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008433{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008434 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008435}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008436
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008437/*
8438 * For releasepage() and invalidatepage() we have a race window where
8439 * end_page_writeback() is called but the subpage spinlock is not yet released.
8440 * If we continue to release/invalidate the page, we could cause use-after-free
8441 * for subpage spinlock. So this function is to spin and wait for subpage
8442 * spinlock.
8443 */
8444static void wait_subpage_spinlock(struct page *page)
8445{
8446 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8447 struct btrfs_subpage *subpage;
8448
8449 if (fs_info->sectorsize == PAGE_SIZE)
8450 return;
8451
8452 ASSERT(PagePrivate(page) && page->private);
8453 subpage = (struct btrfs_subpage *)page->private;
8454
8455 /*
8456 * This may look insane as we just acquire the spinlock and release it,
8457 * without doing anything. But we just want to make sure no one is
8458 * still holding the subpage spinlock.
8459 * And since the page is not dirty nor writeback, and we have page
8460 * locked, the only possible way to hold a spinlock is from the endio
8461 * function to clear page writeback.
8462 *
8463 * Here we just acquire the spinlock so that all existing callers
8464 * should exit and we're safe to release/invalidate the page.
8465 */
8466 spin_lock_irq(&subpage->lock);
8467 spin_unlock_irq(&subpage->lock);
8468}
8469
Chris Masone6dcd2d2008-07-17 12:53:50 -04008470static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008471{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008472 int ret = try_release_extent_mapping(page, gfp_flags);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008473
8474 if (ret == 1) {
8475 wait_subpage_spinlock(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008476 clear_page_extent_mapped(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008477 }
Chris Masona52d9a82007-08-27 16:49:44 -04008478 return ret;
8479}
Chris Mason39279cc2007-06-12 06:35:45 -04008480
Chris Masone6dcd2d2008-07-17 12:53:50 -04008481static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8482{
Chris Mason98509cf2008-09-11 15:51:43 -04008483 if (PageWriteback(page) || PageDirty(page))
8484 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008485 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008486}
8487
Roman Gushchinf8e66082020-03-04 16:57:35 -08008488#ifdef CONFIG_MIGRATION
8489static int btrfs_migratepage(struct address_space *mapping,
8490 struct page *newpage, struct page *page,
8491 enum migrate_mode mode)
8492{
8493 int ret;
8494
8495 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8496 if (ret != MIGRATEPAGE_SUCCESS)
8497 return ret;
8498
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008499 if (page_has_private(page))
8500 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008501
Qu Wenruof57ad932021-04-07 19:22:13 +08008502 if (PageOrdered(page)) {
8503 ClearPageOrdered(page);
8504 SetPageOrdered(newpage);
Roman Gushchinf8e66082020-03-04 16:57:35 -08008505 }
8506
8507 if (mode != MIGRATE_SYNC_NO_COPY)
8508 migrate_page_copy(newpage, page);
8509 else
8510 migrate_page_states(newpage, page);
8511 return MIGRATEPAGE_SUCCESS;
8512}
8513#endif
8514
Lukas Czernerd47992f2013-05-21 23:17:23 -04008515static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8516 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008517{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008518 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
Qu Wenruob945a462021-05-31 16:50:46 +08008519 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008520 struct extent_io_tree *tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008521 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008522 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008523 u64 page_end = page_start + PAGE_SIZE - 1;
Qu Wenruo3b835842021-04-06 19:54:53 +08008524 u64 cur;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008525 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008526
Chris Mason8b62b722009-09-02 16:53:46 -04008527 /*
Qu Wenruo266a2582021-04-06 08:27:18 +08008528 * We have page locked so no new ordered extent can be created on this
8529 * page, nor bio can be submitted for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008530 *
Qu Wenruo266a2582021-04-06 08:27:18 +08008531 * But already submitted bio can still be finished on this page.
Qu Wenruof57ad932021-04-07 19:22:13 +08008532 * Furthermore, endio function won't skip page which has Ordered
8533 * (Private2) already cleared, so it's possible for endio and
8534 * invalidatepage to do the same ordered extent accounting twice
8535 * on one page.
Qu Wenruo266a2582021-04-06 08:27:18 +08008536 *
8537 * So here we wait for any submitted bios to finish, so that we won't
8538 * do double ordered extent accounting on the same page.
Chris Mason8b62b722009-09-02 16:53:46 -04008539 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008540 wait_on_page_writeback(page);
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08008541 wait_subpage_spinlock(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008542
Qu Wenruobcd77452021-05-31 16:50:55 +08008543 /*
8544 * For subpage case, we have call sites like
8545 * btrfs_punch_hole_lock_range() which passes range not aligned to
8546 * sectorsize.
8547 * If the range doesn't cover the full page, we don't need to and
8548 * shouldn't clear page extent mapped, as page->private can still
8549 * record subpage dirty bits for other part of the range.
8550 *
8551 * For cases that can invalidate the full even the range doesn't
8552 * cover the full page, like invalidating the last page, we're
8553 * still safe to wait for ordered extent to finish.
8554 */
8555 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008556 btrfs_releasepage(page, GFP_NOFS);
8557 return;
8558 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008559
8560 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008561 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008562
Qu Wenruo3b835842021-04-06 19:54:53 +08008563 cur = page_start;
8564 while (cur < page_end) {
8565 struct btrfs_ordered_extent *ordered;
8566 bool delete_states;
8567 u64 range_end;
Qu Wenruob945a462021-05-31 16:50:46 +08008568 u32 range_len;
Qu Wenruo3b835842021-04-06 19:54:53 +08008569
8570 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8571 page_end + 1 - cur);
8572 if (!ordered) {
8573 range_end = page_end;
8574 /*
8575 * No ordered extent covering this range, we are safe
8576 * to delete all extent states in the range.
8577 */
8578 delete_states = true;
8579 goto next;
8580 }
8581 if (ordered->file_offset > cur) {
8582 /*
8583 * There is a range between [cur, oe->file_offset) not
8584 * covered by any ordered extent.
8585 * We are safe to delete all extent states, and handle
8586 * the ordered extent in the next iteration.
8587 */
8588 range_end = ordered->file_offset - 1;
8589 delete_states = true;
8590 goto next;
8591 }
8592
8593 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8594 page_end);
Qu Wenruob945a462021-05-31 16:50:46 +08008595 ASSERT(range_end + 1 - cur < U32_MAX);
8596 range_len = range_end + 1 - cur;
8597 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008598 /*
Qu Wenruof57ad932021-04-07 19:22:13 +08008599 * If Ordered (Private2) is cleared, it means endio has
8600 * already been executed for the range.
Qu Wenruo3b835842021-04-06 19:54:53 +08008601 * We can't delete the extent states as
8602 * btrfs_finish_ordered_io() may still use some of them.
8603 */
8604 delete_states = false;
8605 goto next;
8606 }
Qu Wenruob945a462021-05-31 16:50:46 +08008607 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
Qu Wenruo3b835842021-04-06 19:54:53 +08008608
Chris Masoneb84ae02008-07-17 13:53:27 -04008609 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008610 * IO on this page will never be started, so we need to account
8611 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8612 * here, must leave that up for the ordered extent completion.
Qu Wenruo3b835842021-04-06 19:54:53 +08008613 *
8614 * This will also unlock the range for incoming
8615 * btrfs_finish_ordered_io().
Chris Masoneb84ae02008-07-17 13:53:27 -04008616 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008617 if (!inode_evicting)
Qu Wenruo3b835842021-04-06 19:54:53 +08008618 clear_extent_bit(tree, cur, range_end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008619 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008620 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008621 EXTENT_DEFRAG, 1, 0, &cached_state);
Qu Wenruo3b835842021-04-06 19:54:53 +08008622
8623 spin_lock_irq(&inode->ordered_tree.lock);
8624 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8625 ordered->truncated_len = min(ordered->truncated_len,
8626 cur - ordered->file_offset);
8627 spin_unlock_irq(&inode->ordered_tree.lock);
8628
8629 if (btrfs_dec_test_ordered_pending(inode, &ordered,
David Sterbaf41b6ba2021-07-26 14:15:10 +02008630 cur, range_end + 1 - cur)) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008631 btrfs_finish_ordered_io(ordered);
8632 /*
8633 * The ordered extent has finished, now we're again
8634 * safe to delete all extent states of the range.
8635 */
8636 delete_states = true;
8637 } else {
8638 /*
8639 * btrfs_finish_ordered_io() will get executed by endio
8640 * of other pages, thus we can't delete extent states
8641 * anymore
8642 */
8643 delete_states = false;
8644 }
8645next:
8646 if (ordered)
8647 btrfs_put_ordered_extent(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008648 /*
Qu Wenruo3b835842021-04-06 19:54:53 +08008649 * Qgroup reserved space handler
8650 * Sector(s) here will be either:
Qu Wenruo266a2582021-04-06 08:27:18 +08008651 *
Qu Wenruo3b835842021-04-06 19:54:53 +08008652 * 1) Already written to disk or bio already finished
8653 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8654 * Qgroup will be handled by its qgroup_record then.
8655 * btrfs_qgroup_free_data() call will do nothing here.
8656 *
8657 * 2) Not written to disk yet
8658 * Then btrfs_qgroup_free_data() call will clear the
8659 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8660 * reserved data space.
8661 * Since the IO will never happen for this page.
Chris Mason8b62b722009-09-02 16:53:46 -04008662 */
Qu Wenruo3b835842021-04-06 19:54:53 +08008663 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008664 if (!inode_evicting) {
Qu Wenruo3b835842021-04-06 19:54:53 +08008665 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008666 EXTENT_DELALLOC | EXTENT_UPTODATE |
8667 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
Qu Wenruo3b835842021-04-06 19:54:53 +08008668 delete_states, &cached_state);
8669 }
8670 cur = range_end + 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008671 }
Qu Wenruo3b835842021-04-06 19:54:53 +08008672 /*
8673 * We have iterated through all ordered extents of the page, the page
Qu Wenruof57ad932021-04-07 19:22:13 +08008674 * should not have Ordered (Private2) anymore, or the above iteration
8675 * did something wrong.
Qu Wenruo3b835842021-04-06 19:54:53 +08008676 */
Qu Wenruof57ad932021-04-07 19:22:13 +08008677 ASSERT(!PageOrdered(page));
Qu Wenruo3b835842021-04-06 19:54:53 +08008678 if (!inode_evicting)
8679 __btrfs_releasepage(page, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04008680 ClearPageChecked(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08008681 clear_page_extent_mapped(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008682}
8683
Chris Mason9ebefb182007-06-15 13:50:00 -04008684/*
8685 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8686 * called from a page fault handler when a page is first dirtied. Hence we must
8687 * be careful to check for EOF conditions here. We set the page up correctly
8688 * for a written page which means we get ENOSPC checking when writing into
8689 * holes and correct delalloc and unwritten extent mapping on filesystems that
8690 * support these features.
8691 *
8692 * We are not allowed to take the i_mutex here so we have to play games to
8693 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008694 * truncate_setsize() writes the inode size before removing pages, once we have
8695 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008696 * beyond EOF, then the page is guaranteed safe against truncation until we
8697 * unlock the page.
8698 */
Souptick Joardera528a242018-06-06 19:54:44 +05308699vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008700{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008701 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008702 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008703 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008704 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8705 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008706 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008707 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008708 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008709 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308710 vm_fault_t ret;
8711 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008712 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308713 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008714 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008715 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308716 u64 end;
8717
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008718 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008719
Jan Karab2b5ef52012-06-12 16:20:45 +02008720 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008721 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008722 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308723 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008724
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308725 /*
8726 * Reserving delalloc space after obtaining the page lock can lead to
8727 * deadlock. For example, if a dirty page is locked by this function
8728 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8729 * dirty page write out, then the btrfs_writepage() function could
8730 * end up waiting indefinitely to get a lock on the page currently
8731 * being processed by btrfs_page_mkwrite() function.
8732 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008733 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8734 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308735 if (!ret2) {
8736 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008737 reserved = 1;
8738 }
Souptick Joardera528a242018-06-06 19:54:44 +05308739 if (ret2) {
8740 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008741 if (reserved)
8742 goto out;
8743 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008744 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008745
Nick Piggin56a76f82009-03-31 15:23:23 -07008746 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008747again:
Josef Bacik8318ba72021-02-10 17:14:33 -05008748 down_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008749 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008750 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008751
Chris Mason9ebefb182007-06-15 13:50:00 -04008752 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008753 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008754 /* page got truncated out from underneath us */
8755 goto out_unlock;
8756 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008757 wait_on_page_writeback(page);
8758
David Sterbaff13db42015-12-03 14:30:40 +01008759 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Qu Wenruo32443de2021-01-26 16:34:00 +08008760 ret2 = set_page_extent_mapped(page);
8761 if (ret2 < 0) {
8762 ret = vmf_error(ret2);
8763 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8764 goto out_unlock;
8765 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766
Chris Masoneb84ae02008-07-17 13:53:27 -04008767 /*
8768 * we can't set the delalloc bits if there are pending ordered
8769 * extents. Drop our locks and wait for them to finish
8770 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008771 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8772 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008774 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008775 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008776 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008777 up_read(&BTRFS_I(inode)->i_mmap_lock);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008778 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779 btrfs_put_ordered_extent(ordered);
8780 goto again;
8781 }
8782
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008783 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008784 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008785 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008786 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308787 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008788 btrfs_delalloc_release_space(BTRFS_I(inode),
8789 data_reserved, page_start,
8790 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308791 }
8792 }
8793
Josef Bacikfbf19082009-10-01 17:10:23 -04008794 /*
Liu Bo54160342016-12-13 12:15:19 -08008795 * page_mkwrite gets called when the page is firstly dirtied after it's
8796 * faulted in, but write(2) could also dirty a page and set delalloc
8797 * bits, thus in this case for space account reason, we still need to
8798 * clear any delalloc bits within this page range since we have to
8799 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008800 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308801 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008802 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8803 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008804
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008805 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008806 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308807 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008808 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008809 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008810 ret = VM_FAULT_SIGBUS;
8811 goto out_unlock;
8812 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008813
8814 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008816 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008817 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008818 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008819
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008820 if (zero_start != PAGE_SIZE) {
Ira Weinyd048b9c2021-05-04 18:40:07 -07008821 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008822 flush_dcache_page(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823 }
Chris Mason247e7432008-07-17 12:53:51 -04008824 ClearPageChecked(page);
Qu Wenruo2d8ec402021-05-31 16:50:52 +08008825 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8826 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008827
Filipe Mananabc0939f2021-02-23 12:08:48 +00008828 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008829
David Sterbae43bbe52017-12-12 21:43:52 +01008830 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Josef Bacik8318ba72021-02-10 17:14:33 -05008831 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason9ebefb182007-06-15 13:50:00 -04008832
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008833 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8834 sb_end_pagefault(inode->i_sb);
8835 extent_changeset_free(data_reserved);
8836 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008837
8838out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008839 unlock_page(page);
Josef Bacik8318ba72021-02-10 17:14:33 -05008840 up_read(&BTRFS_I(inode)->i_mmap_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -05008841out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008842 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008843 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008844 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008845out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008846 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008847 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008848 return ret;
8849}
8850
Filipe Manana213e8c52018-02-06 20:40:31 +00008851static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008853 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008854 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008855 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008856 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008857 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008858 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008859 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008860 u64 extents_found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008861
Filipe Manana213e8c52018-02-06 20:40:31 +00008862 if (!skip_writeback) {
8863 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8864 (u64)-1);
8865 if (ret)
8866 return ret;
8867 }
Chris Mason39279cc2007-06-12 06:35:45 -04008868
Josef Bacikfcb80c22011-05-03 10:40:22 -04008869 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008870 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8871 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008872 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008873 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008874 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008875 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008876 * be free'd up by the truncate operation, but also have some slack
8877 * space reserved in case it uses space during the truncate (thank you
8878 * very much snapshotting).
8879 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008880 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008881 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008882 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008883 * doesn't end up using space reserved for updating the inode. We also
8884 * need to be able to stop the transaction and start a new one, which
8885 * means we need to be able to update the inode several times, and we
8886 * have no idea of knowing how many times that will be, so we can't just
8887 * reserve 1 item for the entirety of the operation, so that has to be
8888 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008889 *
8890 * So that leaves us with
8891 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008892 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008893 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008894 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008895 * updating the inode.
8896 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008897 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008898 if (!rsv)
8899 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008900 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008901 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008902
Josef Bacik907cbce2011-08-08 13:46:15 -04008903 /*
Josef Bacik07127182011-08-19 10:29:59 -04008904 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008905 * 1 for updating the inode.
8906 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008907 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008908 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008909 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008910 goto out;
8911 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008912
Josef Bacik907cbce2011-08-08 13:46:15 -04008913 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008914 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008915 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008916 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008917
Josef Bacikca7e70f2012-08-27 17:48:15 -04008918 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008919
Yan, Zheng80825102009-11-12 09:35:36 +00008920 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008921 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008922 inode->i_size,
Filipe Manana0d7d3162021-05-24 11:35:55 +01008923 BTRFS_EXTENT_DATA_KEY,
8924 &extents_found);
Josef Bacikddfae632017-10-19 14:16:02 -04008925 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008926 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008927 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008928
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008929 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008930 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008931 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008932
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008933 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008934 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008935
8936 trans = btrfs_start_transaction(root, 2);
8937 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008938 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008939 trans = NULL;
8940 break;
8941 }
8942
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008943 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008944 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008945 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008946 BUG_ON(ret); /* shouldn't happen */
8947 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008948 }
8949
Josef Bacikddfae632017-10-19 14:16:02 -04008950 /*
8951 * We can't call btrfs_truncate_block inside a trans handle as we could
8952 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8953 * we've truncated everything except the last little bit, and can do
8954 * btrfs_truncate_block and then update the disk_i_size.
8955 */
8956 if (ret == NEED_TRUNCATE_BLOCK) {
8957 btrfs_end_transaction(trans);
8958 btrfs_btree_balance_dirty(fs_info);
8959
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008960 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008961 if (ret)
8962 goto out;
8963 trans = btrfs_start_transaction(root, 1);
8964 if (IS_ERR(trans)) {
8965 ret = PTR_ERR(trans);
8966 goto out;
8967 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008968 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008969 }
8970
Chris Mason917c16b2011-11-08 14:49:59 -05008971 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008972 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008973
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008974 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008975 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008976 if (ret2 && !ret)
8977 ret = ret2;
8978
8979 ret2 = btrfs_end_transaction(trans);
8980 if (ret2 && !ret)
8981 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008982 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008983 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008984out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008985 btrfs_free_block_rsv(fs_info, rsv);
Filipe Manana0d7d3162021-05-24 11:35:55 +01008986 /*
8987 * So if we truncate and then write and fsync we normally would just
8988 * write the extents that changed, which is a problem if we need to
8989 * first truncate that entire inode. So set this flag so we write out
8990 * all of the extents in the inode to the sync log so we're completely
8991 * safe.
8992 *
8993 * If no extents were dropped or trimmed we don't need to force the next
8994 * fsync to truncate all the inode's items from the log and re-log them
8995 * all. This means the truncate operation did not change the file size,
8996 * or changed it to a smaller size but there was only an implicit hole
8997 * between the old i_size and the new i_size, and there were no prealloc
8998 * extents beyond i_size to drop.
8999 */
9000 if (extents_found > 0)
9001 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009002
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009003 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009004}
9005
Sven Wegener3b963622008-06-09 21:57:42 -04009006/*
Chris Masond352ac62008-09-29 15:18:18 -04009007 * create a new subvolume directory/inode (helper for the ioctl).
9008 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009009int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009010 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02009011 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04009012{
Chris Mason39279cc2007-06-12 06:35:45 -04009013 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009014 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009015 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02009016 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04009017
Nikolay Borisov23125102020-12-07 17:32:36 +02009018 err = btrfs_get_free_objectid(new_root, &ino);
9019 if (err < 0)
9020 return err;
9021
9022 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009023 S_IFDIR | (~current_umask() & S_IRWXUGO),
9024 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009025 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009026 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009027 inode->i_op = &btrfs_dir_inode_operations;
9028 inode->i_fop = &btrfs_dir_file_operations;
9029
Miklos Szeredibfe86842011-10-28 14:13:29 +02009030 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009031 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009032 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009033
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009034 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9035 if (err)
9036 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009037 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009038 new_root->root_key.objectid, err);
9039
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009040 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009041
Yan, Zheng76dda932009-09-21 16:00:26 -04009042 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009043 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009044}
9045
Chris Mason39279cc2007-06-12 06:35:45 -04009046struct inode *btrfs_alloc_inode(struct super_block *sb)
9047{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009048 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009049 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009050 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009051
David Sterba712e36c2017-10-31 17:08:27 +01009052 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009053 if (!ei)
9054 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009055
9056 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009057 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009058 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009059 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009060 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009061 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009062 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009063 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009064 ei->disk_i_size = 0;
9065 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009066 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009067 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009068 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009069 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01009070 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009071 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009072
Josef Bacik9e0baf62011-07-15 15:16:44 +00009073 spin_lock_init(&ei->lock);
9074 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009075 if (sb->s_magic != BTRFS_TEST_MAGIC)
9076 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9077 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009078 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009079 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009080 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009081
Miao Xie16cdcec2011-04-22 18:12:22 +08009082 ei->delayed_node = NULL;
9083
chandan r9cc97d62012-07-04 12:48:07 +05309084 ei->i_otime.tv_sec = 0;
9085 ei->i_otime.tv_nsec = 0;
9086
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009087 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009088 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009089 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9090 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9091 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05009092 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9093 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01009094 ei->io_tree.track_uptodate = true;
9095 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009096 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009097 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009098 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009099 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009100 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009101 RB_CLEAR_NODE(&ei->rb_node);
Josef Bacik8318ba72021-02-10 17:14:33 -05009102 init_rwsem(&ei->i_mmap_lock);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009103
9104 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009105}
9106
Josef Bacikaaedb552013-10-11 14:44:09 -04009107#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9108void btrfs_test_destroy_inode(struct inode *inode)
9109{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009110 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009111 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9112}
9113#endif
9114
Al Viro26602ca2019-04-10 15:14:41 -04009115void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009116{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009117 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9118}
9119
Nikolay Borisov633cc812020-09-18 12:15:49 +03009120void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009121{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009122 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03009123 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9124 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009125
Nikolay Borisov633cc812020-09-18 12:15:49 +03009126 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9127 WARN_ON(vfs_inode->i_data.nrpages);
9128 WARN_ON(inode->block_rsv.reserved);
9129 WARN_ON(inode->block_rsv.size);
9130 WARN_ON(inode->outstanding_extents);
9131 WARN_ON(inode->delalloc_bytes);
9132 WARN_ON(inode->new_delalloc_bytes);
9133 WARN_ON(inode->csum_bytes);
9134 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009135
Chris Mason5a3f23d2009-03-31 13:27:11 -04009136 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009137 * This can happen where we create an inode, but somebody else also
9138 * created the same inode and we need to destroy the one we already
9139 * created.
9140 */
9141 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009142 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009143
Chris Masond3977122009-01-05 21:25:51 -05009144 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009145 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009146 if (!ordered)
9147 break;
9148 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03009149 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009150 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08009151 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03009152 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009153 btrfs_put_ordered_extent(ordered);
9154 btrfs_put_ordered_extent(ordered);
9155 }
9156 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03009157 btrfs_qgroup_check_reserved_leak(inode);
9158 inode_tree_del(inode);
9159 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9160 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9161 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04009162}
9163
Al Viro45321ac2010-06-07 13:43:19 -04009164int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009165{
9166 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009167
Naohiro Aota6379ef92013-06-06 09:56:34 +00009168 if (root == NULL)
9169 return 1;
9170
Liu Bofa6ac872013-02-20 14:10:23 +00009171 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009172 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009173 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009174 else
Al Viro45321ac2010-06-07 13:43:19 -04009175 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009176}
9177
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009178static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009179{
9180 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9181
9182 inode_init_once(&ei->vfs_inode);
9183}
9184
David Sterbae67c7182018-02-19 17:24:18 +01009185void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009186{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009187 /*
9188 * Make sure all delayed rcu free inodes are flushed before we
9189 * destroy cache.
9190 */
9191 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009192 kmem_cache_destroy(btrfs_inode_cachep);
9193 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009194 kmem_cache_destroy(btrfs_path_cachep);
9195 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009196 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009197}
9198
Liu Bof5c29bd2017-11-02 17:21:50 -06009199int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009200{
David Sterba837e1972012-09-07 03:00:48 -06009201 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009202 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009203 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9204 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009205 if (!btrfs_inode_cachep)
9206 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009207
David Sterba837e1972012-09-07 03:00:48 -06009208 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009209 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009210 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009211 if (!btrfs_trans_handle_cachep)
9212 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009213
David Sterba837e1972012-09-07 03:00:48 -06009214 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009215 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009216 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009217 if (!btrfs_path_cachep)
9218 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009219
David Sterba837e1972012-09-07 03:00:48 -06009220 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009221 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009222 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009223 if (!btrfs_free_space_cachep)
9224 goto fail;
9225
Christophe Leroy3acd4852019-08-21 15:05:55 +00009226 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9227 PAGE_SIZE, PAGE_SIZE,
David Sterba34e49992021-03-15 15:18:24 +01009228 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009229 if (!btrfs_free_space_bitmap_cachep)
9230 goto fail;
9231
Chris Mason39279cc2007-06-12 06:35:45 -04009232 return 0;
9233fail:
9234 btrfs_destroy_cachep();
9235 return -ENOMEM;
9236}
9237
Christian Brauner549c7292021-01-21 14:19:43 +01009238static int btrfs_getattr(struct user_namespace *mnt_userns,
9239 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00009240 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009241{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009242 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009243 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009244 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009245 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009246 u32 bi_flags = BTRFS_I(inode)->flags;
9247
9248 stat->result_mask |= STATX_BTIME;
9249 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9250 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9251 if (bi_flags & BTRFS_INODE_APPEND)
9252 stat->attributes |= STATX_ATTR_APPEND;
9253 if (bi_flags & BTRFS_INODE_COMPRESS)
9254 stat->attributes |= STATX_ATTR_COMPRESSED;
9255 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9256 stat->attributes |= STATX_ATTR_IMMUTABLE;
9257 if (bi_flags & BTRFS_INODE_NODUMP)
9258 stat->attributes |= STATX_ATTR_NODUMP;
9259
9260 stat->attributes_mask |= (STATX_ATTR_APPEND |
9261 STATX_ATTR_COMPRESSED |
9262 STATX_ATTR_IMMUTABLE |
9263 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009264
Christian Brauner0d56a452021-01-21 14:19:30 +01009265 generic_fillattr(&init_user_ns, inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009266 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009267
9268 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009269 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00009270 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009271 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00009272 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009273 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009274 return 0;
9275}
9276
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009277static int btrfs_rename_exchange(struct inode *old_dir,
9278 struct dentry *old_dentry,
9279 struct inode *new_dir,
9280 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009282 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009283 struct btrfs_trans_handle *trans;
9284 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009285 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009286 struct inode *new_inode = new_dentry->d_inode;
9287 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009288 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009289 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9290 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009291 u64 old_idx = 0;
9292 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009293 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009294 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009295 bool root_log_pinned = false;
9296 bool dest_log_pinned = false;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009297 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009298
NeilBrown3f79f6f2021-08-06 14:26:24 +10009299 /*
9300 * For non-subvolumes allow exchange only within one subvolume, in the
9301 * same inode namespace. Two subvolumes (represented as directory) can
9302 * be exchanged as they're a logical link and have a fixed inode number.
9303 */
9304 if (root != dest &&
9305 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9306 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009307 return -EXDEV;
9308
9309 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009310 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9311 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009312 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009313
9314 /*
9315 * We want to reserve the absolute worst case amount of items. So if
9316 * both inodes are subvols and we need to unlink them then that would
9317 * require 4 item modifications, but if they are both normal inodes it
9318 * would require 5 item modifications, so we'll assume their normal
9319 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9320 * should cover the worst case number of items we'll modify.
9321 */
9322 trans = btrfs_start_transaction(root, 12);
9323 if (IS_ERR(trans)) {
9324 ret = PTR_ERR(trans);
9325 goto out_notrans;
9326 }
9327
Josef Bacik00aa8e82021-03-12 15:25:02 -05009328 if (dest != root) {
9329 ret = btrfs_record_root_in_trans(trans, dest);
9330 if (ret)
9331 goto out_fail;
9332 }
Josef Bacik3e174092019-11-15 15:43:06 -05009333
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009334 /*
9335 * We need to find a free sequence number both in the source and
9336 * in the destination directory for the exchange.
9337 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009338 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009339 if (ret)
9340 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009341 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009342 if (ret)
9343 goto out_fail;
9344
9345 BTRFS_I(old_inode)->dir_index = 0ULL;
9346 BTRFS_I(new_inode)->dir_index = 0ULL;
9347
9348 /* Reference for the source. */
9349 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9350 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009351 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009352 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009353 btrfs_pin_log_trans(root);
9354 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009355 ret = btrfs_insert_inode_ref(trans, dest,
9356 new_dentry->d_name.name,
9357 new_dentry->d_name.len,
9358 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009359 btrfs_ino(BTRFS_I(new_dir)),
9360 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009361 if (ret)
9362 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009363 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009364 }
9365
9366 /* And now for the dest. */
9367 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9368 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009369 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009370 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009371 btrfs_pin_log_trans(dest);
9372 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009373 ret = btrfs_insert_inode_ref(trans, root,
9374 old_dentry->d_name.name,
9375 old_dentry->d_name.len,
9376 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009377 btrfs_ino(BTRFS_I(old_dir)),
9378 new_idx);
Josef Bacikdc09ef32021-05-19 14:04:21 -04009379 if (ret) {
9380 if (need_abort)
9381 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009382 goto out_fail;
Josef Bacikdc09ef32021-05-19 14:04:21 -04009383 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009384 }
9385
9386 /* Update inode version and ctime/mtime. */
9387 inode_inc_iversion(old_dir);
9388 inode_inc_iversion(new_dir);
9389 inode_inc_iversion(old_inode);
9390 inode_inc_iversion(new_inode);
9391 old_dir->i_ctime = old_dir->i_mtime = ctime;
9392 new_dir->i_ctime = new_dir->i_mtime = ctime;
9393 old_inode->i_ctime = ctime;
9394 new_inode->i_ctime = ctime;
9395
9396 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009397 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9398 BTRFS_I(old_inode), 1);
9399 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9400 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009401 }
9402
9403 /* src is a subvolume */
9404 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009405 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009406 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009407 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9408 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009409 old_dentry->d_name.name,
9410 old_dentry->d_name.len);
9411 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009412 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009413 }
9414 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009415 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009416 goto out_fail;
9417 }
9418
9419 /* dest is a subvolume */
9420 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009421 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009422 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009423 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9424 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009425 new_dentry->d_name.name,
9426 new_dentry->d_name.len);
9427 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009428 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009429 }
9430 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009431 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009432 goto out_fail;
9433 }
9434
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009435 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009436 new_dentry->d_name.name,
9437 new_dentry->d_name.len, 0, old_idx);
9438 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009439 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009440 goto out_fail;
9441 }
9442
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009443 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009444 old_dentry->d_name.name,
9445 old_dentry->d_name.len, 0, new_idx);
9446 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009447 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009448 goto out_fail;
9449 }
9450
9451 if (old_inode->i_nlink == 1)
9452 BTRFS_I(old_inode)->dir_index = old_idx;
9453 if (new_inode->i_nlink == 1)
9454 BTRFS_I(new_inode)->dir_index = new_idx;
9455
Filipe Manana86e8aa02016-05-05 02:02:27 +01009456 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009457 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9458 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009459 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009460 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009462 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009463 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9464 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009465 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009466 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009467 }
9468out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009469 /*
9470 * If we have pinned a log and an error happened, we unpin tasks
9471 * trying to sync the log and force them to fallback to a transaction
9472 * commit if the log currently contains any of the inodes involved in
9473 * this rename operation (to ensure we do not persist a log with an
9474 * inconsistent state for any of these inodes or leading to any
9475 * inconsistencies when replayed). If the transaction was aborted, the
9476 * abortion reason is propagated to userspace when attempting to commit
9477 * the transaction. If the log does not contain any of these inodes, we
9478 * allow the tasks to sync it.
9479 */
9480 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009481 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9482 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9483 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009484 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009485 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009486 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009487
9488 if (root_log_pinned) {
9489 btrfs_end_log_trans(root);
9490 root_log_pinned = false;
9491 }
9492 if (dest_log_pinned) {
9493 btrfs_end_log_trans(dest);
9494 dest_log_pinned = false;
9495 }
9496 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009497 ret2 = btrfs_end_transaction(trans);
9498 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009500 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9501 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009502 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503
9504 return ret;
9505}
9506
9507static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9508 struct btrfs_root *root,
9509 struct inode *dir,
9510 struct dentry *dentry)
9511{
9512 int ret;
9513 struct inode *inode;
9514 u64 objectid;
9515 u64 index;
9516
Nikolay Borisov543068a2020-12-07 17:32:33 +02009517 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 if (ret)
9519 return ret;
9520
9521 inode = btrfs_new_inode(trans, root, dir,
9522 dentry->d_name.name,
9523 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009524 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 objectid,
9526 S_IFCHR | WHITEOUT_MODE,
9527 &index);
9528
9529 if (IS_ERR(inode)) {
9530 ret = PTR_ERR(inode);
9531 return ret;
9532 }
9533
9534 inode->i_op = &btrfs_special_inode_operations;
9535 init_special_inode(inode, inode->i_mode,
9536 WHITEOUT_DEV);
9537
9538 ret = btrfs_init_inode_security(trans, inode, dir,
9539 &dentry->d_name);
9540 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009541 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009542
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009543 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9544 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009546 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009548 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009549out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009551 if (ret)
9552 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 iput(inode);
9554
Filipe Mananac9901612016-05-05 01:41:57 +01009555 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556}
9557
Chris Mason39279cc2007-06-12 06:35:45 -04009558static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009559 struct inode *new_dir, struct dentry *new_dentry,
9560 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009561{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009562 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009563 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009564 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009565 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9566 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009567 struct inode *new_inode = d_inode(new_dentry);
9568 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009569 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009570 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009571 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009572 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009573 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009574
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009575 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009576 return -EPERM;
9577
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009578 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009579 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009580 return -EXDEV;
9581
Li Zefan33345d012011-04-20 10:31:50 +08009582 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009583 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009584 return -ENOTEMPTY;
9585
Chris Mason39279cc2007-06-12 06:35:45 -04009586 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009587 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009588 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009589
9590
9591 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009592 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009593 new_dentry->d_name.name,
9594 new_dentry->d_name.len);
9595
9596 if (ret) {
9597 if (ret == -EEXIST) {
9598 /* we shouldn't get
9599 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309600 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009601 return ret;
9602 }
9603 } else {
9604 /* maybe -EOVERFLOW */
9605 return ret;
9606 }
9607 }
9608 ret = 0;
9609
Chris Mason5a3f23d2009-03-31 13:27:11 -04009610 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009611 * we're using rename to replace one file with another. Start IO on it
9612 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009613 */
Chris Mason8d875f92014-08-12 10:47:42 -07009614 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009615 filemap_flush(old_inode->i_mapping);
9616
Yan, Zheng76dda932009-09-21 16:00:26 -04009617 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009618 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009619 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009620 /*
9621 * We want to reserve the absolute worst case amount of items. So if
9622 * both inodes are subvols and we need to unlink them then that would
9623 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009625 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9626 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009627 * If our rename has the whiteout flag, we need more 5 units for the
9628 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9629 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009630 */
Filipe Manana5062af32016-05-05 10:26:26 +01009631 trans_num_items = 11;
9632 if (flags & RENAME_WHITEOUT)
9633 trans_num_items += 5;
9634 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009635 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009636 ret = PTR_ERR(trans);
9637 goto out_notrans;
9638 }
Chris Mason5f39d392007-10-15 16:14:19 -04009639
Josef Bacikb0fec6f2021-03-12 15:25:03 -05009640 if (dest != root) {
9641 ret = btrfs_record_root_in_trans(trans, dest);
9642 if (ret)
9643 goto out_fail;
9644 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009645
Nikolay Borisov877574e2017-02-20 13:50:33 +02009646 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009647 if (ret)
9648 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009649
Miao Xie67de1172013-12-26 13:07:06 +08009650 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009651 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009652 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009653 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009654 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009655 btrfs_pin_log_trans(root);
9656 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009657 ret = btrfs_insert_inode_ref(trans, dest,
9658 new_dentry->d_name.name,
9659 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009660 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009661 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009662 if (ret)
9663 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009664 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009665
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009666 inode_inc_iversion(old_dir);
9667 inode_inc_iversion(new_dir);
9668 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009669 old_dir->i_ctime = old_dir->i_mtime =
9670 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009671 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009672
Chris Mason12fcfd22009-03-24 10:24:20 -04009673 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009674 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9675 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009676
Li Zefan33345d012011-04-20 10:31:50 +08009677 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009678 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009679 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009680 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9681 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009682 old_dentry->d_name.name,
9683 old_dentry->d_name.len);
9684 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009685 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009686 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009687 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009688 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009689 goto out_fail;
9690 }
Chris Mason39279cc2007-06-12 06:35:45 -04009691
9692 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009693 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009694 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009695 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009696 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009697 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009698 BUG_ON(new_inode->i_nlink == 0);
9699 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009700 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9701 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009702 new_dentry->d_name.name,
9703 new_dentry->d_name.len);
9704 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009705 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009706 ret = btrfs_orphan_add(trans,
9707 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009708 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009709 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009710 goto out_fail;
9711 }
Chris Mason39279cc2007-06-12 06:35:45 -04009712 }
Josef Bacikaec74772008-07-24 12:12:38 -04009713
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009714 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009715 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009716 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009717 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009718 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009719 goto out_fail;
9720 }
Chris Mason39279cc2007-06-12 06:35:45 -04009721
Miao Xie67de1172013-12-26 13:07:06 +08009722 if (old_inode->i_nlink == 1)
9723 BTRFS_I(old_inode)->dir_index = index;
9724
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009725 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009726 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9727 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009728 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009729 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009730 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009731
9732 if (flags & RENAME_WHITEOUT) {
9733 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9734 old_dentry);
9735
9736 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009737 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738 goto out_fail;
9739 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009740 }
Chris Mason39279cc2007-06-12 06:35:45 -04009741out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009742 /*
9743 * If we have pinned the log and an error happened, we unpin tasks
9744 * trying to sync the log and force them to fallback to a transaction
9745 * commit if the log currently contains any of the inodes involved in
9746 * this rename operation (to ensure we do not persist a log with an
9747 * inconsistent state for any of these inodes or leading to any
9748 * inconsistencies when replayed). If the transaction was aborted, the
9749 * abortion reason is propagated to userspace when attempting to commit
9750 * the transaction. If the log does not contain any of these inodes, we
9751 * allow the tasks to sync it.
9752 */
9753 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009754 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9755 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9756 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009757 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009758 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009759 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009760
9761 btrfs_end_log_trans(root);
9762 log_pinned = false;
9763 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009764 ret2 = btrfs_end_transaction(trans);
9765 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009766out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009767 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009768 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009769
Chris Mason39279cc2007-06-12 06:35:45 -04009770 return ret;
9771}
9772
Christian Brauner549c7292021-01-21 14:19:43 +01009773static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9774 struct dentry *old_dentry, struct inode *new_dir,
9775 struct dentry *new_dentry, unsigned int flags)
Miklos Szeredi80ace852014-07-23 15:15:32 +02009776{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009778 return -EINVAL;
9779
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 if (flags & RENAME_EXCHANGE)
9781 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9782 new_dentry);
9783
9784 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009785}
9786
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009787struct btrfs_delalloc_work {
9788 struct inode *inode;
9789 struct completion completion;
9790 struct list_head list;
9791 struct btrfs_work work;
9792};
9793
Miao Xie8ccf6f192012-10-25 09:28:04 +00009794static void btrfs_run_delalloc_work(struct btrfs_work *work)
9795{
9796 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009797 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009798
9799 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9800 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009801 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009802 filemap_flush(inode->i_mapping);
9803 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9804 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009805 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009806
Nikolay Borisov076da912018-04-23 10:54:16 +03009807 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009808 complete(&delalloc_work->completion);
9809}
9810
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009811static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009812{
9813 struct btrfs_delalloc_work *work;
9814
David Sterba100d5702015-12-08 14:39:32 +01009815 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009816 if (!work)
9817 return NULL;
9818
9819 init_completion(&work->completion);
9820 INIT_LIST_HEAD(&work->list);
9821 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009822 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009823
9824 return work;
9825}
9826
Chris Masond352ac62008-09-29 15:18:18 -04009827/*
9828 * some fairly slow code that needs optimization. This walks the list
9829 * of all the inodes with pending delalloc and forces them to disk.
9830 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009831static int start_delalloc_inodes(struct btrfs_root *root,
9832 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009833 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009834{
Chris Masonea8c2812008-08-04 23:17:27 -04009835 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009836 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009837 struct btrfs_delalloc_work *work, *next;
9838 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009839 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009840 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009841 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009842
Miao Xie8ccf6f192012-10-25 09:28:04 +00009843 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009844 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009845
Miao Xie573bfb72014-03-06 13:55:03 +08009846 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009847 spin_lock(&root->delalloc_lock);
9848 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009849 while (!list_empty(&splice)) {
9850 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009851 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009852
Miao Xieeb73c1b2013-05-15 07:48:22 +00009853 list_move_tail(&binode->delalloc_inodes,
9854 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009855
9856 if (in_reclaim_context &&
9857 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9858 continue;
9859
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009860 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009861 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009862 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009863 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009864 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009865 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009866
Ethan Lien3cd24c62018-11-01 14:49:03 +08009867 if (snapshot)
9868 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9869 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009870 if (full_flush) {
9871 work = btrfs_alloc_delalloc_work(inode);
9872 if (!work) {
9873 iput(inode);
9874 ret = -ENOMEM;
9875 goto out;
9876 }
9877 list_add_tail(&work->list, &works);
9878 btrfs_queue_work(root->fs_info->flush_workers,
9879 &work->work);
9880 } else {
Josef Bacikb3776302021-07-14 14:47:23 -04009881 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009882 btrfs_add_delayed_iput(inode);
9883 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009884 goto out;
9885 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009886 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009887 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009888 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009889 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009890
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009891out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009892 list_for_each_entry_safe(work, next, &works, list) {
9893 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009894 wait_for_completion(&work->completion);
9895 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009896 }
9897
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009898 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009899 spin_lock(&root->delalloc_lock);
9900 list_splice_tail(&splice, &root->delalloc_inodes);
9901 spin_unlock(&root->delalloc_lock);
9902 }
Miao Xie573bfb72014-03-06 13:55:03 +08009903 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009904 return ret;
9905}
9906
Filipe Mananaf9baa502021-04-22 12:08:05 +01009907int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009908{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009909 struct writeback_control wbc = {
9910 .nr_to_write = LONG_MAX,
9911 .sync_mode = WB_SYNC_NONE,
9912 .range_start = 0,
9913 .range_end = LLONG_MAX,
9914 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009915 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009916
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009917 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009918 return -EROFS;
9919
Filipe Mananaf9baa502021-04-22 12:08:05 +01009920 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009921}
9922
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009923int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009924 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009925{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009926 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009927 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009928 .sync_mode = WB_SYNC_NONE,
9929 .range_start = 0,
9930 .range_end = LLONG_MAX,
9931 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009932 struct btrfs_root *root;
9933 struct list_head splice;
9934 int ret;
9935
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009936 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009937 return -EROFS;
9938
9939 INIT_LIST_HEAD(&splice);
9940
Miao Xie573bfb72014-03-06 13:55:03 +08009941 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009942 spin_lock(&fs_info->delalloc_root_lock);
9943 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009944 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009945 /*
9946 * Reset nr_to_write here so we know that we're doing a full
9947 * flush.
9948 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009949 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009950 wbc.nr_to_write = LONG_MAX;
9951
Miao Xieeb73c1b2013-05-15 07:48:22 +00009952 root = list_first_entry(&splice, struct btrfs_root,
9953 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009954 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009955 BUG_ON(!root);
9956 list_move_tail(&root->delalloc_root,
9957 &fs_info->delalloc_roots);
9958 spin_unlock(&fs_info->delalloc_root_lock);
9959
Josef Bacike076ab2a2021-01-07 17:08:30 -05009960 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009961 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009962 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009963 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009964 spin_lock(&fs_info->delalloc_root_lock);
9965 }
9966 spin_unlock(&fs_info->delalloc_root_lock);
9967
Miao Xie6c255e62014-03-06 13:55:01 +08009968 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009969out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009970 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009971 spin_lock(&fs_info->delalloc_root_lock);
9972 list_splice_tail(&splice, &fs_info->delalloc_roots);
9973 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009974 }
Miao Xie573bfb72014-03-06 13:55:03 +08009975 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009976 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009977}
9978
Christian Brauner549c7292021-01-21 14:19:43 +01009979static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9980 struct dentry *dentry, const char *symname)
Chris Mason39279cc2007-06-12 06:35:45 -04009981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009982 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009983 struct btrfs_trans_handle *trans;
9984 struct btrfs_root *root = BTRFS_I(dir)->root;
9985 struct btrfs_path *path;
9986 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009987 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009988 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009989 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309990 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009991 int name_len;
9992 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009993 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009994 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009995 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009996
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009997 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009998 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009999 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010000
Josef Bacik9ed74f22009-09-11 16:12:44 -040010001 /*
10002 * 2 items for inode item and ref
10003 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010004 * 1 item for updating parent inode item
10005 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010006 * 1 item for xattr if selinux is on
10007 */
Filipe Manana9269d122015-12-31 18:16:29 +000010008 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010009 if (IS_ERR(trans))
10010 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010011
Nikolay Borisov543068a2020-12-07 17:32:33 +020010012 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +080010013 if (err)
10014 goto out_unlock;
10015
Josef Bacikaec74772008-07-24 12:12:38 -040010016 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010017 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10018 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010019 if (IS_ERR(inode)) {
10020 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010021 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010022 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010023 }
Chris Mason39279cc2007-06-12 06:35:45 -040010024
Casey Schauflerad19db72011-12-15 10:09:07 -050010025 /*
10026 * If the active LSM wants to access the inode during
10027 * d_instantiate it needs these. Smack checks to see
10028 * if the filesystem supports xattrs by looking at the
10029 * ops vector.
10030 */
10031 inode->i_fop = &btrfs_file_operations;
10032 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010033 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010034
10035 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10036 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010037 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010038
Chris Mason39279cc2007-06-12 06:35:45 -040010039 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010040 if (!path) {
10041 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010042 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010043 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010044 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010045 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010046 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010047 datasize = btrfs_file_extent_calc_inline_size(name_len);
10048 err = btrfs_insert_empty_item(trans, root, path, &key,
10049 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010050 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010051 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010052 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010053 }
Chris Mason5f39d392007-10-15 16:14:19 -040010054 leaf = path->nodes[0];
10055 ei = btrfs_item_ptr(leaf, path->slots[0],
10056 struct btrfs_file_extent_item);
10057 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10058 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010059 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010060 btrfs_set_file_extent_encryption(leaf, ei, 0);
10061 btrfs_set_file_extent_compression(leaf, ei, 0);
10062 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10063 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10064
Chris Mason39279cc2007-06-12 06:35:45 -040010065 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010066 write_extent_buffer(leaf, symname, ptr, name_len);
10067 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010068 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010069
Chris Mason39279cc2007-06-12 06:35:45 -040010070 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010071 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010072 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010073 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010074 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +000010075 /*
10076 * Last step, add directory indexes for our symlink inode. This is the
10077 * last step to avoid extra cleanup of these indexes if an error happens
10078 * elsewhere above.
10079 */
10080 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010081 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10082 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010083 if (err)
10084 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010085
Al Viro1e2e5472018-05-04 08:23:01 -040010086 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010087
10088out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010089 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010090 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010091 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010092 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010093 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010094 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010095 return err;
10096}
Chris Mason16432982008-04-10 10:23:21 -040010097
Filipe Manana8fccebf2020-09-08 11:27:20 +010010098static struct btrfs_trans_handle *insert_prealloc_file_extent(
10099 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010100 struct btrfs_inode *inode,
10101 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +080010102 u64 file_offset)
10103{
10104 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +010010105 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010106 struct btrfs_trans_handle *trans = trans_in;
10107 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010108 u64 start = ins->objectid;
10109 u64 len = ins->offset;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010110 int qgroup_released;
Qu Wenruo9729f102020-06-10 09:04:41 +080010111 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +080010112
10113 memset(&stack_fi, 0, sizeof(stack_fi));
10114
10115 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10116 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10117 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10118 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10119 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10120 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10121 /* Encryption and other encoding is reserved and all 0 */
10122
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010123 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10124 if (qgroup_released < 0)
10125 return ERR_PTR(qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010126
10127 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010128 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +000010129 file_offset, &stack_fi,
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010130 true, qgroup_released);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010131 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010132 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010133 return trans;
10134 }
10135
10136 extent_info.disk_offset = start;
10137 extent_info.disk_len = len;
10138 extent_info.data_offset = 0;
10139 extent_info.data_len = len;
10140 extent_info.file_offset = file_offset;
10141 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010142 extent_info.is_new_extent = true;
Qu Wenruofbf48bb2021-03-03 18:41:51 +080010143 extent_info.qgroup_reserved = qgroup_released;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010144 extent_info.insertions = 0;
10145
10146 path = btrfs_alloc_path();
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010147 if (!path) {
10148 ret = -ENOMEM;
10149 goto free_qgroup;
10150 }
Filipe Manana8fccebf2020-09-08 11:27:20 +010010151
Nikolay Borisovbfc78472021-02-17 15:12:47 +020010152 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +010010153 file_offset + len - 1, &extent_info,
10154 &trans);
10155 btrfs_free_path(path);
10156 if (ret)
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010157 goto free_qgroup;
Filipe Manana8fccebf2020-09-08 11:27:20 +010010158 return trans;
Qu Wenruoa3ee79b2021-03-03 18:41:52 +080010159
10160free_qgroup:
10161 /*
10162 * We have released qgroup data range at the beginning of the function,
10163 * and normally qgroup_released bytes will be freed when committing
10164 * transaction.
10165 * But if we error out early, we have to free what we have released
10166 * or we leak qgroup data reservation.
10167 */
10168 btrfs_qgroup_free_refroot(inode->root->fs_info,
10169 inode->root->root_key.objectid, qgroup_released,
10170 BTRFS_QGROUP_RSV_DATA);
10171 return ERR_PTR(ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +080010172}
Filipe Manana8fccebf2020-09-08 11:27:20 +010010173
Josef Bacik0af3d002010-06-21 14:48:16 -040010174static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10175 u64 start, u64 num_bytes, u64 min_size,
10176 loff_t actual_len, u64 *alloc_hint,
10177 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010178{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010180 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10181 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010182 struct btrfs_root *root = BTRFS_I(inode)->root;
10183 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010184 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -050010185 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010186 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010187 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010188 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010189 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010190 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010191 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010192
Josef Bacik0af3d002010-06-21 14:48:16 -040010193 if (trans)
10194 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010195 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +090010196 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010197 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010198 /*
10199 * If we are severely fragmented we could end up with really
10200 * small allocations, so if the allocator is returning small
10201 * chunks lets make its job easier by only searching for those
10202 * sized chunks.
10203 */
10204 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010205 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10206 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010207 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -040010208 break;
Josef Bacikb778cf92020-02-13 10:47:31 -050010209
10210 /*
10211 * We've reserved this space, and thus converted it from
10212 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10213 * from here on out we will only need to clear our reservation
10214 * for the remaining unreserved area, so advance our
10215 * clear_offset by our extent size.
10216 */
10217 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010218
Josef Bacik0b670dc2015-09-23 17:11:16 -040010219 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +020010220 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10221 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +010010222 /*
10223 * Now that we inserted the prealloc extent we can finally
10224 * decrement the number of reservations in the block group.
10225 * If we did it before, we could race with relocation and have
10226 * relocation miss the reserved extent, making it fail later.
10227 */
10228 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010229 if (IS_ERR(trans)) {
10230 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010231 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010232 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010233 break;
10234 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010235
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010236 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010237 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010238
Josef Bacik5dc562c2012-08-17 13:14:17 -040010239 em = alloc_extent_map();
10240 if (!em) {
10241 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10242 &BTRFS_I(inode)->runtime_flags);
10243 goto next;
10244 }
10245
10246 em->start = cur_offset;
10247 em->orig_start = cur_offset;
10248 em->len = ins.offset;
10249 em->block_start = ins.objectid;
10250 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010251 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010252 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010253 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10254 em->generation = trans->transid;
10255
10256 while (1) {
10257 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010258 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010259 write_unlock(&em_tree->lock);
10260 if (ret != -EEXIST)
10261 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010262 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010263 cur_offset + ins.offset - 1,
10264 0);
10265 }
10266 free_extent_map(em);
10267next:
Yan Zhengd899e052008-10-30 14:25:28 -040010268 num_bytes -= ins.offset;
10269 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010270 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010271
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010272 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010273 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010274 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010275 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010276 (actual_len > inode->i_size) &&
10277 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010278 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010279 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010280 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010281 i_size = cur_offset;
10282 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +020010283 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010284 }
10285
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010286 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010287
10288 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010289 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010290 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010291 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010292 break;
10293 }
Yan Zhengd899e052008-10-30 14:25:28 -040010294
Filipe Manana8fccebf2020-09-08 11:27:20 +010010295 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010296 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +010010297 trans = NULL;
10298 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010299 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010300 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +030010301 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -050010302 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010303 return ret;
10304}
10305
Josef Bacik0af3d002010-06-21 14:48:16 -040010306int btrfs_prealloc_file_range(struct inode *inode, int mode,
10307 u64 start, u64 num_bytes, u64 min_size,
10308 loff_t actual_len, u64 *alloc_hint)
10309{
10310 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10311 min_size, actual_len, alloc_hint,
10312 NULL);
10313}
10314
10315int btrfs_prealloc_file_range_trans(struct inode *inode,
10316 struct btrfs_trans_handle *trans, int mode,
10317 u64 start, u64 num_bytes, u64 min_size,
10318 loff_t actual_len, u64 *alloc_hint)
10319{
10320 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10321 min_size, actual_len, alloc_hint, trans);
10322}
10323
Chris Masone6dcd2d2008-07-17 12:53:50 -040010324static int btrfs_set_page_dirty(struct page *page)
10325{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010326 return __set_page_dirty_nobuffers(page);
10327}
10328
Christian Brauner549c7292021-01-21 14:19:43 +010010329static int btrfs_permission(struct user_namespace *mnt_userns,
10330 struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010331{
Li Zefanb83cc962010-12-20 16:04:08 +080010332 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010333 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010334
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010335 if (mask & MAY_WRITE &&
10336 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10337 if (btrfs_root_readonly(root))
10338 return -EROFS;
10339 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10340 return -EACCES;
10341 }
Christian Brauner47291ba2021-01-21 14:19:24 +010010342 return generic_permission(&init_user_ns, inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010343}
Chris Mason39279cc2007-06-12 06:35:45 -040010344
Christian Brauner549c7292021-01-21 14:19:43 +010010345static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10346 struct dentry *dentry, umode_t mode)
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010347{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010348 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010349 struct btrfs_trans_handle *trans;
10350 struct btrfs_root *root = BTRFS_I(dir)->root;
10351 struct inode *inode = NULL;
10352 u64 objectid;
10353 u64 index;
10354 int ret = 0;
10355
10356 /*
10357 * 5 units required for adding orphan entry
10358 */
10359 trans = btrfs_start_transaction(root, 5);
10360 if (IS_ERR(trans))
10361 return PTR_ERR(trans);
10362
Nikolay Borisov543068a2020-12-07 17:32:33 +020010363 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010364 if (ret)
10365 goto out;
10366
10367 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010368 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010369 if (IS_ERR(inode)) {
10370 ret = PTR_ERR(inode);
10371 inode = NULL;
10372 goto out;
10373 }
10374
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010375 inode->i_fop = &btrfs_file_operations;
10376 inode->i_op = &btrfs_file_inode_operations;
10377
10378 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010379
Chris Masonb0d5d102014-09-08 13:08:51 -070010380 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10381 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010382 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010383
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +020010384 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -070010385 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010386 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010387 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010388 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010389 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010390
Filipe Manana5762b5c2014-08-01 00:10:32 +010010391 /*
10392 * We set number of links to 0 in btrfs_new_inode(), and here we set
10393 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10394 * through:
10395 *
10396 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10397 */
10398 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010399 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010400 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010401 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010402out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010403 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010404 if (ret && inode)
10405 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010406 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010407 return ret;
10408}
10409
Qu Wenruod2a91062021-05-31 16:50:49 +080010410void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010411{
Qu Wenruod2a91062021-05-31 16:50:49 +080010412 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacikc6100a42017-05-05 11:57:13 -040010413 unsigned long index = start >> PAGE_SHIFT;
10414 unsigned long end_index = end >> PAGE_SHIFT;
10415 struct page *page;
Qu Wenruod2a91062021-05-31 16:50:49 +080010416 u32 len;
Josef Bacikc6100a42017-05-05 11:57:13 -040010417
Qu Wenruod2a91062021-05-31 16:50:49 +080010418 ASSERT(end + 1 - start <= U32_MAX);
10419 len = end + 1 - start;
Josef Bacikc6100a42017-05-05 11:57:13 -040010420 while (index <= end_index) {
Qu Wenruod2a91062021-05-31 16:50:49 +080010421 page = find_get_page(inode->vfs_inode.i_mapping, index);
Josef Bacikc6100a42017-05-05 11:57:13 -040010422 ASSERT(page); /* Pages should be in the extent_io_tree */
Qu Wenruod2a91062021-05-31 16:50:49 +080010423
10424 btrfs_page_set_writeback(fs_info, page, start, len);
Josef Bacikc6100a42017-05-05 11:57:13 -040010425 put_page(page);
10426 index++;
10427 }
10428}
10429
Omar Sandovaled46ff32016-11-03 10:28:14 -070010430#ifdef CONFIG_SWAP
10431/*
10432 * Add an entry indicating a block group or device which is pinned by a
10433 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10434 * negative errno on failure.
10435 */
10436static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10437 bool is_block_group)
10438{
10439 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10440 struct btrfs_swapfile_pin *sp, *entry;
10441 struct rb_node **p;
10442 struct rb_node *parent = NULL;
10443
10444 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10445 if (!sp)
10446 return -ENOMEM;
10447 sp->ptr = ptr;
10448 sp->inode = inode;
10449 sp->is_block_group = is_block_group;
Filipe Manana195a49e2021-02-05 12:55:37 +000010450 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010451
10452 spin_lock(&fs_info->swapfile_pins_lock);
10453 p = &fs_info->swapfile_pins.rb_node;
10454 while (*p) {
10455 parent = *p;
10456 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10457 if (sp->ptr < entry->ptr ||
10458 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10459 p = &(*p)->rb_left;
10460 } else if (sp->ptr > entry->ptr ||
10461 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10462 p = &(*p)->rb_right;
10463 } else {
Filipe Manana195a49e2021-02-05 12:55:37 +000010464 if (is_block_group)
10465 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010466 spin_unlock(&fs_info->swapfile_pins_lock);
10467 kfree(sp);
10468 return 1;
10469 }
10470 }
10471 rb_link_node(&sp->node, parent, p);
10472 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10473 spin_unlock(&fs_info->swapfile_pins_lock);
10474 return 0;
10475}
10476
10477/* Free all of the entries pinned by this swapfile. */
10478static void btrfs_free_swapfile_pins(struct inode *inode)
10479{
10480 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10481 struct btrfs_swapfile_pin *sp;
10482 struct rb_node *node, *next;
10483
10484 spin_lock(&fs_info->swapfile_pins_lock);
10485 node = rb_first(&fs_info->swapfile_pins);
10486 while (node) {
10487 next = rb_next(node);
10488 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10489 if (sp->inode == inode) {
10490 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana195a49e2021-02-05 12:55:37 +000010491 if (sp->is_block_group) {
10492 btrfs_dec_block_group_swap_extents(sp->ptr,
10493 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010494 btrfs_put_block_group(sp->ptr);
Filipe Manana195a49e2021-02-05 12:55:37 +000010495 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010496 kfree(sp);
10497 }
10498 node = next;
10499 }
10500 spin_unlock(&fs_info->swapfile_pins_lock);
10501}
10502
10503struct btrfs_swap_info {
10504 u64 start;
10505 u64 block_start;
10506 u64 block_len;
10507 u64 lowest_ppage;
10508 u64 highest_ppage;
10509 unsigned long nr_pages;
10510 int nr_extents;
10511};
10512
10513static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10514 struct btrfs_swap_info *bsi)
10515{
10516 unsigned long nr_pages;
10517 u64 first_ppage, first_ppage_reported, next_ppage;
10518 int ret;
10519
10520 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10521 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10522 PAGE_SIZE) >> PAGE_SHIFT;
10523
10524 if (first_ppage >= next_ppage)
10525 return 0;
10526 nr_pages = next_ppage - first_ppage;
10527
10528 first_ppage_reported = first_ppage;
10529 if (bsi->start == 0)
10530 first_ppage_reported++;
10531 if (bsi->lowest_ppage > first_ppage_reported)
10532 bsi->lowest_ppage = first_ppage_reported;
10533 if (bsi->highest_ppage < (next_ppage - 1))
10534 bsi->highest_ppage = next_ppage - 1;
10535
10536 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10537 if (ret < 0)
10538 return ret;
10539 bsi->nr_extents += ret;
10540 bsi->nr_pages += nr_pages;
10541 return 0;
10542}
10543
10544static void btrfs_swap_deactivate(struct file *file)
10545{
10546 struct inode *inode = file_inode(file);
10547
10548 btrfs_free_swapfile_pins(inode);
10549 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10550}
10551
10552static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10553 sector_t *span)
10554{
10555 struct inode *inode = file_inode(file);
Filipe Mananadd0734f2021-02-05 12:55:38 +000010556 struct btrfs_root *root = BTRFS_I(inode)->root;
10557 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010558 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10559 struct extent_state *cached_state = NULL;
10560 struct extent_map *em = NULL;
10561 struct btrfs_device *device = NULL;
10562 struct btrfs_swap_info bsi = {
10563 .lowest_ppage = (sector_t)-1ULL,
10564 };
10565 int ret = 0;
10566 u64 isize;
10567 u64 start;
10568
10569 /*
10570 * If the swap file was just created, make sure delalloc is done. If the
10571 * file changes again after this, the user is doing something stupid and
10572 * we don't really care.
10573 */
10574 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10575 if (ret)
10576 return ret;
10577
10578 /*
10579 * The inode is locked, so these flags won't change after we check them.
10580 */
10581 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10582 btrfs_warn(fs_info, "swapfile must not be compressed");
10583 return -EINVAL;
10584 }
10585 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10586 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10587 return -EINVAL;
10588 }
10589 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10590 btrfs_warn(fs_info, "swapfile must not be checksummed");
10591 return -EINVAL;
10592 }
10593
10594 /*
10595 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010596 * under us. The exclop protection makes sure they aren't running/won't
10597 * run concurrently while we are mapping the swap extents, and
10598 * fs_info->swapfile_pins prevents them from running while the swap
10599 * file is active and moving the extents. Note that this also prevents
10600 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010601 * really worth the trouble to allow it.
10602 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010603 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010604 btrfs_warn(fs_info,
10605 "cannot activate swapfile while exclusive operation is running");
10606 return -EBUSY;
10607 }
Filipe Mananadd0734f2021-02-05 12:55:38 +000010608
10609 /*
10610 * Prevent snapshot creation while we are activating the swap file.
10611 * We do not want to race with snapshot creation. If snapshot creation
10612 * already started before we bumped nr_swapfiles from 0 to 1 and
10613 * completes before the first write into the swap file after it is
10614 * activated, than that write would fallback to COW.
10615 */
10616 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10617 btrfs_exclop_finish(fs_info);
10618 btrfs_warn(fs_info,
10619 "cannot activate swapfile because snapshot creation is in progress");
10620 return -EINVAL;
10621 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010622 /*
10623 * Snapshots can create extents which require COW even if NODATACOW is
10624 * set. We use this counter to prevent snapshots. We must increment it
10625 * before walking the extents because we don't want a concurrent
10626 * snapshot to run after we've already checked the extents.
10627 */
Filipe Mananadd0734f2021-02-05 12:55:38 +000010628 atomic_inc(&root->nr_swapfiles);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010629
10630 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10631
10632 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10633 start = 0;
10634 while (start < isize) {
10635 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010636 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010637 u64 len = isize - start;
10638
Omar Sandoval39b07b52019-12-02 17:34:23 -080010639 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010640 if (IS_ERR(em)) {
10641 ret = PTR_ERR(em);
10642 goto out;
10643 }
10644
10645 if (em->block_start == EXTENT_MAP_HOLE) {
10646 btrfs_warn(fs_info, "swapfile must not have holes");
10647 ret = -EINVAL;
10648 goto out;
10649 }
10650 if (em->block_start == EXTENT_MAP_INLINE) {
10651 /*
10652 * It's unlikely we'll ever actually find ourselves
10653 * here, as a file small enough to fit inline won't be
10654 * big enough to store more than the swap header, but in
10655 * case something changes in the future, let's catch it
10656 * here rather than later.
10657 */
10658 btrfs_warn(fs_info, "swapfile must not be inline");
10659 ret = -EINVAL;
10660 goto out;
10661 }
10662 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10663 btrfs_warn(fs_info, "swapfile must not be compressed");
10664 ret = -EINVAL;
10665 goto out;
10666 }
10667
10668 logical_block_start = em->block_start + (start - em->start);
10669 len = min(len, em->len - (start - em->start));
10670 free_extent_map(em);
10671 em = NULL;
10672
Boris Burkova84d5d42020-08-18 11:00:05 -070010673 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010674 if (ret < 0) {
10675 goto out;
10676 } else if (ret) {
10677 ret = 0;
10678 } else {
10679 btrfs_warn(fs_info,
10680 "swapfile must not be copy-on-write");
10681 ret = -EINVAL;
10682 goto out;
10683 }
10684
10685 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10686 if (IS_ERR(em)) {
10687 ret = PTR_ERR(em);
10688 goto out;
10689 }
10690
10691 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10692 btrfs_warn(fs_info,
10693 "swapfile must have single data profile");
10694 ret = -EINVAL;
10695 goto out;
10696 }
10697
10698 if (device == NULL) {
10699 device = em->map_lookup->stripes[0].dev;
10700 ret = btrfs_add_swapfile_pin(inode, device, false);
10701 if (ret == 1)
10702 ret = 0;
10703 else if (ret)
10704 goto out;
10705 } else if (device != em->map_lookup->stripes[0].dev) {
10706 btrfs_warn(fs_info, "swapfile must be on one device");
10707 ret = -EINVAL;
10708 goto out;
10709 }
10710
10711 physical_block_start = (em->map_lookup->stripes[0].physical +
10712 (logical_block_start - em->start));
10713 len = min(len, em->len - (logical_block_start - em->start));
10714 free_extent_map(em);
10715 em = NULL;
10716
10717 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10718 if (!bg) {
10719 btrfs_warn(fs_info,
10720 "could not find block group containing swapfile");
10721 ret = -EINVAL;
10722 goto out;
10723 }
10724
Filipe Manana195a49e2021-02-05 12:55:37 +000010725 if (!btrfs_inc_block_group_swap_extents(bg)) {
10726 btrfs_warn(fs_info,
10727 "block group for swapfile at %llu is read-only%s",
10728 bg->start,
10729 atomic_read(&fs_info->scrubs_running) ?
10730 " (scrub running)" : "");
10731 btrfs_put_block_group(bg);
10732 ret = -EINVAL;
10733 goto out;
10734 }
10735
Omar Sandovaled46ff32016-11-03 10:28:14 -070010736 ret = btrfs_add_swapfile_pin(inode, bg, true);
10737 if (ret) {
10738 btrfs_put_block_group(bg);
10739 if (ret == 1)
10740 ret = 0;
10741 else
10742 goto out;
10743 }
10744
10745 if (bsi.block_len &&
10746 bsi.block_start + bsi.block_len == physical_block_start) {
10747 bsi.block_len += len;
10748 } else {
10749 if (bsi.block_len) {
10750 ret = btrfs_add_swap_extent(sis, &bsi);
10751 if (ret)
10752 goto out;
10753 }
10754 bsi.start = start;
10755 bsi.block_start = physical_block_start;
10756 bsi.block_len = len;
10757 }
10758
10759 start += len;
10760 }
10761
10762 if (bsi.block_len)
10763 ret = btrfs_add_swap_extent(sis, &bsi);
10764
10765out:
10766 if (!IS_ERR_OR_NULL(em))
10767 free_extent_map(em);
10768
10769 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10770
10771 if (ret)
10772 btrfs_swap_deactivate(file);
10773
Filipe Mananadd0734f2021-02-05 12:55:38 +000010774 btrfs_drew_write_unlock(&root->snapshot_lock);
10775
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010776 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010777
10778 if (ret)
10779 return ret;
10780
10781 if (device)
10782 sis->bdev = device->bdev;
10783 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10784 sis->max = bsi.nr_pages;
10785 sis->pages = bsi.nr_pages - 1;
10786 sis->highest_bit = bsi.nr_pages - 1;
10787 return bsi.nr_extents;
10788}
10789#else
10790static void btrfs_swap_deactivate(struct file *file)
10791{
10792}
10793
10794static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10795 sector_t *span)
10796{
10797 return -EOPNOTSUPP;
10798}
10799#endif
10800
Filipe Manana2766ff62020-11-04 11:07:34 +000010801/*
10802 * Update the number of bytes used in the VFS' inode. When we replace extents in
10803 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10804 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10805 * always get a correct value.
10806 */
10807void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10808 const u64 add_bytes,
10809 const u64 del_bytes)
10810{
10811 if (add_bytes == del_bytes)
10812 return;
10813
10814 spin_lock(&inode->lock);
10815 if (del_bytes > 0)
10816 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10817 if (add_bytes > 0)
10818 inode_add_bytes(&inode->vfs_inode, add_bytes);
10819 spin_unlock(&inode->lock);
10820}
10821
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010822static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010823 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010824 .lookup = btrfs_lookup,
10825 .create = btrfs_create,
10826 .unlink = btrfs_unlink,
10827 .link = btrfs_link,
10828 .mkdir = btrfs_mkdir,
10829 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010830 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010831 .symlink = btrfs_symlink,
10832 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010833 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010834 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010835 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010836 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010837 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010838 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010839 .tmpfile = btrfs_tmpfile,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010840 .fileattr_get = btrfs_fileattr_get,
10841 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010842};
Yan, Zheng76dda932009-09-21 16:00:26 -040010843
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010844static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010845 .llseek = generic_file_llseek,
10846 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010847 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010848 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010849 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010850#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010851 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010852#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010853 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010854 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010855};
10856
Chris Mason35054392009-01-21 13:11:13 -050010857/*
10858 * btrfs doesn't support the bmap operation because swapfiles
10859 * use bmap to make a mapping of extents in the file. They assume
10860 * these extents won't change over the life of the file and they
10861 * use the bmap result to do IO directly to the drive.
10862 *
10863 * the btrfs bmap call would return logical addresses that aren't
10864 * suitable for IO and they also will change frequently as COW
10865 * operations happen. So, swapfile + btrfs == corruption.
10866 *
10867 * For now we're avoiding this by dropping bmap.
10868 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010869static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010870 .readpage = btrfs_readpage,
10871 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010872 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010873 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010874 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010875 .invalidatepage = btrfs_invalidatepage,
10876 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010877#ifdef CONFIG_MIGRATION
10878 .migratepage = btrfs_migratepage,
10879#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010880 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010881 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010882 .swap_activate = btrfs_swap_activate,
10883 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010884};
10885
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010886static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010887 .getattr = btrfs_getattr,
10888 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010889 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010890 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010891 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010892 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010893 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010894 .update_time = btrfs_update_time,
Miklos Szeredi97fc2972021-04-07 14:36:43 +020010895 .fileattr_get = btrfs_fileattr_get,
10896 .fileattr_set = btrfs_fileattr_set,
Chris Mason39279cc2007-06-12 06:35:45 -040010897};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010898static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010899 .getattr = btrfs_getattr,
10900 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010901 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010902 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010903 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010904 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010905 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010906};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010907static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010908 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010909 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010910 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010911 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010912 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010913 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010914};
Yan, Zheng76dda932009-09-21 16:00:26 -040010915
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010916const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010917 .d_delete = btrfs_dentry_delete,
10918};